Skip to content

Instantly share code, notes, and snippets.

@bileki
Created May 15, 2018 05:28
Show Gist options
  • Save bileki/689917a669d7f5502d5b17e24fb69b60 to your computer and use it in GitHub Desktop.
Save bileki/689917a669d7f5502d5b17e24fb69b60 to your computer and use it in GitHub Desktop.
#!/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, book
from market_gym.envs import make
import market_gym.utils.di_utilities as di_utilities
'''
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
# AttributeError: 'YieldCrvEnv' object has no attribute 'book'
testBook = e.book.get_n_top_prices(10, True)
##############################################################
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]}
print(testBook[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
if __name__ == '__main__':
l_instrument=['DI1F18']
s_main_intrument='DI1F18'
s_root = 'data/preprocessed/'
s_date = '20170201'
l_files = [s_root + s_date + '/' + s_date + '_{}_{}_new.zip']
l_du = []
l_pu = []
l_price_adj = []
l_du, l_pu, l_price_adj = di_utilities.get_param_to_env(s_date, l_instrument)
collect_book_info(l_files, l_instrument, s_main_intrument, l_du, l_pu, l_price_adj)
@ucaiado
Copy link

ucaiado commented May 15, 2018

Opa. Não é o Environment que tem este método, é um book específico (https://github.com/ucaiado/rl_trading/blob/master/market_gym/lob/book.py#L672). Então, neste caso:

    # testBook = e.book.get_n_top_prices(10, True)
    book_aux = e.order_matching.get_order_book_obj(s_instr)
    df = book_aux.get_n_top_prices(10, True)
    print(df)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment