maintain same number of buy and sell positions in MT5

To add comments, please log in or register
1324007
11
1324007  

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 :

if

open buy orders < open sell order

open new buy order (with specified parameters)

if 

open buy orders > open sell orders

open new sell order (with specified

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.

fxsaber
12947
fxsaber  
#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;
  }
}
1324007
11
1324007  
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 
To add comments, please log in or register