class TradeManager(object): ## ## This is the entry point for TradeManager ## TradeManager uses the IndicatorLibrary and the indicators that were passed in through the config file to ## determine if any of the indictors return true. ## It then calls the DataManager with the start and end dates, historical data period and mark out period ## to collect price information for a given symbol. def __init__(self, config, logger): self.config = config self.logger = logger self.start_date = config.get_value('PORTFOLIO', 'startdate') self.end_date = config.get_value('PORTFOLIO', 'enddate') self.name = self.config.get_value('PORTFOLIO', 'name') self.prevent_overlaps = { } ## used to disallow same ticker+markout overlapping ## Get the list of indicators from the config file, then start IndicatorLibrary self.list_of_user_indicators = [ s.upper() for s in Util.str_to_list( config.get_value('STRATEGY', 'indicators')) ] if not self.list_of_user_indicators: self.logger.critical("Unable to determine list of user indicators") sys.exit(1) self.strategy = IndicatorLibrary(self.list_of_user_indicators) self.list_of_markout_periods = [] try: for x in Util.str_to_list( config.get_value('STRATEGY', 'markout_periods')): self.list_of_markout_periods.append(int(x)) except: self.logger.critical( "Non integer-type value provided in STRATEGY.markout_periods") sys.exit(1) max_markout_periods = max(self.list_of_markout_periods) max_historical_periods = self.strategy.periods_required() self.dm = DataManager(logger, self.start_date, self.end_date, max_historical_periods, max_markout_periods) self.__trade_log_fn() def __trade_log_fn(self): strategy_name = self.config.get_value('STRATEGY', 'name') ext = self.config.get_value('PORTFOLIO', 'trade_log_file_ext') self.trade_log_name = ".".join([ str(self.start_date), str(self.end_date), self.name, strategy_name, ext ]) print self.trade_log_name self.config.put('trade_log_file_name', self.trade_log_name) ## ## This method calls DataManager to get the future date and price for each markout period for a given symbol ## def run(self): with open(self.trade_log_name, 'w') as trade_log: trade_log.write( 'date,ticker,mo_period,entry_price,exit_price,exit_date\n') self.logger.info("Writing to " + self.trade_log_name) list_of_user_tickers = [ s.upper() for s in Util.str_to_list( self.config.get_value("PORTFOLIO", "tickers")) ] if not list_of_user_tickers: self.logger.critical( "Unable to determine list of user-provided tickers") sys.exit(1) for trade_date in self.dm.trading_dates(): for ticker in list_of_user_tickers: if (self.strategy.calc(self.dm, ticker, trade_date)): today_price = self.dm.get(ticker, trade_date).close for mo_period in self.list_of_markout_periods: future_date = self.dm.date_by_offset( trade_date, mo_period) overlap_key = ticker + str(mo_period) if overlap_key in self.prevent_overlaps and trade_date <= self.prevent_overlaps[ overlap_key]: continue self.prevent_overlaps[overlap_key] = future_date future_price = self.dm.get(ticker, future_date).close trade_log.write("{0},{1},{2},{3},{4},{5}\n".format( trade_date, ticker, mo_period, today_price, future_price, future_date)) return True
class TradeManager(object): ## ## This is the entry point for TradeManager ## TradeManager uses the IndicatorLibrary and the indicators that were passed in through the config file to ## determine if any of the indictors return true. ## It then calls the DataManager with the start and end dates, historical data period and mark out period ## to collect price information for a given symbol. def __init__(self, config, logger): self.config = config self.logger = logger self.start_date = config.get_value('PORTFOLIO','startdate') self.end_date = config.get_value('PORTFOLIO', 'enddate') self.name = self.config.get_value('PORTFOLIO', 'name') self.prevent_overlaps = {} ## used to disallow same ticker+markout overlapping ## Get the list of indicators from the config file, then start IndicatorLibrary self.list_of_user_indicators = [s.upper() for s in Util.str_to_list(config.get_value('STRATEGY', 'indicators'))] if not self.list_of_user_indicators: self.logger.critical("Unable to determine list of user indicators") sys.exit(1) self.strategy = IndicatorLibrary(self.list_of_user_indicators) self.list_of_markout_periods = [] try: for x in Util.str_to_list(config.get_value('STRATEGY', 'markout_periods')): self.list_of_markout_periods.append(int(x)) except: self.logger.critical("Non integer-type value provided in STRATEGY.markout_periods") sys.exit(1) max_markout_periods = max(self.list_of_markout_periods) max_historical_periods = self.strategy.periods_required() self.dm = DataManager(logger, self.start_date,self.end_date,max_historical_periods,max_markout_periods) self.__trade_log_fn() def __trade_log_fn(self): strategy_name = self.config.get_value('STRATEGY','name') ext = self.config.get_value('PORTFOLIO', 'trade_log_file_ext') self.trade_log_name = ".".join([str(self.start_date),str(self.end_date),self.name,strategy_name,ext]) print self.trade_log_name self.config.put('trade_log_file_name', self.trade_log_name) ## ## This method calls DataManager to get the future date and price for each markout period for a given symbol ## def run(self): with open(self.trade_log_name, 'w') as trade_log: trade_log.write('date,ticker,mo_period,entry_price,exit_price,exit_date\n') self.logger.info("Writing to " + self.trade_log_name) list_of_user_tickers = [s.upper() for s in Util.str_to_list(self.config.get_value("PORTFOLIO", "tickers"))] if not list_of_user_tickers: self.logger.critical("Unable to determine list of user-provided tickers") sys.exit(1) for trade_date in self.dm.trading_dates(): for ticker in list_of_user_tickers: if (self.strategy.calc(self.dm, ticker, trade_date)): today_price = self.dm.get(ticker, trade_date).close for mo_period in self.list_of_markout_periods: future_date = self.dm.date_by_offset(trade_date, mo_period) overlap_key = ticker + str(mo_period) if overlap_key in self.prevent_overlaps and trade_date <= self.prevent_overlaps[overlap_key]: continue self.prevent_overlaps[overlap_key] = future_date future_price = self.dm.get(ticker, future_date).close trade_log.write("{0},{1},{2},{3},{4},{5}\n".format(trade_date,ticker,mo_period,today_price,future_price,future_date)) return True