ucaiado / rl_trading Goto Github PK
View Code? Open in Web Editor NEWAn environment to high-frequency trading agents under reinforcement learning
License: Apache License 2.0
An environment to high-frequency trading agents under reinforcement learning
License: Apache License 2.0
Make the library closer to OpenAI gym's behavior. Currently, all updates happened inside environment step, including the agent's update. Would be clearer if it was performed outside step. So, following the Gym's behavior, we would do:
import gym
env = gym.make('YieldCurve')
for i_episode in range(20):
# here I am not so sure.
observation = env.reset()
for t in range(100):
# it is equivalent to do a random action
action = env.action_space.sample()
# the environment step would answer with all info needed
# now, the environment answer with the msgs generated by the
# interaction between env and agent
observation, reward, done, info = env.step(action)
# the flag done could be used by different reasons
if done:
print("Episode finished after {} timesteps".format(t+1))
break
My only concern is that the Enrironment handles when the agent need to hedge positions. Where should I perform that action if not in the env.step?
Around the line 134, in environment.py, change:
if self.primary_agent == agent:
if carry_pos and f_pos != 0:
# set up d_pos variable properly
if s_instr not in d_pos:
d_pos[s_instr] = {}
d_pos[s_instr]['Q'] = f_pos
d_pos[s_instr]['P'] = f_price_adj
to
if self.primary_agent: # include this line
if self.primary_agent == agent:
if carry_pos and f_pos != 0:
# set up d_pos variable properly
if s_instr not in d_pos:
d_pos[s_instr] = {}
d_pos[s_instr]['Q'] = f_pos
d_pos[s_instr]['P'] = f_price_adj
The f_pnl in the loop should be changed in this piece of code in log_trial()
method from Environment
class:
# log metrics
f_pnl = float('{:0.2f}'.format(d_info['pnl']))
na_pnl = filterout_outliers(d_info['pnl_hist'], d_info['time'])
# calculate the MDD
f_max = 0.
l_aux = []
for f_pnl in na_pnl:
if f_pnl > f_max:
f_max = f_pnl
l_aux.append(f_max-f_pnl)
There are many conditinals that could be optimized using try (I guess)
Include the method in Run
class from agent.py
:
def _basic(self, e, f_aux, s_valfunc=None, i_version=None):
'''
'''
s_fdate = e.order_matching.s_file.split('_')[0].split('/')[-1]
e.set_reward_function('pnl')
s_hedging_on = self._get_from_argv('s_hedging_on')
s1 = e.s_main_intrument[-3:]
s2 = s_hedging_on[-3:]
e.set_log_file('basic_{}{}_{}'.format(s1, s2, s_fdate))
b_hedging = self._get_from_argv('b_hedging')
b_hedging = True
# check if there are initial positions
d_initial_pos = self._get_from_argv('d_initial_pos')
b_keep_pos = self._get_from_argv('b_keep_pos')
if not d_initial_pos:
d_initial_pos = {}
if not s_hedging_on:
s_hedging_on = 'DI1F19'
a = e.create_agent(BasicAgent,
f_min_time=f_aux,
d_initial_pos=d_initial_pos,
f_ttoupdate=30.)
return a
Modify the set_experiments_opotions(self)
to:
def set_experiments_options(self):
'''
'''
d_experiments = {'_basic': self._basic,
'_qlearning': self._qlearning,
'_domino_q': self._domino_q,
'_random': self._random,
'_domino_rand': self._domino_rand}
return d_experiments
And finally, in the class BasicAgent
in agent_frwk.py
include in the last line d_rtn['features'] = {'null': None}
to the method get_intern_state(self, inputs, state)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.