For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Flipping from long to short on the same bar



  • Hello all,

    I'm testing a strategy which should go long when ADX < 10 and short when ADX > 20.

    There are 4 scenarios

    1. Not in position and ADX < 10 -> Should go long

    2. In short position and ADX < 10 -> Should close short, and go long.

    3. Not in position and ADX > 20 -> Should go long

    4. In long position and ADX > 20 -> Should close long, and go short.

    Scenario 2 and 4 is where I have problems. I'll paraphrase my code for Scenario 2

    In Strategy Class next method I check whether ADX < 10 and whether I'm in a position. If I am, I use self.close(closeorder=True) to close my short.

    Now in Strategy class notify_order method, I check whether there is a closing order, and if so, I use self.buy() to long.

    Problem: The long order is processed after the next bar. So there is a gap of 1 bar (my close of short is for example at 18:00 and my following long is at 19:00 if I'm at the H1 timeframe). I want to close my order AND do my long at the same time. I can't do this in the next() method as I first want to make sure my close order went through before processing my long.

    Thanks!!

    Kind regards



  • Use both self.close and self.buy (or self.sell) in the next if you want your orders to be executed at the same bar. Also instead of regular orders you may want to use target orders - Docs - Target Orders.



  • @ab_trader Thank you for the quick response!

    The problem is that if I use the self.sell or self.buy order in the next() method after calling self.close(), I have no confirmation whether self.close() actually closed my position. For this theoretical backtest that is no problem, but I would like to also use live trading through backtrader once I have a profitable strategy, and there it does become a problem doesn't it?

    Thanks!



  • self.close is a market order and, unless you are trading really non-liquid shares, it should be no problem to execute it. If you still want to check that your order is executed, than yes, one bar should be spent for check, and order will be executed on the next bar. Or you need to move to smaller time frame.

    If you are trading on the daily bar, you may want to play with cheat-on-open feature.



  • @ab_trader i am in a dilema on this too. With the way backtrader is designed its not possible to do it right? Next() has to go to notify_order().

    I mean i can close my position and then jump straight to a new buy. But my cash available is less because the trade that was closed have not been 'processed'.



  • @J-T said in Flipping from long to short on the same bar:

    With the way backtrader is designed its not possible to do it right?

    Not sure what is not possible to do with bt. bt simulates real trading, I assume the way you want to operate is not possible in real trading working on bar by bar sequence.

    @J-T said in Flipping from long to short on the same bar:

    I mean i can close my position and then jump straight to a new buy. But my cash available is less because the trade that was closed have not been 'processed'.

    You are right, because you issue your orders at the close of the current bar, and they will be executed on the open of the next bar. bt (and regular people) don't know future price of the next open. You can send orders closing current position and orders opening new position at the same bar, but the execution of the latter will depend on the open price. Which is unknown at the time you send the orders.

    As I mentioned above - check cheat-on-open (Docs - Broker - Cheat-on-open), it seems it can help you to achieve what you want. :) or move to the smaller timeframe.


Log in to reply
 

});