asynchronous order processing? (futures with PRT+IBRK)

Viewing 9 posts - 16 through 24 (of 24 total)
  • Author
    Posts
  • #266586 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    You may use

    defparam flatafter = hhmmss
    

    provided that at hhmmss there’s a candlestick (of the default timeframe) opening/closing.

    So

    defparam flatafter = 200000
    

    will be ignored on a Daily or 4-hour timeframe, as there is no Daily, nor 4-hour candle opening at 200000.




    #266588 quote
    justisanjustisan
    Participant
    Senior

    guys, thanks for kidding me… I admit that I for very long time did not use set stop ploss xx / set target pprofit yy any more, so I removed almost all capital from one account, left only 2080EUR – enough to meet margin for one dax micro lot, but not for two lots, and ran the test live with this code, according you suggestion using set stop ploss 50 / set target pprofit 50:


    if not onmarket[0] and not onmarket[1] then
    buy 1 lot at market
    set stop ploss 50
    set target pprofit 50
    endif
    
    
    if onmarket then
    sell at market
    endif
    

    and the result is same, the pending takeprofit limit oder triggers margin request, asking for approx 3000EUR on account (which is for two micro lots), and it gets rejected and deactivates the algo. it cost me 3EUR to test this live, but ok, I can afford it 😀

    limit-order-triggering-margin-request.png limit-order-triggering-margin-request.png
    #266589 quote
    justisanjustisan
    Participant
    Senior


    robertogozzi wrote: You may use defparam flatafter = hhmmss Copy provided that at hhmmss there’s a candlestick (of the default timeframe) opening/closing. So defparam flatafter = 200000 Copy will be ignored on a Daily or 4-hour timeframe, as there is no Daily, nor 4-hour candle opening at 200000.

    as you see in my initial post, I used it (using it), it does not make any difference…

    #266590 quote
    GraHalGraHal
    Participant
    Legend


    Yes, this is normal behaviour for Interactive Brokers (IBKR) when executing automated strategies via third-party platforms like ProRealTime (PRT).

    While it seems illogical that a take-profit order (which is meant to close a position) requires extra margin, it happens due to how IBKR’s risk engine evaluates independent orders and how ProRealTime transmits them.


    Why IBKR Requests Extra Margin

    • Independent Order Evaluation. Unlike some retail brokers that automatically treat a limit order in the opposite direction as a “reduce-only” close, IBKR evaluates standalone orders as potential new opening positions. If a SELL LIMIT order is submitted independently rather than strictly linked as a child order inside an IBKR Bracket Order, IBKR’s margin engine calculates the worst-case scenario—that you might end up holding a new short position.
    • The “Double Margin” Trap (Race Condition). Look closely at the second block of the code:
    • prorealtime
    if onmarket then
    sell at market
    endif
    
    • Use code with caution.

    • When the position opens, PRT has a pending SELL LIMIT (take profit) sitting on IBKR’s order book. On the very next candle, if onmarket becomes true and triggers sell at market. For a split second before IBKR cancels the pending take-profit limit order, IBKR sees two active sell orders against one long position (1 market sell + 1 limit sell = 2 lots total).
    • Margin Rejection. Because IBKR sees potential exposure for 2 micro lots (1 to close the long, plus 1 leftover short), it immediately checks if the account has enough Available Funds to cover the margin for two contracts (roughly €3,000). When the account lacks that buffer, IBKR rejects the order, and ProRealTime automatically halts the algorithm as a safety precaution.



    How to Prevent This in ProRealTime

    To avoid triggering IBKR’s margin checks on exit orders, PRT users typically adjust how the code handles exits:

    • Avoid Combining Market Exits with Pending Limits. If you use set target pprofit, let the broker’s server-side bracket handle the exit rather than firing a separate sell at market command while the limit order is still active.
    • Cancel Pending Orders Before Exiting. If you must exit at market while a stop or target is active, structure the code so that the stop/target is removed or reset to 0 on the bar before sending a market exit, ensuring IBKR never sees two simultaneous sell orders.
    • Maintain a Margin Buffer. When running automated strategies on IBKR (especially on futures or leveraged CFDs/FX), keeping excess liquidity above the single-contract initial margin prevents momentary order overlaps from halting the system.









    #266592 quote
    justisanjustisan
    Participant
    Senior

    …thank you ai for the bla-bla, but no solution 😀

    #266593 quote
    justisanjustisan
    Participant
    Senior

    you know grahal, or rather you don’t while “trading” on demo-accounts only, that to keep enough capital for more than 2x margin requirement is pure luxury? especially considering that IBRK requires approx 100% of standard exchange margin… so if you trade 10 lots of standard dax contract, you need approx 350k+ EUR permanently on the account (and 500k+ if you keep positions overnight), and double means 700k EUR (or 1000k+ if keeping positions overnight). this is just for one strategy. if you have another one on mini dax and another one on micro it escalates, and if you have severeal account between which you have to allocate available capital efficiently it gets terrible, you have to keep on accounts tons of money for nothing… just because PRT/IBRK does not process your orders in the right/logical sequence…

    #266594 quote
    GraHalGraHal
    Participant
    Legend


    justisan wrote: while “trading” on demo-accounts only

    My Algos are mostly running on Demo Account, but all my manual trading (and lots of!) is on Real Live Account.


    Re your comments above re IBKR … seems to me that any advantage IBKR has re less spread (than IG) and any other advantage is negated by the fact that you would always be trading micro / mini lots and therefore never make any big money?

    #266595 quote
    GraHalGraHal
    Participant
    Legend


    justisan wrote: but no solution

    Just trying to get to the source of the frustrations you are experiencing.

    No solution, but least you now know that all is running correctly as it should do and the oddities you experience are down to IBKR rules and procedures?

    #266597 quote
    PeterStPeterSt
    Participant
    Master

    No. And it is more complicated than you’d expect. Plus : This is one of my outstanding 2000+ tickets (how come I always need to mention this ?). I think back in 2019 I already reported this from manual trading. This is important, because it more easily shows what is going on.

    Side note : it will be nothing new to justisan, but it could help him to further decide (as in : plainly give up on this one).


    1.

    If you have a manual position and want to revert that, the exact same situation occurs.

    Side note : I have reported about this in this forum as well (several times) and I recall that in IG you can’t even perform the “reversal” action. Within the IB version of PRT the order types are subject to change and currently I don’t even know whether this “revert” is still possible.

    No worries : you can perform a Sell 2 on an open 1x Long and it is exactly the same (but feels as if you need 2 extra positions for margin).


    2.

    It gets complicated as PRT is in between that as well, actively (!). For example, PRT may explicitly ask for the margin headroom prior to putting an order to the broker. Or it may not (a sloppy programmer forgot it in the situation of concern) and then the broker brings the message to PRT, PRT passing it on to you. Note that this gets exciting when you earn a lot in your current session and with say 10K portfolio but 100K profit (this exists) you can’t do a thing, never mind you’d need 5% of the total only for your next order. This is (GraHal) indeed IBKR’s risk calculation, which *also* adds some just in case (your AI forgot to dig that out). Example : you have 1 spare in margin, want to buy 1, but it is rejected because you need 2 (you can sort out the remainder with AI because it is good at this, but first ask for it (guide it)). Already this phenomenon makes that you lose track (no logic). Anyway PRT does *not* contain this logic – it is the broker who rules here (and never make the mistake to compare this with IG because IG is a joke).


    3.

    #2 has to be extended with the PRT specialties. So, exactly in the realm of justisan’s issues, I once was allowed (by PRT !) to decide how PRT should act when I’d put a limit buy order under an already open position. But this quite impossible to explain. An attempt :

    You have a position of 10 lots and it makes a profit of 100. I now put a limit under that position with 1 lot. Now the profit shows 101. Huh ? yes. huh. It is now PRT who decides that the 100 will be 101 once the limit order is filled. It is even logic, once you see that the price drops towards the Limit and ends up at 90 once it is there, and your limit of 1 lot it filled; the profit will now be ~ 91 (it will be 90 but if the price rises to the original 100 profit level, it will be 101).

    Sort of. This is to be mimicked in Live before this can be grasped.

    Anyway I objected to this once introduced way of working and was asked whether I could find a better way of expressing (by the PRT platform) how this should work. The context of this indeed was : what about the margin headroom ? hence, the situation is the same : you have a position eating your margin, put a new limit and once that limit will be filled you’re in trouble.

    Note : turn this into a Buy Stop order (yes, above the price) while you *know* that you will have sufficient $ by the time the price went all the way up to that level, and the situation becomes 100% justisan’s but from a very different angle. Thus, by that time you earned 3 lots, want to buy one extra, but the Buy Stop is rejected because at this moment you have room for 1 only. This just s*cks.

    *Yes, in PRT-IB such Buy Stop orders can be placed via the chart – not via program code (there they are marketable right away (“wrong side of the price”)).


    These specialties mostly are not possible in PRT-IG, but again mostly (with a different subset) are possible in the PRT coding environment because that is general (not dedicated to either IG or IBKR). It is trialing what works where, and justisan clearly did that (all ?).

    justisan, what I don’t see you write about is the checking via TWS how the orders look like; you won’t find nothing new, but where OCO emerges and where it breaks could be interesting to track. And mind you, it is *here* where PRT is involved severely, because it is there where the order numbers are created (thus also : what forms an OCO and what will not). Also : this is totally different for IG. You just need to look at all the connection lines in IG, to see that a virtual none of these exist in PRT-IB. This tells me : the OCO’s are not even there in the first place and PRT manages them (by means of so called “soft orders” (some of those are even denoted like that explicitly in the order types form)).

    Regarding this latter I could also say : such connection lines *can* be there when applied manually (right click on the order label and choose from the presented options (those who were in the way of GraHal two weeks or so back) and from there you’d get the connection lines (a real OCO). But I never ever I saw such connection lines from whatever the nest of order mess I create in PRT-IB code. And the very same code applied in IG *does* create those lines.

    Last thing : these “OCO lines” are created by the broker, not by PRT (the OCO lays with the broker (or even exchange) not with PRT). Thus, don’t see the lines – and you already *know* there is no OCO. That simple. Conclusion : PRT-IB can not do this from your code (PRT-IG can).

    That this fact leads to numerous errors (and a 100+ open tickets which never get solved) is clear, because trying to control a software OCO (which is what PRT-IB does) is quite impossible. This leads to communication errors in the API (between PRT and IBKR) and the 100s of faults I experienced (this is not exaggerated); you just can’t get it synchronized decently, knowing that the broker (or mostly exchange for what we do with futures) is 100% leading.


    This was in a nutshell why you can’t get this done.

    But wait … of course you can get it done, because you know what is going on. Just turn your reversal into a Sell and in the next bar followed by a Sell again. Not such a big deal if your timeframe is 1 second (but if it is not, you’re in trouble). And then still … then will appear the synchronization issues again because if you sell the one second and sell again the next, 1 out of 10 or so the first sell did not happen yet, and now you have laying two sells at the broker and the margin issue is there again.

    Plus PRT throws you out, because it knows it is still waiting for the confirmation of the first sell and it can’t bear that.

    There is no real way out of this and this is already outside the margin issue. No such thing with IG because that is “immediate” (there is no broker). IG is like backtesting (but has a million other issues).



Viewing 9 posts - 16 through 24 (of 24 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

From idea to ProBuilder code in seconds.

Your coding copilot for ProRealTime™. Ask in plain English — get clean, ready-to-paste indicators, screeners and systems.

Available in 7 languages
Try ProRealAI

asynchronous order processing? (futures with PRT+IBRK)


Platform Support: Charts, Data & Broker Setup

New Reply
Author
author-avatar
justisan @justisan Participant
Summary

This topic contains 23 replies,
has 5 voices, and was last updated by PeterStPeterSt
1 day, 1 hour ago.

Topic Details
Forum: Platform Support: Charts, Data & Broker Setup
Language: English
Started: 11/27/2026
Status: Active
Attachments: 1 files
ProRealCode ProRealCode
Loading...