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.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.data_live = False #Define dictionaries and lists to be accessed from all timeframes self.inds = dict() self.rnghigh_dict = dict() self.rnglow_dict= dict() self.stop_dict = defaultdict(list) 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() #Determine interval for timeframe looping if not self.modelp.get('live_status'): datalist = UserInputs.datalist('hist') self.data_feed_count = len(self.datas) 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'): ibdatalist = UserInputs.datalist('ib') self.data_feed_count = len(self.datas) 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)) #Initialize dictionaries by appending 0 value self.inds[d._name] = dict() #Dict for all indicators self.stop_dict[d._name].append(0) 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 at start self.size_dict[d._name].append(0) self.inorder_dict[d._name].append(False) #Get available cash self.cash_avail = self.broker.getcash() #Instantiate exact data references (can't loop or will only spit out last value) if d._name == 'TICK-NYSE0': self.tick_close = d.close if d._name == 'SPY0': self.spy_close = d.close #Calculate VWAP self.inds[d._name]['vwap'] = btind.vwap(d, plot=True) #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 same bar, prior day self.inds[d._name]['priorday'] = btind.priorday(d, 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) self.inds[d._name]['atr_stop'] = btind.atr_stop(d,self.inds[d._name]['atr'], atrdist = self.p.atrdist, dollars_risked = self.p.total_dollars_risked, dollars_per_trade = self.p.dollars_risked_per_trade, 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) #Calculate Hammer Candle Signal self.inds[d._name]['hammer'] = btind.HammerCandles(d, plot=False) #Calculate Engulfing Candle Signal self.inds[d._name]['engulfing'] = btind.EngulfingCandles(d, plot=False) #Calculate Engulfing Candle Signal self.inds[d._name]['three_line_strike'] = btind.three_line_strike(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): #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.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() self.indp = UserInputs.ind_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): self.name_t0 = d._name[:-1]+'0' self.name_t1 = d._name[:-1]+'1' self.name_t2 = d._name[:-1]+'2' print('Datas included in Strategy: {}'.format(d._name)) #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 current ohlcv bars self.inds[d._name]['ohlc'] = btind.ohlc(d, period=self.indp.get('ohlc'), plot=False) #Determine prior day bars self.inds[d._name]['prior_ohlc'] = btind.priorday(d, period=self.indp.get('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.indp.get('breakout_per'), plot=False) #AVERAGE TRUE RANGE INDICATOR self.inds[d._name]['atr'] = btind.ATR(d, period=self.indp.get('atrperiod'), plot=False) #Moving Average Indicators - FAST, SLOW, and CROSS self.inds[d._name]['sma1'] = btind.SMA(d, period=self.indp.get('sma1'), plot=False) self.inds[d._name]['sma2'] = btind.SMA(d, period=self.indp.get('sma2'), plot=False) self.inds[d._name]['ema1'] = btind.EMA(d, period=self.indp.get('ema1'), plot=True) self.inds[d._name]['ema2'] = btind.EMA(d, period=self.indp.get('ema2'), plot=True) """ #This will double pre-next 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.indp.get('bollinger_period'), devfactor = self.indp.get('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.indp.get('stoch_per'), period_dfast= self.indp.get('stoch_fast'), safediv=True, plot=False) #ADX self.inds[d._name]['adx'] = btind.ADX(d, period=self.indp.get('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.indp.get('breakout_per'), plot=False) self.inds[d._name]['lowest'] = btind.Lowest(d.low, period=self.indp.get('breakout_per'), plot=False) #Slope indicators self.inds[d._name]['slope']= btind.Slope(d.close, period=self.indp.get('slope_period'), plot=False) self.inds[d._name]['slope_sma1'] = btind.Slope(self.inds[d._name]['sma1'], period=self.indp.get('slope_period'), plot=False) self.inds[d._name]['slope_of_slope_sma1'] = btind.Slope(self.inds[d._name]['slope_sma1'], period=self.indp.get('slope_period'), plot=False) """ self.inds[d._name]['slope_of_slope_sma1'] = btind.Slope(self.inds[d._name]['slope_sma1'], period=self.indp.get('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.indp.get('slope_period'), plot=False) self.inds[d._name]['slope_adx'] = btind.Slope(self.inds[d._name]['adx'], period=self.indp.get('slope_period'), plot=False) self.inds[d._name]['slope_of_slope_adx'] = btind.Slope(self.inds[d._name]['slope_adx'], period=self.indp.get('slope_period'), plot=False) self.inds[d._name]['slope_rsi'] = btind.Slope(self.inds[d._name]['rsi'], period=self.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=self.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=self.indp.get('slope_period'), plot=False, plotname = 'Slope_EMA1') self.inds[d._name]['slope_ema2'] = btind.Slope(self.inds[d._name]['ema2'], period=self.indp.get('slope_period'), plot=False, plotname = 'Slope_EMA2') """ self.inds[d._name]['resistance'] = btind.Resistance(d, period=self.indp.get('lookback'), plot=True) self.inds[d._name]['support'] = btind.Support(d, period=self.indp.get('lookback'), plot=True) if d._name == d._name[:-1]+'0' or d._name == d._name[:-1]+'1': self.inds[d._name]['avgvolume'] = btind.avgvolume(d, period=self.indp.get('avgvolume'), plot=False) if d._name == d._name[:-1]+'0': self.inds[d._name]['priorday'] = btind.priorday(d, period=self.indp.get('priorday'), plot=False) #Calculate VWAP and On Balance Volume (in VWAP module) self.inds[d._name]['vwap'] = btind.vwap(d, period=self.indp.get('vwap_lookback'), plot=True) self.inds[d._name]['slope_obv'] = btind.Slope(self.inds[d._name]['vwap'].lines.obv, period=self.indp.get('slope_period'), 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')