def algoStrategy(self, ohlc): sp = StrategyPattern() tsup = TradeSupport() buysg,sellsg = sp.divergencyCross(self.pdi, self.ndi, 2) # TODO closesg = sp.covergency(self.pdi, self.ndi, 2) #diff = self.pdi/self.ndi #process these signale for backtest ''' signal = map(sp.mergeSignal, buysg,sellsg,closesg) ohlc['signal'] = signal ''' ohlc['dmibuy'] = buysg ohlc['dmisell'] = sellsg ''' if (self.debug): ohlc['buysignal'] = buysg ohlc['sellsignal'] = sellsg ohlc['pdi'] = self.pdi ohlc['ndi'] = self.ndi ohlc['adx'] = self.adx print ohlc ''' self.ind['dmi_dif'] = self.pdi[-1]/self.ndi[-1] #ohlc['dmi_dif'] = self.pdi/self.ndi tsup.getLastSignal(buysg,sellsg, self.ind,'dmi_buy','dmi_sell') pass
def __init__(self): BaseIndPx.__init__(self) self.ind_sctr = ind_sctr() self.ind_kdj = ind_kdj() self.ind_dmi = ind_dmi() self.sp = StrategyPattern() self.tsup = TradeSupport() pass
def algoStrategy(self, ohlc): # price cross above MA10 and crosee below MA10 sp = StrategyPattern() tsup = TradeSupport() buysg1, sellsg1 = sp.cross(self.avgTypEmaLst, self.avgHacEmaLst) #offset=0 buysg2, sellsg2 = sp.compare(ohlc['Close'], ohlc['Open']) buysg = sp.combineAndSignal(buysg1, buysg2) sellsg = sp.combineAndSignal(sellsg1, sellsg2) closesg = sp.covergency1(self.avgTypEmaLst, self.avgHacEmaLst) signal = map(sp.mergeSignal, buysg, sellsg, closesg) ohlc['signal'] = signal #ohlc['buy1'] = buysg1 #ohlc['buy2'] = buysg2 ohlc['buy3'] = buysg #ohlc['sell1'] = sellsg1 #ohlc['sell2'] = sellsg2 ohlc['sell3'] = sellsg ohlc['close'] = closesg print ohlc #tsup.getLastSignal(buysg,sellsg,self.ind,'ma10_buy','ma10_sell') #too lag, how about px cross MA50 ''' if (self.ma50 and self.ma200): sp.initData(self.ma50, self.ma200, 200) self.ind['ma50_200_buy'] = sp.crossAbove() self.ind['ma50_200_sell'] = sp.crossBelow() ''' pass
def algoStrategy(self, ohlc): sp = StrategyPattern() tsup = TradeSupport() buysg, sellsg = sp.crossValue(self.quolst, self.shortquolst, 0, 0, 2) #process these signale for backtest signal = map(sp.mergeSignal, buysg, sellsg, "") ohlc['signal'] = signal if (self.debug): ohlc['buysignal'] = buysg ohlc['sellsignal'] = sellsg ohlc['fast'] = self.quolst ohlc['slow'] = self.shortquolst print ohlc tsup.getLastSignal(buysg, sellsg, self.ind, 'quo_buy', 'quo_sell') pass
class st_greenline(BaseIndPx): def __init__(self): BaseIndPx.__init__(self) self.ind_sctr = ind_sctr() self.ind_kdj = ind_kdj() self.ind_dmi = ind_dmi() self.sp = StrategyPattern() self.tsup = TradeSupport() pass #main process routine def runIndicator(self,symbol,ohlc,param={}): ''' my STO implementation is more promise ''' self.setupParam(param) #parent func self.ind_sctr.runIndicator(symbol,ohlc,param) self.ind['sctr'] = self.ind_sctr.ind['sctr'] self.ind_kdj.runIndicator(symbol,ohlc,param) self.ind['money_wave'] = self.ind_kdj.ind['slow_d'] buysg,sellsg = self.sp.crossValue(self.ind_sctr.score, self.ind_sctr.score,0,0,2) self.tsup.getLastSignal(buysg,sellsg, self.ind,'sctr_buy','sctr_sell') #self.ind_dmi.runIndicator(symbol,ohlc,param) #print self.ind_kdj. #df = STO(ohlc,5) #df = ADX(ohlc,14,14) #print df #self.algoStrategy(ohlc) pass def runScan(self,df): if not self.param: col = df.columns.values return df,col total = len(df) df['sctrrank'] = df['sctr'].rank(ascending=1)/total*100 df.sort_index(by="sctrrank",inplace=True,ascending=False) col = df.columns.values df,fcols = self.mtd.evalCriteria(df,self.param,col) return df,fcols
class st_k(BaseIndPx): def __init__(self): self.sp = StrategyPattern() self.tsup = TradeSupport() def setupParam(self, param): pass def _algo(self, ohlc): pass def runIndicator(self, symbol, ohlc, param={}): ind_ma = importStrategy("ind_ma", symbol, ohlc) #s1 = ind_ma.ma50 s1 = pandas.rolling_mean(ind_ma.ma50, 3) #.tolist() s2 = s1.shift(2) delta = s1 - s2 #ohlc['delta'] = delta # ohlc buysg, sellsg = self.sp.crossValue(delta, delta, 0, 0, 1) ohlc['ag50b'] = buysg ohlc['ag50s'] = sellsg self.tsup.getLastSignal(buysg, sellsg, self.ind, 'ag50b','ag50s') s1 = pandas.rolling_mean(ind_ma.ma10, 3) #.tolist() s2 = s1.shift(2) delta = s1 - s2 #ohlc['delta'] = delta # ohlc buysg, sellsg = self.sp.crossValue(delta, delta, 0, 0, 2) ohlc['ag10b'] = buysg ohlc['ag10s'] = sellsg # get all signal idx ag10bidx, ag10sidx = self.tsup.getLastSignal(buysg, sellsg, self.ind, 'ag10b', 'ag10s') vol10bidx = ind_ma.ind['vol10b'] ma10bidx = ind_ma.ind['ma10b'] pass
def algoStrategy(self, symbol, ohlc): # price cross above MA10 and crosee below MA10 sp = StrategyPattern() tsup = TradeSupport() px = ohlc['Adj Close'] if (not self.ma10.empty): # not using divergencyCross buysg, sellsg = sp.cross(px, self.ma10, self.nbar) tsup.getLastSignal(buysg, sellsg, self.ind, 'ma10b', 'ma10s') ohlc['ma10b'] = buysg ohlc['ma10s'] = sellsg #ohlc['ma10']=self.ma10 ma50bsg = [] if not self.ma50.empty: buysg, sellsg = sp.cross(px, self.ma50, self.nbar) tsup.getLastSignal(buysg, sellsg, self.ind, 'ma50b', 'ma50s') ohlc['ma50b'] = buysg ohlc['ma50s'] = sellsg ma50bsg = buysg s1 = pandas.rolling_mean(self.ma50, 3) #.tolist() s2 = s1.shift(2) delta = s1 - s2 buysg, sellsg = sp.crossValue(delta, delta, 0, 0, 1) tsup.getLastSignal(buysg, sellsg, self.ind, 'ag50b', 'ag50s') ohlc['ag50b'] = buysg ohlc['ag50s'] = sellsg # TODO # #buysg,sellsg = sp.supportline(px, self.ma50, self.nbar) # tsup.getLastSignal(buysg, sellsg, self.ind, 'sup50', 'res50') if not self.ma50.empty and not self.ma10.empty: buysg, sellsg = sp.cross(self.ma10, self.ma50, self.nbar) tsup.getLastSignal(buysg, sellsg, self.ind, 'ma1050b', 'ma1050s') ohlc['ma1050b'] = buysg ohlc['ma1050s'] = sellsg # find the distance #self.dist50 = 20 lastsell = -self.dist50 distsg = [] for idx, val in enumerate(ohlc['ma1050s']): sig = "" if val == "sell": lastsell = idx # print "dist50",idx buyval = ma50bsg[idx] if buyval == "buy": ''' # TODO test if symbol=="QCOM": print "dist50", idx, lastsell, self.dist50 ''' if lastsell >= 0 and idx - lastsell > self.dist50: sig = "buy" else: sig = "" distsg.append(sig) ohlc['dist50'] = distsg tsup.getLastSignal(distsg, [], self.ind, 'dist50', '') # ready to cross above ''' flag1 = (self.ma10[-1] > self.ma50[-1]*0.97) dif1 = (self.ma50[-1]-self.ma10[-1]) dif2 = (self.ma50[-2]-self.ma10[-2]) dif3 = (self.ma50[-3]-self.ma10[-3]) flag2 = (dif1<dif2) and (dif2<dif3) and (dif1>0) if (flag1 and flag2): self.ind['ma1050e']=1 else: self.ind['ma1050e']=0 ''' #too lag, how about px cross MA50 ''' l1=len(self.ma50) l2=len(self.ma200) if (l1!=l2): print l1,l2 print self.ma200 print "========" print self.ma50 ''' l1 = len(self.ma50) l2 = len(self.ma200) #print l1,l2,symbol if (l1 == l2 and l1 > 0): #print "test golder and death" buysg, sellsg = sp.cross(self.ma50, self.ma200, self.nbar) ohlc['ma50200s'] = sellsg tsup.getLastSignal(buysg, sellsg, self.ind, 'ma50200b', 'ma50200s') ''' lastsell = -self.dist200 distsg = [] for idx, val in enumerate(ohlc['ma50200s']): sig = "" if val == "sell": lastsell = idx buyval = buysg[idx] if (buyval == "buy"): #TODO test if symbol=="QCOM": print "dist200",idx,lastsell,self.dist200 if lastsell >= 0 and idx-lastsell > self.dist200: sig = "buy" else: sig = "" distsg.append(sig) ohlc['dist200'] = distsg tsup.getLastSignal(distsg, [], self.ind, 'dist200', '') ''' #support line #print self.ma50 #volume buysg, sellsg = sp.cross2factors(px, self.volma20ra, self.ma10, self.volra, 2) tsup.getLastSignal(buysg, sellsg, self.ind, 'vol10b', 'vol10s') ohlc['vol10b'] = buysg ohlc['vol10s'] = sellsg #debug #ohlc['ma50']=self.ma50 #print ohlc pass
def algoStrategy(self, ohlc): # price cross above MA10 and crosee below MA10 sp = StrategyPattern() tsup = TradeSupport() px = ohlc['Adj Close'] if (self.ma10): # not using divergencyCross buysg, sellsg = sp.cross(px, self.ma10, self.nbar) tsup.getLastSignal(buysg, sellsg, self.ind, 'ma10b', 'ma10s') ''' ohlc['ma10b']=buysg ohlc['ma10s']=sellsg ohlc['ma10']=self.ma10 ''' if (self.ma50): buysg, sellsg = sp.cross(px, self.ma50, self.nbar) tsup.getLastSignal(buysg, sellsg, self.ind, 'ma50b', 'ma50s') if (self.ma50 and self.ma10): buysg, sellsg = sp.cross(self.ma10, self.ma50, self.nbar) tsup.getLastSignal(buysg, sellsg, self.ind, 'ma1050b', 'ma1050s') # ready to cross above flag1 = (self.ma10[-1] > self.ma50[-1] * 0.97) dif1 = (self.ma50[-1] - self.ma10[-1]) dif2 = (self.ma50[-2] - self.ma10[-2]) dif3 = (self.ma50[-3] - self.ma10[-3]) flag2 = (dif1 < dif2) and (dif2 < dif3) and (dif1 > 0) if (flag1 and flag2): self.ind['ma1050e'] = 1 else: self.ind['ma1050e'] = 0 #too lag, how about px cross MA50 if (self.ma50 and not self.ma200.empty): #print "test golder and death" buysg, sellsg = sp.cross(self.ma50, self.ma200, self.nbar) #print sellsg tsup.getLastSignal(buysg, sellsg, self.ind, 'ma50200b', 'ma50200s') #support line buysg, sellsg = sp.supportline(px, self.ma50, self.nbar) tsup.getLastSignal(buysg, sellsg, self.ind, 'sup50', 'res50') #print ohlc pass
def __init__(self): # price cross above MA10 and crosee below MA10 self.sp = StrategyPattern() self.tsup = TradeSupport()
def _combineSignal(self, ohlc, buydct, selldct): flag = True for key in buydct: if key not in ohlc: flag = False for key in selldct: if key not in ohlc: flag = False if not flag: ohlc['signal'] = ['']*len(ohlc) return tsup = TradeSupport() def cleardct(dct): for key in dct: dct[key] = 0 pass def buyspread(dct): minv = 100000 maxv = 0 spread = 6 for key in dct: minv = min(dct[key], minv) maxv = max(dct[key], maxv) if (maxv-minv) > spread: return False else: return True pass #print buydct,selldct cleardct(buydct) cleardct(selldct) signallst = [] idx = 1 for row_index, row in ohlc.iterrows(): signal = "" for bs in buydct: if row[bs] == "buy": buydct[bs] = idx for ss in selldct: if row[ss] == "sell": selldct[ss] = 1 buy_flag = True for bs in buydct: if buydct[bs] == 0: buy_flag = False all_sell_flag = True one_sell_flag = False for ss in selldct: if selldct[ss] != 1: all_sell_flag = False else: one_sell_flag = True if one_sell_flag: # reset buydict buy_flag = False for bs in buydct: buydct[bs] = 0 if one_sell_flag: signal = "sell" for ss in selldct: selldct[ss] = 0 if buy_flag: if buyspread(buydct): signal = "buy" for key in buydct: buydct[key] = 0 idx += 1 signallst.append(signal) ohlc['brkout'] = signallst tsup.getLastSignal(signallst, signallst, self.ind, 'brkoutb', 'brkouts')
def __init__(self): self.sp = StrategyPattern() self.tsup = TradeSupport()