def __init__(self): first, second = self.datas[0], self.datas[1] beta = btind.SMA(first, period=self.p.period) / btind.SMA( second, period=self.p.period) spread = first - (beta * second) self.lines.signal = (spread - btind.SMA(spread, period=self.p.period) ) / btind.StdDev(spread, period=self.p.period)
def __init__(self, args): print("[Loading strategy]") self.update = args.update_memory ## Updating params with input args ## self.p.stoploss, self.p.takeprofit = args.stoploss, args.takeprofit self.p.rsi_period = args.rsi_period self.p.ema_period = args.ema_period self.p.fast_period = args.fast_period self.p.slow_period = args.slow_period self.p.atr_period = args.atr_period ## Indicators Data ## self.rsi = btind.RSI_EMA(period=self.p.rsi_period) self.ema = btind.EMA(period=self.p.ema_period) self.fast = btind.SMA(period=self.p.fast_period) self.slow = btind.SMA(period=self.p.slow_period) self.sar = btind.PSAR(period=self.p.sar_period, af=self.p.sar_step, afmax=self.p.sar_max) self.order = None self.tp, self.sl = None, None self.data_name = 'AIBot' self._close = [] self._rsi = [] self._fast = [] self._slow = [] self._sar = [] self.inc = 0
def __init__(self): self.benchmark = self.getdatabyname(self.p.benchmark_name) self.inds = dict() for d in self.datas : if d._name == self.p.benchmark_name : continue self.inds[d] = dict() self.inds[d]['rsc'] = RSC.rsc(d,self.benchmark,subplot=False) self.inds[d]['rscma'] = btind.SMA(self.inds[d]['rsc'],period=10) self.inds[d]['rscCrossUpCondition'] = btind.crossover.CrossUp(self.inds[d]['rsc'], self.inds[d]['rscma']) self.inds[d]['rscu'] = rscu.rscu(d,self.benchmark,subplot=False) self.inds[d]['hrscu'] = self.inds[d]['rscu'].l.hrscu self.inds[d]['lrscd'] = self.inds[d]['rscu'].l.lrscd self.inds[d]['csma200'] = btind.SMA(d.close,period=200) self.inds[d]['order'] = None self.inds[d]['tradeNo'] = 0 self.inds[d]['counter'] = 0 self.inds[d]['eligible'] = False self.bar_counter = 0 self.highest_lowest = [] hi, lo = self.data.high, self.data.low
def __init__(self): self.sma_vel = btind.SMA(period=self.p.Med_vel) self.sma_len = btind.SMA(period=self.p.Med_len) self.buysig = btind.CrossOver(self.sma_vel, self.sma_len) self.dataclose = self.datas[0].close
def __init__(self, args): print("[Loading strategy]") ## Memory Params ## self.update = args.update_memory self.mem_close = memory.content(filename="close.dat", exists=True) self.mem_rsi = memory.content(filename="rsi.dat", exists=True) self.mem_fast = memory.content(filename="fast.dat", exists=True) self.mem_slow = memory.content(filename="slow.dat", exists=True) self.mem_sar = memory.content(filename="sar.dat", exists=True) self.mem_results = memory.content(filename="results.dat", exists=True) res = self.mem_results.getdata() self.mem_close.train(num_layers=self.p.feature_window, results=res) self.mem_rsi.train(num_layers=self.p.feature_window, results=res) self.mem_fast.train(num_layers=self.p.feature_window, results=res) self.mem_slow.train(num_layers=self.p.feature_window, results=res) self.mem_sar.train(num_layers=self.p.feature_window, results=res) self.p.stoploss, self.p.takeprofit = args.stoploss, args.takeprofit ## Indicators Data ## self.rsi = btind.RSI_EMA(period=self.p.rsi_period) self.ema = btind.EMA(period=self.p.ema_period) self.fast = btind.SMA(period=self.p.fast_period) self.slow = btind.SMA(period=self.p.slow_period) self.sar = btind.PSAR(period=self.p.sar_period, af=self.p.sar_step, afmax=self.p.sar_max) self.tp, self.sl = None, None self.order = None self.inc = 0
def __init__(self): """Initialize the strategy""" self.fastma = dict() self.slowma = dict() self.regime = dict() if self.params.var1 > self.params.var2: vfast = self.params.var1 self.params.var1 = self.params.var2 self.params.var2 = vfast for d in self.getdatanames(): # The moving averages self.fastma[d] = btind.SMA( self.getdatabyname(d), # The symbol for the moving average period=self.params.var1, # Fast moving average plotname="FastMA: " + d) self.slowma[d] = btind.SMA( self.getdatabyname(d), # The symbol for the moving average period=self.params.var2, # Slow moving average plotname="SlowMA: " + d) # Get the regime self.regime[d] = self.fastma[d] - self.slowma[ d] # Positive when bullish
def __init__(self): self.inds = dict() for d in self.datas: if d._name == self.p.benchmark_name: continue self.inds[d] = dict() self.inds[d]['csma200'] = btind.SMA(d.close, period=200) self.inds[d]['dmi'] = dmi_inds.BCH(d, subplot=False) self.inds[d]['bbh'] = self.inds[d]['dmi'].bbh self.inds[d]['adx'] = btind.AverageDirectionalMovementIndex( d, period=14, subplot=False) self.inds[d]['adxma'] = btind.SMA(self.inds[d]['adx'], period=2) self.inds[d]['rbbhl'] = RBBBHL.RBBHL(d, subplot=False) self.inds[d]['hbb'] = self.inds[d]['rbbhl'].hbb self.inds[d]['order'] = None self.inds[d]['sellStop'] = None self.inds[d]['onceTrue'] = False self.inds[d]['sell_condition'] = btind.crossover.CrossDown( self.inds[d]['adx'], self.inds[d]['adxma']) self.inds[d]['bar_counter'] = 0
def __init__(self): self.dataclose = self.datas[0].close self.datavolume = self.datas[0].volume # self.datape200 = self.datas[0].pe200 # self.datape1000 = self.datas[0].pe1000 self.ts_code = self.params.ts_code self.startDate = self.params.startDate self.endDate = self.params.endDate self.dividentData = None # 待处理的股利股息 self.stk_div = None # 待处理的现金股息 self.cash_div = None self.order = None self.buyprice = None self.buycomm = None # 读取除权除息数据 self.getDivident() # 添加移动平均指标 self.sma = bt.indicators.SimpleMovingAverage(self.datas[0], period=self.params.maperiod) # 添加自定义指标 # self.ssa = Ssa(ssa_window=self.params.ssa_window, subplot=False) # self.pehist = Pehist(subplot=False) btind.SMA(self.data.pe, period=1, subplot=True, plotname='pe') btind.SMA(self.data.pe200, period=1, subplot=True, plotname='pe200') btind.SMA(self.data.pe1000, period=1, subplot=True, plotname='pe1000')
def __init__(self): self.sma_small_tf = btind.SMA(self.data, period=self.p.period) bt.indicators.MACD(self.data0) if not self.p.onlydaily: self.sma_large_tf = btind.SMA(self.data1, period=self.p.period) bt.indicators.MACD(self.data1)
def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close # To keep track of pending orders and buy price/commission self.order = None self.buyprice = None self.buycomm = None btind.SMA(self.data.vp, period=1, subplot=True) btind.SMA(self.data.q_g, period=1, subplot=True)
def __init__(self): # 指标函数也支持简写 SimpleMovingAverage → SMA # 最简方式:直接省略指向的数据集 self.sma1 = btind.SimpleMovingAverage(period=5) # 只指定第一个数据表格 self.sma2 = btind.SMA(self.data, period=5) # 指定第一个数据表格的 close 线 self.sma3 = btind.SMA(self.data.close, period=5) # 完整写法 self.sma4 = btind.SMA(self.datas[0].lines[0], period=5)
def __init__(self): #keep a log of where we are in the datafeed self.dataclose = self.datas[0].close #create fast and slow moving averages self.ma_fast = btind.SMA(period=5) self.ma_slow = btind.SMA(period=30) # set values for when the fast ma crosses the slow ma w backtrader's indicator # -1 is bearish, 1 is bullish self.crossover = btind.CrossOver(self.ma_fast, self.ma_slow)
def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close sma_fast = btind.SMA(period=self.p.fast) sma_slow = btind.SMA(period=self.p.slow) self.buysig = btind.CrossOver( sma_fast, sma_slow) #10일과 30일 이동평균의 교차점 찾아줌, -1혹은 1 output print('-----------------------------------------------') print(" This is test strategy ") print('-----------------------------------------------')
def __init__(self, args): self.dataclose = self.datas[0].close self.order = None self.smafast, self.smaslow = btind.SMA(period=args.fastma), btind.SMA( period=args.slowma) self.stoploss = args.stoploss self.takeprofit = args.takeprofit self.sl, self.tp = None, None self.buyfinaltime = args.buyfinaltime self.timetosell = args.timetosell self.logging = args.log
def __init__(self): self.l.adx = btind.AverageDirectionalMovementIndex(self.data0) self.l.adxma = btind.SMA(self.l.adx(0), period=2) self.crossup_condition = btind.crossover.CrossUp( self.l.adx(0), self.l.adxma(0)) self.hi = [] self.lo = []
def __init__(self): self.dataclose = self.datas[0].close self.order = None #self.macd = bt.indicators.MACD(self.datas[0]) #self.sma05 = btind.SMA(self.datas[0],period = self.params.mapperiod,subplot=False) self.sma20 = btind.SMA(self.datas[0], period=self.params.mapperiod02, subplot=False) self.bbrands = btind.BBands(self.datas[0], subplot=False) self.max20 = bt.talib.MAX(self.datas[0], period=self.params.mapperiod02, plot=False) self.min20 = bt.talib.MIN(self.datas[0], period=self.params.mapperiod02, plot=False) self.atr = btind.ATR(self.datas[0], plot=False) self.signaltop = btind.CrossOver(self.dataclose, self.bbrands.top, plot=False) self.signalcrossover = btind.CrossOver(self.dataclose, self.sma20, plot=False) self.signallow = btind.CrossOver(self.datas[0], self.bbrands.bot) self.signaltop = btind.CrossOver(self.datas[0], self.bbrands.top) self.buyprice = None self.buycomm = None
def __init__(self): self.inds = dict() for d in self.datas: if d._name == self.p.benchmark_name: continue self.inds[d] = dict() self.inds[d]['mach'] = MA_Channel.MAChannel(d, subplot=False) self.inds[d]['MACHH'] = self.inds[d]['mach'].MACHH self.inds[d]['csma50'] = btind.SMA(d.close, period=50) self.inds[d]['order'] = None self.inds[d]['buy_condition'] = btind.crossover.CrossUp( d.high, self.inds[d]['MACHH']) self.inds[d]['tradeNo'] = 0 self.inds[d]['counter'] = 0 self.inds[d]['eligible'] = False self.bar_counter = 0 hi, lo = self.data.high, self.data.low
def __init__(self): self.bar_num = 0 #MyTrix(self.data, period=self.p.trixperiod) #self.data_priceDiv = PriceDiv(self.data) self.data_obv = OnBalanceVolume(self.data) ## ##btind.obv(self.data,period=3, plot=True) self.data_obv20 = btind.SMA(self.data_obv, period=20)
def __init__(self): #keep a log of where we are in the datafeed self.dataclose = self.datas[0].close #create fast and slow moving averages self.ma_fast = btind.SMA(period=self.p.smafast) self.ma_slow = btind.SMA(period=self.p.smaslow) self.macd = btind.MACD(self.data, period_me1=self.p.macd1, period_me2=self.p.macd2, period_signal=self.p.macdsig) # set values for when the fast ma crosses the slow ma w backtrader's indicator # -1 is bearish, 1 is bullish self.crossover = btind.CrossOver(self.ma_fast, self.ma_slow)
def __init__(self): btind.SMA() btind.Stochastic() btind.RSI() btind.MACD() btind.CCI() TestInd().plotinfo.plot = False
def __init__(self): self.dataclose = self.datas[0].close self.order = None #self.macd = bt.indicators.MACD(self.datas[0]) #self.sma05 = btind.SMA(self.datas[0],period = self.params.mapperiod,subplot=False) self.sma20 = btind.SMA(self.datas[0], period=self.params.mapperiod02, subplot=False) self.bbrands = btind.BBands(self.datas[0], subplot=False) self.max55 = bt.talib.MAX(self.datas[0].high, period=self.params.mapperiod, plot=False) self.min55 = bt.talib.MIN(self.datas[0].low, period=self.params.mapperiod, plot=False) self.atr = btind.ATR(self.datas[0]) self.stake = 4000 self.ownprice = 0 ## self.signaltop = btind.CrossOver(self.dataclose,self.bbrands.top,plot=False) self.buyprice = None self.buycomm = None
def __init__(self): self.inds = dict() for d in self.datas: if d._name == self.p.benchmark_name: continue self.inds[d] = dict() self.inds[d]['pdimdiCH'] = PDIMDIHL.PDIMDIHL(d, subplot=False) self.inds[d]['pdi'] = self.inds[d]['pdimdiCH'].l.pdi self.inds[d]['mdi'] = self.inds[d]['pdimdiCH'].l.mdi self.inds[d]['adx'] = btind.AverageDirectionalMovementIndex( d, subplot=False) self.inds[d]['adxma'] = btind.SMA(self.inds[d]['adx'].l.adx, period=2, subplot=False) self.inds[d]['LLPCH'] = hhpc.PC(d, subplot=False) self.inds[d]['llpc'] = self.inds[d]['LLPCH'].l.llpc self.inds[d]['PC'] = pchannel.priceChannel(d, subplot=False) self.inds[d]['pch'] = self.inds[d]['PC'].l.pch self.inds[d]['order'] = None self.inds[d]['eligible'] = False self.inds[d]['counter'] = 0 self.inds[d]['tradeno'] = 0 self.inds[d]['is_last'] = False self.bar_counter = 0
def __init__(self): self.inds = dict() for d in self.datas : if d._name == self.p.benchmark_name : continue self.inds[d] = dict() self.inds[d]['hhpcCH'] = hhpc.PC(d,subplot=False) self.inds[d]['llpc'] = self.inds[d]['hhpcCH'].l.llpc self.inds[d]['priceCH'] = pchannel.priceChannel(d,subplot=False) self.inds[d]['pcl'] = self.inds[d]['priceCH'].l.pcl self.inds[d]['csma200'] = btind.SMA(d.close,period=200) self.inds[d]['order'] = None self.inds[d]['tradeNo'] = 0 self.inds[d]['counter'] = 0 self.inds[d]['eligible'] = False self.bar_counter = 0 self.highest_lowest = [] hi, lo = self.data.high, self.data.low
def __init__(self): # Add indicators to add load btind.SMA(period=self.p.smaperiod) btind.MACD(period_me1=self.p.macdperiod1, period_me2=self.p.macdperiod2, period_signal=self.p.macdperiod3)
def __init__(self): self.benchmark = self.getdatabyname(self.p.benchmark_name) # Initialize & assign Indicators here self.rsc = RSC.rsc(self.data0, self.benchmark, subplot=True) self.rscma = btind.SMA(self.rsc, period=10, subplot=False) self.rsc_up = rscu.rscu(self.data0, self.benchmark, subplot=False)
def __init__(self): self.l.pdi = btind.PlusDirectionalIndicator(self.data0, period=14) self.l.mdi = btind.MinusDirectionalIndicator(self.data0, period=14) self.l.adx = btind.AverageDirectionalMovementIndex(self.data0, period=14) self.l.adxma = btind.SMA(self.l.adx, period=2) self.adx_condition = btind.crossover.CrossUp(self.adx(0), self.adxma(0))
def __init__(self): # SimpleMovingAverage on main data sma = btind.SMA(period=self.p.smaperiod) # CrossOver (1: up, -1: down) close / sma self.crossover = btind.CrossOver(self.data.close, sma, plot=True) # Set to None: new ordersa allowed self.order = None
def __init__(self): # SimpleMovingAverage on main data # Equivalent to -> sma = btind.SMA(self.data, period=self.p.smaperiod) sma = btind.SMA(period=self.p.smaperiod) # CrossOver (1: up, -1: down) close / sma self.buysell = btind.CrossOver(self.data.close, sma, plot=True) # Sentinel to None: new ordersa allowed self.order = None
def __init__(self): btind.SMA() btind.Stochastic() btind.RSI() btind.MACD() btind.CCI() TestInd().plotinfo.plot = False sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30) crossover = bt.ind.CrossOver(sma1, sma2) self.signal_add(bt.SIGNAL_LONG, crossover)
def __init__(self): # To control operation entries self.orderid = None # Create SMA on 2nd data sma = btind.MovAv.SMA(self.data, period=self.p.period) # Create a CrossOver Signal from close an moving average self.signal = btind.CrossOver(self.data.close, sma) self.signal.csv = self.p.csvcross btind.SMA( self.data.pe, period=1, subplot=False) # A way to draw the new data field on the chart