def set_datalines(self): # Override stat line: self.stat_asset = self.data.spread = SpreadConstructor() # Spy on reward behaviour: self.reward_tracker = CumSumReward() self.data.std1 = btind.StdDev(self.datas[0], period=self.p.time_dim, safepow=True) self.data.std1.plotinfo.plot = False self.data.std2 = btind.StdDev(self.datas[1], period=self.p.time_dim, safepow=True) self.data.std2.plotinfo.plot = False self.data.features1 = [ btind.EMA(self.datas[0], period=period) for period in self.p.features_parameters ] self.data.features2 = [ btind.EMA(self.datas[1], period=period) for period in self.p.features_parameters ] initial_time_period = np.asarray( self.p.features_parameters).max() + self.p.time_dim self.data.dim_sma = btind.SimpleMovingAverage( self.datas[0], period=initial_time_period) self.data.dim_sma.plotinfo.plot = False
def __init__(self): close = self.data.close self.lines.ma_5 = btind.EMA(self.data, period=self.p.maperiod_5) self.lines.ma_10 = btind.EMA(self.data, period=self.p.maperiod_10) self.lines.ma_20 = btind.EMA(self.data, period=self.p.maperiod_20) self.lines.ma_30 = btind.EMA(self.data, period=self.p.maperiod_30) self.lines.ma_60 = btind.EMA(self.data, period=self.p.maperiod_60)
def __init__(self): self.orderid = None # to control operation entries fast_ema = btind.EMA(period=self.p.fast) slow_ema = btind.EMA(period=self.p.slow) long_ema = btind.EMA(period=self.p.long) self.signal = btind.CrossOver(fast_ema, slow_ema) self.log(f'Initial portfolio value of {self.broker.get_value():.2f}\n')
def __init__(self): #ema20 = btind.ExponentialMovingAverage(self.data, period=self.p.shortPeriod) #ema60 = btind.ExponentialMovingAverage(self.data, period=self.p.midPeriod) ema20 = btind.EMA(self.data, period=self.p.shortPeriod) ema60 = btind.EMA(self.data, period=self.p.midPeriod) ema120 = btind.EMA(self.data, period=self.p.longPeriod) self.lines.cs = ((self.data - ema20) / ema20) * 100.0 self.lines.sm = ((ema20 - ema60) / ema60) * 100.0 self.lines.ml = ((ema60 - ema120) / ema120) * 100.0 """ another squeeze indicator from trading view
def __init__(self): self.start_datetime=self.datas[0].p.fromdate self.start_portfolio_value = self.params.cerebro.broker.getvalue() self.brought_today=False self.order =None self.sma_short = btind.EMA(self.datas[0], period=self.params.short_period) self.sma_long= btind.EMA(self.datas[0], period=self.params.long_period)
def __init__(self): self.inds = {} for d in self.datas: self.inds[d] = {} self.inds[d]['fast_ema'] = btind.EMA( period=self.p.trend_filter_fast_period) self.inds[d]['slow_ema'] = btind.EMA( period=self.p.trend_filter_slow_period) self.inds[d]['long_filter'] = self.inds[d]['fast_ema'] > self.inds[ d]['slow_ema'] self.inds[d]['dc'] = DonchianChannels( period=self.p.donchian_channel_period) self.inds[d]['atr'] = btind.ATR( period=self.p.trailing_stop_atr_period)
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, 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 set_datalines(self): self.data.spread = btind.SimpleMovingAverage(self.datas[0] - self.datas[1], period=1) self.data.spread.plotinfo.subplot = True self.data.spread.plotinfo.plotabove = True self.data.spread.plotinfo.plotname = list(self.p.asset_names)[0] # Override stat line: # self.stat_asset = btind.SimpleMovingAverage((self.datas[0] + self.datas[1]) / 2, period=1) # self.stat_asset.plotinfo.plot = False self.stat_asset = self.data.spread self.data.std = btind.StdDev(self.data.spread, period=self.p.time_dim, safepow=True) self.data.std.plotinfo.plot = False self.data.features = [ # btind.SimpleMovingAverage(self.data.spread, period=period) for period in self.p.features_parameters btind.EMA(self.data.spread, period=period) for period in self.p.features_parameters ] initial_time_period = np.asarray(self.p.features_parameters).max() + self.p.time_dim self.data.dim_sma = btind.SimpleMovingAverage( self.datas[0], period=initial_time_period ) self.data.dim_sma.plotinfo.plot = False
def __init__(self): # self.MA = btind.SMA(self.data) self.BBPct = btind.BollingerBandsPct(self.data) self.EMA = btind.EMA(self.data) self.PPO = btind.PPO(self.data) # self.MACD = btind.EMA(self.data, period=12) - btind.EMA(self.data, period=26) self.RSI = btind.RSI_EMA(safediv=True)
def __init__(self): self.willy = btind.WilliamsR(period=21) self.l.willy = self.willy.l.percR self.l.ema = btind.EMA(self.willy, period=13) self.shift = -1 * self.p.shift self.l.divergence = Divergence(self.data, self.data1, self.l.ema, shift=self.p.shift)
def __init__(self, genome): # 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 # actual indicators usage #################################################################### self.wlen = len(genome) >> 1 start = 4 powers = np.arange(start, self.wlen + start - 1, 1) self.emas = list(btind.EMA(period=2**i) for i in powers) self.wbull = genome[:self.wlen] self.wbear = genome[self.wlen:]
def __init__(self): me1 = btind.EMA(self.data, period=self.p.period_me1) me2 = btind.EMA(self.data, period=self.p.period_me2) self.l.macd = me1 - me2 self.l.signal = btind.EMA(self.l.macd, period=self.p.period_signal) self.l.histo = self.l.macd - self.l.signal
def __init__(self): self.MA = btind.SMA(self.data) self.EMA = btind.EMA(self.data) self.MACD = btind.EMA(self.data, period=12) - btind.EMA(self.data, period=26) self.RSI = btind.RSI_EMA(safediv=True)
def __init__(self): #Set program start time start_time=datetime.now().time() print('Program start at {}'.format(start_time)) #print(self.params.sma1, self.p.ema1, self.params.atrperiod) #Proof deep copy worked for params #initialize counters for prenext/next self.nextcounter = 0 self.counter = 0 self.counttostop = 0 self.datastatus = 0 self.prenext_done = False self.bought = 0 self.sold = 0 self.target_long_price = 0 self.target_short_price = 0 self.trade_open_counter = 0 self.trade_close_counter = 0 self.trade_total_counter = 0 self.lost_counter = 0 self.won_counter = 0 #Define dictionaries and lists to be accessed from all timeframes self.atr_list =[] self.inds = dict() self.gap_dict=dict() self.rnghigh_dict = dict() self.rnglow_dict= dict() self.longstop_dict = dict() self.shortstop_dict = dict() self.target_long_dict = dict() self.target_short_dict = dict() self.size_dict = dict() self.inorder_dict = dict() self.sup_dict = dict() self.res_dict = dict() self.pos_dict = defaultdict(list) #Create/Instantiate objects to access user input parameters modelp = UserInputs.model_params() indp = UserInputs.ind_params() datalist = UserInputs.datalist('hist') ibdatalist = UserInputs.datalist('ib') #Determine interval for timeframe looping if not modelp.get('live_status'): data_feed_count = len(self.datas) ticker_count = len(datalist) self.ticker_interval = int(data_feed_count/ticker_count) #Needs to be an integer elif modelp.get('live_status'): data_feed_count = len(self.datas) ticker_count = len(ibdatalist) self.ticker_interval = int(data_feed_count/ticker_count) #Needs to be an integer #************************INITITIALIZE INDICATORS********************************************************* #Initialize dictionary's for x in range(0, len(self.datas), self.ticker_interval): d = self.datas[x] print(d._name) if not (d._name[:-1]=='VIX' or d._name[:-1]=='TICK-NYSE'): #Order dictionaries self.target_long_dict[d._name] = dict() self.target_short_dict[d._name] = dict() self.inorder_dict[d._name] = dict() self.target_long_dict[d._name] = 0 self.target_short_dict[d._name] = 0 self.inorder_dict[d._name] = False for i, d in enumerate(self.datas): if not (d._name[:-1]=='VIX' or d._name[:-1]=='TICK-NYSE'): #Sizing dictionary self.size_dict[d._name] = dict() self.size_dict[d._name] = 0 #For support/resistance dictionaries self.sup_dict[d._name] = dict() self.res_dict[d._name] = dict() self.sup_dict[d._name] = 0 self.res_dict[d._name] = 10000 #For all indicators self.inds[d._name] = dict() #Moving Average Indicators - FAST, SLOW, and CROSS self.inds[d._name]['sma1'] = btind.SMA(d, period=indp.get('sma1'), plot=False) self.inds[d._name]['sma2'] = btind.SMA(d, period=indp.get('sma2'), plot=True) self.inds[d._name]['ema1'] = btind.EMA(d, period=indp.get('ema1'), plot=False) self.inds[d._name]['ema2'] = btind.EMA(d, period=indp.get('ema2'), plot=False) self.inds[d._name]['ema3'] = btind.EMA(d, period=indp.get('ema3'), plot=False) #This will double pre-next self.inds[d._name]['cross'] = btind.CrossOver(self.inds[d._name]['ema2'], self.inds[d._name]['ema3'], plot=False) #RSI self.inds[d._name]['rsi']= btind.RSI(d, safediv=True, plot=True) #AVERAGE TRUE RANGE INDICATOR self.inds[d._name]['atr'] = btind.ATR(d, period=indp.get('atrperiod'), plot=False) #Bollinger Band self.inds[d._name]['bollinger'] = btind.BollingerBands(d, period=indp.get('bollinger_period'), devfactor = indp.get('bollinger_dist'), plot=False) #Stochastics self.inds[d._name]['stochastic'] = btind.StochasticFast(d, period=indp.get('stoch_per'), period_dfast= indp.get('stoch_fast'), safediv=True, plot=True) #ADX self.inds[d._name]['adx'] = btind.ADX(d,plot=True) """ #Pivots self.inds[d._name]['pivots'] = btind.pivotpoint.PivotPoint(d, plot=False) """ #Average Volume Indicator self.inds[d._name]['avg_volume'] = btind.Average(d.volume, period=indp.get('avg_per'), plot=False) #Highest and Lowest Values of Period Indicator self.inds[d._name]['highest'] = btind.Highest(d.high, period=indp.get('breakout_per'), plot=False) self.inds[d._name]['lowest'] = btind.Lowest(d.low, period=indp.get('breakout_per'), plot=False) #Slope indicators self.inds[d._name]['slope']= btind.Slope(d.close, period=indp.get('slope_period'), plot=False) self.inds[d._name]['slope_sma1'] = btind.Slope(self.inds[d._name]['sma1'], period=indp.get('slope_period'), plot=False, plotname = 'Slope_SMA1') self.inds[d._name]['slope_of_slope_sma1'] = btind.Slope(self.inds[d._name]['slope_sma1'], period=indp.get('slope_period'), plot=False, plotname = 'Slope_of_Slope_SMA1') self.inds[d._name]['slope_sma_width'] = btind.Slope(self.inds[d._name]['sma1']-self.inds[d._name]['sma2'], period=indp.get('slope_period'), plot=False, plotname = 'Slope_SMA_WIDTH') self.inds[d._name]['slope_adx'] = btind.Slope(self.inds[d._name]['adx'], period=indp.get('slope_period'), plot=False, plotname = 'Slope_ADX') self.inds[d._name]['slope_of_slope_adx'] = btind.Slope(self.inds[d._name]['slope_adx'], period=indp.get('slope_period'), plot=False, plotname = 'Slope_of_Slope_ADX') self.inds[d._name]['slope_rsi'] = btind.Slope(self.inds[d._name]['rsi'], period=indp.get('slope_period'), plot=False, plotname = 'Slope_RSI') self.inds[d._name]['slope_of_slope_rsi'] = btind.Slope(self.inds[d._name]['slope_rsi'], period=indp.get('slope_period'), plot=False, plotname = 'Slope_of_Slope_RSI') self.inds[d._name]['slope_ema1'] = btind.Slope(self.inds[d._name]['ema1'], period=indp.get('slope_period'), plot=False, plotname = 'Slope_EMA1') self.inds[d._name]['slope_ema2'] = btind.Slope(self.inds[d._name]['ema2'], period=indp.get('slope_period'), plot=False, plotname = 'Slope_EMA2') self.inds[d._name]['slope_ema3'] = btind.Slope(self.inds[d._name]['ema3'], period=indp.get('slope_period'), plot=False, plotname = 'Slope_EMA3') #Plot ADX and Slope on same subplot as stochastic self.inds[d._name]['adx'].plotinfo.plotmaster = self.inds[d._name]['rsi']
def __init__(self): #Set program start time start_time=datetime.now().time() print('Program start at {}'.format(start_time)) print('Program time period: {} to {}'.format( UserInputs.model_params().get('start_date'), UserInputs.model_params().get('end_date'))) print('Program Parameters: {}'.format(self.params._getitems())) #initialize counters for prenext/next self.dayperiod = 0 self.nextcounter = 0 self.counter = 0 self.prenext_done = False self.target_short_price = 0 self.target_long_price = 0 self.pos = 0 self.cash_avail = 0 self.rng_high = 0 self.rng_low = 0 self.tick_close = 0 #Define dictionaries and lists to be accessed from all timeframes self.inds = dict() self.rnghigh_dict = dict() self.rnglow_dict= dict() self.longstop_dict = dict() self.shortstop_dict = dict() self.target_long_dict = defaultdict(list) self.target_short_dict = defaultdict(list) self.size_dict = defaultdict(list) self.inorder_dict = defaultdict(list) #Create/Instantiate objects to access user input parameters self.modelp = UserInputs.model_params() datalist = UserInputs.datalist('hist') ibdatalist = UserInputs.datalist('ib') self.data_feed_count = len(self.datas) #Determine interval for timeframe looping if not self.modelp.get('live_status'): self.ticker_count = len(datalist) self.number_timeframes = int(self.data_feed_count/self.ticker_count) #Needs to be an integer elif self.modelp.get('live_status'): self.ticker_count = len(ibdatalist) self.number_timeframes = int(self.data_feed_count/self.ticker_count) #Needs to be an integer self.minimum_data = int(self.ticker_count * self.modelp.get('timeframe2')/self.modelp.get('timeframe0')) #since iterating over 5 min periods, and longest timeframe is 1 hour, there are 12 5 min periods in an hour #Determine # of base timeframe periods within trading day self.intraday_periods = int(390/self.modelp.get('timeframe0')) #************************INITITIALIZE INDICATORS********************************************************* #Initialize dictionary's for i, d in enumerate(self.datas): print("Datas in Strategy {}".format(d._name)) self.name_t0 = d._name[:-1]+'0' self.name_t1 = d._name[:-1]+'1' self.name_t2 = d._name[:-1]+'2' #Initialize dictionaries by appending 0 value self.target_long_dict[d._name].append(0) self.target_short_dict[d._name].append(0) self.size_dict[d._name].append(0) #Need to append twice to reference 2nd to last value self.size_dict[d._name].append(0) self.inorder_dict[d._name].append(False) #Get available cash self.cash_avail = round(self.broker.getcash(),2) #For all indicators self.inds[d._name] = dict() #Determine on balance volume self.inds[d._name]['obv'] = btind.obv(d, period=self.p.obv, plot=True) #Determine current ohlcv bars self.inds[d._name]['ohlc'] = btind.ohlc(d, period=self.p.ohlc, plot=False) #Determine prior day bars self.inds[d._name]['prior_ohlc'] = btind.priorday(d, period=self.p.ohlc, plot=False) #Determine opening gap and opening hi/low range (defined by b_time parameter) self.inds[d._name]['gap'] = btind.gap(d, period=self.p.breakout_per, plot=False) #AVERAGE TRUE RANGE INDICATOR self.inds[d._name]['atr'] = btind.ATR(d, period=self.p.atrperiod, plot=False) #Moving Average Indicators - FAST, SLOW, and CROSS self.inds[d._name]['sma1'] = btind.SMA(d, period=self.p.sma1, plot=False) self.inds[d._name]['sma2'] = btind.SMA(d, period=self.p.sma2, plot=False) self.inds[d._name]['ema1'] = btind.EMA(d, period=self.p.ema1, plot=True) self.inds[d._name]['ema2'] = btind.EMA(d, period=self.p.ema2, plot=True) self.inds[d._name]['cross'] = btind.CrossOver(self.inds[d._name]['ema1'], self.inds[d._name]['ema2'], plot=False) """ #RSI self.inds[d._name]['rsi']= btind.RSI(d, safediv=True, plot=False) #Bollinger Band self.inds[d._name]['bollinger'] = btind.BollingerBands(d, period=self.p.bollinger_period, devfactor = self.p.bollinger_dist'), plot=False) """ #Stochastics - just prints Slow %d line (not %K also which would be "StochasticFast") self.inds[d._name]['stochastic'] = btind.StochasticSlow(d, period=self.p.stoch_per, period_dfast= self.p.stoch_fast, safediv=True, plot=False) #ADX self.inds[d._name]['adx'] = btind.ADX(d, period=self.p.adx, plot=False) """ #Pivots self.inds[d._name]['pivots'] = btind.pivotpoint.PivotPoint(d, plot=False) """ #Highest and Lowest Values of Period Indicator self.inds[d._name]['highest'] = btind.Highest(d.high, period=self.p.breakout_per, plot=False) self.inds[d._name]['lowest'] = btind.Lowest(d.low, period=self.p.breakout_per, plot=False) #Slope indicators self.inds[d._name]['slope']= btind.Slope(d.close, period=self.p.slope_period, plot=False) self.inds[d._name]['slope_obv'] = btind.Slope(self.inds[d._name]['obv'], period=self.p.slope_period, plot=False) self.inds[d._name]['slope_of_slope_obv'] = btind.Slope(self.inds[d._name]['slope_obv'], period=self.p.slope_period, plot=False) self.inds[d._name]['slope_sma1'] = btind.Slope(self.inds[d._name]['sma1'], period=self.p.slope_period, plot=False) self.inds[d._name]['slope_of_slope_sma1'] = btind.Slope(self.inds[d._name]['slope_sma1'], period=self.p.slope_period, plot=False) """ self.inds[d._name]['slope_of_slope_sma1'] = btind.Slope(self.inds[d._name]['slope_sma1'], period=self.p.slope_period, plot=False) self.inds[d._name]['slope_sma_width'] = btind.Slope(self.inds[d._name]['sma1']-self.inds[d._name]['sma2'], period=self.p.slope_period, plot=False) self.inds[d._name]['slope_adx'] = btind.Slope(self.inds[d._name]['adx'], period=self.p.slope_period, plot=False) self.inds[d._name]['slope_of_slope_adx'] = btind.Slope(self.inds[d._name]['slope_adx'], period=self.p.slope_period, plot=False) self.inds[d._name]['slope_rsi'] = btind.Slope(self.inds[d._name]['rsi'], period=self.p.slope_period, plot=False, plotname = 'Slope_RSI') self.inds[d._name]['slope_of_slope_rsi'] = btind.Slope(self.inds[d._name]['slope_rsi'], period=self.p.slope_period, plot=False, plotname = 'Slope_of_Slope_RSI') self.inds[d._name]['slope_ema1'] = btind.Slope(self.inds[d._name]['ema1'], period=self.p.slope_period, plot=False, plotname = 'Slope_EMA1') self.inds[d._name]['slope_ema2'] = btind.Slope(self.inds[d._name]['ema2'], period=self.p.slope_period, plot=False, plotname = 'Slope_EMA2') """ self.inds[d._name]['resistance'] = btind.Resistance(d, period=self.p.lookback, min_touches = self.p.min_touches, tolerance_perc = self.p.tolerance_perc, bounce_perc = self.p.bounce_perc, plot=True) self.inds[d._name]['support'] = btind.Support(d, period=self.p.lookback, min_touches = self.p.min_touches, tolerance_perc = self.p.tolerance_perc, bounce_perc = self.p.bounce_perc, plot=True) if d._name == d._name[:-1]+'0': self.inds[d._name]['priorday'] = btind.priorday(d, period=self.p.priorday, plot=False) #Calculate VWAP self.inds[d._name]['vwap'] = btind.vwap(d, period=self.p.vwap_lookback, plot=True) #Calculate Hammer Candle Signal if not d._name == 'TICK-NYSE0': self.inds[d._name]['hammer'] = btind.HammerCandles(d, plot=False) #Plot ADX and Stochastic on same subplot as stochastic #self.inds[d._name]['adx'].plotinfo.plotmaster = self.inds[d._name]['stochastic'] print('Start preloading data to meet minimum data requirements')
def __init__(self): super(MyTrixSignalInherited, self).__init__() self.lines.signal = btind.EMA(self.lines.trix, period=self.p.sigperiod)
def __init__(self): self.lines.trix = MyTrix(self.data, period=self.p.period) self.lines.signal = btind.EMA(self.lines.trix, period=self.p.sigperiod)
def __init__(self): ema1 = btind.EMA(self.data, period=self.p.period) ema2 = btind.EMA(ema1, period=self.p.period) ema3 = btind.EMA(ema2, period=self.p.period) self.lines.trix = 100.0 * (ema3 - ema3(-1)) / ema3(-1)
def __init__(self): print(self.p.sigperiod) super(MyTrixSignal, self).__init__() self.lines.signal = btind.EMA(self.lines.trix, period=self.p.sigperiod)