예제 #1
0
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
예제 #2
0
    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
예제 #3
0
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
예제 #4
0
    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