Hi,
I had the same problem (many pending orders) and I solved it this way:
after a pending order is opened, I check the free margin per minute and if it falls below a limit, I close
all pending orders. If there is enough free margin again, the pending order could then be reopened.
You should also think about reducing the lot size so that the low margin situation is very rare.
Best regards
-
There is no need to create pending orders in code.
- The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)
- Don't worry about it unless you're scalping M1 or trading news.
- Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.
-
Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.
-
You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support.
-
AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)
-
Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
Lot value calculation off by a factor of 100 - MQL5 programming forum (2019) -
You must normalize lots properly and check against min and max.
-
You must also check Free Margin to avoid stop out
-
For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)
Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.
-
Hi,
I had the same problem (many pending orders) and I solved it this way:
after a pending order is opened, I check the free margin per minute and if it falls below a limit, I close
all pending orders. If there is enough free margin again, the pending order could then be reopened.
You should also think about reducing the lot size so that the low margin situation is very rare.
Best regards
Hi,
Thank for your replys. It's a good idea.
You said "... if it falls below a limit...". I think maybe if it's negative should be enough to delete the pending orders, right ?
-
There is no need to create pending orders in code.
- The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)
- Don't worry about it unless you're scalping M1 or trading news.
- Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.
-
Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.
-
You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support.
-
AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)
-
Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
Lot value calculation off by a factor of 100 - MQL5 programming forum (2019) -
You must normalize lots properly and check against min and max.
-
You must also check Free Margin to avoid stop out
-
For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)
Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.
-
Thank you William
Yes you're right. Maybe I should use direct market orders since the begining, now doing this requires a lot of code rewrite. I will test previous answer and take some time to rewrite the code.
Thank for your help
Hi,
Thank for your replys. It's a good idea.
You said "... if it falls below a limit...". I think maybe if it's negative should be enough to delete the pending orders, right ?
Hi michel,
Normally, when the EA runs on an account and the free margin becomes negative, your broker will close all your open positions.
Best regards
Your code | switch(SOrder[index].order_type) { case OP_BUY: check_order_type=OP_BUY; break; case OP_SELL: check_order_type=OP_SELL; break; case OP_BUYLIMIT: check_order_type=OP_BUY; break; case OP_SELLLIMIT: check_order_type=OP_SELL; break; case OP_BUYSTOP: check_order_type=OP_BUY; break; case OP_SELLSTOP: check_order_type=OP_SELL; break; default: return(false); break; } |
Simplified | check_order_type=SOrder[index].order_type % 2;
|

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello everyone,
I'm wrinting an EA which has a strategy which open a lot of pending orders. Before sending the command to open, I'm checking if there is enough money.
The problem is when some pending orders are triggered there is not enough money, so the strategy tester delete them and close all the trades.
These are the kinds of message I've in logs:
2022.08.06 08:08:17.349 2022.01.19 12:29:28 Tester: PrevBalance: 500.00, PrevPL: -426.46, PrevEquity 73.54, PrevMargin: 288.54, NewMargin: 289, FreeMargin: -215.57
2022.08.06 08:08:17.349 2022.01.19 12:29:28 Tester: not enough money for buy 0.28 XAUUSD at 1818.29 sl: 0.00 tp: 0.00 [2022.01.19 12:29]
What others tests/checks I should do before opening a order or maybe I should perform other checkings ?