Exit on the same bar of entry problem



  • Hi:

    Let's say that I want to entry a long trade and set the profit target order at 10 pips higher than the entry price.
    How to make sure that the trade exit at the same bar if the target price is hit.
    My code is like:

    def next(self):
        self.buy(size=1, exectype=bt.Order.Market)
    
    def notify_order(self, order):
        target = ...
        self.targetorder = self.sell(size=self.position.size, price=target, exectype=bt.Order.Limit)
    

    However, the problem is that the target order only be sent at the next bar but not within this bar.

    Is there a way to solve the problem.

    Thanks



  • @asuralm
    issue both orders in the next. They will both be sent to system at the next bar open. And target order will be executed if price will go up enough to hit it.



  • @ab_trader

    There is a problem if I send both orders in the next.

    1. the actual fit price is not known in next. For example, I want to set a 30 point target price, if the close price of this bar is 1000 and it issues a long signal. The open price next bar is 1100, then the real entry price is 1100 and target price should be set to 1130. But in next I can only do it 1030.

    2. if I am not using market price but stop/limit price to enter a trade, same problem as 1.



  • @asuralm
    You can use cheat-on-open feature for market orders, get one more call of next and send target order at that time.

    For stop/limit entries try bracket orders.

    Or you can go to lower timeframe.


  • administrators

    @asuralm said in Exit on the same bar of entry problem:

    However, the problem is that the target order only be sent at the next bar but not within this bar.

    This is not a problem and the order is not sent with the next bar. It is sent when issued, it is only considered for execution when the new prices are available.

    Because the "current bar" is already closed (you cannot operate on its prices without cheating)

    @asuralm said in Exit on the same bar of entry problem:

    @ab_trader
    There is a problem if I send both orders in the next.

    the actual fit price is not known in next.

    That's again not a problem. It's how trading works. You cannot operate on the price you currently see (you can only expect to get it in the future, but you may not) and you don't know what the next incoming prices will be.

    @ab_trader said in Exit on the same bar of entry problem:

    You can use cheat-on-open feature

    As suggested by @ab_trader you can use cheat-on-open to have a chance to operate on incoming prices, but that defeats the purpose of backtesting and getting a profitable entry/exit approach, because you are examining prices before executing actions, what you actually cannot do in real life.

    See: Blog - Cheat On Open


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.