Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Need help to add web socket live streaming Data to cerebro()

    General Code/Help
    2
    3
    1075
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      Saish Gadekar last edited by

      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:

      1. Now i need to construct open, high, low, close data using above streaming output
      2. 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!

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by backtrader

        Your best bet is to look at the OandaStore (and associated OandaData and OandaBroker) 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:

        • https://github.com/backtrader/backtrader/blob/master/backtrader/stores/oandastore.py
        • https://github.com/backtrader/backtrader/blob/master/backtrader/feeds/oanda.py
        • https://github.com/backtrader/backtrader/blob/master/backtrader/brokers/oandabroker.py
        1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators @Saish Gadekar last edited by

          @saish-gadekar said in Need help to add web socket live streaming Data to cerebro():

          1. 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

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }