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

Bracket Order - Limit and Stop Orders to close on exact prices specified



  • Hello

    How can I get backtrader to close the limit and stop orders in a bracket order at the exact prices specified in the limit and stop order? I understand that backtrader calculates a price to close at to mimic real trading, but I specifically need to close at the exact prices specified.

    Would appreciate any input on this. Thanks!


  • administrators

    This may seem like a very dumb question:

    • If you already know the execution price, what do you need issuing orders for?


  • Maybe my question wasn't clear, here's an example, I'm assuming a risk-reward ratio of 1:1 while risking a maximum of 0.25% per trade, therefore on a starting portfolio value of $1000000 I'm expecting either $2500 profit if the take profit was hit or a -$2500 loss if my stop loss was hit, but given a bracket order the Limit/Stop orders don't close at the exact prices specified in the Limit/Stop Order.. this might clarify it further:

    INFO: Starting Portfolio Value: 1000000.00000
    INFO: LONG PREDICTION [1] PROBABILITY [53.91] @ 2017-11-27 14:30:00
    INFO: LONG CREATE, 1.33688
    INFO: Position size = 1041666.67
    INFO: BUY EXECUTED, Price: 1.33688, Cost: 1392583.33333, Comm 139.25833 @ 2017-11-27 14:31:00
    INFO: SL HIT @ Price: 1.33448, Cost: 1392583.33333, Comm 139.00833 @ 2017-11-27 16:22:00
    INFO: Order #159 Canceled
    INFO: OPERATION PROFIT, GROSS -2500.00000, NET -2778.26667

    INFO: LONG PREDICTION [1] PROBABILITY [77.96] @ 2017-11-28 14:00:00
    INFO: LONG CREATE, 1.32718
    INFO: Position size = 1038772.64
    INFO: BUY EXECUTED, Price: 1.32718, Cost: 1378638.27088, Comm 137.86383 @ 2017-11-28 14:01:00
    INFO: SL HIT @ Price: 1.32464, Cost: 1378638.27088, Comm 137.59998 @ 2017-11-28 16:50:00
    INFO: Order #162 Canceled
    INFO: OPERATION PROFIT, GROSS -2638.48250, NET -2913.94631

    INFO: LONG PREDICTION [1] PROBABILITY [50.09] @ 2017-11-28 19:00:00
    INFO: LONG CREATE, 1.33275
    INFO: Position size = 1035737.28
    INFO: BUY EXECUTED, Price: 1.33275, Cost: 1380378.85746, Comm 138.03789 @ 2017-11-28 19:01:00
    INFO: TP HIT @ Price: 1.33632, Cost: 1380378.85746, Comm 138.40764 @ 2017-11-28 19:13:00
    INFO: Order #164 Canceled
    INFO: OPERATION PROFIT, GROSS 3697.58208, NET 3421.13655

    Notice how the first trade lost $2500 which is correct, the second trade lost $2638 which is incorrect and the last trade won $3697 which is again incorrect.

    How can I tell backtrader to close the orders at the exact prices specified so that, the %0.25 risk per trade is always achieved consistently? (as mentioned earlier, I'm aware that achieving the exact execution price in backtesting does not mimic real world trading)

    Thanks for your support


  • administrators

    @z03 said in Bracket Order - Limit and Stop Orders to close on exact prices specified:

    the second trade lost $2638 which is incorrect and the last trade won $3697 which is again incorrect.

    Sorry but the statements are incorrect.

    A Stop order behaves like a Market order once the trigger is activated. The trade has lost $2638, because once the trigger was active, the 1st price for the match (Market behavior) was some ticks away from the trigger point.

    The 2nd trade wins $3697 because the 1st price at which the order can be matched above the Limit is aways from the actual limit set in the order. This happens when a gap takes place.

    How can I tell backtrader to close the orders at the exact prices specified

    You cannot.

    What you can do is work with a smaller timeframe and the profit and losses will magically converge around $2500 as you are expecting.



  • @backtrader - thanks for clarifying, the backtest is running on a 1 minute chart, I can confirm that gaps don't exist in the data as it is cleaned up before being used in the backtest. If I understood you correctly, I can guarantee exact prices (execution at trigger points) only if I start backtesting on tick data? How is the price matched in backtrader after the trigger is hit? Thanks a lot for your support and for your efforts in making backtrader awesome!


  • administrators

    @z03 said in Bracket Order - Limit and Stop Orders to close on exact prices specified:

    How is the price matched in backtrader after the trigger is hit?

    By looking at the OHLC values in the trigger bar first to infer if the execution price could be in the bar. In the case of a pure Stop order which then works as a Market order it is alway possible. If not, the prices in the next bar will be used with regular matching.

    A gap is meant from closing to opening price, even if both bars are in the same range.



  • Thanks a lot for your input


Log in to reply
 

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