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): self.ma = bt.ind.EMA(period=10) self.cross = bt.ind.CrossOver(self.datas[0].close, self.ma) # Single logic self.lg = bt.Log(self.datas[0].close) self.cl = bt.Ceiling(self.datas[0].close) self.fl = bt.Floor(self.datas[0].close) self.cross_abs = bt.Abs(self.cross) # Check Multi still works self.mx = bt.Max(self.datas[0].close, self.datas[0].open)
def __init__(self): o = self.data.open h = self.data.high l = self.data.low c = self.data.close self.l.ha_close = ha_close = (o + h + l + c) / 4.0 self.l.ha_open = ha_open = (self.l.ha_open(-1) + ha_close(-1)) / 2.0 self.l.ha_high = bt.Max(h, ha_open, ha_close) self.l.ha_low = bt.Min(l, ha_open, ha_close) super(HeikinAshi, self).__init__()
def __init__(self): h0l1 = abs(self.data.high(0) - self.data.low(-1)) vm_plus = bt.ind.SumN(h0l1, period=self.p.period) l0h1 = abs(self.data.low(0) - self.data.high(-1)) vm_minus = bt.ind.SumN(l0h1, period=self.p.period) h0c1 = abs(self.data.high(0) - self.data.close(-1)) l0c1 = abs(self.data.low(0) - self.data.close(-1)) h0l0 = abs(self.data.high(0) - self.data.low(0)) tr = bt.ind.SumN(bt.Max(h0l0, h0c1, l0c1), period=self.p.period) self.l.vi_plus = vm_plus / tr self.l.vi_minus = vm_minus / tr
def __init__(self): wick_range = self.data.high - self.data.low body_high = bt.Max(self.data.close, self.data.open) body_low = bt.Min(self.data.close, self.data.open) body_range = body_high - body_low wick_buy = (body_low - self.data.low) >= ( self.p.wick_multiplier_min * body_range) wick_sell = (self.data.high - body_high) >= ( self.p.wick_multiplier_min * body_range) # be careful, if wick range = 0 then close-low = 0 ; so avoiding divide # by zero list this is correct close_percent = (self.data.close - self.data.low) / bt.If( wick_range == 0.0, 0.1, wick_range) close_buy = (close_percent >= (1 - self.p.close_percent_max)) close_sell = (close_percent <= self.p.close_percent_max) self.lines.wick = bt.If(bt.And(wick_buy, close_buy), self.data.low, bt.If(bt.And(wick_sell, close_sell), self.data.high, np.NaN) )
def __init__(self): self.strat = self._owner # alias for clarity self.smaShort = bt.ind.SMA(self.data, period=self.p.period_short) self.smaMid = bt.ind.SMA(self.data, period=self.p.period_middle) self.smaLong = bt.ind.SMA(self.data, period=self.p.period_long) self.emaShort = bt.ind.EMA(self.data, period=self.p.period_short) self.emaMid = bt.ind.EMA(self.data, period=self.p.period_middle) self.emaLong = bt.ind.EMA(self.data, period=self.p.period_long) self.boll = bt.ind.BollingerBands(self.data) self.bollDiff = self.boll.top - self.boll.bot self.bollSignal = bt.ind.EMA(self.bollDiff, period=self.p.period_short) self.l.BollRange = bt.If(bt.And(self.bollDiff/self.p.boll_range_ThresholdValue > self.bollSignal, self.data > self.boll.mid), 1, 0) self.l.BollRange = bt.If( bt.And(self.bollDiff / self.p.boll_range_ThresholdValue > self.bollSignal, self.data < self.boll.mid), -1, self.l.BollRange) self.bollHisto = self.bollDiff - self.bollSignal jxmj_cs = abs(self.data / self.smaShort - 1)*100 jxmj_sm = abs(self.smaShort / self.smaMid - 1) * 100 jxmj_ml = abs(self.smaMid / self.smaLong - 1) * 100 self.JXMJ = JXMJDayFsumIndicator(bt.Max(jxmj_cs,jxmj_sm,jxmj_ml), period=self.p.period_jxmj).l.JXMJFsum self.l.JXMJ = bt.If( bt.And(self.JXMJ > self.p.jxmj_ThresholdValue, self.smaMid > self.smaLong), 1, 0) self.l.JXMJHoldState = self.data - self.data self.l.JXMJBuySellPoint = self.data - self.data self.lastClosePrice = 0 dfgl_cs = self.data - self.emaShort dfgl_sm = self.emaShort - self.emaMid dfgl_ml = self.emaMid - self.emaLong self.dfgl = bt.ind.PercentRank(abs(dfgl_sm), period=self.p.period_dfgl) self.l.DFGL = bt.If( bt.And(self.dfgl > self.p.dfgl_ThresholdValue, dfgl_sm > 0), 1, 0) self.holdStateLow = 9999 self.holdStateHigh = 0
def __init__(self): hl2 = (self.data.high + self.data.low) / 2 #ttr = bt.indicators.TrueRange(self.data) #ttr = bt.Max(self.data.high- self.data.low, # bt.Max(abs(self.data.high(0) - self.data.close(-1)), # abs(self.data.low(0) - self.data.close(-1)))) #atr2 = RelativeMovingAverage(ttr, period=self.p.ATRPeriod) #atr30 = bt.ind.SMA(ttr, period=30) #atr20 = bt.ind.SMA(ttr, period=20) #atr2 = atr30 - atr20 + bt.ind.SMA(ttr, period=self.p.ATRPeriod) #atr2 = bt.ind.EMA(ttr, period=self.p.ATRPeriod) atr2 = bt.indicators.AverageTrueRange(self.data, period=self.p.ATRPeriod) upSrc = hl2 - (self.p.Multiplier * atr2) #up := close[1] > up1 ? max(up,up1) : up self.lines.up = btind.If( self.data.close(-1) > upSrc(-1), bt.Max(upSrc, upSrc(-1)), upSrc) dnSrc = hl2 + (self.p.Multiplier * atr2) #dn := close[1] < dn1 ? min(dn, dn1) : dn self.lines.dn = btind.If( self.data.close(-1) < dnSrc(-1), bt.Min(dnSrc, dnSrc(-1)), dnSrc) """ trd = self.data.close - self.data.close + 1 trd2 = btind.If(trd(-1) < 0, btind.If(self.data.close > dn,1,trd2(-1)), btind.If(self.data.close < up, -1, trd2(-1))) #trd2 = btind.If((trd(-1) < 0) and (self.data.close(0) > dn(0)), #1, btind.If(trd(-1) > 0 and self.data.close(0) < up(0), -1, 0)) self.lines.trend = trd2 #btind.If((trd(-1) < 0) and (self.data.close > dn), # 1, btind.If(trd(-1) > 0 and self.data.close < up, -1, 0)) """ super(AtrTrend, self).__init__()
def __init__(self): self.lines.mlli = bt.Max(-1.0, self.lag_index()) super(MlLagIndicator, self).__init__()
def __init__(self): # __init__() 中是对 line 进行运算,最终也以 line 的形式返回,所以运算结果直接赋值给了 self.l.dummyline; self.l.dummyline = bt.Max(0.0, self.p.value)
def __init__(self): self.lines.dummyline = bt.Max(0.0, self.params.value)