def initialize_index(self): for k, v in commons.getIndexCodes().items(): index_t = v[-8:] self.index_composition[index_t] = pd.read_hdf( commons.data_path + 'PCT_' + index_t + '.h5', 'table', mode='r')
def play_for_a_day(idx_external, dix, alpha, gamma, sim_uuid, train_uuid, scenario): global f, m temperature = 1.5 state = dict() proposed_action = dict() reward = dict() dba.db_main.flush() index_t = commons.getIndexCodes()[idx_external][-8:] for ticker in commons.getHistSp500Composition( commons.date_index_external[dix])[index_t]: reward[ticker] = 9999 if f.trained(train_uuid, ticker): state[ticker] = f.get_forecast_state(ticker, dix) proposed_action[ticker] = dba.get_softmax_action( ticker, state[ticker], temperature, scenario) price = f.get_order_price(ticker, state[ticker], dix, proposed_action[ticker], m.get_closing_price(ticker, dix)) dba.log_recommendation(sim_uuid, dix, ticker, proposed_action[ticker], price, state[ticker]['12dd_Close'], m.get_index_portfolio(index_t, dix)[ticker]) else: dba.log_recommendation(sim_uuid, dix, ticker, 9, 0, 0, m.get_index_portfolio(index_t, dix)[ticker])
def align_index_portfolio(self, dix): for k, v in commons.getIndexCodes().items(): #sell everything and repurchase with the current index composition index_t = v[-8:] cash = 0 for ticker, volume in self.portfolio[index_t].items(): cash += volume * self.get_closing_price(ticker, dix) self.initialize_portfolio(k, dix, cash)
def __init__(self, dba, initial_budget, firstDix, earliest=False, portfolioValue=dict()): self.dba = dba self.transaction_price = 10 self.initialize_index() self.data_sp500 = pd.read_hdf(commons.data_path + 'WIKI_SP500.h5', 'table') self.data_sp500 = self.data_sp500.fillna(method='backfill') startdate = dict() for k, v in commons.getIndexCodes().items(): if earliest: startdate[v[-8:]] = min(self.index_composition[v[-8:]].index) else: startdate[v[-8:]] = commons.date_index_external[firstDix] for k, v in commons.getIndexCodes().items(): index_t = v[-8:] if startdate[index_t] <= min( self.index_composition[index_t].index): startdate[index_t] = min(self.index_composition[index_t].index) if startdate[index_t] not in self.index_composition[index_t].index: while startdate[index_t] not in self.index_composition[ index_t].index: startdate[index_t] = startdate[index_t] + dt.timedelta( days=1) if portfolioValue == dict(): self.initialize_portfolio( k, commons.date_index_internal[startdate[index_t]], initial_budget) else: self.initialize_portfolio( k, commons.date_index_internal[startdate[index_t]], portfolioValue[index_t]) self.startdates[index_t] = startdate[index_t]
def initialize_portfolio(self, idx_external, dix, budget): index_t = commons.getIndexCodes()[idx_external][-8:] self.portfolio[index_t] = dict() self.portfolio[index_t] = self.get_index_portfolio(index_t, dix) portfolio = dict() for ticker, pct in self.portfolio[index_t].items(): portfolio[ticker] = pct * budget / self.get_closing_price( ticker, dix) self.portfolio[index_t] = portfolio try: a = self.portfolio_log[index_t] except KeyError: self.portfolio_log[index_t] = pd.DataFrame()
def calcFundamentals(self,ticker,indicator): comp=commons.getSp500CompositionAll() for k,v in commons.getIndexCodes().items(): columns=list([]) for t in comp[k]: columns.append(str(t)+indicator) if self.df1st_date[ticker]<min(self.dfFundamentals.index): min_date=min(self.dfFundamentals.index) else: min_date=self.df1st_date[ticker] ret=self.dfFundamentals.ix[min_date:,columns].mean(axis=1).to_frame() ret.columns=list([str(indicator).strip('_ARQ')]) return ret
def play_for_a_day(idx_external,dix,sim_uuid,minTraining,offsetTraining): global rQ state=dict() proposed_action=dict() dba.db_main.flush() index_t=commons.getIndexCodes()[idx_external][-8:] for ticker in commons.getHistSp500Composition(commons.date_index_external[dix])[index_t]: if rQ.trained(ticker): state[ticker]=rQ.get_forecast_state(ticker,dix) proposed_action[ticker]=rQ.get_best_action(state[ticker]) price=rQ.get_order_price(ticker,state[ticker],dix,proposed_action[ticker]) dba.log_recommendation(minTraining,offsetTraining,sim_uuid,dix,ticker,proposed_action[ticker],price,state[ticker]['12dd_Close'],rQ.get_index_portfolio(index_t,dix)[ticker],rQ.getModelAccuracy(ticker,'1dd_Close')) else: dba.log_recommendation(minTraining,offsetTraining,sim_uuid,dix,ticker,9,[0,0,0],0,rQ.get_index_portfolio(index_t,dix)[ticker],0)
def readPortfolio(): globals portfolio,portfolioValue,cash with open(daily_path+'currentPortfolio.csv','r') as csvfile: csvreader=csv.reader(csvfile, delimiter=',') lI=0 for k,v in commons.getIndexCodes().items(): index_t=v[-8:] portfolio[index_t]=dict() for row in csvreader: if lI>0: if dt.datetime.strptime(row[0],'%m/%d/%Y')==commons.idx_today: if row[2]=='PORTFOLIOCASH': cash[row[1]]=int(row[3]) elif row[2]=='PORTFOLIOVALUE': portfolioValue[row[1]]=int(row[3]) else: portfolio[row[1]][row[2]]=int(row[3]) else: raise 'currentPortfolioWrongDate' iI+=1 csvfile.close()
def index_portfolio_value(self, idx_external, dix): index_t = commons.getIndexCodes()[idx_external][-8:] value = 0 for ticker, volume in self.portfolio[index_t].items(): value += volume * self.get_closing_price(ticker, dix) return value
for dix in range(firstDate, commons.date_index_internal[commons.max_date['WIKI_SP500']]): print 'date;', commons.date_index_external[dix] if (dix - 14) % 20 == 0: start = time.time() train_uuid = uuid.uuid1().hex #train_uuid='2bf75d91cdb411e68bbbc82a142bddcf' print 'Retraining the models. Date:', commons.date_index_external[ dix], 'training guid:', train_uuid newTraining = cl_trainSection(dix - 1, train_uuid, scenario, offset, True) newTraining.train() f = forecast(m, train_uuid) end = time.time() print 'Training took', end - start, 'seconds.' if commons.date_index_external[dix].year != runningyear: maxsim = getMaxSimrun(dba) print 'Simulation for year:', commons.date_index_external[ dix].year, 'started. Simulation:', maxsim runningyear = commons.date_index_external[dix].year p.log_portfolio(dix, sim_uuid) for k, v in commons.getIndexCodes().items(): index_t = v[-8:] play_for_a_day(k, dix, alpha, gamma, sim_uuid, train_uuid, scenario) import extract_stats
def play_for_a_day(idx_external, dix, alpha, gamma, sim_uuid, train_uuid, scenario): global f temperature = 1.5 state = dict() proposed_action = dict() order_entry = dict() order_random = dict() order_untrained = dict() reward = dict() action = dict() dba.db_main.flush() random_action = dict() index_t = commons.getIndexCodes()[idx_external][-8:] sp500_ticker = commons.getHistSp500Ticker(commons.date_index_external[dix]) for ticker in commons.getHistSp500Composition( commons.date_index_external[dix])[index_t]: reward[ticker] = 9999 if f.trained(train_uuid, ticker): if scenario == 'q_learning': try: state[ticker] = new_state[ticker] except KeyError: state[ticker] = f.get_forecast_state(ticker, dix) else: state[ticker] = f.get_forecast_state(ticker, dix) proposed_action[ticker] = dba.get_softmax_action( ticker, state[ticker], temperature, scenario) max_q_action = dba.get_max_q(ticker, state[ticker]) if scenario == 'q_learning': if max_q_action[2] == False and max_q_action[ 1] == proposed_action[ticker]: random_action[ticker] = False else: random_action[ticker] = True else: random_action[ticker] = False ##compare max_axction and proposed action and if different then set volume to 1 next_dix = dix + 1 #sell if proposed_action[ticker] == commons.action_code['sell']: try: if random_action[ticker] == False: vol = p.portfolio[index_t][ticker] else: vol = 1 if vol > 0: opening_price = m.get_opening_price(ticker, next_dix) forecast_price = f.get_order_price( ticker, state[ticker], dix, proposed_action[ticker], m.get_closing_price(ticker, dix)) # if opening_price>(forecast_price*.997): # forecast_price=opening_price*.997 reward[ticker] = p.execute_order( ticker, vol, next_dix, forecast_price, proposed_action[ticker], m.get_closing_price(ticker, dix), state[ticker]['12dd_Close']) dba.log_recommendation(sim_uuid, dix, ticker, commons.action_code['sell'], vol) except KeyError: p.portfolio[index_t][ticker] = 0 print 'New Ticker: ', ticker #buy, but only after everythin has been sold if proposed_action[ticker] == commons.action_code['buy']: if random_action[ticker] == False: order_entry[ticker] = m.get_opening_price(ticker, next_dix) else: order_random[ticker] = m.get_opening_price( ticker, next_dix) dba.log_recommendation(sim_uuid, dix, ticker, commons.action_code['buy']) else: #for the tickers that are not trained yet align with the index action[ticker] = f.getActionUntrained(p, index_t, ticker, dix) if action[ticker][0] == commons.action_code['buy']: order_untrained[ticker] = m.get_opening_price(ticker, next_dix) elif action[ticker][0] == commons.action_code['sell']: x = p.execute_order(ticker, action[1], next_dix, m.get_opening_price(ticker, next_dix), commons.action_code['sell'], m.get_closing_price(ticker, dix), state[ticker]['12dd_Close']) #allocate money for the randoms for ticker, opening_price in order_random.items(): forecast_price = f.get_order_price(ticker, state[ticker], dix, commons.action_code['buy'], m.get_closing_price(ticker, dix)) if order_random[ticker] * 1.01 < forecast_price: forecast_price = order_random[ticker] * 1.01 reward[ticker] = p.execute_order(ticker, 1, next_dix, forecast_price, commons.action_code['buy'], m.get_closing_price(ticker, dix), state[ticker]['12dd_Close']) #allocate for alignment for ticker, opening_price in order_untrained.items(): x = p.execute_order(ticker, action[ticker][1], next_dix, m.get_opening_price(ticker, next_dix), commons.action_code['buy'], m.get_closing_price(ticker, dix), 0) budget = dict() for k, v in commons.getIndexCodes().items(): index_t = v[-8:] budget[index_t] = p.cash[index_t] for ticker, price in order_entry.items(): index_t = sp500_ticker[ticker] budget[index_t] += p.portfolio[index_t][ticker] * m.get_closing_price( ticker, dix) #order book; realign the portfolio to the index according to buying recommendations orderBook = p.get_portfolio_alignment(budget, order_entry, dix) for ticker, volume in orderBook.items(): if volume < 0: #selling what we have too much of forecast_price = f.get_order_price( ticker, state[ticker], dix, commons.action_code['sell'], m.get_closing_price(ticker, dix)) # if order_entry[ticker]>(forecast_price*.997): # forecast_price=order_entry[ticker]*.997 a = p.execute_order(ticker, 0 - volume, next_dix, forecast_price, commons.action_code['sell'], m.get_closing_price(ticker, dix), state[ticker]['12dd_Close']) for ticker, volume in orderBook.items(): if volume > 0: if commons.data_sp500_1st_date[ ticker] <= commons.date_index_external[dix]: forecast_price = f.get_order_price( ticker, state[ticker], dix, commons.action_code['buy'], m.get_closing_price(ticker, dix)) if order_entry[ticker] < forecast_price: forecast_price = order_entry[ticker] reward[ticker] = p.execute_order( ticker, volume, next_dix, forecast_price, commons.action_code['buy'], m.get_closing_price(ticker, dix), state[ticker]['12dd_Close']) dba.log_recommendation(sim_uuid, dix, ticker, commons.action_code['buy'], volume) #on the way to the next q dix += 1 if scenario == 'q_learning': for ticker in commons.getHistSp500Composition( commons.date_index_external[dix])[index_t]: if f.trained(train_uuid, ticker): new_state[ticker] = f.get_forecast_state(ticker, dix) try: if reward[ticker] != 9999: newQ=dba.get_reward(ticker,state[ticker],proposed_action[ticker])+\ alpha*(reward[ticker]+gamma*dba.get_max_q(ticker,new_state[ticker])[0]-dba.get_reward(ticker,state[ticker],proposed_action[ticker])) dba.update_q_table(ticker, state[ticker], proposed_action[ticker], newQ, dix) except KeyError: p.portfolio[index_t][ticker] = 0 print 'Date:', dix, ' New Ticker:', ticker
def playForADay(idx_external,sim_uuid,train_uuid,scenario): global f,refDix,refDate,tradeDix,tradeDate temperature=1.5 state=dict() proposed_action=dict() order_entry=dict() order_untrained=dict() reward=dict() action=dict() dba.db_main.flush() index_t=commons.getIndexCodes()[idx_external][-8:] sp500_ticker=commons.getHistSp500Ticker(commons.date_index_external[refDix]) for ticker in commons.getHistSp500Composition(commons.date_index_external[refDix])[index_t]: reward[ticker]=9999 if f.trained(train_uuid,ticker): state[ticker]=f.get_forecast_state(ticker,refDix) proposed_action[ticker]=dba.get_softmax_action(ticker,state[ticker],temperature,scenario) #sell if proposed_action[ticker]==commons.action_code['sell']: vol=p.portfolio[index_t][ticker] forecastPrice=f.get_order_price(ticker,state[ticker],refDix,proposed_action[ticker],\ m.get_closing_price(ticker,refDix)) x=p.execute_order(ticker,vol,dix,forecast_price,proposed_action[ticker],\ m.get_closing_price(ticker,dix),state[ticker]['12dd_Close'],False) logRecommendation(tradeDate,ticker,'sell',vol,forecastPrice,tradeDate,state[ticker]['12dd_Close']) #buy, but only after everythin has been sold if proposed_action[ticker]==commons.action_code['buy']: order_entry[ticker]=0 else: #for the tickers that are not trained yet align with the index action[ticker]=f.getActionUntrained(p,index_t,ticker,refDix) if action[ticker][0]==commons.action_code['buy']: order_untrained[ticker]=0 elif action[ticker][0]==commons.action_code['sell']: x=p.execute_order(ticker,action[ticker][1],tradeDix,m.get_closing_price(ticker,refDix),\ commons.action_code['sell'],m.get_closing_price(ticker,refDix),0,False) logRecommendation(tradeDate,ticker,'sellOpen',action[ticker][1],0,tradeDate,0) #allocate for alignment for ticker,opening_price in order_untrained.items(): x=p.execute_order(ticker,action[ticker][1],tradeDix,m.get_closing_price(ticker,refDix),\ commons.action_code['buy'],m.get_closing_price(ticker,refDix),0,False) logRecommendation(tradeDate,ticker,'buyOpen',action[ticker][1],0,tradeDate,0) budget=dict() for k,v in commons.getIndexCodes().items(): index_t=v[-8:] budget[index_t]=p.cash[index_t] for ticker,price in order_entry.items(): index_t=sp500_ticker[ticker] budget[index_t]+=p.portfolio[index_t][ticker]*m.get_closing_price(ticker,refDix) #order book; realign the portfolio to the index according to buying recommendations orderBook=p.get_portfolio_alignment(budget,order_entry,refDix) for ticker,volume in orderBook.items(): if volume<0: #selling what we have too much of forecast_price=f.get_order_price(ticker,state[ticker],refDix,commons.action_code['sell'],\ m.get_closing_price(ticker,refDix)) x=p.execute_order(ticker,0-volume,tradeDix,forecast_price,commons.action_code['sell'],\ m.get_closing_price(ticker,refDix),state[ticker]['12dd_Close'],False) logRecommendation(tradeDate,ticker,'sell',volume,forecast_price,tradeDate,state['ticker']['12dd_Close']) for ticker,volume in orderBook.items(): if volume>0: if commons.data_sp500_1st_date[ticker]<=refDate: forecast_price=f.get_order_price(ticker,state[ticker],refDix,commons.action_code['buy'],\ m.get_closing_price(ticker,refDix)) x=p.execute_order(ticker,volume,tradeDix,forecast_price,commons.action_code['buy'],\ m.get_closing_price(ticker,refDix),state[ticker]['12dd_Close'],False) logRecommendation(tradeDate,ticker,'buy',volume,forecast_price,tradeDate,state['ticker']['12dd_Close'])
def initializeIndex(self): for k,v in commons.getIndexCodes().items(): index_t=v[-8:] self.indexComposition[index_t]=commons.read_dataframe(commons.data_path+'PCT_'+index_t+'.h5')