Backtrader Community

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

    booboothefool

    @booboothefool

    3
    Reputation
    26
    Profile views
    22
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    booboothefool Unfollow Follow

    Best posts made by booboothefool

    • Bracket orders - am I supposed to only have one open at a time?

      In the documentation: https://www.backtrader.com/blog/posts/2017-04-01-bracket/bracket/

      There is code like:

      def next(self):
              if self.orefs:
                  return  # pending orders do nothing
      
      def notify_order(self, order):
              if order.status == order.Completed:
                  self.holdstart = len(self)
      
              if not order.alive() and order.ref in self.orefs:
                  self.orefs.remove(order.ref)
      
       self.orefs = [o.ref for o in os]
      
              else:  # in the market
                  if (len(self) - self.holdstart) >= self.p.hold:
                      pass  # do nothing in this case
      
      

      I am wondering what the point of all this is? It makes it look like it's not a good idea to have multiple brackets open at a time. I am indeed running into issues with duplicates/bracket orders hanging around. Can someone clarify if I should be using this template of "pending orders do nothing"?

      posted in General Code/Help
      B
      booboothefool
    • RE: strategy logging with trailing stop losses

      In notify_order, you can check if order.exectype is 5=StopTrail as opposed to whatever exectype your regular close is.

      posted in General Code/Help
      B
      booboothefool
    • RE: Group orders with transmit but with 2 orders instead of 3?

      Oh, forgot to mention. It works perfectly fine in backtesting but craps out live.

      posted in General Code/Help
      B
      booboothefool

    Latest posts made by booboothefool

    • RE: Issues with closing multiple positions

      @dasch Ok, I looked into this more.

      Say I have the open position:
      USD/CHF 2359 Sell

      But I am seeing logs like:

      NO DOWNTREND, CLOSE SELL => BUY
      2020-06-04: Order ref: 4 / Type Buy / Status Submitted / ExecType Market / Size 9736.0 / Alive True / Price 0.0 / Position -9736.0
      LIVE, 4, Data0, 0522, 06-04 01:05:00, O 0.961700, H 0.961730, L 0.961550, C 0.961560, ema_fast 0.961758, ema_mid 0.961704, ema_slow 0.961619, C[1] 0.961660, ema_anchor_fast 0.961527, ema_anchor_slow 0.961770, ema_anchor_baseline 0.964495, -9.999999999998899e-05 🔴, PnL -1.431505
      trend=0
      NO DOWNTREND, CLOSE SELL => BUY
      2020-06-04: Order ref: 5 / Type Buy / Status Submitted / ExecType Market / Size 9736.0 / Alive True / Price 0.0 / Position -9736.0
      LIVE, 4, Data0, 0523, 06-04 01:10:00, O 0.961520, H 0.961600, L 0.961500, C 0.961530, ema_fast 0.961708, ema_mid 0.961679, ema_slow 0.961611, C[1] 0.961660, ema_anchor_fast 0.961527, ema_anchor_slow 0.961770, ema_anchor_baseline 0.964495, -2.999999999997449e-05 🟢, PnL -1.139425
      trend=0
      NO DOWNTREND, CLOSE SELL => BUY
      2020-06-04: Order ref: 6 / Type Buy / Status Submitted / ExecType Market / Size 9736.0 / Alive True / Price 0.0 / Position -9736.0
      LIVE, 4, Data0, 0524, 06-04 01:15:00, O 0.961570, H 0.961720, L 0.961550, C 0.961720, ema_fast 0.961710, ema_mid 0.961685, ema_slow 0.961621, C[1] 0.961660, ema_anchor_fast 0.961527, ema_anchor_slow 0.961770, ema_anchor_baseline 0.964495, +0.00019000000000002348 🟢, PnL -2.989265
      trend=0
      NO DOWNTREND, CLOSE SELL => BUY
      2020-06-04: Order ref: 7 / Type Buy / Status Submitted / ExecType Market / Size 9736.0 / Alive True / Price 0.0 / Position -9736.0
      LIVE, 4, Data0, 0525, 06-04 01:20:00, O 0.961680, H 0.961720, L 0.961600, C 0.961700, ema_fast 0.961708, ema_mid 0.961687, ema_slow 0.961628, C[1] 0.961660, ema_anchor_fast 0.961527, ema_anchor_slow 0.961770, ema_anchor_baseline 0.964495, -2.0000000000020002e-05 🟢, PnL -2.794545
      trend=0
      NO DOWNTREND, CLOSE SELL => BUY
      2020-06-04: Order ref: 8 / Type Buy / Status Submitted / ExecType Market / Size 9736.0 / Alive True / Price 0.0 / Position -9736.0
      LIVE, 4, Data0, 0526, 06-04 01:25:00, O 0.961740, H 0.961750, L 0.961700, C 0.961750, ema_fast 0.961717, ema_mid 0.961696, ema_slow 0.961639, C[1] 0.961660, ema_anchor_fast 0.961527, ema_anchor_slow 0.961770, ema_anchor_baseline 0.964495, +4.999999999999449e-05 🟢, PnL -3.281345
      trend=0
      NO DOWNTREND, CLOSE SELL => BUY
      2020-06-04: Order ref: 9 / Type Buy / Status Submitted / ExecType Market / Size 9736.0 / Alive True / Price 0.0 / Position -9736.0
      LIVE, 4, Data0, 0527, 06-04 01:30:00, O 0.961700, H 0.961910, L 0.961670, C 0.961890, ema_fast 0.961756, ema_mid 0.961724, ema_slow 0.961662, C[1] 0.961660, ema_anchor_fast 0.961527, ema_anchor_slow 0.961770, ema_anchor_baseline 0.964495, +0.000140000000000029 🟢, PnL -4.644385
      trend=0
      NO DOWNTREND, CLOSE SELL => BUY
      

      This is wrong because backtrader thinks I have a position -9736.0, so it tries to buy 9736. In reality, I have position -2359, so it should be trying to buy 2359.

      In addition, all of these orders get canceled.

      USD/CHF	9 736	Buy	Market					0.96189			Cancelled		6043	
      USD/CHF	9 736	Buy	Market					0.96190			Cancelled		6057	
      USD/CHF	9 736	Buy	Market					0.96189			Cancelled		6063	
      USD/CHF	9 736	Buy	Market					0.96208			Cancelled		6073	
      USD/CHF	9 736	Buy	Market					0.96216			Cancelled		6085
      

      So it seems I am unable to properly close positions. Backtrader appears to be storing the wrong size and the close orders get canceled.

      posted in General Code/Help
      B
      booboothefool
    • RE: Issues with closing multiple positions

      Actually, this is strange.

      I think it is saying it was Long => 2936, and tried to Close => 0, but closed twice, so now it is Short => -2936.

      I do not mean to reverse the position, simply close once so it should be 0 and the position should be gone. I will add some more clear logs to see if it works as intended.

      posted in General Code/Help
      B
      booboothefool
    • RE: Issues with closing multiple positions

      @dasch Hello my friend! This is what I mean:

      TREND ENDED, CLOSE
      2020-06-03: Order ref: 3 / Type Sell / Status Submitted / ExecType Market / Size -2936.0 / Alive True / Price 0.0 / Position 2936.0
      LIVE, 4, Data0, 0536, 06-03 21:05:00, O 0.692110, H 0.692110, L 0.691570, C 0.691590, ema_fast 0.691988, ema_mid 0.692127, ema_slow 0.692318, C[1] 0.692110, ema_anchor_fast 0.692384, ema_anchor_slow 0.691245, ema_anchor_baseline 0.672130, -0.0005199999999999649 🔴, PnL -7.046400
      trend=0
      TREND ENDED, CLOSE
      2020-06-03: Order ref: 4 / Type Sell / Status Submitted / ExecType Market / Size -2936.0 / Alive True / Price 0.0 / Position 2936.0
      LIVE, 4, Data0, 0537, 06-03 21:10:00, O 0.691590, H 0.692010, L 0.691590, C 0.692010, ema_fast 0.691993, ema_mid 0.692110, ema_slow 0.692290, C[1] 0.692110, ema_anchor_fast 0.692384, ema_anchor_slow 0.691245, ema_anchor_baseline 0.672130, +0.00041999999999997595 🟢, PnL -5.813280
      trend=0
      TREND ENDED, CLOSE
      2020-06-03: Order ref: 5 / Type Sell / Status Submitted / ExecType Market / Size -2936.0 / Alive True / Price 0.0 / Position 2936.0
      LIVE, 4, Data0, 0538, 06-03 21:15:00, O 0.692060, H 0.692060, L 0.691100, C 0.691100, ema_fast 0.691795, ema_mid 0.691966, ema_slow 0.692182, C[1] 0.692110, ema_anchor_fast 0.692384, ema_anchor_slow 0.691245, ema_anchor_baseline 0.672130, -0.0009099999999999664 🔴, PnL -8.485040
      trend=0
      TREND ENDED, CLOSE
      

      As you can see, it tries many times to close the position. Market order is Submitted but not Accepted or Completed. So order stays and I lose money! :C

      Screenshot 2020-06-03 18.33.19.png

      posted in General Code/Help
      B
      booboothefool
    • Issues with closing multiple positions

      Hi, during live trading, I noticed if have multiple positions open such as:

      EUR/USD Buy 1
      EUR/USD Buy 2
      EUR/USD Buy 3
      

      and I call self.close() there are instances where either they just won't close, or it'll only close one of the positions, instead of the entire net position. My code involves adding many times to a position, so I guess multiple ones get formed that way.

      Has anyone else encountered this? If so, what is the remedy? I want to make sure positions get closed when they get the signal, else it gets bad when they go the other direction.

      I was also wondering if you open a position, then restart the program, does it have a way of knowing the net position that was entered before the program was started?

      posted in General Code/Help
      B
      booboothefool
    • RE: Group orders with transmit but with 2 orders instead of 3?

      Oh, forgot to mention. It works perfectly fine in backtesting but craps out live.

      posted in General Code/Help
      B
      booboothefool
    • Group orders with transmit but with 2 orders instead of 3?

      So this will work:

      self.E1 = self.buy(price=entry_price, size=size, exectype=bt.Order.Stop, transmit=False)
      self.SL1 = self.sell(price=stoploss_price, size=size, exectype=bt.Order.Stop, transmit=False, parent=self.E1)
      self.TP1 = self.sell(price=TP1_price, size=size, exectype=bt.Order.Limit, transmit=True, parent=self.E1)
      

      This will not:

      self.E1 = self.buy(price=entry_price, size=size, exectype=bt.Order.Stop, transmit=False)
      self.SL1 = self.sell(price=stoploss_price, size=size, exectype=bt.Order.Stop, transmit=True, parent=self.E1)
      

      It gives an error saying self.SL1 = self.sell(price=stoploss_price, size=size, exectype=bt.Order.Stop, transmit=True) doesn't work because it is unpacked into parent, stop so it's expecting like parent, stop = self.sell(price=stoploss_price, size=size, exectype=bt.Order.Stop, transmit=True, parent=self.E1) for some reason, which is not the case if you had 3 like TP1.

      But I just want to group the entry with a stop, without take profit. How should this be done?

      posted in General Code/Help
      B
      booboothefool
    • RE: Need clarification on cheating/replay/timers to buy at open and sell minutes before it closes

      Um, this may also be relevant: https://www.backtrader.com/docu/filters-reference/#barreplayer_open

      posted in General Code/Help
      B
      booboothefool
    • Need clarification on cheating/replay/timers to buy at open and sell minutes before it closes

      So on the 1H chart, I am just trying to buy when a candle opens and sell 5 minutes before it closes.

      I've read some docs:
      https://www.backtrader.com/blog/posts/2017-05-01-cheat-on-open/cheat-on-open/
      https://www.backtrader.com/docu/data-replay/data-replay/
      https://www.backtrader.com/docu/timers/timers/

      But I kinda need an ELI5 and clarification on what they do.

      To buy at the open, it sounds like cheat-on-open might be what I want. From what I understand, by default things operate on next() when candles close and indicators are recalculated, so it's "cheating" if you try to do something before the complete close of the current candle, so you only have the previous candle close to work with at the open of the current candle, and you don't know where that opening current candle will end up. Am I understanding this correctly?

      Also I'm not sure of the difference between resample and replay, but it sounds like 'replay' might be along the lines of what I want. I also saw that I can add timers. To sell 5 minutes before a candle closes, should I use notify_timer or replay?

      Thanks!

      posted in General Code/Help
      B
      booboothefool
    • RE: strategy logging with trailing stop losses

      In notify_order, you can check if order.exectype is 5=StopTrail as opposed to whatever exectype your regular close is.

      posted in General Code/Help
      B
      booboothefool
    • RE: Setting a Stop loss and target in a strategy (missing piece in a sample)

      @backtrader said in Setting a Stop loss and target in a strategy (missing piece in a sample):

      Such a sample already exists. It is ATR-Based

      See: https://www.backtrader.com/blog/posts/2016-07-30-macd-settings/macd-settings.html

      (Of course, it is included in the sources)

      Interesting. So if I understand correctly, in the example above, it's basically an ATR trailing stop, but it just uses a self.close() so it's kind of like an invisible stop loss, instead of updating a real stop loss by cancelling the old stop order and submitting a new stop order? Both ways will work? I wonder which is recommended?

      posted in Indicators/Strategies/Analyzers
      B
      booboothefool