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 get_index_portfolio(self, index_t, dix): portfolio = dict() for t in commons.getHistSp500Composition( commons.date_index_external[dix])[index_t]: if dix >= commons.date_index_internal[ commons.data_sp500_1st_date[t]]: portfolio[t] = self.index_composition[index_t].ix[ commons.date_index_external[dix], t] return portfolio
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 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'])