Backtrader Community

    • 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/

    Using bt.Strategy class for both algorithm deployment and backtesting

    General Code/Help
    3
    4
    392
    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.
    • C
      clxyder last edited by

      Hello everyone I am new the community! I had a general question about the use of backtrader framework. I would ideally like to have a single strategy class for each of my strategies where I can both backtest and then with the needed modifications use the same class to deploy my strategy with my broker i.e., Alpaca. But whenever I initialize MyStrategy class it calls on the __call__ method from MetaBase class and it fails. I am assuming that is because I am initializing the class without Cerebro, and thus is has no owner. Does anyone have an idea if there is simple way to achieve this? My current solution is to just overload the function calls, but I want to do this as elegant as possible. Thank you very much!

      vladisld 1 Reply Last reply Reply Quote 2
      • vladisld
        vladisld @clxyder last edited by

        @clxyder Are you going to deploy the strategy to Alpaca outside of the Backtrader framework? How do you envision this will work technically?

        C 1 Reply Last reply Reply Quote 1
        • C
          clxyder @vladisld last edited by

          Hey @vladisld thanks for the quick response. I envision breaking up my strategy into key functions that can be called as needed in next() for the backtrader framework and then another function say deploy() that would run through every thing needed to send my orders out with Alpaca. I was thinking something along the lines of:

          strat = MyStrategy(deployment=True)
          strat.deploy() 
          

          to deploy with Alpaca. Where setting deployment = True would disable the backtesting functionality. And it would by default be set to False so I can pass the MyStrategy to Cerebro without a problem.

          The main reason I ask this is because I figured if I am already backtesting the strategy can't I just augment the class a little and make it work with Alpaca.

          But if this seem a round about way of approaching I can just make two separate classes.

          1 Reply Last reply Reply Quote 1
          • A
            ab_trader last edited by

            You may want to check live trading sections in bt docs. I am not sure why do you want to get strategy from cerebro. Replacing backtest broker with the live broker will do the live trading with the same strategy class. And I believe alpaca broker is already implemented for bt. Search the forum.

            • If my answer helped, hit reputation up arrow at lower right corner of the post.
            • Python Debugging With Pdb
            • New to python and bt - check this out
            1 Reply Last reply Reply Quote 3
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors