Need help to add web socket live streaming Data to cerebro()
-
Hi backtrader, thank your for awesome backtrader platform!
I use Upstox API, Indian stock broker. i want to add live streaming web socket data to cerebro to perform live trading. Following is my code to retrieve streaming data:
from upstox_api.api import * import datetime import pandas as pd api_key = open('api_key.txt','r').read() access_token = open('access_token.txt','r').read().strip() u = Upstox(api_key,access_token) master_contract = u.get_master_contract('MCX_FO') u.subscribe(u.get_instrument_by_symbol('MCX_FO', 'CRUDEOILM18DECFUT'), LiveFeedType.LTP) def event_handler_quote_update(message): df1 = pd.DataFrame.from_dict(message) df1.drop('exchange',axis=1,inplace=True) df1.drop('close',axis=1,inplace=True) df1.drop('symbol',axis=1,inplace=True) df1.drop('timestamp',axis=1,inplace=True) df1.drop('instrument',axis=1,inplace=True) df2 = df1.iloc[0,0] print(df2) u.set_on_quote_update(event_handler_quote_update) u.start_websocket(True)
Output:
3558.0
3559.0
3559.0
3558.0
3558.0
3558.0
3560.0
.....
.....
My Questions:- Now i need to construct open, high, low, close data using above streaming output
- Then resample above constructed data to "5Min" and then ADD this data as a live feed data to cerebro.
and please mention where to locate codes that you will be providing.
Please help me out with code to achieve my need. Thanks again!
-
Your best bet is to look at the
OandaStore
(and associatedOandaData
andOandaBroker
) for an implementation. Things you will for sure need:- A background thread in which things run
- Because it is a websockets implementation: a web server (although there are wrappers which isolate you from this and the Upstox API seems to already do this)
- A lot of patience during the implementation, mostly related to testing and ironing out the specific details of the provider.
References:
-
@saish-gadekar said in Need help to add web socket live streaming Data to cerebro():
- Then resample above constructed data to "5Min" and then ADD this data as a live feed data to cerebro.
Resampling is already built-in and is not bound to any specific data feed: Docs - Resampling