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/

    Newbie error in both RSI and MACD

    General Code/Help
    2
    3
    134
    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.
    • Vandit D Jain
      Vandit D Jain last edited by

      Both my strategies work fine when going long individually. However they don't work together. Can someone please help?

      Here is my MACD code:

      def next(self):
                  if self.position.size== 0:
                      if ((self.crossover >0)& (self.macd.macd>0) & (self.macd.signal>0)) :
                          amount_to_invest=self.params.order_percentage*self.broker.cash
                          self.size=math.floor(amount_to_invest/self.data.close)
                          
                          print("BUY CREATED at {}".format(self.data.close[0]))
                          self.order= self.buy(size= self.size) 
                      elif ((self.crossover <0)& (self.macd.macd<0) & (self.macd.signal<0)) :
                          amount_to_invest=self.params.order_percentage*self.broker.cash
                          self.size=math.floor(amount_to_invest/self.data.close)
                          
                          print("SELL at {}".format(self.data.close[0]))
                          self.order= self.sell(size= self.size)
                  elif (self.position.size >0):
                      if ((self.crossover <0)&(self.order.isbuy())):
                          print("close at {}".format(self.data.close[0])) 
                          self.close()
                      elif ((self.crossover>0) & (self.order.issell())):
                          print("close at {}".format(self.data.close[0])) 
                          self.close()
      

      Here is my RSI code:

      def next(self):
                  if self.position.size== 0:
                      if self.rsi < self.params.lowerbound:
                          amount_to_invest=self.params.order_percentage*self.broker.cash
                          self.size=math.floor(amount_to_invest/self.data.close)
                              
                          print("BUY at {}".format(self.data.close[0]))
                          self.buy(size= self.size)
                      elif self.rsi> self.params.upbound:
                          amount_to_invest=self.params.order_percentage*self.broker.cash
                          self.size=math.floor(amount_to_invest/self.data.close)
                              
                          print("SELL Created at {}".format(self.data.close[0]))
                          self.sell(size= self.size)
                  
                  if self.position.size > 0:
                      if (self.rsi < self.params.upbound) or ( self.rsi> self.params.lowerbound):
                           print("Sell at {}".format(self.data.close[0])) 
                           self.close()'''
      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        Can you include the combined code that didn't work and your error messaage?

        RunBacktest.com

        Vandit D Jain 1 Reply Last reply Reply Quote 0
        • Vandit D Jain
          Vandit D Jain @run-out last edited by

          @run-out I mean there is no error message. My algorithm seems to always underperform in MACD.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors