Beispiel #1
0
def downloadStockData(stock):
	log.debug('downloading stock data ' + stock['symbol'] + ' ...')
	
	d = '{:0>2}'.format(datetime.datetime.now().month - 1)
	e = '{:0>2}'.format(datetime.datetime.now().day)
	f = str(datetime.datetime.now().year)
	
	url = 'http://ichart.yahoo.com/table.csv?s=' + stock['symbol'] + '&a=00&b=01&c=2000'
	url += '&d=' + d + '&e=' + e + '&f=' + f + '&g=d'
	req = urllib.request.Request(url)
	req.add_header('Referer', 'http://www.yahoo.com/') 
	resp = urllib.request.urlopen(req, timeout=20)
	text = resp.read()
	#if resp.info().get('Content-Encoding') == 'gzip': 
	#	buf = StringIO.StringIO(resp.read()) 
	#	f = gzip.GzipFile(fileobj=buf) 
	#	text = f.read() 
	#else: 
	#	text = resp.read()
		
	dataFolder = os.path.join(os.path.dirname(__file__), 'datas/stocks')
	if not os.path.isdir(dataFolder): os.makedirs(dataFolder) 
	datafile = open(os.path.join(dataFolder, stock['file']), 'wb')
	datafile.write(text)
	datafile.close()
Beispiel #2
0
def importMetal(fileName):
	log.debug('loading metal data from ' + fileName + ' ...')
	prices = []
	prec = 0
	with open(os.path.join(path, 'datas/' + fileName), 'rt') as csvfile:
		rdr = csv.reader(csvfile)
		for row in rdr:
			p = {}
			p['open'] = float(row[2])
			p['high'] = float(row[3])
			p['low'] = float(row[4])
			p['close'] = float(row[5])
			p['vol'] = float(row[6])
			p['trade'] = round(p['close'] * 6.1 / 31.1035, 3)
			p['dt'] = datetime.datetime.strptime(row[0] + ' ' + row[1], '%Y.%m.%d %H:%M') #2009.06.01,00:00
			p['dtlong'] = time.mktime(p['dt'].timetuple())
			p['date'] = p['dt'].strftime('%Y-%m-%d')
			p['time'] = p['dt'].strftime('%H:%M:%S')
			p['chan'] = p['close'] - prec
			if prec == 0:
				p['per'] = 0
			else:
				p['per'] = round((p['close'] - prec) * 100 / prec, 3)
			prec = p['close']
			prices.append(p)
	return prices[-const.DATA_NUM:]
Beispiel #3
0
def importYahooStock(sourcType, fileName):
	log.debug('loading yahoo stock data from ' + fileName + ' ...')
	prices = []
	prec = 0
	cnt = 0
	
	dataPath = os.path.join(path, 'datas')
	if const.SOURCE_TYPE == 4: dataPath = os.path.join(dataPath, 'stocks')
	with open(os.path.join(dataPath, fileName), 'rt') as csvfile:
		rdr = csv.reader(csvfile)
		next(rdr)
		for row in rdr:
			p = {}
			p['open'] = float(row[1])
			p['high'] = float(row[2])
			p['low'] = float(row[3])
			p['close'] = float(row[4])
			p['trade'] = p['close']
			p['vol'] = float(row[5])
			if sourcType == 2 and p['vol'] == 0:
				continue
			
			p['dt'] = datetime.datetime.strptime(row[0], '%Y-%m-%d') #10/31/2014
			p['dtlong'] = time.mktime(p['dt'].timetuple())
			p['date'] = p['dt'].strftime('%Y-%m-%d')
			p['chan'] = p['close'] - prec
			if prec == 0:
				p['per'] = 0
			else:
				p['per'] = round((p['close'] - prec) * 100 / prec, 3)
			prec = p['close']
			prices.insert(0, p)
			cnt += 1
			if cnt > const.DATA_NUM: break
	return prices
Beispiel #4
0
def downloadTecentStockHistoryData(stock, year):
	
	if stock['symbol'][-2:] == 'SS': symb = 'sh' + stock['id']
	elif stock['symbol'][-2:] == 'SZ': symb = 'sz' + stock['id']
	
	log.debug('downloading Tecent Stock data ' + symb + ' year ' + year + '...')
	sleepTime = random.randint(1, 2)
	log.debug('Sleeping for ' + str(sleepTime) + ' seconds...')
	time.sleep(sleepTime)
	
	url = 'http://data.gtimg.cn/flashdata/hushen/daily/' + year + '/' + symb + '.js'
	
	webtext = None
	attempts = 0
	while attempts < 10:
		try:
			req = urllib.request.Request(url)
			req.add_header('Referer', 'http://stockapp.finance.qq.com/mstats/') 
			resp = urllib.request.urlopen(req, timeout=10)
			webtext = resp.read()
			break
			log.debug('Http error, retrying ... ' + str(attempts))
		except urllib.error.HTTPError as e:
			if e.code == 404:
				log.debug('No data for ' + symb + ', in ' + year)
				webtext = None
				break
		except Exception as e:
			attempts += 1
			log.debug('Connection error, retrying ... ' + str(attempts))
	
	return webtext
Beispiel #5
0
def importVSignalList(fileName):
	log.debug('loading v signal list from ' + fileName + ' ...')
	stocks = []
	
	with open(os.path.join(path, 'datas/' + fileName), 'rt', encoding='GBK') as csvfile:
		rdr = csv.reader(csvfile)
		for row in rdr:
			stock = {}
			stock['id'] = row[0]
			stock['vol'] = int(row[1])
			stock['name'] = row[2]
			stock['symbol'] = row[3]
			stock['file'] = stock['id'] + '.csv'
			stock['vfmt'] = row[4]
			stock['vfm'] = int(row[5])
			stock['vsmt'] = row[6]
			stock['vsm'] = int(row[7])
			stock['afmt'] = row[8]
			stock['afm'] = int(row[9])
			stock['asmt'] = row[10]
			stock['asm'] = int(row[11])
			stock['bfmt'] = row[12]
			stock['bfm'] = int(row[13])
			stock['bsmt'] = row[14]
			stock['bsm'] = int(row[15])
			stocks.append(stock)
	
	return stocks
Beispiel #6
0
def importMetal(fileName):
    log.debug('loading metal data from ' + fileName + ' ...')
    prices = []
    prec = 0
    with open(os.path.join(path, 'datas/' + fileName), 'rt') as csvfile:
        rdr = csv.reader(csvfile)
        for row in rdr:
            p = {}
            p['open'] = float(row[2])
            p['high'] = float(row[3])
            p['low'] = float(row[4])
            p['close'] = float(row[5])
            p['vol'] = float(row[6])
            p['trade'] = round(p['close'] * 6.1 / 31.1035, 3)
            p['dt'] = datetime.datetime.strptime(
                row[0] + ' ' + row[1], '%Y.%m.%d %H:%M')  #2009.06.01,00:00
            p['dtlong'] = time.mktime(p['dt'].timetuple())
            p['date'] = p['dt'].strftime('%Y-%m-%d')
            p['time'] = p['dt'].strftime('%H:%M:%S')
            p['chan'] = p['close'] - prec
            if prec == 0:
                p['per'] = 0
            else:
                p['per'] = round((p['close'] - prec) * 100 / prec, 3)
            prec = p['close']
            prices.append(p)
    return prices[-const.DATA_NUM:]
Beispiel #7
0
def runSignal(stock, prices):
    log.debug('beginning signal for ' + stock['id'] + ' ...')

    ps = [p['close'] for p in prices]
    pdt = [p['dt'] for p in prices]

    fma = getMas(stock['fmt'], stock['fm'], ps)
    s1ma = getMas(stock['s1mt'], stock['s1m'], ps)
    s2ma = getMas(stock['s2mt'], stock['s2m'], ps)

    dotNum = const.SIGNAL_GRAPH_NUM
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.set_ylabel('Price')
    ax1.xaxis.set_major_formatter(DateFormatter('%m-%d'))
    ax1.plot_date(pdt[-dotNum:],
                  ps[-dotNum:],
                  color='yellow',
                  linestyle='-',
                  marker='',
                  label='Price')
    ax1.plot_date(pdt[-dotNum:],
                  fma[-dotNum:],
                  color='red',
                  linestyle='-',
                  marker='',
                  label='Price')
    ax1.plot_date(pdt[-dotNum:],
                  s1ma[-dotNum:],
                  color='blue',
                  linestyle='-',
                  marker='',
                  label='Price')
    ax1.plot_date(pdt[-dotNum:],
                  s2ma[-dotNum:],
                  color='green',
                  linestyle='-',
                  marker='',
                  label='Price')
    ax1.grid()

    resultPath = os.path.join(os.path.dirname(__file__), const.RESULT_PATH)
    plt.savefig(os.path.join(resultPath, stock['id'] + '.png'), dpi=150)
    plt.close(fig)

    opos = 0
    pos = 0
    if fma[-2] > s1ma[-2]: opos += stock['vol']
    if fma[-2] > s2ma[-2]: opos += stock['vol']
    if fma[-1] > s1ma[-1]: pos += stock['vol']
    if fma[-1] > s2ma[-1]: pos += stock['vol']

    if opos == pos:
        desc = str(pos)
    else:
        desc = str(opos) + ' -> ' + str(pos)

    dt = datetime.datetime.now().strftime('%Y-%m-%d')

    return stock['symbol'] + ',' + dt + ',' + str(ps[-1]) + ',' + desc
Beispiel #8
0
def downloadTecentStockData(stock):
	log.debug('downloading Tecent Stock data ' + stock['symbol'] + ' ...')
	
	if stock['symbol'][-2:] == 'SS': symb = 'sh' + stock['id']
	elif stock['symbol'][-2:] == 'SZ': symb = 'sz' + stock['id']
	
	years = ['08', '09', '10', '11', '12', '13', '14', '15']
	currentYear = '15'
	
	plines = []
	histFolder = os.path.join(os.path.dirname(__file__), 'datas/history')
	if not os.path.isdir(histFolder): os.makedirs(histFolder) 
	for year in years:
		webtext = html = None
		fileName = stock['id'] + '.' + year
		histFile = os.path.join(histFolder, fileName)
		if not os.path.isfile(histFile): 
			webtext = downloadTecentStockHistoryData(stock, year)
			if webtext and year != currentYear:
				datafile = open(os.path.join(histFolder, fileName), 'wb')
				datafile.write(webtext)
				datafile.close()
			if webtext: html = str(webtext, encoding='utf8')
		else:
			f = open(histFile, 'rb')
			html = str(f.read(), encoding='utf8')
			f.close()
		
		if html:
			lines = html.splitlines() [1:-1]
			for line in lines:
				plines.append(line[:-3])
	
	plines.reverse()
	
	dataFolder = os.path.join(os.path.dirname(__file__), 'datas/stocks')
	if not os.path.isdir(dataFolder): os.makedirs(dataFolder) 
	datafile = open(os.path.join(dataFolder, stock['file']), 'wt')
	datafile.write('Date,Open,High,Low,Close,Volume,Adj Close\n')
	#print(len(datas))
	for pline in plines:
		data = pline.split(' ')
		p = {}
		p['dt'] = datetime.datetime.strptime(str(data[0]), '%y%m%d')
		p['date'] = p['dt'].strftime('%Y-%m-%d')
		p['open'] = float(data[1])
		p['close'] = float(data[2])
		p['high'] = float(data[3])
		p['low'] = float(data[4])
		p['vol'] = int(data[5]) * 100
		
		datafile.write(str(p['date']) + ',' + str(p['open']) + ',' + str(p['high']) + ',' + str(p['low']) + ',')
		datafile.write(str(p['close']) + ',' + str(p['vol']) + ',' + str(p['close']) + '\n')
	
	datafile.close()
Beispiel #9
0
def importStockList(fileName):
	log.debug('loading stock list from ' + fileName + ' ...')
	stocks = []
	
	with open(os.path.join(path, 'datas/' + fileName), 'rt') as csvfile:
		rdr = csv.reader(csvfile)
		for row in rdr:
			stock = {}
			stock['id'] = row[0]
			stock['name'] = row[1]
			stock['symbol'] = row[2]
			stock['file'] = row[3]
			stocks.append(stock)
	
	return stocks
Beispiel #10
0
def importStockList(fileName):
    log.debug('loading stock list from ' + fileName + ' ...')
    stocks = []

    with open(os.path.join(path, 'datas/' + fileName), 'rt') as csvfile:
        rdr = csv.reader(csvfile)
        for row in rdr:
            stock = {}
            stock['id'] = row[0]
            stock['name'] = row[1]
            stock['symbol'] = row[2]
            stock['file'] = row[3]
            stocks.append(stock)

    return stocks
Beispiel #11
0
def downloadStockData(stock):
    log.debug('downloading stock data ' + stock['symbol'] + ' ...')

    d = '{:0>2}'.format(datetime.datetime.now().month - 1)
    e = '{:0>2}'.format(datetime.datetime.now().day)
    f = str(datetime.datetime.now().year)

    url = 'http://ichart.yahoo.com/table.csv?s=' + stock[
        'symbol'] + '&a=00&b=01&c=2000'
    url += '&d=' + d + '&e=' + e + '&f=' + f + '&g=d'
    req = urllib.request.Request(url)
    req.add_header('Referer', 'http://www.yahoo.com/')
    resp = urllib.request.urlopen(req, timeout=20)
    text = resp.read()

    dataFolder = os.path.join(os.path.dirname(__file__), 'datas/stocks')
    if not os.path.isdir(dataFolder): os.makedirs(dataFolder)
    datafile = open(os.path.join(dataFolder, stock['file']), 'wb')
    datafile.write(text)
    datafile.close()
    time.sleep(5)
Beispiel #12
0
def runSignal(stock, prices):
	log.debug('beginning signal for ' + stock['id'] + ' ...')
	
	ps = [p['close'] for p in prices]
	pdt = [p['dt'] for p in prices]
	
	fma = getMas(stock['fmt'], stock['fm'], ps)
	s1ma = getMas(stock['s1mt'], stock['s1m'], ps)
	s2ma = getMas(stock['s2mt'], stock['s2m'], ps)
	
	dotNum = const.SIGNAL_GRAPH_NUM
	fig = plt.figure()
	ax1 = fig.add_subplot(111)
	ax1.set_ylabel('Price')
	ax1.xaxis.set_major_formatter(DateFormatter('%m-%d'))
	ax1.plot_date(pdt[-dotNum:], ps[-dotNum:], color='yellow', linestyle='-', marker='', label='Price')
	ax1.plot_date(pdt[-dotNum:], fma[-dotNum:], color='red', linestyle='-', marker='', label='Price')
	ax1.plot_date(pdt[-dotNum:], s1ma[-dotNum:], color='blue', linestyle='-', marker='', label='Price')
	ax1.plot_date(pdt[-dotNum:], s2ma[-dotNum:], color='green', linestyle='-', marker='', label='Price')
	ax1.grid()
	
	resultPath = os.path.join(os.path.dirname(__file__), const.RESULT_PATH)
	plt.savefig(os.path.join(resultPath, stock['id'] + '.png'), dpi=150)
	plt.close(fig)
	
	opos = 0
	pos = 0
	if fma[-2] > s1ma[-2]: opos += stock['vol']
	if fma[-2] > s2ma[-2]: opos += stock['vol']
	if fma[-1] > s1ma[-1]: pos += stock['vol']
	if fma[-1] > s2ma[-1]: pos += stock['vol']
	
	if opos == pos:
		desc = str(pos)
	else:
		desc = str(opos) + ' -> ' + str(pos)
	
	dt = datetime.datetime.now().strftime('%Y-%m-%d')
		
	return stock['symbol'] + ',' + dt + ',' + str(ps[-1]) + ',' + desc
Beispiel #13
0
def runStrategy(in_prices):
	global prices, mas, emas, smas, lwmas, vmas, vemas, vsmas, vlwmas
	log.debug('beginning vma strategy ...')
	prices = in_prices
	
	vols = [p['vol'] for p in prices]
	
	vmalength = const.VMA_MAX + 1
	vmas = [0] * vmalength
	vemas = [0] * vmalength
	vsmas = [0] * vmalength
	vlwmas = [0] * vmalength
	for period in range(1, vmalength):
		vmas[period] = ma.calc_ma(vols, period)
		vemas[period] = ma.calc_ema(vols, period)
		vsmas[period] = ma.calc_sma(vols, period)
		vlwmas[period] = ma.calc_lwma(vols, period)
		
	ps = [p['close'] for p in prices]
	
	malength = const.MA_MAX + 1
	mas = [0] * malength
	emas = [0] * malength
	smas = [0] * malength
	lwmas = [0] * malength
	for period in range(1, malength):
		mas[period] = ma.calc_ma(ps, period)
		emas[period] = ma.calc_ema(ps, period)
		smas[period] = ma.calc_sma(ps, period)
		lwmas[period] = ma.calc_lwma(ps, period)
	
	log.debug('running ma strategy ...')
	starttime = datetime.datetime.now()
	
	pool = Pool(const.POOL_SIZE)
	
	for vft, vf in [(matype, period) for matype in const.VMA_TYPES for period in const.VMA_FAST]:
		for vst, vs in [(matype, period) for matype in const.VMA_TYPES for period in const.VMA_SLOW]:
			if vs != 0 and vs <= vf: continue
			poola = Pool(const.POOL_SIZE)
			poolb = Pool(const.POOL_SIZE)
			
			for ft, f in [(matype, period) for matype in const.MA_TYPES for period in const.MA_FAST]:
				for s1t, s1 in [(matype, period) for matype in const.MA_TYPES for period in const.MA_SLOW1]:
					if s1 != 0 and s1 <= f: continue
					elapsed = (datetime.datetime.now() - starttime).seconds
					log.debug('== ' + str(elapsed) + ',' + vft + '_' + str(vf) + ',' + vst + '_' + str(vs) + ',' + ft + '_' + str(f) + ',' + s1t + '_' + str(s1) + ' ==')
					
					doTrade(poola, vft, vf, vst, vs, ft, f, s1t, s1, '', 0, '', 0)
					doTrade(poolb, vft, vf, vst, vs, '', 0, '', 0, ft, f, s1t, s1)
			
			for ia in range(len(poola.strategies)):
				for ib in range(len(poolb.strategies)):
					sa = poola.strategies[ia]
					sb = poolb.strategies[ib]
					if sa[0] == 0 or sb[0] == 0: continue
					t = doTrade(pool, vft, vf, vst, vs, sa[0].args[0], sa[0].args[1], sa[0].args[2], sa[0].args[3], sb[0].args[4], sb[0].args[5], sb[0].args[6], sb[0].args[7])
				
	pool.showStrategies()
	return pool.strategies[0][0]
Beispiel #14
0
def makeupStockFromSina(stock, prices):
	# fetch price of AG T+D
	try:
		
		if stock['symbol'][-2:] == 'SS': symb = 'sh' + stock['id']
		elif stock['symbol'][-2:] == 'SZ': symb = 'sz' + stock['id']
		
		f = urllib.request.urlopen('http://hq.sinajs.cn/rn=1386417950746&list=' + symb, timeout=20)
		html = f.read()
		html = html.decode('gbk')
		#print(html)
		html = html[21:len(html) - 3]
		arr = re.split(',', html)
		
		p = {}
		p['dt'] = datetime.datetime.strptime(arr[30], '%Y-%m-%d')
		p['open'] = float(arr[1])
		p['high'] = float(arr[4])
		p['low'] = float(arr[5])
		p['close'] = float(arr[3])
		p['vol'] = float(arr[8])
		p['per'] = round((p['close'] - float(arr[2])) * 100 / float(arr[2]), 3)
		
		if p['dt'] > prices[-1]['dt']:
			log.debug('add price from sina: ' + p['dt'].strftime('%m-%d') + ', ' + prices[-1]['dt'].strftime('%m-%d'))
			prices.append(p)
		else:
			log.debug('DO NOT NEED MAKEUP from sina: ' + p['dt'].strftime('%m-%d') + ', ' + prices[-1]['dt'].strftime('%m-%d'))
			
		return prices
	except:
		log.debug('Error occur with sina')
		return prices
Beispiel #15
0
def downloadYahooStockData(stock):
	time.sleep(random.randint(3, 10))
	
	log.debug('downloading yahoo stock data ' + stock['symbol'] + ' ...')
	
	d = '{:0>2}'.format(datetime.datetime.now().month - 1)
	e = '{:0>2}'.format(datetime.datetime.now().day)
	f = str(datetime.datetime.now().year)
	
	url = 'http://ichart.yahoo.com/table.csv?s=' + stock['symbol'] + '&a=00&b=01&c=2000'
	url += '&d=' + d + '&e=' + e + '&f=' + f + '&g=d'
	#print(url)
	#return
	req = urllib.request.Request(url)
	req.add_header('Referer', 'http://www.yahoo.com/') 
	resp = urllib.request.urlopen(req, timeout=20)
	text = resp.read()
	
	dataFolder = os.path.join(os.path.dirname(__file__), 'datas/stocks')
	if not os.path.isdir(dataFolder): os.makedirs(dataFolder) 
	datafile = open(os.path.join(dataFolder, stock['file']), 'wb')
	datafile.write(text)
	datafile.close()
Beispiel #16
0
def runStrategy(in_prices):
	global prices, ps
	log.debug('beginning pattern strategy ...')
	
	prices = in_prices
	ps = [p['close'] for p in prices]
	
	starttime = datetime.datetime.now()
	
	pool = Pool(const.POOL_SIZE)
	
	for i in range(20, 81)[::5]:
		for j in range(1, 6)[::1]:
			if i < j: continue
			for f in range(5, 16)[::2]:
				elapsed = (datetime.datetime.now() - starttime).seconds
				log.debug('== ' + str(elapsed) + ', ' + str(i) + ',' + str(j) + ',' + str(f) + ' ==')
				for sl in range(15, 31)[::3]:
					for si in range(3, 16)[::2]:
						doTrade(pool, i, j, f, sl, si)
		
	pool.showStrategies()
	return pool.strategies[0][0]
Beispiel #17
0
def importYahooStock(sourcType, fileName):
    log.debug('loading yahoo stock data from ' + fileName + ' ...')
    prices = []
    prec = 0
    cnt = 0

    dataPath = os.path.join(path, 'datas')
    if const.SOURCE_TYPE == 4: dataPath = os.path.join(dataPath, 'stocks')
    with open(os.path.join(dataPath, fileName), 'rt') as csvfile:
        rdr = csv.reader(csvfile)
        next(rdr)
        for row in rdr:
            p = {}
            p['open'] = float(row[1])
            p['high'] = float(row[2])
            p['low'] = float(row[3])
            p['close'] = float(row[4])
            p['trade'] = p['close']
            p['vol'] = float(row[5])
            if sourcType == 2 and p['vol'] == 0:
                continue

            p['dt'] = datetime.datetime.strptime(row[0],
                                                 '%Y-%m-%d')  #10/31/2014
            p['dtlong'] = time.mktime(p['dt'].timetuple())
            p['date'] = p['dt'].strftime('%Y-%m-%d')
            p['chan'] = p['close'] - prec
            if prec == 0:
                p['per'] = 0
            else:
                p['per'] = round((p['close'] - prec) * 100 / prec, 3)
            prec = p['close']
            prices.insert(0, p)
            cnt += 1
            if cnt > const.DATA_NUM: break
    return prices
Beispiel #18
0
def downloadHexunStockData(stock):
	log.debug('downloading hexun stock data ' + stock['symbol'] + ' ...')
	
	m = '{:0>2}'.format(datetime.datetime.now().month)
	d = '{:0>2}'.format(datetime.datetime.now().day)
	y = str(datetime.datetime.now().year)
	dt = y + m + d + '150000'
	
	if stock['symbol'][-2:] == 'SS': symb = 'sse' + stock['id']
	elif stock['symbol'][-2:] == 'SZ': symb = 'szse' + stock['id']
	
	url = 'http://webstock.quote.hermes.hexun.com/a/kline?code=' + symb + '&start=' + dt + '&number=-1000&type=5&callback=callback'
	attempts = 0
	while attempts < 3:
		try:
			sleepTime = random.randint(3, 10)
			log.debug('Sleeping for ' + str(sleepTime) + ' seconds...')
			time.sleep(sleepTime)
			req = urllib.request.Request(url)
			req.add_header('Referer', 'http://www.hexun.com/') 
			resp = urllib.request.urlopen(req, timeout=30)
			html = str(resp.read(), encoding='utf8')
			break
		except Exception as e:
			attempts += 1
			log.debug('Connection error, retrying ... ' + str(attempts))
	
	html = html[html.find('Data') + 7 : ]
	html = html[ : html.find(']],') + 2]
	datas = eval(html)
	datas.reverse()
		
	dataFolder = os.path.join(os.path.dirname(__file__), 'datas/stocks')
	if not os.path.isdir(dataFolder): os.makedirs(dataFolder) 
	datafile = open(os.path.join(dataFolder, stock['file']), 'wt')
	datafile.write('Date,Open,High,Low,Close,Volume,Adj Close\n')
	#print(len(datas))
	for data in datas:
		p = {}
		p['dt'] = datetime.datetime.strptime(str(data[0]), '%Y%m%d%H%M%S')
		p['date'] = p['dt'].strftime('%Y-%m-%d')
		p['open'] = float(data[2]) / 100
		p['close'] = float(data[3]) / 100
		p['high'] = float(data[4]) / 100
		p['low'] = float(data[5]) / 100
		p['vol'] = data[6]
		
		datafile.write(str(p['date']) + ',' + str(p['open']) + ',' + str(p['high']) + ',' + str(p['low']) + ',')
		datafile.write(str(p['close']) + ',' + str(p['vol']) + ',' + str(p['close']) + '\n')
	
	datafile.close()
Beispiel #19
0
def runStrategy(in_prices):
    global mas, emas, smas, lwmas, std, prices
    log.debug('beginning ma strategy ...')

    prices = in_prices
    ps = [p['close'] for p in prices]

    std = [0] * 51
    l = len(prices)
    for period in range(2, 51):
        std[period] = [0] * l
        for i in range(period - 1, l):
            std[period][i] = np.std(ps[i - period + 1:i + 1],
                                    dtype=np.float64,
                                    ddof=0)

    malength = const.MA_MAX + 1
    mas = [0] * malength
    emas = [0] * malength
    smas = [0] * malength
    lwmas = [0] * malength
    for period in range(1, malength):
        mas[period] = ma.calc_ma(ps, period)
        emas[period] = ma.calc_ema(ps, period)
        smas[period] = ma.calc_sma(ps, period)
        lwmas[period] = ma.calc_lwma(ps, period)

    log.debug('running ma strategy ...')
    starttime = datetime.datetime.now()

    matypes = ['MA', 'EMA', 'SMA', 'LWMA']

    pool = Pool(const.POOL_SIZE)
    for ft, f in [(matype, period) for matype in matypes
                  for period in const.MA_FAST]:
        for s1t, s1 in [(matype, period) for matype in matypes
                        for period in const.MA_SLOW1]:
            if s1 != 0 and s1 <= f: continue
            elapsed = (datetime.datetime.now() - starttime).seconds
            log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) + ',' +
                      s1t + '_' + str(s1) + ' ==')
            for s2t, s2 in [(matype, period) for matype in matypes
                            for period in const.MA_SLOW2]:
                if s2 != 0 and s2 <= s1: continue
                #run
                doTrade(pool, ft, f, s1t, s1, s2t, s2)

    pool.showStrategies()
    return pool.strategies[0][0]
Beispiel #20
0
def runStrategy(in_prices):
	global mas, emas, smas, lwmas, std, prices
	log.debug('beginning ma strategy ...')
	
	prices = in_prices
	ps = [p['close'] for p in prices]
	
	std = [0] * 51
	l = len(prices)
	for period in range(2, 51):
		std[period] = [0] * l
		for i in range(period - 1, l):
			std[period][i] = np.std(ps[i-period+1 : i+1], dtype=np.float64, ddof=0)
	
	malength = const.MA_MAX + 1
	mas = [0] * malength
	emas = [0] * malength
	smas = [0] * malength
	lwmas = [0] * malength
	for period in range(1, malength):
		mas[period] = ma.calc_ma(ps, period)
		emas[period] = ma.calc_ema(ps, period)
		smas[period] = ma.calc_sma(ps, period)
		lwmas[period] = ma.calc_lwma(ps, period)
	
	log.debug('running ma strategy ...')
	starttime = datetime.datetime.now()
	
	matypes = ['MA', 'EMA', 'SMA', 'LWMA']
	
	pool = Pool(const.POOL_SIZE)
	for ft, f in [(matype, period) for matype in matypes for period in const.MA_FAST]:
		for s1t, s1 in [(matype, period) for matype in matypes for period in const.MA_SLOW1]:
			if s1 != 0 and s1 <= f: continue
			elapsed = (datetime.datetime.now() - starttime).seconds
			log.debug('== ' + str(elapsed) + ',' + ft + '_' + str(f) + ',' + s1t + '_' + str(s1) + ' ==')
			for s2t, s2 in [(matype, period) for matype in matypes for period in const.MA_SLOW2]:
				if s2 != 0 and s2 <= s1: continue
				#run 
				doTrade(pool, ft, f, s1t, s1, s2t, s2)
				
	pool.showStrategies()
	return pool.strategies[0][0]
Beispiel #21
0
def runSignal(stock, prices):
    log.debug('beginning signal for ' + stock['id'] + ' ...')

    ps = [p['close'] for p in prices]
    vols = [p['vol'] for p in prices]
    pdt = [p['dt'] for p in prices]

    vfma = getMas(stock['vfmt'], stock['vfm'], vols)
    vsma = getMas(stock['vsmt'], stock['vsm'], vols)
    afma = getMas(stock['afmt'], stock['afm'], ps)
    asma = getMas(stock['asmt'], stock['asm'], ps)
    bfma = getMas(stock['bfmt'], stock['bfm'], ps)
    bsma = getMas(stock['bsmt'], stock['bsm'], ps)

    #print(stock['afmt'] + ',' + str(stock['afm']))

    active = 'A'
    if vfma[-1] > vsma[-1]: active = 'B'
    pactive = 'A'
    if vfma[-2] > vsma[-2]: pactive = 'B'

    oapos = 0
    apos = 0
    if pactive == 'A' and afma[-2] > asma[-2]: oapos += stock['vol']
    if active == 'A' and afma[-1] > asma[-1]: apos += stock['vol']

    if active != 'A' and afma[-2] > asma[-2] and afma[-1] > asma[-1]:
        adesc = '='
    elif oapos == apos:
        adesc = str(apos)
    else:
        adesc = str(oapos) + ' -> ' + str(apos)

    obpos = 0
    bpos = 0
    if pactive == 'B' and bfma[-2] > bsma[-2]: obpos += stock['vol']
    if active == 'B' and bfma[-1] > bsma[-1]: bpos += stock['vol']

    if active != 'B' and bfma[-2] > bsma[-2] and bfma[-1] > bsma[-1]:
        bdesc = '='
    elif obpos == bpos:
        bdesc = str(bpos)
    else:
        bdesc = str(obpos) + ' -> ' + str(bpos)

    dt = datetime.datetime.now().strftime('%Y-%m-%d')
    gdt = datetime.datetime.now().strftime('%m/%d/%y')
    gtitle = gdt + ', ' + stock['id'] + ', A: ' + adesc + ', B: ' + bdesc

    dotNum = const.SIGNAL_GRAPH_NUM
    fig = plt.figure()
    ax1 = fig.add_subplot(311)
    ax1.set_ylabel('Vol: ' + str(int(vfma[-1] - vsma[-1])))
    ax1.set_title(gtitle)
    ax1.xaxis.set_major_formatter(DateFormatter('%m-%d'))
    ax1.plot_date(pdt[-dotNum:],
                  vols[-dotNum:],
                  color='yellow',
                  linestyle='-',
                  marker='',
                  label='Volume')
    ax1.plot_date(pdt[-dotNum:],
                  vfma[-dotNum:],
                  color='red',
                  linestyle='-',
                  marker='',
                  label='Volume')
    ax1.plot_date(pdt[-dotNum:],
                  vsma[-dotNum:],
                  color='blue',
                  linestyle='-',
                  marker='',
                  label='Volume')
    ax1.grid()

    ax2 = fig.add_subplot(312)
    if active == 'A':
        ax2.set_ylabel('*A: ' + str(round(afma[-1] - asma[-1], 2)))
    else:
        ax2.set_ylabel('A: ' + str(round(afma[-1] - asma[-1], 2)))
    ax2.xaxis.set_major_formatter(DateFormatter('%m-%d'))
    ax2.plot_date(pdt[-dotNum:],
                  ps[-dotNum:],
                  color='yellow',
                  linestyle='-',
                  marker='',
                  label='Price')
    ax2.plot_date(pdt[-dotNum:],
                  afma[-dotNum:],
                  color='red',
                  linestyle='-',
                  marker='',
                  label='Price')
    ax2.plot_date(pdt[-dotNum:],
                  asma[-dotNum:],
                  color='blue',
                  linestyle='-',
                  marker='',
                  label='Price')
    ax2.grid()

    ax3 = fig.add_subplot(313)
    if active == 'B':
        ax3.set_ylabel('*B:' + str(round(bfma[-1] - bsma[-1], 2)))
    else:
        ax3.set_ylabel('B:' + str(round(bfma[-1] - bsma[-1], 2)))
    ax3.xaxis.set_major_formatter(DateFormatter('%m-%d'))
    ax3.plot_date(pdt[-dotNum:],
                  ps[-dotNum:],
                  color='yellow',
                  linestyle='-',
                  marker='',
                  label='Price')
    ax3.plot_date(pdt[-dotNum:],
                  bfma[-dotNum:],
                  color='red',
                  linestyle='-',
                  marker='',
                  label='Price')
    ax3.plot_date(pdt[-dotNum:],
                  bsma[-dotNum:],
                  color='blue',
                  linestyle='-',
                  marker='',
                  label='Price')
    ax3.grid()

    resultPath = os.path.join(os.path.dirname(__file__), const.RESULT_PATH)
    plt.savefig(os.path.join(resultPath, stock['id'] + '.png'), dpi=150)
    plt.close(fig)

    return stock['symbol'] + ', ' + dt + ', ' + str(
        ps[-1]) + ', ' + active + ', ' + adesc + ', ' + bdesc
Beispiel #22
0
def runSignal(stock, prices):
    log.debug("beginning signal for " + stock["id"] + " ...")

    ps = [p["close"] for p in prices]
    vols = [p["vol"] for p in prices]
    pdt = [p["dt"] for p in prices]

    vfma = getMas(stock["vfmt"], stock["vfm"], vols)
    vsma = getMas(stock["vsmt"], stock["vsm"], vols)
    afma = getMas(stock["afmt"], stock["afm"], ps)
    asma = getMas(stock["asmt"], stock["asm"], ps)
    bfma = getMas(stock["bfmt"], stock["bfm"], ps)
    bsma = getMas(stock["bsmt"], stock["bsm"], ps)

    # print(stock['afmt'] + ',' + str(stock['afm']))

    active = "A"
    if vfma[-1] > vsma[-1]:
        active = "B"
    pactive = "A"
    if vfma[-2] > vsma[-2]:
        pactive = "B"

    oapos = 0
    apos = 0
    if pactive == "A" and afma[-2] > asma[-2]:
        oapos += stock["vol"]
    if active == "A" and afma[-1] > asma[-1]:
        apos += stock["vol"]

    if active != "A" and afma[-2] > asma[-2] and afma[-1] > asma[-1]:
        adesc = "="
    elif oapos == apos:
        adesc = str(apos)
    else:
        adesc = str(oapos) + " -> " + str(apos)

    obpos = 0
    bpos = 0
    if pactive == "B" and bfma[-2] > bsma[-2]:
        obpos += stock["vol"]
    if active == "B" and bfma[-1] > bsma[-1]:
        bpos += stock["vol"]

    if active != "B" and bfma[-2] > bsma[-2] and bfma[-1] > bsma[-1]:
        bdesc = "="
    elif obpos == bpos:
        bdesc = str(bpos)
    else:
        bdesc = str(obpos) + " -> " + str(bpos)

    dt = datetime.datetime.now().strftime("%Y-%m-%d")
    gdt = datetime.datetime.now().strftime("%m/%d/%y")
    gtitle = gdt + ", " + stock["id"] + ", A: " + adesc + ", B: " + bdesc

    dotNum = const.SIGNAL_GRAPH_NUM
    fig = plt.figure()
    ax1 = fig.add_subplot(311)
    ax1.set_ylabel("Vol: " + str(int(vfma[-1] - vsma[-1])))
    ax1.set_title(gtitle)
    ax1.xaxis.set_major_formatter(DateFormatter("%m-%d"))
    ax1.plot_date(pdt[-dotNum:], vols[-dotNum:], color="yellow", linestyle="-", marker="", label="Volume")
    ax1.plot_date(pdt[-dotNum:], vfma[-dotNum:], color="red", linestyle="-", marker="", label="Volume")
    ax1.plot_date(pdt[-dotNum:], vsma[-dotNum:], color="blue", linestyle="-", marker="", label="Volume")
    ax1.grid()

    ax2 = fig.add_subplot(312)
    if active == "A":
        ax2.set_ylabel("*A: " + str(round(afma[-1] - asma[-1], 2)))
    else:
        ax2.set_ylabel("A: " + str(round(afma[-1] - asma[-1], 2)))
    ax2.xaxis.set_major_formatter(DateFormatter("%m-%d"))
    ax2.plot_date(pdt[-dotNum:], ps[-dotNum:], color="yellow", linestyle="-", marker="", label="Price")
    ax2.plot_date(pdt[-dotNum:], afma[-dotNum:], color="red", linestyle="-", marker="", label="Price")
    ax2.plot_date(pdt[-dotNum:], asma[-dotNum:], color="blue", linestyle="-", marker="", label="Price")
    ax2.grid()

    ax3 = fig.add_subplot(313)
    if active == "B":
        ax3.set_ylabel("*B:" + str(round(bfma[-1] - bsma[-1], 2)))
    else:
        ax3.set_ylabel("B:" + str(round(bfma[-1] - bsma[-1], 2)))
    ax3.xaxis.set_major_formatter(DateFormatter("%m-%d"))
    ax3.plot_date(pdt[-dotNum:], ps[-dotNum:], color="yellow", linestyle="-", marker="", label="Price")
    ax3.plot_date(pdt[-dotNum:], bfma[-dotNum:], color="red", linestyle="-", marker="", label="Price")
    ax3.plot_date(pdt[-dotNum:], bsma[-dotNum:], color="blue", linestyle="-", marker="", label="Price")
    ax3.grid()

    resultPath = os.path.join(os.path.dirname(__file__), const.RESULT_PATH)
    plt.savefig(os.path.join(resultPath, stock["id"] + ".png"), dpi=150)
    plt.close(fig)

    return stock["symbol"] + ", " + dt + ", " + str(ps[-1]) + ", " + active + ", " + adesc + ", " + bdesc
Beispiel #23
0
def runStrategy(in_prices):
    global prices, mas, emas, smas, lwmas, vmas, vemas, vsmas, vlwmas
    log.debug('beginning vma strategy ...')
    prices = in_prices

    vols = [p['vol'] for p in prices]

    vmalength = const.VMA_MAX + 1
    vmas = [0] * vmalength
    vemas = [0] * vmalength
    vsmas = [0] * vmalength
    vlwmas = [0] * vmalength
    for period in range(1, vmalength):
        vmas[period] = ma.calc_ma(vols, period)
        vemas[period] = ma.calc_ema(vols, period)
        vsmas[period] = ma.calc_sma(vols, period)
        vlwmas[period] = ma.calc_lwma(vols, period)

    ps = [p['close'] for p in prices]

    malength = const.MA_MAX + 1
    mas = [0] * malength
    emas = [0] * malength
    smas = [0] * malength
    lwmas = [0] * malength
    for period in range(1, malength):
        mas[period] = ma.calc_ma(ps, period)
        emas[period] = ma.calc_ema(ps, period)
        smas[period] = ma.calc_sma(ps, period)
        lwmas[period] = ma.calc_lwma(ps, period)

    log.debug('running ma strategy ...')
    starttime = datetime.datetime.now()

    pool = Pool(const.POOL_SIZE)

    for vft, vf in [(matype, period) for matype in const.VMA_TYPES
                    for period in const.VMA_FAST]:
        for vst, vs in [(matype, period) for matype in const.VMA_TYPES
                        for period in const.VMA_SLOW]:
            if vs != 0 and vs <= vf: continue
            poola = Pool(const.POOL_SIZE)
            poolb = Pool(const.POOL_SIZE)

            for ft, f in [(matype, period) for matype in const.MA_TYPES
                          for period in const.MA_FAST]:
                for s1t, s1 in [(matype, period) for matype in const.MA_TYPES
                                for period in const.MA_SLOW1]:
                    if s1 != 0 and s1 <= f: continue
                    elapsed = (datetime.datetime.now() - starttime).seconds
                    log.debug('== ' + str(elapsed) + ',' + vft + '_' +
                              str(vf) + ',' + vst + '_' + str(vs) + ',' + ft +
                              '_' + str(f) + ',' + s1t + '_' + str(s1) + ' ==')

                    doTrade(poola, vft, vf, vst, vs, ft, f, s1t, s1, '', 0, '',
                            0)
                    doTrade(poolb, vft, vf, vst, vs, '', 0, '', 0, ft, f, s1t,
                            s1)

            for ia in range(len(poola.strategies)):
                for ib in range(len(poolb.strategies)):
                    sa = poola.strategies[ia]
                    sb = poolb.strategies[ib]
                    if sa[0] == 0 or sb[0] == 0: continue
                    t = doTrade(pool, vft, vf, vst, vs, sa[0].args[0],
                                sa[0].args[1], sa[0].args[2], sa[0].args[3],
                                sb[0].args[4], sb[0].args[5], sb[0].args[6],
                                sb[0].args[7])

    pool.showStrategies()
    return pool.strategies[0][0]