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): 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)
def __init__(self): movav = self.p.movav(self.data, period=self.p.period) self.l.overunder = bt.Cmp(movav, self.data)
def volume_indicator(self, ind, params, plot=True): """ Function that returns and binary volume 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 Volume Indicator """ # Check Inputs self.check_input(ind, params) # Create Logic Blocks if ind == 'cvi': chaikin = ChaikinVolatility(self.data, ema_period=params[0], roc_period=params[1], plot=plot) volume = chaikin.cvi > 0.0 return volume elif ind == 'tdfi': tdfi = TrendDirectionForceIndex(self.data, period=params[0], plot=plot) volume1 = bt.If(tdfi.ntdf > params[1], 1.0, 0.0) volume2 = bt.If(tdfi.ntdf < -params[1], -1.0, 0.0) volume = volume1 + volume2 return volume elif ind == 'wae': wae = WaddahAttarExplosion(self.data, sensitivity=params[0], fast=params[1], slow=params[2], channel=params[3], mult=params[4], dead=params[5], plot=plot) # Buy Volume buy = bt.All(wae.utrend > wae.exp, wae.utrend > wae.dead, wae.exp > wae.dead) sell = bt.All(wae.dtrend > wae.exp, wae.dtrend > wae.dead, wae.exp > wae.dead) volume = buy - sell return volume elif ind == 'squeeze': sqz = SqueezeVolatility(self.data, period=params[0], mult=params[1], period_kc=params[2], mult_kc=params[3], movav=params[4]) return sqz.sqz elif ind == 'damiani': dv = DamianiVolatmeter(self.data, atr_fast=params[0], std_fast=params[1], atr_slow=params[2], std_slow=params[3], thresh=params[4], lag_supress=params[5]) volume = bt.If(bt.Cmp(dv.v, dv.t) > 0, 1.0, 0.0) return volume
def entry_indicator(self, ind, params, plot=True): """ Function that returns and binary entry 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 Entry Indicator """ # Check Inputs self.check_input(ind, params) # Create Logic Blocks for Each Indicator if ind == 'itrend': itrend = iTrend(self.data, period=params[0], plot=plot) entry = bt.Cmp(itrend.trigger, itrend.itrend) return entry elif ind == 'cybercycle': cc = CyberCycle(self.data, period=params[0], plot=plot) entry = bt.Cmp(cc.trigger, cc.cycle) return entry elif ind == 'adaptivecybercycle': acc = AdaptiveCyberCycle(self.data, period=params[0], lag=params[1], plot=plot) entry = bt.Cmp(acc.signal, acc.trigger) return entry elif ind == 'ssl': ssl = SSLChannel(self.data, period=params[0], plot=plot) entry = bt.Cmp(ssl.sslu, ssl.ssld) return entry elif ind == 'aroon': aud = bt.indicators.AroonUpDown(self.data, period=params[0], plot=plot) entry = bt.Cmp(aud.aroonup, aud.aroondown) return entry elif ind == 'ttf': ttf = TrendTriggerFactor(self.data, period=params[0], plot=plot) long = bt.indicators.CrossUp(ttf.ttf, -100.0, plot=plot) short = bt.indicators.CrossDown(ttf.ttf, 100.0, plot=plot) entry = long + -1 * short return entry elif ind == 'tdfi': tdfi = TrendDirectionForceIndex(self.data, period=params[0], plot=plot) e1 = bt.If(tdfi.ntdf > params[1], 1.0, 0.0) e2 = bt.If(tdfi.ntdf < -params[1], -1.0, 0.0) entry = e1 + e2 return entry elif ind == 'cmf': chaikin = ChaikinMoneyFlow(self.data, period=params[0], plot=plot) entry = bt.Cmp(chaikin.money_flow, 0.0) return entry elif ind == 'ash': ash = ASH(self.data, period=params[0], smoothing=params[1], mode=params[2], rsifactor=params[3], movav=params[4], smoothav=params[5], pointsize=params[6]) entry = bt.Cmp(ash.bulls, ash.bears) return entry elif ind == 'roof': roof = RoofingFilter(self.data, hp_period=params[0], ss_period=params[1], smooth=params[2], plot=plot) entry = bt.Cmp(roof.iroof, 0.0) return entry elif ind == 'mama': mama = MAMA(self.data, fast=params[0], slow=params[1], plot=plot) entry = bt.Cmp(mama.MAMA, mama.FAMA) return entry elif ind == 'dosc': dosc = DecyclerOscillator(self.data, hp_period=params[0], plot=plot) entry = bt.Cmp(dosc.osc, 0.0) return entry elif ind == 'idosc': idosc = iDecycler(self.data, hp_period=params[0], smooth=params[1], plot=plot) entry = bt.Cmp(idosc.idosc, 0.0) return entry elif ind == 'schaff': schaff = SchaffTrendCycle(self.data, fast=params[0], slow=params[1], cycle=params[2], factor=params[3], plot=plot) buy = bt.indicators.CrossUp(schaff.schaff, 25.0, plot=plot) sell = bt.indicators.CrossDown(schaff.schaff, 75.0, plot=plot) binary = buy - sell entry = SignalFiller(binary, plot=plot).signal return entry