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/

    Avoid optstrategy redundant calculation

    General Code/Help
    optstrategy redundant
    1
    1
    427
    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.
    • H
      Heichang Lee last edited by

      How can I calculate self.ma2 and self.rsi first (and only once) and feed them to TestStrategy?
      I do not want TestStrategy to recalculate self.ma2 and self.rsi during optstrategy iteration. They remain same.

      Code:

      class TestStrategy(bt.Strategy):
      
      def __init__(self):
      self.ma1 = bt.talib.SMA(self.datas[0].close, timeperiod=int(self.p.maShort_period))  
      self.ma2 = bt.talib.SMA(self.datas[0].close, timeperiod=int(self.p.maLong_period))
      self.rsi = bt.talib.RSI(self.datas[0].close, timeperiod=int(self.p.rsi_period))
      
      cerebro.optstrategy(
          TestStrategy, maShort_period=range(15, 25)
      
      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors