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.
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 😀
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…
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.
…thank you ai for the bla-bla, but no solution 😀
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…
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?
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?