bool ClosePosition_real(int currentMagic, double &_profit)
{
MqlTradeRequest tradeRequestClose;
MqlTradeResult tradeResultClose;
int total=PositionsTotal(); // number of open positions for(int i=total-1; i>=0; i--)
{
ulong position_ticket=PositionGetTicket(i); // ticket of the positionstring position_symbol=PositionGetString(POSITION_SYMBOL); // symbol ulong magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber of the positionint digits=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS); // number of decimal placesdouble volume=PositionGetDouble(POSITION_VOLUME); // volume of the positionENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); // type of the positionif(magic==currentMagic)
{
ZeroMemory(tradeRequestClose);
ZeroMemory(tradeResultClose);
tradeRequestClose.action =TRADE_ACTION_DEAL; // type of trade operation
tradeRequestClose.position =position_ticket; // ticket of the position
tradeRequestClose.symbol =position_symbol; // symbol
tradeRequestClose.volume =volume; // volume of the position
tradeRequestClose.deviation=tradeRequest.deviation; // allowed deviation from the price
tradeRequestClose.magic = currentMagic; // MagicNumber of the positionif(type==POSITION_TYPE_BUY)
{
tradeRequestClose.price=SymbolInfoDouble(position_symbol,SYMBOL_BID);
tradeRequestClose.type =ORDER_TYPE_SELL;
}
else
{
tradeRequestClose.price=SymbolInfoDouble(position_symbol,SYMBOL_ASK);
tradeRequestClose.type =ORDER_TYPE_BUY;
}
_profit = NormalizeDouble(PositionGetDouble(POSITION_PROFIT), __digits);
double __debugVal;
OrderCalcProfit(tradeRequestClose.type, symbol, volume, tradeRequestClose.price, currentDealPrice, __debugVal);
__debugVal = NormalizeDouble(__debugVal, __digits);
if(OrderSend(tradeRequestClose,tradeResultClose))
{
returntrue;
}
else
{
PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error codereturnfalse;
}
}
}
returnfalse;
}
The first way for viewing profits is the PositionGetDouble function that gives me the 16.6999 number and the second one is the OrderCalcProfit functionthat gives me 16.7199 number.
So the results are different, but I expect the same result because there are identical input data.
Can you explain to me why the results are different? How to fix it?
I have written a method for close the position
The first way for viewing profits is the PositionGetDouble function that gives me the 16.6999 number and the second one is the OrderCalcProfit function that gives me 16.7199 number.
So the results are different, but I expect the same result because there are identical input data.
Can you explain to me why the results are different? How to fix it?
Look at the attached file: