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/

    Correct way to use nested If statements on a SignalStrategy

    Indicators/Strategies/Analyzers
    1
    2
    146
    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.
    • hfrog713
      hfrog713 last edited by

      Hello Guys,

      Fairly new, trying to figure out the correct way to do nested if statements.
      I do the following but it seems like the function itself is showing up as opposed to the evaluated If return.
      Can someone please help?

      Thanks,
      Hernando

      class SMACloseSignalStrategy(bt.SignalStrategy):
      params = (('fastMA',22),('slowMA', 23),('macd1',7),('macd2',8),('macdsig',3),('period',30),)

      def log(self, txt, dt=None):
          '''Logging function for the strategy'''
          dt = dt or self.datas[0].datetime.date(0)
          print('%s, %s' % (dt.isoformat(), txt))
          
      
      def __init__(self):
          self.high = self.datas[0].high
          self.low = self.datas[0].low
          self.close = self.datas[0].close
          
          self.ema_slow = self.datas[0].ema_slow
          self.ema_fast = self.datas[0].ema_fast
          self.macd_delta = self.datas[0].macd_delta
          self.high_low2 = self.datas[0].high_low2
          
          close_over_emaS = self.close > self.ema_slow
          delta_over_zero = self.macd_delta > 0
          hl2_over_emaF = self.high_low2 > self.ema_fast
          
          close_below_emaS = self.close < self.ema_slow
          delta_below_zero = self.macd_delta < 0
          hl2_below_emaF = self.high_low2 < self.ema_slow
          
          
          delta_and_hl2_up = bt.And(delta_over_zero, hl2_over_emaF)
          delta_and_hl2_down = bt.And(delta_below_zero, hl2_below_emaF)
          
          delta_over_flag = bt.If(delta_over_zero, 1, 0)
          hl2_over_flag = bt.If(delta_and_hl2_up, 1, 0)
          
          delta_under_flag = bt.If(delta_below_zero, 1, 0)
          hl2_under_flag = bt.If(delta_and_hl2_down, 1, 0)
          
          #long_signal = bt.If(close_over_emaS, bt.If(delta_over_zero, 1, 0), bt.If(delta_and_hl2_up, 1, 0))
          #short_signal = bt.If(close_below_emaS, bt.If(delta_below_zero, 1, 0), bt.If(delta_and_hl2_down, 1, 0))
          
          long_signal = bt.If(close_over_emaS, delta_over_flag, hl2_over_flag)
          short_signal = bt.If(close_below_emaS, delta_under_flag, hl2_under_flag)
          
          #Another working example with 2 mil profit lol ....
          #long_signal = bt.If(close_over_emaS, 1, 0)
          #short_signal = bt.If(close_below_emaS, -1, 0)
          breakpoint()
          #Selling on SMA strategy......seems pretty profitable
          sma1, sma2 = bt.ind.SMA(period=self.p.fastMA), bt.ind.SMA(period=self.p.slowMA)        
          #self.signal_add(bt.SIGNAL_LONG, bt.ind.CrossOver(sma1,sma2))
          #self.signal_add(bt.SIGNAL_SHORT, bt.ind.CrossOver(sma2,sma1))
          self.signal_add(bt.SIGNAL_LONG, long_signal)
          self.signal_add(bt.SIGNAL_SHORT, short_signal)
      
      hfrog713 1 Reply Last reply Reply Quote 0
      • hfrog713
        hfrog713 @hfrog713 last edited by

        @hfrog713 said in Correct way to use nested If statements on a SignalStrategy:

        delta_under_flag = bt.If(delta_below_zero, 1, 0)
        hl2_under_flag = bt.If(delta_and_hl2_down, 1, 0)
        

        Seems to be working fine, i accidentally had 1 on both the buy and the sell signal.
        Now everything seems to be working well.
        Please let me know if there are any comments.

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