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/

    Possible to pass and return a dataframe to a strategy?

    Indicators/Strategies/Analyzers
    3
    5
    334
    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.
    • M
      mapesd last edited by

      Is it possible to pass and return a dataframe to a strategy?

      Got started with this post.

      Here is a code snippet of the strategy parameters.

      class TestStrategy(bt.Strategy):
          params = (
              ('results', None),       # This is the dataframe
              ('fastmaperiod', 10),
              ('midmaperiod', 20),
              ('slowmaperiod', 50),
              ('atrperiod', 15),       # ATR Period (standard)
              ('atrdist', 3.0),        # ATR distance for the strike price
      
      cerebro.addstrategy(TestStrategy, results = results_df)
      

      I want to return the results dataframe after the strategy has completed.

      Thanks,
      Dave

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

        Cerebro returns the strategy instances that have run.

        The definition of what's returned is in the Cerebro reference: https://www.backtrader.com/docu/cerebro/, section Returning the Results

        You may pass a dataframe and later get it from the executed strategy.

        1 Reply Last reply Reply Quote 1
        • M
          mapesd last edited by

          Can you give me a hint how to get the dataframe from the executed strategy? May be obvious to a python expert but is not obvious to me. A simple example is usually good enough.

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

            Executed strategy instances are returned. See

            • https://www.backtrader.com/docu/cerebro/#returning-the-results

            And each strategy has its parameters available as instance.params.parameter_name

            1 Reply Last reply Reply Quote 0
            • Tejas Krishna Reddy
              Tejas Krishna Reddy @mapesd last edited by

              @mapesd

              Hey,

              Just to provide an example,

              
              strategy1 = cerebro.run()
              st = strategy1[0]
              
              # To return something back from the strategy - 
              df = st.params.results      # Will return your df back.
              st.params.midmaperiod   # will return back 20 and so on. 
              
              
              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(); }); }