Exemplo n.º 1
0
    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
Exemplo n.º 2
0
    def __init__(self):
        super().__init__(verbose=self.p.verbose)
        if self.p.verbose:
            log.setLevel(logging.DEBUG)
        self.hlc3 = (self.data.high + self.data.low + self.data.close)/3
        self.baseline = btind.WMA(self.hlc3, period = self.p.wma_period)
#        self.exit = btind.HMA(self.hlc3, period = self.p.hma_period)
        self.atr = btind.ATR(self.data, period=self.p.atr_period)
#        self.stddev = btind.StdDev(self.data.close, period=self.p.stddev_period)
        #self.highest = btind.Highest(self.data.high, period=self.p.squeeze_period, plot=False)
        #self.lowest = btind.Lowest(self.data.low, period=self.p.squeeze_period, plot=False)
        #self.sma = btind.MovAv.SMA(self.data.close, period=self.p.squeeze_period, plot=False)
        #self.mean = (self.highest + self.lowest + self.sma) / 3.0
        #self.squeeze = btind.MovAv.SMA((self.data.close - self.mean) / self.data.close, period=self.p.squeeze_period, plot=True, subplot=True)

        self.squeeze = JackVortex(squeeze_period=self.p.squeeze_period)

#        self.macd = btind.MACDHisto(self.data.close, period_me1=self.p.macd_fast_period,
#                                    period_me2=self.p.macd_slow_period, period_signal=self.p.macd_signal_period)

#        self.macd_ema = btind.MovAv.EMA(self.data.close, period=self.p.macd_ema_period)
        self.stage = self.Start
        self.main_order = None
        self.stop_order = None
        self.limit_order = None
        self.exit_order = None
        self.main_order_price = None
        self.target1_price = None
        self.size = None
        self.symbol_parameter = SymbolParameter(self.p.symbol, self.broker.get_cash(), self.p.max_loss_percent)
    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
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
    def __init__(self):
        super().__init__()
        # self.ema_short = btind.MovAv.EMA(self.data.close, period=self.p.ema_short_period)
        self.atr = btind.ATR(self.data, period=self.p.atr_period)
        self.stddev = btind.StdDev(self.data.close,
                                   period=self.p.stddev_period)
        self.highest = btind.Highest(self.data.high,
                                     period=self.p.squeeze_period,
                                     plot=False)
        self.lowest = btind.Lowest(self.data.low,
                                   period=self.p.squeeze_period,
                                   plot=False)
        self.sma = btind.MovAv.SMA(self.data.close,
                                   period=self.p.squeeze_period,
                                   plot=False)
        self.mean = ((self.highest + self.lowest) / 2.0 + self.sma) / 2.0
        self.squeeze = btind.MovAv.SMA(
            (self.data.close - self.mean) / self.data.close,
            period=self.p.squeeze_period)

        #    self.macd_fast   = btind.MovAv.EMA(self.data.close, period=self.p.macd_fast_period, plot=False)
        #    self.macd_slow   = btind.MovAv.EMA(self.data.close, period=self.p.macd_slow_period, plot=False)
        #    self.macd        = self.macd_fast - self.macd_slow
        #    self.macd_signal = btind.MovAv.EMA(self.macd, period=self.p.macd_signal_period, plot=False)
        #    self.macd_histogram = self.macd - self.macd_signal
        self.macd = btind.MACDHisto(self.data.close,
                                    period_me1=self.p.macd_fast_period,
                                    period_me2=self.p.macd_slow_period,
                                    period_signal=self.p.macd_signal_period)

        self.macd_ema = btind.MovAv.EMA(self.data.close,
                                        period=self.p.macd_ema_period)
        self.stage = self.Start
        self.main_order = None
        self.stop_order = None
        self.limit_order = None
        self.exit_order = None
        self.main_order_price = None
        self.target1_price = None
        self.size = None
        self.symbol_parameter = SymbolParameter(self.p.symbol,
                                                self.broker.get_cash(),
                                                self.p.max_loss_percent)
Exemplo n.º 6
0
    def __init__(self):
        super().__init__()
        # indicators
        self.atr = btind.ATR(self.data, period=self.p.atr_period)
        self.hlc3 = (self.data.high + self.data.low + self.data.close) / 3
        self.baseline = btind.WMA(self.hlc3, period=self.p.baseline_period)
        self.highest_close = btind.Highest(self.data.close,
                                           period=self.p.breakout_period)
        self.lowest_close = btind.Lowest(self.data.close,
                                         period=self.p.breakout_period)

        self.stage = self.Start
        self.main_order = None
        self.stop_order = None
        self.limit_order = None
        self.exit_order = None
        self.main_order_price = None
        self.target1_price = None
        self.size = None
        self.symbol_parameter = SymbolParameter(self.p.symbol,
                                                self.broker.get_cash(),
                                                self.p.max_loss_percent)
	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')	
Exemplo n.º 8
0
	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']
Exemplo n.º 9
0
 def __init__(self):
     self.atr_base = btind.ATR(period=self.p.period)
	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

		#Define dictionaries and lists to be accessed from all timeframes
		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 = 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 and prior day bars
			self.inds[d._name]['ohlc'] = btind.OHLC(d,
											period=self.indp.get('ohlc'),
											plot=False)
											
			self.inds[d._name]['prior_ohlc'] = btind.priorday(d,
											period=self.indp.get('ohlc'),
											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=True)
													
			self.inds[d._name]['sma2'] = btind.SMA(d,
													period=self.indp.get('sma2'),
													plot=True)										
			
			"""
			self.inds[d._name]['ema1'] = btind.EMA(d,
													period=self.indp.get('ema1'),
													plot=False)
														
			self.inds[d._name]['ema2'] = btind.EMA(d,
													period=self.indp.get('ema2'),
													plot=False)
												
			
			#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=True)
			
			#ADX
			self.inds[d._name]['adx'] = btind.ADX(d,
												period=self.indp.get('adx'),
												plot=True)
			"""												
			#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)
																									
			if d._name == d._name[:-1]+'0' or d._name == d._name[:-1]+'1':
				self.inds[d._name]['vwap'] = btind.vwap(d,
															period=self.indp.get('vwap_lookback'),
															plot=True)
																								
			#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')	
Exemplo n.º 11
0
 def __init__(self):
     ATR = btind.ATR()
     self.l.ATR = ATR
     self.l.SMMA = btind.SMMA(self.l.ATR, period=self.p.period)