def get_reward(new_state, time_step, action, xdata, signal, terminal_state, eval=False, epoch=0): reward = 0 signal.fillna(value=0, inplace=True) if eval == False: bt = twp.Backtest(pd.Series(data=[x for x in xdata[time_step - 2:time_step]], index=signal[time_step - 2:time_step].index.values), signal[time_step - 2:time_step], signalType='shares') reward = ((bt.data['price'].iloc[-1] - bt.data['price'].iloc[-2]) * bt.data['shares'].iloc[-1]) if terminal_state == 1 and eval == True: # save a figure of the test set bt = twp.Backtest(pd.Series(data=[x for x in xdata], index=signal.index.values), signal, signalType='shares') reward = bt.pnl.iloc[-1] plt.figure(figsize=(3, 4)) bt.plotTrades() plt.axvline(x=400, color='black', linestyle='--') plt.text(250, 400, 'training data') plt.text(450, 400, 'test data') plt.suptitle(str(epoch)) plt.savefig('plt/' + 'value_iter_' + str(epoch) + '.png', bbox_inches='tight', pad_inches=1, dpi=72) plt.close('all') print(time_step, terminal_state, eval, reward) return reward
def get_reward(new_state, time_step, action, xdata, signal, terminal_state, eval=False, epoch=0): reward = 0 signal.fillna(value=0, inplace=True) if eval == False: # bt = twp.Backtest(pd.Series(data=[x for x in xdata], index=signal.index.values), signal, signalType='shares') # reward = bt.pnl.iloc[-1] # print(reward) bt = twp.Backtest(pd.Series(data=[x for x in xdata[time_step-2:time_step]], index=signal[time_step-2:time_step].index.values), signal[time_step-2:time_step], signalType='shares') reward = ((bt.data['price'].iloc[-1] - bt.data['price'].iloc[-2])*bt.data['shares'].iloc[-1]) # print(reward) # print(xdata[time_step-2:time_step]) # print(signal[time_step-2:time_step]) # print(bt.data) # print('*******************************') if terminal_state == 1 and eval == True: #save a figure of the test set # print(signal) bt = twp.Backtest(pd.Series(data=[x for x in xdata], index=signal.index.values), signal, signalType='shares') reward = bt.pnl.iloc[-1] # print(reward) # print(bt.data) # plt.figure(figsize=(3,4)) # bt.plotTrades() # plt.axvline(x=400, color='black', linestyle='--') # plt.text(250, 400, 'training data') # plt.text(450, 400, 'test data') # plt.suptitle(str(epoch)) # plt.savefig('plt/'+str(epoch)+'.png') #print(time_step, terminal_state, eval, reward) return reward
def get_reward(new_state, time_step, action, xdata, signal, terminal_state, eval=False, epoch=0): reward = 0 signal.fillna(value=0, inplace=True) if eval == False: bt = twp.Backtest(pd.Series(data=[x for x in xdata[time_step-2:time_step]], index=signal[time_step-2:time_step].index.values), signal[time_step-2:time_step], signalType='shares') reward = ((bt.data['price'].iloc[-1] - bt.data['price'].iloc[-2])*bt.data['shares'].iloc[-1]) if terminal_state == 1 and eval == True: #save a figure of the test set bt = twp.Backtest(pd.Series(data=[x for x in xdata], index=signal.index.values), signal, signalType='shares') reward = bt.pnl.iloc[-1] #print(time_step, terminal_state, eval, reward) return reward
def get_reward(new_state, time_step, action, xdata, signal, terminal_state, epoch=0): reward = 0 signal.fillna(value=0, inplace=True) if terminal_state == 0: # get reward for the most current action if signal[time_step] != signal[time_step - 1] and terminal_state == 0: i = 1 while signal[time_step - i] == signal[time_step - 1 - i] and time_step - 1 - i > 0: i += 1 reward = (xdata[time_step - 1, 0] - xdata[time_step - i - 1, 0] ) * signal[time_step - 1] * -100 + i * np.abs( signal[time_step - 1]) / 10.0 if signal[time_step] == 0 and signal[time_step - 1] == 0: reward -= 10 # calculate the reward for all actions if the last iteration in set if terminal_state == 1: # run backtest, send list of trade signals and asset data to backtest function bt = twp.Backtest(pd.Series(data=[x[0] for x in xdata]), signal, signalType='shares') reward = bt.pnl.iloc[-1] return reward
def get_reward(new_state, time_step, action, xdata, signal, terminal_state, epoch=0): # counter = 0; # counter +=1; # # print(counter) print('*******Get_reward********') reward = 0 print('time_step', time_step) signal.fillna(value=0, inplace=True) if terminal_state == 0: #get reward for the most current action if signal[time_step] != signal[time_step - 1] and terminal_state == 0: i = 1 print('signal[time_step]', signal[time_step]) print('SIGNALsignal[time_step-1]', signal[time_step - 1]) print('SIGNAL', signal) while signal[time_step - i] == signal[time_step - 1 - i] and time_step - 1 - i > 0: print('time_step-1-i', time_step - 1 - i) i += 1 print('*****i*****', i) reward = (xdata[time_step - 1, 0] - xdata[time_step - i - 1, 0] ) * signal[time_step - 1] * -100 + i * np.abs( signal[time_step - 1]) / 10.0 print('xdata', xdata) print('xdata[time_step-1, 0]', xdata[time_step - 1, 0]) print('xdata[time_step - i-1, 0]', xdata[time_step - i - 1, 0]) print('(xdata[time_step-1, 0] - xdata[time_step - i-1, 0])', (xdata[time_step - 1, 0] - xdata[time_step - i - 1, 0])) print('signal[time_step - 1]', signal[time_step - 1]) print('signal[time_step - 1]*-100', signal[time_step - 1] * -100) print('(-)*signal', (xdata[time_step - 1, 0] - xdata[time_step - i - 1, 0]) * signal[time_step - 1] * -100) print('i*np.abs(signal[time_step - 1])', i * np.abs(signal[time_step - 1])) if signal[time_step] == 0 and signal[time_step - 1] == 0: reward -= 10 # if you didn't trade for two days, you get a negative -10 #calculate the reward for all actions if the last iteration in set if terminal_state == 1: #run backtest, send list of trade signals and asset data to backtest function bt = twp.Backtest(pd.Series(data=[x[0] for x in xdata]), signal, signalType='shares') reward = bt.pnl.iloc[-1] # print(bt.data) # print(reward) print('*******Get_reward********') return reward
def get_reward(new_state, time_step, action, xdata, signal, terminal_state, reward_dict, eval=False, epoch=0): reward = 0 signal.fillna(value=0, inplace=True) if eval == False: for j in range(xdata.shape[1]): bt = twp.Backtest(pd.Series( data=[x[j] for x in xdata[time_step - 2:time_step]], index=signal[time_step - 2:time_step].index.values), signal[time_step - 2:time_step], signalType='shares') reward = (bt.data['price'].iloc[-1] - bt.data['price'].iloc[-2]) * bt.data['shares'].iloc[-1] reward_dict[j].append(reward) if terminal_state == 1 and eval == True: for j in range(xdata.shape[1]): bt = twp.Backtest(pd.Series(data=[x[j] for x in xdata], index=signal.index.values), signal, signalType='shares') reward = bt.pnl.iloc[-1] reward_dict[j].append(reward) plt.figure(figsize=(9, 16)) bt.plotTrades() plt.axvline(x=xdata.shape[0] - 100, color='black', linestyle='--') plt.text(250, 400, 'training data') plt.text(450, 400, 'test data') plt.suptitle(str(epoch)) plt.savefig('plt/' + 'knapsack_' + str(j) + ' ' + str(epoch) + '.png') plt.close('all') print('@ eval=TRUE') # print(time_step, terminal_state, eval, reward) return reward_dict
def setUp(self): self.data = data.Data(1, live=False) print("Loading Data:", DATA_FILE_NAME) data.loadData(DATA_FILE_NAME) self.backtester = backtest.Backtest(cash=10000, data=self.data, minBrokerFee=1.00, perShareFee=0.0075, simple=True, stateObj=True)
def get_reward(new_state, time_step, action, xdata, signal, terminal_state, eval=False, epoch=0): reward = 0 signal.fillna(value=0, inplace=True) if eval == False: try: bt = twp.Backtest(pd.Series(data=[x[0] for x in xdata[time_step - 2:time_step]], index=signal[time_step - 2:time_step].index.values), signal[time_step - 2:time_step], signalType='shares') reward = np.max((bt.data['price'].iloc[-1] - bt.data['price'].iloc[-2]) * bt.data['shares'].iloc[-1]) except: pass if terminal_state == 1 and eval == True: bt = twp.Backtest(pd.Series(data=[x[0] for x in xdata], index=signal.index.values), signal, signalType='shares') reward = bt.pnl.iloc[-1] plt.figure(figsize=(9, 16)) bt.plotTrades() plt.axvline(x=400, color='black', linestyle='--') plt.text(250, 400, 'training data') plt.text(450, 400, 'test data') plt.suptitle(str(epoch)) plt.savefig('plt/' + 'knapsack_' + str(epoch) + '.png') plt.close('all') ''' # save a figure of the test set plt.figure(figsize=(10, 25)) for i in range(xdata.T.shape[0]): #frame = pd.concat(btFrame, axis=1) bt = twp.Backtest(pd.Series(data=[x for x in xdata.T[i]], index=signal.index.values), signal, signalType='shares') reward += np.max(bt.pnl.iloc[-1]) bt.plotTrades() #plt.axvline(x=400, color='black', linestyle='--') #plt.text(250, 400, 'training data') #plt.text(450, 400, 'test data') #plt.suptitle(str(epoch)) plt.savefig('plt/' + 'knapsack_' + str(epoch) + '.png', bbox_inches='tight', pad_inches=1, dpi=72) plt.close('all') ''' # print(time_step, terminal_state, eval, reward) return reward
def test2(df): ema2h = ind.iMA(df, 2, ma_method='EMA', applied_price='Hight') ema2l = ind.iMA(df, 2, ma_method='EMA', applied_price='Low') ema40 = ind.iMA(df, 40, ma_method='EMA') # Buy : ema2lがema40よりも上側にあり、Close値がema2lにタッチした場合、エントリーし、 # BuyExit : High値がema2hにタッチしたら決済。 be = ((df.Close < enve['Lower']) & rsi10 < 30) bExt = ema20 < df.Close se = ((enve['Upper'] < df.Close) & 70 < rsi10) sExt = df.Close < ema20 t, pl = bt.Backtest(df, be, se, bExt, sExt, TP=1000, SL=5000, Limit=20) eq = bt.BacktestReport(t, pl)
def revTry(df): rsi10 = ind.iRSI(df, 10, 'Close') ema20 = ind.iMA(df, 20, ma_method='EMA') enve = ind.iEnvelopes(df, 10, 0.05) #ccc = pd.DataFrame({'Close' : df['Close'], 'EMA20': ema20, 'RSI' : rsi10, 'Upper' : enve['Upper'], 'Low' : enve['Lower']}) #print(ccc) # Buy : エンベロープの下に突き抜ける + RSIが30以下で買い。 be = ((df.Close < enve['Lower']) & rsi10 < 30) bExt = ema20 < df.Close se = ((enve['Upper'] < df.Close) & 70 < rsi10) sExt = df.Close < ema20 t, pl = bt.Backtest(df, be, se, bExt, sExt, TP=1000, SL=5000, Limit=20) eq = bt.BacktestReport(t, pl)
def main(): adxcross = MyStrategy() btest = bt.Backtest(500000.0, OTP_DATA, adxcross) btest.run() indicators = [ ind.cci(OTP_DATA, span=10), ind.cci(OTP_DATA, span=20), ] plotter = plt.plotter(OTP_DATA) for i in indicators: plotter.add_plot(i) plotter.plot()
def pair_target(up_bound, low_bound): up_bound = mu + up_bound * select_sigma low_bound = mu - low_bound * select_sigma Signal = arbitrage.Pairsignal() pairsignal = Signal.generate(spread = spread, mu = mu, \ up = up_bound, low = low_bound) data_backtest1 = copy.deepcopy(pairsignal) data_backtest1['price'] = data[asset2] - beta * data[asset1] data_backtest1['date'] = data_backtest.index data_backtest1['volumn'] = 1 Pairbacktest = backtest.Backtest(stop_lose=-1, stop_profit=1) backtest_result1 = Pairbacktest.run(data_backtest1, visual=False) # annulized vol vol = backtest_result1['daily_PLrate'].std() * np.sqrt(252) # annualized return return_ = (backtest_result1['new_value'].iloc[-1] - 1) * 252.0 / len(backtest_result1) # shape ratio sharpe = (return_ - rf) * 1.0 / (vol + 0.001) return sharpe
def getReward(timeStep, signal, price, state, endState): unit_reward = 1 if not endState: # net earning from previous action net = (price[timeStep] - price[timeStep - 1]) * signal[timeStep - 1] else: bt = twp.Backtest(price, signal, signalType='shares') net = bt.pnl.iloc[-1] rewards = 0 #intuition reward if net > 0: rewards += unit_reward * 2 elif net < 0: rewards -= unit_reward / 2 else: #rewards -= 1 #don't encourage hold rewards += 0 '''# sma reward if not endState: #sma20 = state[0,0,2] #sma80 = state[0,0,3] sma20 = state[0, 2] sma80 = state[0,3] sma_net = sma20 - sma80 if sma_net < 0 : # short sma < long sma, down trend if signal[timeStep - 1] < 0: rewards += unit_reward elif sma_net > 0: #short sma > long sma, up trend if signal[timeStep - 1] > 0: rewards += unit_reward''' return rewards
def test_agent(pdata, model, data, episode_i): signal = pd.Series(index=np.arange(len(data))) signal.fillna(value=0, inplace=True) signal.loc[0] = 1 state = initializeState(pdata) endState = 0 timeStep = 1 realProfit = 0 realInventory = [] while not endState: Q = model.predict(state, batch_size=1) action = (np.argmax(Q)) nextState, timeStep, signal, endState, realProfit, reward = trade( action, pdata, signal, timeStep, realInventory, data, realProfit) state = nextState while len(realInventory) > 0: realProfit += (data.iloc[-1] - realInventory.pop(0)) * 10 long = 0 short = 0 hold = 0 newdici = changedecision(signal) for j in range(signal.shape[0]): if signal.iloc[j] < 0: short += 1 elif signal.iloc[j] > 0: long += 1 else: hold += 1 newprofitwhold = profitcal(data, newdici, 5000) print("Profit with hold: ", newprofitwhold, " Orignal Profit: ", realProfit) print('Episode #: ', episode_i, ' No Random, Buy: ', long, ', Sell: ', short, ', Hold: ', hold) bt = twp.Backtest(data, signal, signalType='shares') plt.figure(figsize=(20, 20)) plt.subplot(2, 1, 1) plt.title("trades " + str(episode_i)) plt.xlabel("timestamp") bt.plotTrades() plt.subplot(2, 1, 2) plt.title("PnL " + str(episode_i)) plt.xlabel("timestamp") bt.pnl.plot(style='-') if episode_i % 10 == 0: plt.savefig('plot/' + str(episode_i) + '.png') plt.show() plt.close() bt2 = twp.Backtest(data, newdici, signalType='shares') plt.figure(figsize=(20, 20)) plt.subplot(2, 1, 1) plt.title("trades " + str(episode_i)) plt.xlabel("timestamp") bt2.plotTrades() plt.subplot(2, 1, 2) plt.title("PnL " + str(episode_i)) plt.xlabel("timestamp") bt2.pnl.plot(style='-') if episode_i % 10 == 0: plt.savefig('plot/' + str(episode_i) + '_hold.png') plt.show() plt.close() endReward = bt.pnl.iloc[-1] return endReward, realProfit
model.fit(state.reshape(1, 2), y, batch_size=1, nb_epoch=1, verbose=0) state = new_state if terminal_state == 1: # terminal state status = 0 eval_reward = evaluate_Q(indata, model) print("Epoch #: %s Reward: %f Epsilon: %f" % (i, eval_reward, epsilon)) learning_progress.append((eval_reward)) if epsilon > 0.1: epsilon -= (1.0 / epochs) elapsed = np.round(timeit.default_timer() - start_time, decimals=2) print("Completed in %f" % (elapsed, )) # plot results bt = twp.Backtest(pd.Series(data=[x[0] for x in xdata]), signal, signalType='shares') bt.data['delta'] = bt.data['shares'].diff().fillna(0) print(bt.data) plt.figure() bt.plotTrades() plt.suptitle('epoch' + str(i)) plt.savefig('plt/final_trades' + '.png', bbox_inches='tight', pad_inches=1, dpi=72) # assumes there is a ./plt dir plt.close('all') plt.figure()
import pandas as pd import backtest as twp from functions import getData import numpy as np # get price & techinical indicator data as pandas dataframe data, data_prev, sma20, sma80, slowD, slowK = getData() signal = pd.Series(index=np.arange(len(data))) for i in range(signal.shape[0]): signal.loc[i] = 10 #signal.loc[1] = 0 signal.loc[0] = 1 print(signal) bt = twp.Backtest(pd.Series(data=[x for x in data], index=signal.index.values), signal, signalType='shares') endReward = bt.pnl.iloc[-1] print(bt.data) print(signal.shape[0]) print(endReward)
xform_data = self.transform_.transform(X, y) except: xform_data = self.transform_.transform(X) return np.append(X, xform_data, axis=1) class LogExpPipeline(Pipeline): def fit(self, X, y): super(LogExpPipeline, self).fit(X, np.log1p(y)) def predict(self, X): return np.expm1(super(LogExpPipeline, self).predict(X)) import backtest bt = backtest.Backtest() bt.init_candle() bt.resetbacktest() bt.index = bt.size bt.updateIndicators() truth = bt.df memory = joblib.Memory(cachedir=".") n = truth.shape[1] # # XGBoost model # xgb_params = {} xgb_params['objective'] = 'reg:linear' xgb_params['learning_rate'] = 0.001 xgb_params['max_depth'] = int(6.0002117448743721)
up = up_bound, low = low_bound) data_backtest = copy.deepcopy(pairsignal) coin_beta = EGresult_df.loc['amid', 'gas'].beta[0][0] data_backtest['price'] = data['gas'] - coin_beta * data['amid'] data_backtest['date'] = data_backtest.index data_backtest['volumn'] = 1 # visualization data_backtest['signal'].plot() plt.title('Pair Trading Strategy Signal Result') plt.xlabel('Time') plt.ylabel('Spread') plt.show() # ---------------------------------------------------------------------------- # # ------------------------------ Strategy Backtest --------------------------- # # ---------------------------------------------------------------------------- # Pairbacktest = backtest.Backtest(stop_lose=-1, stop_profit=1) backtest_result = Pairbacktest.run(data_backtest, visual=True) # return distribution analysis #dist_return = backtest_result['daily_PLrate'][backtest_result['daily_PLrate'] != 0] #Return_dist = evaluation.Distribution(dist_return) #Return_dist.analysis() # signal analysis print '====================== Signal Analysis ======================' Pair_signal_est = evaluation.Signal_est(data=pairsignal) Pair_signal_est.analysis() # risk analysis print '======================= Risk Analysis =======================' Pair_risk_est = evaluation.Risk_est(data=backtest_result) Pair_risk_est.analysis() # drawdown analysis print '======================= P&L Analysis ========================'
if __name__ == "__main__": # DATA_FILE_NAME = "resource\\OIH_adjusted.csv" DATA_FILE_NAME = "resource\\SPY_EOD.csv" trader = trader.Trader() data = data.Data(1, live=False) print("Loading Data:", DATA_FILE_NAME) data.loadData(DATA_FILE_NAME) print("Data Loaded") featureB = featureBuild.FeatureBuilder(data=data, queueSize=QUEUE_SIZE) print("Feature Builder Created") backTester = backtest.Backtest(cash=10000, data=data, counter=QUEUE_SIZE, minBrokerFee=1.00, perShareFee=0.0075, simple=True, stateObj=True) print("Backtester Created") agent = ddpg.DeepDPG(state_shape=QUEUE_SIZE, normal_state_shape=featureB.numFeatures, criticParams=ddpg.CriticParams(), policyParams=ddpg.PolicyParams(), OUParams=ddpg.OUParams(), episodes=500) print("Agent Created") manager = manager.Manager(data=data, agent=agent, featureB=featureB, backtester=backTester,
# Update EMA calcs for imported data d.update_ema(ma_1) d.update_ema(ma_2) # Loop for x minutes # for mins in range(720): # d.refresh_ohlc() # print('Refreshing OHLC data. Last ID: ' + str(d.ohlc_last_id)) # time.sleep(60) # Set up brain object with data and trade vol b = brain.Brain(d) # Backtest - pass data and brain object parameters bt = backtest.Backtest(d, b) # Monte carlo simulation from min to max MA bt.run_ma_sim(type, 1, 50) # Simulate single MA combo and export simulated trades # bt.ma_sim(type, ma_1, ma_2) # bt.export_trades() # Save data # d.export_ohlc() # d.export_ohlc_csv() # Testing # d.get_cryptobal() # d.get_fiatbal()
def bestow(self, new_state, step, action, prices, trades, term, qs=None, epoch=None, plot=False, evaluate=False): ''' returns reward for agent given the action taken ''' bestowal = [0] * len(self.symbols) if evaluate == False: for i in range(len(self.symbols)): b_test = bt.Backtest( pd.Series(data=prices[i, step - 2:step], index=np.arange(step - 2, step)).astype(float), pd.Series(data=trades[step - 2:step, i], index=np.arange(step - 2, step)).astype(float), roundShares=False, signalType='shares') bestowal[i] = ((b_test.data['price'].iloc[-1] - b_test.data['price'].iloc[-2]) * b_test.data['trades'].iloc[-1]) elif evaluate == True and term == True: for i in range(len(self.symbols)): close = self.data_test[:, 0, 0] b_test = bt.Backtest(pd.Series(data=prices[i, :], index=np.arange(len(trades))), pd.Series(data=trades[:, i]), roundShares=False, initialCash=self.init_cash, signalType='shares') b_test2 = bt.Backtest(pd.Series(data=close, index=np.arange(len(trades))), pd.Series(data=trades[:, i]), roundShares=False, initialCash=self.init_cash, signalType='shares') bestowal[i] = b_test.pnl.iloc[-1] if (plot == True) and ((epoch % 20 == 0) or epoch == 1): matplotlib.rcParams['font.size'] = 24 f = plt.figure(figsize=(12, 12)) ax1 = f.add_subplot(2, 1, 1) b_test2.plotTrades() plt.axvline(x=round(self.data_size * 0.8), color='black', linestyle='--') ax2 = f.add_subplot(2, 1, 2) ax2.plot(b_test2.data.netProfit, color='green', label='Net Profit') ax2.plot(b_test2.pnl, color='k', label='Adj. Profit') plt.title('Profit and Loss') plt.legend() plt.savefig('./images/{}_{}sgd_summary.png'.format( self.symbols[i], epoch), bbox_inches='tight', pad_inches=0.5, dpi=60) plt.close('all') self.init_cash = 1000 self.init_cash = 1000 return bestowal