Live Streaming Historical Data

AnkitDoshi
I have a small confusion-
Please help me on this as I m little new to Automating trading
I cant understand one thing -
When i call historical data (be it of any interval), I get the data till that particular time. This is good for backtesting.
But when I want to trade in live where I would require some historical data to apply logic and than take it to live data of 'that particular Interval' how do I do it?

For example-
instrument_token= crudeoil token
from_date= '2016-11-20'
to_date='2016-11-28'
crudeoil=kite.historical(instrument_token, from_date, to_date,interval)
Suppose i call this @6:30 pm. I get data till 6:30 pm
Now as time passes this data doesnt get updated as expected as it is till a particular time.

I run a strategy on crudeoil , this is nothing but backtest till 6:30 pm of 28th Nov..

How do I take it further with live data and most importantly with same time interval (in this case 5minute OHLC) to run strategy live?
Does websocket help me?
If yes than How?
  • Matti
    @AnkitDoshi , you can plot your own live candles using the data streamed via the websocket.
  • AnkitDoshi
    Is that the only way?
    This is how the others do who uses intervals other than TickData?
  • sujith
    Hi @AnkitDoshi,

    In future we may stream candles until then this is the only way.
  • pg1337
    @AnkitDoshi : If you are using Python, you would want to use some kind of scheduler to fetch the latest candle data every 5 minutes ( or whatever is your time frame), generate your trading signals and send orders as necessary. In between, use the web socket data to monitor the price and trigger stop loss/ take profit orders as necessary.

    As an alternative, you need not request Candles after the first call, as you can store it and then build candles yourself from the tick data from the websockets.
  • AnkitDoshi
    @pg1337 :wink: Thanks mate
    1. You mean I use Historical candles data till some point say previous days's 15:30 and for next day i run a function that fetches next 5 min candles as and when it happens into the same excel file or database and keep on adding new rows subsequently, right?

    please help me on this-
    2. I was reading on the same thing in stackoverflow. This is what I have come up with-
    - I will first Initialize WebSocket and def on_tick and on_connect functions in it. But under on_tick instead of printing ticks i will write that data to say CSV file [ I dont know whether it will keep updating as and when ticks come]
    - Than I will give it a timestamp which will be the server's on which i will run the logic
    - Using Pandas DataFrame i will convert the CSV data to a proper 5 min OHLC data using re-sampling method into a new excel sheet
    - Ya that's it

    Let me know your views for both the methodologies.
    I m sure many here are facing same issue
    If i come up with some concrete codes, I will surely distribute here for other newbies like me
  • pg1337
    1. Not necessarily previous day. It gives you data until the most recently completed candle even during the current day.
    2. Yes, you don't need Excel though. Keep it in memory for fast access, maybe backup to CSV at regular intervals. Excel is unnecessary overhead and I/O. Try using something like Redis if you need to share data among several processes, and multi threading.
  • AnkitDoshi
    Okay. So for 2nd point,
    You mean the OHLC tabular form that I will prepare, I let it be in memory rather in excel or anywhere else and run logic on it directly but than shouldn't I save it somewhere for next day and continue data from thereon?
    If possible can you take some time out and elaborate it step by step for me?



    I m just 2 months old in programming so some questions might look very naive to you.
  • AnkitDoshi
    import csv
    from kiteconnect import WebSocket
    api=' '
    public=' '
    userid=' '

    kws=WebSocket(api,public,userid)

    def on_tick(tick,ws):
    crudeoilm = open('crudeoilm.csv', 'w')
    crudeoilWriter = csv.writer(crudeoilm)
    crudeoilWriter.writerow([tick, '\n']):


    def on_connect(ws):
    ws.subscribe([53315591])
    ws.set_mode(ws.MODE_LTP,[53315591])

    kws.on_tick=on_tick
    kws.on_connect=on_connect

    kws.connect()




    What am I doing wrong?
    The above code generates a csv file but doesnt update new LTPs as and when it comes. It gets only one value, also when i reopen CSV it takes changes to current LTP.
  • pg1337
    crudeoilm = open('crudeoilm.csv', 'w')

    -You're opening the file in write mode, it will overwrite existing contents every time. Switch to append mode "a". Seems to be fine otherwise.

    You mean the OHLC tabular form that I will prepare, I let it be in memory rather in excel or anywhere else and run logic on it directly but than shouldn't I save it somewhere for next day and continue data from thereon?

    -You could save it, but don't need to, since you can always request the data from the API when needed anyway. You may want to store other metrics- like drawdown, order status etc, although that is also available in the API.
  • anandneeli
    AnkitDoshi, Were you able to get tick data and store it in csv and use pandas to read and get ohlc charts?
    I'm stuck with the pandas thing, Can you please give some pointers.
Sign In or Register to comment.