Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. jennywestwong
    3. Posts
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    J
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 4
    • Best 0
    • Groups 0

    Posts made by jennywestwong

    • RE: Can somebody explain me how to use the heikin ashi indicator into a strategy

      @backtrader, @momentum @Simone with addfilter, the whole data source changes to heikin ashi values and the backtest produces fake results with non existing value. But what if I only want to use specific heiken ashi o/h/l/c and normal o/h/l/c values in my strategy to compute something?

      posted in General Code/Help
      J
      jennywestwong
    • RE: Stoploss Strategy of looking back X number of candles to see if it breached Y% relative to filled price.

      @run-out said in Stoploss Strategy of looking back X number of candles to see if it breached Y% relative to filled price.:

      self.smooth_rsi c

      Thank you for the reply @run-out . I am a python beginner and even my fundamentals are not in place. I have been trying hard to translate my TradingView strategy into python just for backtesting because TradingView's backtest is very limited.

      posted in General Code/Help
      J
      jennywestwong
    • RE: Stoploss Strategy of looking back X number of candles to see if it breached Y% relative to filled price.

      @backtrader, @ab_trader, @vladisld, @run-out, @RandyT I see that you guys are the most skilled and active members on this community and would love to hear if you guys have any idea if this is approachable.

      posted in General Code/Help
      J
      jennywestwong
    • Stoploss Strategy of looking back X number of candles to see if it breached Y% relative to filled price.

      Hi fellow backtraders, I am trying to write a stoploss reversal strategy where it can look at the last 5 candles' price, and check if in total, breached 0.5% relative to the executed price.

      However, I am getting weird result where it would buy and sell every candle. I am guessing most likely it is the def long_stoploss and def_short_stoploss that is not written correctly. If someone can please shine some light on my codes, it would be much appreciated. Thank you in advance.

      class RSI(bt.Strategy):
      
          def log(self, txt, dt=None):
              # Logging function for this strategy
              dt = dt or self.datas[0].datetime.datetime(0)
              print('%s, %s' % (dt.isoformat(), txt))
      
          def __init__(self):
              self.rsi = bt.indicators.RSI_Safe(self.data.close, period=6)
              
              self.crossover = bt.indicators.CrossUp(self.smooth_rsi, 30)
              self.crossunder = bt.indicators.CrossDown(self.smooth_rsi, 70)
      
          def notify_order(self, order):
              if order.status in [order.Completed]:
                  if order.isbuy():
                      self.log('BUY EXECUTED, Price: %.2f' % order.executed.price)
                      self.buyprice = order.executed.price
                  elif order.issell():
                      self.log('SELL EXECUTED, Price: %.2f' % order.executed.price)
                  self.bar_executed = len(self)
      
          def next(self):
              def long_stoploss():     #if high is 0.5% more than filled price for the past 5 candles
                  self.data.loc[self.data['high'] > self.order.executed.price * 1.005, 'long_stoploss'] = 1     
                  self.data.loc[self.data['high'] <= self.order.executed.price * 1.005, 'long_stoploss'] = 0
                  flipLong = sum(self.data.long_stoploss[-5:])     #look back last 5/5 candles breached 0.5%
      
              def short_stoploss():      #if low is 0.5% less than filled price for the past 5 candles
                  self.data.loc[self.data['low'] < self.order.executed.price * 1.005, 'short_stoploss'] = 1
                  self.data.loc[self.data['low'] >= self.order.executed.price * 1.005, 'short_stoploss'] = 0
                  flipshort = sum(self.data.short_stoploss[-5:])     #look back last 5/5 candles breached 0.5%
      
              if not self.position:
                  if self.crossover:    
                      self.buy()                
                  if self.crossunder:
                      self.sell()
      
              if self.position.size!=0:
                  if long_stoploss:
                      self.sell(size=2)     #sell 2 to reverse
                  if short_stoploss:
                      self.buy(size=2)     #buy 2 to reverse
                                             
      

      Results (only a small portion):

      2020-01-02T23:15:00, SELL EXECUTED, Price: 3263.00
      2020-01-02T23:15:00, SELL EXECUTED, Price: 3263.00
      2020-01-02T23:20:00, BUY EXECUTED, Price: 3262.50
      2020-01-02T23:20:00, BUY EXECUTED, Price: 3262.50
      2020-01-02T23:25:00, SELL EXECUTED, Price: 3262.50
      2020-01-02T23:25:00, SELL EXECUTED, Price: 3262.50
      2020-01-02T23:30:00, BUY EXECUTED, Price: 3262.25
      2020-01-02T23:30:00, BUY EXECUTED, Price: 3262.25
      2020-01-02T23:35:00, SELL EXECUTED, Price: 3262.25
      2020-01-02T23:35:00, SELL EXECUTED, Price: 3262.25
      2020-01-02T23:40:00, BUY EXECUTED, Price: 3262.00
      2020-01-02T23:40:00, BUY EXECUTED, Price: 3262.00
      2020-01-02T23:45:00, SELL EXECUTED, Price: 3261.75
      2020-01-02T23:45:00, SELL EXECUTED, Price: 3261.75
      2020-01-02T23:50:00, BUY EXECUTED, Price: 3261.75
      2020-01-02T23:50:00, BUY EXECUTED, Price: 3261.75
      2020-01-02T23:55:00, SELL EXECUTED, Price: 3261.25
      2020-01-02T23:55:00, SELL EXECUTED, Price: 3261.25
      2020-01-03T00:00:00, BUY EXECUTED, Price: 3261.00
      2020-01-03T00:00:00, BUY EXECUTED, Price: 3261.00
      

      Figure_0.png

      posted in General Code/Help
      J
      jennywestwong
    • 1 / 1