Hi,

I am new to MQL5 learning it. I found one code of grid ea currently its calculating average of max and min position. I am trying to get average of all buy or sell positions with minimum profit. I am stuck on trying this but not able to calculate average of positions can anyone help me on this please below is the code of EA.

```#include <Trade\PositionInfo.mqh> CPositionInfo     m_position;

input int          iTakeProfit         = 300;      // Take Profit (in pips)
input double       iStartLots          = 0.01;     // Start lot
input double       iMaximalLots        = 2;        // Maximal Lots
input double       iLotMultiplier      = 1;        // Lot Multiplier
input int          iPointOrderStep     = 300;      // Point order step (in pips)
input int          iMinimalProfit      = 70;       // Minimal profit for close grid (in pips)
input int          iMagicNumber        = 227;      // Magic Number (in number)
input int          iSlippage           = 30;       // Slippage (in pips)

int OnInit()
{
Comment("Grid EA");

return(INIT_SUCCEEDED);
}

void OnTick()
{
double
SelPriceMin=0,SelPriceMax=0,SelPriceMinLot=0,SelPriceMaxLot=0;

ulong

double
op=0,lt=0,tp=0;

ulong    tk=0;
int b=0,s=0;

int total=PositionsTotal();
for(int k=total-1;k>=0;k--)
if(m_position.SelectByIndex(k))
if(m_position.Symbol()==Symbol())
if(m_position.Magic()==iMagicNumber)
{

op=NormalizeDouble(m_position.PriceOpen(),Digits());
lt=NormalizeDouble(m_position.Volume(),2);
tk=m_position.Ticket();

{
b++;
{
}
{
}
}
// ===
if(m_position.PositionType()==POSITION_TYPE_SELL)
{
s++;
if(op>SelPriceMax || SelPriceMax==0)
{
SelPriceMax    = op;
SelPriceMaxLot = lt;
SelPriceMaxTic = tk;
}
if(op<SelPriceMin || SelPriceMin==0)
{
SelPriceMin    = op;
SelPriceMinLot = lt;
SelPriceMinTic = tk;
}
}
}
//*************************************************************//
if(s>=2) AwerageSelPrice=NormalizeDouble((SelPriceMax*SelPriceMaxLot+SelPriceMin*SelPriceMinLot)/(SelPriceMaxLot+SelPriceMinLot)-iMinimalProfit*Point(),Digits());
//*************************************************************//
if(SelPriceMaxLot==0) SelLot=iStartLots; else SelLot=NormalizeDouble((SelPriceMaxLot*iLotMultiplier),2);
"Next Sell Lot:",SelLot,"\n"
"Balance:",AccountInfoDouble(ACCOUNT_BALANCE),"\n"
"Equity:",AccountInfoDouble(ACCOUNT_EQUITY),"\n"
"Profit:",AccountInfoDouble(ACCOUNT_PROFIT));
;

//*************************************************************//

if(SelLot>iMaximalLots)      SelLot=iMaximalLots;

return;
//*************************************************************//
MqlRates rates[];
CopyRates(Symbol(),PERIOD_CURRENT,0,2,rates);

MqlTick tick;
if(!SymbolInfoTick(Symbol(),tick))
Print("SymbolInfoTick() failed, error = ",GetLastError());

if(rates[1].close>rates[1].open)

Print("OrderSend error #",GetLastError());

if(rates[1].close<rates[1].open)
if((s==0) || (s>0 && (tick.bid-SelPriceMax)>(iPointOrderStep*Point())))
Print("OrderSend error #",GetLastError());
//*************************************************************//

for(int k=total-1;k>=0;k--)
if(m_position.SelectByIndex(k))
if(m_position.Symbol()==Symbol())
if(m_position.Magic()==iMagicNumber)
{

op=NormalizeDouble(m_position.PriceOpen(),Digits());

tp=NormalizeDouble(m_position.TakeProfit(),Digits());

lt=NormalizeDouble(m_position.Volume(),2);
tk=m_position.Ticket();

Print("OrderModify error #",GetLastError());

if(m_position.PositionType()==POSITION_TYPE_SELL && s==1 && tp==0)
Print("OrderModify error #",GetLastError());

{

Print("OrderModify error #",GetLastError());

}
if(m_position.PositionType()==POSITION_TYPE_SELL && s>=2)
{

Print("OrderModify error #",GetLastError());

}
}
}

void OnDeinit(const int reason)
{

}

bool CheckVolumeValue(double volume)
{

double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
if(volume<min_volume)
return(false);

double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
if(volume>max_volume)
return(false);

double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);

int ratio=(int)MathRound(volume/volume_step);
if(MathAbs(ratio*volume_step-volume)>0.0000001)
return(false);

return(true);
}
```