Exemple #1
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
Exemple #2
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
Exemple #3
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