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]
def calc_kd(prices, kPeriod = 5, dPeriod = 3, slowing =3): phighs = [p['high'] for p in prices] plows = [p['low'] for p in prices] ps = [p['close'] for p in prices] l = len(prices) highest = [0] * l lowest = [0] * l kds = {} kds['k'] = [0] * l for i in range(kPeriod - 1, l): highest[i] = max(phighs[i-kPeriod+1 : i+1]) lowest[i] = min(plows[i-kPeriod+1 : i+1]) if i >= kPeriod + slowing - 1: highsum, lowsum = 0, 0 for j in range(i - slowing + 1, i + 1): highsum += highest[j] - lowest[j] lowsum += ps[j] - lowest[j] #print i, highest[i], lowest[i], prices[j], highsum, lowsum #print i, highsum if highsum != 0: kds['k'][i] = round(lowsum / highsum * 100, 5) #print i, kds['k'][i] #print kds['k'] kds['d'] = ma.calc_ma(kds['k'], dPeriod) return kds
def calc_kd(prices, kPeriod=5, dPeriod=3, slowing=3): phighs = [p['high'] for p in prices] plows = [p['low'] for p in prices] ps = [p['close'] for p in prices] l = len(prices) highest = [0] * l lowest = [0] * l kds = {} kds['k'] = [0] * l for i in range(kPeriod - 1, l): highest[i] = max(phighs[i - kPeriod + 1:i + 1]) lowest[i] = min(plows[i - kPeriod + 1:i + 1]) if i >= kPeriod + slowing - 1: highsum, lowsum = 0, 0 for j in range(i - slowing + 1, i + 1): highsum += highest[j] - lowest[j] lowsum += ps[j] - lowest[j] #print i, highest[i], lowest[i], prices[j], highsum, lowsum #print i, highsum if highsum != 0: kds['k'][i] = round(lowsum / highsum * 100, 5) #print i, kds['k'][i] #print kds['k'] kds['d'] = ma.calc_ma(kds['k'], dPeriod) return kds
def calc_macd(prices, fast = 12, slow = 26, sign = 9): ps = [p['close'] for p in prices] macds = {} macds['fast'] = ma.calc_ema(ps, fast) macds['slow'] = ma.calc_ema(ps, slow) macds['macd'] = map(lambda f,s: round(f - s, 5), macds['fast'], macds['slow']) macds['sign'] = ma.calc_ma(macds['macd'], sign) #macds['macd'] = map(lambda f,s: round(f - s, 5), macds['dif'], macds['sign']) return macds
def calc_macd(prices, fast=12, slow=26, sign=9): ps = [p['close'] for p in prices] macds = {} macds['fast'] = ma.calc_ema(ps, fast) macds['slow'] = ma.calc_ema(ps, slow) macds['macd'] = map(lambda f, s: round(f - s, 5), macds['fast'], macds['slow']) macds['sign'] = ma.calc_ma(macds['macd'], sign) #macds['macd'] = map(lambda f,s: round(f - s, 5), macds['dif'], macds['sign']) return macds
def getMas(matype, period, ps): if matype == 'MA': return ma.calc_ma(ps, period) elif matype == 'EMA': return ma.calc_ema(ps, period) elif matype == 'SMA': return ma.calc_sma(ps, period) elif matype == 'LWMA': return ma.calc_lwma(ps, period) else: return None
def getMas(matype, period, ps): if matype == "MA": return ma.calc_ma(ps, period) elif matype == "EMA": return ma.calc_ema(ps, period) elif matype == "SMA": return ma.calc_sma(ps, period) elif matype == "LWMA": return ma.calc_lwma(ps, period) else: return None
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]
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]