예제 #1
0
    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    
예제 #2
0
 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
예제 #3
0
    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
예제 #4
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
예제 #5
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
예제 #6
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
예제 #7
0
    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
예제 #8
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
예제 #9
0
    def algoFunc(self, df0):
        length = 14
        prevHigh = 0.
        prevLow = 0.
        plusDM = 0.
        minusDM = 0.
        index = 0
        tr = []
        pdm = []
        ndm = []
        #ratio=[]
        sp = StrategyPattern()
        #debug
        #length = len(df0)-1
        #lastpx = df0['Adj Close'].iloc[len(df0)-1]

        ###
        #start = time.time()

        for row_index, row in df0.iterrows():
            #print index
            aclose = row['Adj Close']
            close = row['Close']
            high = row['High']
            low = row['Low']
            tr.append(sp.trueRange(high, aclose, low))

            ###
            '''
            if (close>lastpx):
                r = (close-lastpx)/(length-index)
                ratio.append(r)
            else:
                ratio.append(0.)
            '''
            ####
            if (index > 0):
                hiDiff = high - prevHigh
                loDiff = prevLow - low
                if hiDiff > loDiff and hiDiff > 0:
                    plusDM = hiDiff
                else:
                    plusDM = 0

                if loDiff > hiDiff and loDiff > 0:
                    minusDM = loDiff
                else:
                    minusDM = 0

                pdm.append(plusDM)
                ndm.append(minusDM)
            else:
                pdm.append(0.)
                ndm.append(0.)

            prevHigh = high
            prevLow = low
            index += 1
        '''        
        atr = sp.movingAverage(pandas.Series(tr),length)
        self.pdi = 100 * sp.movingAverage(pandas.Series(pdm),length) / atr
        self.ndi = 100 * sp.movingAverage(pandas.Series(ndm),length) / atr
        dx = self.pdi.combine(self.ndi,calcDX2)
        self.adx = sp.movingAverage(dx,length)
        '''
        atr1 = [float('nan') for _ in range(length - 1)]
        atr2 = sp.wma(tr, length)
        atr = atr1 + atr2
        tmpPdi = atr1 + sp.wma(pdm, length)
        tmpNdi = atr1 + sp.wma(ndm, length)
        #print tmpPdi
        self.pdi = [100 * ai / bi for ai, bi in zip(tmpPdi, atr)]
        self.ndi = [100 * ai / bi for ai, bi in zip(tmpNdi, atr)]
        dx = map(calcDX2, self.pdi, self.ndi)
        self.adx = atr1 + sp.wma(dx, length)
        #debug
        #df0['ang'] = ratio

        df0['di+'] = self.pdi
        df0['di-'] = self.ndi
        df0['adx'] = self.adx

        #print df0
        pass
예제 #10
0
 def __init__(self):
     # price cross above MA10 and crosee below MA10
     self.sp = StrategyPattern()
     self.tsup = TradeSupport()
예제 #11
0
 def __init__(self):
     self.sp = StrategyPattern()
     self.tsup = TradeSupport()