maintain same number of buy and sell positions in MT5
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006 #define Bid SymbolInfoDouble(_Symbol, SYMBOL_BID) #define Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK) int GetAmount( const int Type ) { int Amount = 0; for (int i = OrdersTotal() - 1; i >= 0; i--) Amount += OrderSelect(i, SELECT_BY_POS) && (OrderType() == Type) && (OrderSymbol() == _Symbol); return(Amount); } void OnInit() { OnTrade(); } void OnTrade() { bool Res = true; while (Res) { const int AmountBuy = GetAmount(OP_BUY); const int AmountSell = GetAmount(OP_SELL); if (Res = (AmountBuy != AmountSell)) Res = ((AmountBuy > AmountSell) ? OrderSend(_Symbol, OP_SELL, 1, Bid, 100, 0, 0) : OrderSend(_Symbol, OP_BUY, 1, Ask, 100, 0, 0)) > 0; } }
thank you so much fxsaber, very fast indeed. I will add this part in my EA in the evening and begin another thousands of back testings . I hope it will work

You are missing trading opportunities:
- Free trading apps
- Free Forex VPS for 24 hours
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello guys, I start by admiting I am a noob in coding, I only recently begun to understand some basics and I try to build my own EA for testings, gathering parts of code from all the library from many EAs out there
However I am stuck at this thing that I can't find anywhere any code: I want to maintain same number of open buy and sell orders at all times.
I was thinking that the logical solution would be :
this will force this loop to stop exactly when number of buy and sell open orders would match.
however I have no idea how to code that ... if anyone would be so kind to help me with this thing, please.