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/

    What's the right way to use data from API in a strategy?

    General Code/Help
    2
    2
    518
    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.
    • B
      brettelliot last edited by

      Hi everyone,

      Thanks again for the great platform! Having a lot of fun working with it.

      I've built an indicator that reads a csv file of events and my backtest can use those events to make trades. But for live trading, I'd like to hit a JSON api for the event data.

      I could call it in the strategy's next() method. Or I can do that in the indicator and use the indicator in the strategy. Or maybe I should make a feed (cause that's the only place i've seen live connections being used).

      What's the preferred way to do this?

      Thanks!

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

        @brettelliot said in What's the right way to use data from API in a strategy?:

        I could call it in the strategy's next() method

        That would probably be fatal. But it depends on the speed at which you have to make decisions. You'd be blocking the main thread.

        @brettelliot said in What's the right way to use data from API in a strategy?:

        What's the preferred way to do this?

        There is no preferred way. My personal opinion:

        • It is a light processing stream of events: put it in a background thread and put the incoming events in a queue.Queue, which is thread safe. When entering next, you can check if an event is waiting in the queue (pushed by the background thread) or not and do something else.

          Even with the GIL and because processing is light, using threads seems sensible.

        • The stream of events needs some serious processing before being passed to next.

          Use multiprocessing to really use multiple cores and avoid blocking the main thread. The communication channels to pass things back to the main thread have the same (or very similar) APIs, although they are a bit slower because data has to be copied from process to process (this time should be marginal compared with the actual processing time to make it worthwhile)

        • Using a data feed

          It's also a sensible opinion. You have in this case to provide a timestamp in the datetime line. You may simply use it as a timeframe Ticks data feed, without resampling/replaying to get every event. Remember that your real data feeds (which will probably have larger timeframes) will be available, but in most cases won't move forward (the len remains the same) when your event is seen in next

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