Skip to content

Instantly share code, notes, and snippets.

@ucaiado
Created May 2, 2018 15:59
Show Gist options
  • Save ucaiado/d85a445bbe06ea407691e2f7f0ad36b3 to your computer and use it in GitHub Desktop.
Save ucaiado/d85a445bbe06ea407691e2f7f0ad36b3 to your computer and use it in GitHub Desktop.
Use rl_trading library to collect book informations
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Use data already downloaded to recover the bid ask prices of differet
instruments
@author: ucaiado
Created on 05/02/2018
"""
from market_gym.lob import matching_engine
from market_gym.envs import make
'''
Begin help functions
'''
'''
End help functions
'''
def collect_book_info(l_files, l_instrument, s_main_intrument, l_du, l_pu,
l_price_adj, f_time=30):
'''
Collect book information in 30 seconds intervals and return the environment
simulated
:param l_files: list. Name of the files of each instrument
:param l_instrument: list. list of intruments in the simulation
:param s_main_intrument: string. main instrument of simulation
:param l_du: list. time to maturity, in days
:param l_pu: list. settlement prices
:param l_price_adj: list. rates from settlement prices
:param *f_time: float. Time in seconds to collect the book
'''
d_rtn = {'time': [], 'TOB': []}
# set up the stop time object
l_h = [9, 10, 11, 12, 13, 14, 15, 16]
NextStopTime = matching_engine.NextStopTime(l_h, i_milis=500.)
obj_env = make('YieldCurve')
e = obj_env(l_fname=l_files,
l_instrument=l_instrument,
NextStopTime=NextStopTime,
s_main_intrument=s_main_intrument,
l_du=l_du,
l_pu=l_pu,
l_price_adj=l_price_adj)
f_next_check = e.order_matching.f_time + f_time
b_quit = False
while True:
try:
e.step()
if e.order_matching.f_time > f_next_check:
f_next_check = e.order_matching.f_time + f_time
try:
d_book = {}
for s_instr in e.l_instrument:
# get best prices
book_aux = e.order_matching.get_order_book_obj(s_instr)
d_book[s_instr] = {'qBid': book_aux.best_bid[1],
'Bid': book_aux.best_bid[0],
'qAsk': book_aux.best_ask[1],
'Ask': book_aux.best_ask[0]}
d_rtn['time'].append(e.order_matching.f_time)
d_rtn['TOB'].append(d_book)
except TypeError:
pass
except StopIteration:
b_quit = True
finally:
if e.done or b_quit:
break
return e, d_rtn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment