Ejemplo n.º 1
0
    def __init__(self):
        super().__init__()
        self.broker.set_coc(True)

        self.dataclose={}
        self.dataopen={}
        self.datalow={}
        self.datahigh={}
        self.volume={}
        self.order={}
        self.trade_sig={}
        self.signal1={}
        self.signal2={} 
        self.signal3={} 
        self.close_sig={}
        self.w_portfolio=w_portfolio
        self.w_close=[0]*num
        self.option_ratio=option_ratio[opt]
        self.sma5 = bt.indicators.SMA(self.data, period=5)
        self.sma10 = bt.indicators.SMA(self.data, period=10) 
        self.sma20 = bt.indicators.SMA(self.data, period=20)
        self.sma30 = bt.indicators.SMA(self.data, period=30)
        self.sma60 = bt.indicators.SMA(self.data, period=60)         
        vix=Vix()
        vixRatio10=VixRatio10()      
        crossOver=bt.Or(bt.And(self.sma5/self.sma10<1,self.sma20/self.sma60<1,\
                         vixRatio10<=vixRatio10_buy[opt]),\
                         bt.And(self.sma5/self.sma10<1,self.sma20/self.sma60<1,\
                         vix<=self.p.vix_upBound))
        
        crossDown=bt.Or(vixRatio10>=self.p.vixRatio10_sell,\
                        self.sma5/self.sma10>self.p.smaRatio_sell)

        if opt6:
            percent=Percentile()        
            cover_call=percent<=0.2
            protect_put=percent>=0.8
            collar=bt.And(percent<0.8,percent>0.2)

        # Keep a reference to the "close" line in the data dataseries
        for i,data in enumerate(self.datas):
            self.dataclose[i] = self.datas[i].close
            self.dataopen[i]=self.datas[i].open
            self.datalow[i]=self.datas[i].low
            self.datahigh[i]=self.datas[i].high
            self.volume[i]=abs(self.datas[i].volume)
    
            # To keep track of pending orders and buy price/commission
            self.order[i] = None
            self.buyprice = None
            self.buycomm = None
            self.bar_executed=None
            self.trade_sig[i]=crossOver          
            self.close_sig[i]=crossDown
            
            if opt6:
                self.signal1[i]=cover_call
                self.signal2[i]=protect_put
                self.signal3[i]=collar  
Ejemplo n.º 2
0
 def __init__(self):
     self.sma5 = btind.SimpleMovingAverage(period=5)  # 5日均线
     self.sma10 = btind.SimpleMovingAverage(period=10)  # 10日均线
     # bt.And 中所有条件都满足时返回 1;有一个条件不满足就返回 0
     self.And = bt.And(self.data > self.sma5, self.data > self.sma10,
                       self.sma5 > self.sma10)
     # bt.Or 中有一个条件满足时就返回 1;所有条件都不满足时返回 0
     self.Or = bt.Or(self.data > self.sma5, self.data > self.sma10,
                     self.sma5 > self.sma10)
     # bt.If(a, b, c) 如果满足条件 a,就返回 b,否则返回 c
     self.If = bt.If(self.data > self.sma5, 1000, 5000)
     # bt.All,同 bt.And
     self.All = bt.All(self.data > self.sma5, self.data > self.sma10,
                       self.sma5 > self.sma10)
     # bt.Any,同 bt.Or
     self.Any = bt.Any(self.data > self.sma5, self.data > self.sma10,
                       self.sma5 > self.sma10)
     # bt.Max,返回同一时刻所有指标中的最大值
     self.Max = bt.Max(self.data, self.sma10, self.sma5)
     # bt.Min,返回同一时刻所有指标中的最小值
     self.Min = bt.Min(self.data, self.sma10, self.sma5)
     # bt.Sum,对同一时刻所有指标进行求和
     self.Sum = bt.Sum(self.data, self.sma10, self.sma5)
     # bt.Cmp(a,b), 如果 a>b ,返回 1;否则返回 -1
     self.Cmp = bt.Cmp(self.data, self.sma5)
Ejemplo n.º 3
0
    def exit_indicator(self, ind, params, plot=True):
        """
        Function that returns and binary exit confirmation indicator for simple strategy logic useage
        :param ind: Indicator choice (string type) - must be in list of self.indicators
        :param params: Parameters for Indicator - must have right amount of parameters or error thrown
        :return: Binary Trade Exit Indicator

        """

        # Check Inputs
        self.check_input(ind, params)

        # Create Logic Blocks for Binaries

        if ind == 'heikenashi':
            heiken = HeikenAshi(self.data, plot=plot)
            ups = [
                heiken.signal(-i if i > 0 else i) == 1.0
                for i in range(0, params[0])
            ]
            downs = [
                heiken.signal(-i if i > 0 else i) == -1.0
                for i in range(0, params[0])
            ]
            exit = bt.Or(bt.All(*ups), bt.All(*downs)) * heiken.signal
            return exit

        elif ind == 'ssl':
            ssl = SSLChannel(self.data, period=params[0], plot=plot)
            exit = bt.Cmp(ssl.sslu, ssl.ssld)
            return exit

        elif ind == 'itrend':

            itrend = iTrend(self.data, period=params[0], plot=plot)
            exit = bt.Cmp(itrend.trigger, itrend.itrend)
            return exit

        elif ind == 'mama':

            mama = MAMA(self.data, fast=params[0], slow=params[1], plot=plot)
            exit = bt.Cmp(mama.MAMA, mama.FAMA)
            return exit

        elif ind == 'dosc':
            dosc = DecyclerOscillator(self.data,
                                      hp_period=params[0],
                                      plot=plot)
            exit = bt.Cmp(dosc.osc, 0.0)
            return exit
    def __init__(self):
        # Let the indicator get enough data
        self.addminperiod(self.p.period)

        # Plot horizontal Line
        self.plotinfo.plotyhlines = [0]

        # Aliases to avoid long lines
        c = self.data.close
        h = self.data.high
        l = self.data.low
        v = self.data.volume

        self.data.ad = bt.If(bt.Or(bt.And(c == h, c == l), h == l), 0, ((2 * c - l - h) / (h - l)) * v)
        self.lines.money_flow = bt.indicators.SumN(self.data.ad, period=self.p.period) / bt.indicators.SumN(
            self.data.volume, period=self.p.period)