Good day! from the name of the problem, everything is clear. I take the standard code from the help and insert it. when closing, it returns an error. tell me the working code) here is the code from the help:
void OnStart()
{
/ / - - - declaring the query and result of the
mqltraderequest request;
MqlTradeResult result;
int total=positionstotal(); / / number of open positions
/ / - - - iterate through all open positions
for(int i=total-1; i>=>0; i--)
{
//--- parameters of the order
ulong position_ticket=positiongetticket(i); / / position ticket
string position_symbol=positiongetstring(POSITION_SYMBOL); / / character
int digits= (int)symbolinfointeger(position_symbol,SYMBOL_DIGITS); / / number of decimal places
ulong magic=positiongetinteger(POSITION_MAGIC); / / MagicNumber of the position
double volume=positiongetdouble(POSITION_VOLUME); / / position volume
double sl=positiongetdouble(POSITION_SL); / / Stop Loss of position
double tp=positiongetdouble(POSITION_TP); / / Take Profit positions
ENUM_POSITION_TYPE type= (ENUM_POSITION_TYPE)positiongetinteger(POSITION_TYPE); / / position type
//--- display information about the position
PrintFormat("#%I64u %s %s %.2f %s sl: %s tp: %s [%I64d]",
position_ticket,
position_symbol,
EnumToString(type),
volume,
DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),
DoubleToString(sl,digits),
DoubleToString(tp,digits),
magic);
//--- if the MagicNumber matches
if(magic==EXPERT_MAGIC)
{
for(int j=0; jstring symbol=PositionGetSymbol(j); // the symbol of a new position
/ / - - - if the characters of the new and searched positions match
if(symbol==position_symbol & & positiongetinteger(POSITION_MAGIC)==EXPERT_MAGIC)
{
//- - - setting the counter position type
ENUM_POSITION_TYPE type_by=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
/ / - - - output if the source and counter position types are the same
if(type==type_by)
continue;
/ / - - - resetting the query and result values
ZeroMemory(request);
ZeroMemory(result);
/ / - - - setting operation parameters
request.action=TRADE_ACTION_CLOSE_BY; // type of a trade operation
request.position=position_ticket; // position ticket
request. position_by=positiongetinteger(POSITION_TICKET); / / counter position ticket
//request.symbol =position_symbol;
request. magic=EXPERT_MAGIC; // MagicNumber of the position
/ / - - - output of information about closing the counter
PrintFormat("Close #%I64d %s %s by #%I64d",position_ticket,position_symbol,EnumToString(type),request.position_by);
/ / - - - sending a request
if(!OrderSend(request, result))
PrintFormat("OrderSend error %d",GetLastError()); // if the request failed, print the error code
/ / - - - information about the operation
PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
}
}
}
}
}
nektomk
of course offtop, but gentlemen, on the programmer’s forum to start a separate instruction for inserting code on the forum..this is some kind of overkill, beyond good and evil.
For how many years, the site and forum are replenished with features, features and features..Except for one thing-simple insertion, markup, and formatting of MQL code. Or do we have C# in the future and there is no need to waste webmaster resources ?
barabashkakvn
Please use the hint when inserting the code: Correctly insert the code on the forum.
I also recommend using a Code Styler (quick application in the MetaEditor editor: “Ctrl”+”,”).
starij
open two counter orders. Take the code from the help and put it in the script. Throw out all the secondary things. Add the output of intermediate values with the Alert function …