Hello,
I have a problem with a function for partial order close, with a loop in it. Outside the loop i set a variable closed_volume =0, so the idea is to store the amount of closed lots , so that it may equal the previously set lot size for close - vlm .But what happens is when i run the loop once, the information is stored, but the second one comes the closed_volume is reset to 0. I tried everything, break ,return (not together)but what happens is that it just returns to the top of the function and resets the value. Id really use some help with it ! I printed out everything.
void myOrderClose(ENUM_POSITION_TYPE type, double vlm) //
{
double closed_volume=0 ,ord_volume,vlm_diff;
ulong tick;
if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) || !MQLInfoInteger(MQL_TRADE_ALLOWED)) return;
if (type > 1)
{
myAlert("error", "Invalid type in myOrderClose");
return;
}
trade.SetTypeFillingBySymbol(_Symbol);
trade.SetDeviationInPoints(MaxSlippage_);
if(closed_volume<vlm){
//----------------------
for(int i = PositionsTotal()-1;i>=0;i--)
{
if(!position1.SelectByIndex(i))
continue;
if(position1.Symbol()==_Symbol && position1.Magic()==MagicNumber && position1.PositionType()==type)
{
Print("CLV 2 : ",closed_volume);
if(closed_volume==vlm)
{Print("closed volume ",closed_volume);return; }
ord_volume=position1.Volume();
if((closed_volume+ord_volume)<vlm)
{
tick = position1.Ticket();
bool close = trade.PositionClosePartial(tick,ord_volume);
if(!close)
{Print("Error closing position #",tick," ",GetLastError());}
else
{Print("Position #",tick," closed !");closed_volume+=ord_volume;}
}
if((closed_volume+ord_volume)==vlm)
{
tick = position1.Ticket();
bool close = trade.PositionClosePartial(tick,ord_volume);
if(!close)
{Print("Error closing position #",tick," ",GetLastError());}
else
{Print("Position #",tick," closed !");closed_volume+=ord_volume;Print("CLV ",closed_volume);}
}
if(closed_volume<vlm && closed_volume+ord_volume>vlm)
{
vlm_diff=vlm-closed_volume;
tick = position1.Ticket();
bool close = trade.PositionClosePartial(tick,vlm_diff);
if(!close)
{Print("Error closing position #",tick," ",GetLastError());}
else
{Print("Position #",tick," closed !");closed_volume+=vlm_diff;}
}
}
}
}
}
for example see the picture - after one turn in the loop, the second resets !