Order Submission & Execution



  • I am trying to use the inbuilt broker for simulation and I have noticed something strange...

    When I go all in with an order, the order is failing with Insufficient Cash/Margin error.

    First I thought that this error is because I am using today's close price after accounting for commission and slippage when calculating my order size to go all in and because the order is executed on next bar open, the price difference is causing this Margin Call.

    So I used the next open price after accounting for commission and slippage to calculate the order size but it is still failing. This baffled me. I wanted to know which price is being used to reject orders.

    After some digging and debugging, it looks like the inbuilt broker is rejecting the order based on current bar's close price during order creation and also rejecting based on next bar open during execution.

    If this is true, then we are doing something wrong. A real broker will only reject according to the execution price i.e. next open.


  • administrators

    The logic is unclear and with no code sample what you try to really achieve is still unclear.

    @Praveen-Baratam said in Order Submission & Execution:

    After some digging and debugging, it looks like the inbuilt broker is rejecting the order based on current bar's close price during order creation and also rejecting based on next bar open during execution.

    Wrong statement. Orders are never checked when submitted (your creation), they are first queued and then checked during the next cycle to be accepted. There may be something wrong in the platform, but fore sure not what you state.

    Furthermore: the broker will for sure only reject and order once and not twice

    @Praveen-Baratam said in Order Submission & Execution:

    If this is true, then we are doing something wrong. A real broker will only reject according to the execution price i.e. next open.

    Going all-in is for sure wrong. You may also read the docs and disable submission check if you wish. Your orders are quite probably still bound to fail due to insufficient funds for going all in and the price difference. It has happened before to many others.

    You may also use cheat-on-open. People enjoy cheating themselves, hence cheat-on-close and cheat-on-open. Any real backtesting is killed by using those, but feel free to use them.

    Really: some code to at least start explaining your case would be welcome. If there is problem you can for sure reproduce it with a simple strategy issuing a signal with a moving average crossover.



  • @backtrader - As always Thank you for the prompt and detailed response and apologies for not posting code. There was no simple way to replicate the error without constructing another dummy strategy that also raises this error. I will try to do that next time.

    For this issue however, it looks like I found out what went wrong. Broker does check for cash requirements twice by default - once when submitted and once when executed.

    And it is very wise of you that you provided a switch/parameter to turn off check on submit checksubmit in Broker. Once I set it to False everything worked as intended.

    Coming to cheat-on-open I am using similar mechanism to know the open price to go all in and I understand the pitfalls in this approach. Any look-forward can lead to misleading results from backtests but I am only using to calculate my position size.

    The future contracts I trade need a percentage margin based on today's close even if the order is executed on next open and the difference is adjusted in Mark to Market on next close. But in backtrader if I calculate order size based on today's close, some orders will be rejected if the next open is higher.

    I really could not figure out a way to simulate the above scenario on backtrader.


  • administrators

    @Praveen-Baratam said in Order Submission & Execution:

    Broker does check for cash requirements twice by default - once when submitted and once when executed

    Real brokers do it too.

    • Or would you expect to submit your order, take your money from the broker and still get your order executed?

    @Praveen-Baratam said in Order Submission & Execution:

    but I am only using to calculate my position size.

    You are using it to cheat yourself. Formulating it in a political correct manner and with only doesn't change the fact.

    @Praveen-Baratam said in Order Submission & Execution:

    The future contracts I trade need a percentage margin based on today's close even if the order is executed on next open and the difference is adjusted in Mark to Market on next close. But if I calculate order size based on today's close, some orders will be rejected if the next open is higher.

    They are rejected because it reflects reality. You calculate a size to go all-in and the next price (the open) is far away enough to break the calculation and deplete your cash resources.



  • @backtrader - The exchange and subsequently my broker decide the margin requirements based on today's close using a complex math based on volatility and all and this margin can be loosely approximated to a percentage of order value.

    So there is no chance of my all-in order based on current margin requirements getting rejected based on tomorrow's open price. The margin required per lot will only change on next close. Until then it is good i.e. good for that whole day.

    If this can be achieved on backtrader i.e. there should be an additional way to prevent check on execute with a checkexecute parameter similar to checksubmit then I can more realistically simulate my transaction without looking ahead.

    The other option is to leave some buffer cash with my broker and calculate the order size to prevent margin calls which is inefficient because I am not putting all the capital I have to use even when I can.


  • administrators

    This has nothing to do with order execution even if you believe it is your problem. It has to do with margin calculation.

    Define your own commission scheme and your own margin calculation.

    There are additional chapters, including how to extend. One of the things you for sure will need is to override get_margin and return the value that your calculation believes it's right.


Log in to reply
 

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