I recently raised a topic on reopening orders. Thought I had solved the problem, as everything was working very smoothly for a week. But today I have encountered a similar problem only when opening. A lot of orders opened on Finam, and 9 on Otkritie...although only one order should have opened in all cases.
i.e. we can see from the code that if the operation is successful, the variableOpenOrders increases, which initially equals 0
If it is higher than 0, there should be no further opening of the order, but the entire pile of orders is opened with the comment Order1.
I check if there is a positive response in the function opening the order and if the order ticket has been received, but for some reason this function returns false, even though the order is actually set.
Explain what is wrong, how to solve this problem?
Additionally, you can check the opening of a position by trying to select it. And it is better to try several times.
int n = 0; do { n++; } while(!PositionSelectByTicket(tradeResult.deal) && n < 5);
There is both a delay and confirmation that the position is already in place.
I also noticed that there is no check OrderCheck(Request, checkResult), which is bad.
Request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID); forget it
bool SendOrderSyncMode() { MqlTradeRequest request={0}; MqlTradeResult result={0}; order_ticket=0; request.action = TRADE_ACTION_DEAL; request.magic = 9876543211; request.symbol = Symbol(); request.volume = 1; request.price = 0; request.type=ORDER_TYPE_BUY; request.comment="Sync mode"; request.type_filling=ORDER_FILLING_FOK; request.type_time=ORDER_TIME_DAY; if(OrderSend(request,result)) { if((result.retcode==TRADE_RETCODE_DONE) || (result.retcode==TRADE_RETCODE_PLACED)) { if(result.order>0) { order_ticket=result.order; Print(__FUNCTION__," Order sent in sync mode"); return(true); } else { Print(__FUNCTION__," Error order sent in sync mode! Retcode = ",result.retcode); } } else { Print(__FUNCTION__," Error order sent in sync mode! Retcode = ",result.retcode); } } else { Print(__FUNCTION__," Order not sent in sync mode."); } return(false); }
voidOnTradeTransaction(constMqlTradeTransaction &trans, constMqlTradeRequest &request, constMqlTradeResult &result) { switch(trans.type) { caseTRADE_TRANSACTION_HISTORY_ADD: if((order_ticket > 0) && (trans.order == order_ticket)) { //Вот здесь и смотрим что произошло } break; } }
Thanks so much for such a detailed description...and explanation.... And about the price.... yes, more used to MT4, and there you always have to put the price...
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
I recently raised a topic on reopening orders. Thought I had solved the problem, as everything was working very smoothly for a week. But today I have encountered a similar problem only when opening.
A lot of orders opened on Finam, and 9 on Otkritie...although only one order should have opened in all cases.
Here is a code snippet from the EA
{
Coment="Open Sell "+string(OpenOrders+1);
ret=OpenSellPosition(_Symbol,volume,Coment,Sleeppage,Filling);
}
if(ret)
{
OpenOrders++;
PriceLastOpen=price;
}
bool OpenSellPosition(string symbol, double volume, string comment="", ulong deviation=10, ENUM_ORDER_TYPE_FILLING filling=ORDER_FILLING_FOK)
{
MqlTradeRequest Request;
MqlTradeResult Results;
ZeroMemory(Request);
ZeroMemory(Results);
Request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID);
Request.action=TRADE_ACTION_DEAL;
Request.type=ORDER_TYPE_SELL;
Request.symbol=symbol;
Request.volume=volume;
Request.deviation=deviation;
Request.comment=comment;
Request.type_filling=filling;
bool res=false;
res=OrderSend(Request,Results);
if(res)
{
if(Results.deal>0) return(true);
else return(false);
}
return(false);
}
i.e. we can see from the code that if the operation is successful, the variableOpenOrders increases, which initially equals 0
If it is higher than 0, there should be no further opening of the order, but the entire pile of orders is opened with the comment Order1.
I check if there is a positive response in the function opening the order and if the order ticket has been received, but for some reason this function returns false, even though the order is actually set.
Explain what is wrong, how to solve this problem?
do
{
n++;
}
while(!PositionSelectByTicket(tradeResult.deal) && n < 5);
I also noticed that there is no check OrderCheck(Request, checkResult), which is bad.
Oh, and also in market order - price = 0
Request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID); forget it
{
MqlTradeRequest request={0};
MqlTradeResult result={0};
order_ticket=0;
request.action = TRADE_ACTION_DEAL;
request.magic = 9876543211;
request.symbol = Symbol();
request.volume = 1;
request.price = 0;
request.type=ORDER_TYPE_BUY;
request.comment="Sync mode";
request.type_filling=ORDER_FILLING_FOK;
request.type_time=ORDER_TIME_DAY;
if(OrderSend(request,result))
{
if((result.retcode==TRADE_RETCODE_DONE) || (result.retcode==TRADE_RETCODE_PLACED))
{
if(result.order>0)
{
order_ticket=result.order;
Print(__FUNCTION__," Order sent in sync mode");
return(true);
}
else
{
Print(__FUNCTION__," Error order sent in sync mode! Retcode = ",result.retcode);
}
}
else
{
Print(__FUNCTION__," Error order sent in sync mode! Retcode = ",result.retcode);
}
}
else
{
Print(__FUNCTION__," Order not sent in sync mode.");
}
return(false);
}
const MqlTradeRequest &request,
const MqlTradeResult &result)
{
switch(trans.type)
{
case TRADE_TRANSACTION_HISTORY_ADD:
if((order_ticket > 0) && (trans.order == order_ticket))
{
//Вот здесь и смотрим что произошло
}
break;
}
}
Additionally, it is possible to check the position opening by trying to select it. And it is better to try several times
do
{
n++;
}
while(!PositionSelectByTicket(tradeResult.deal) && n < 5);
I also noticed that there is no check OrderCheck(Request, checkResult), which is bad.
What if the position already existed?
Or if there is already a position but the order is not filled completely?
Additionally, one can check the position opening by trying to select it. And several attempts are better.
do
{
n++;
}
while(!PositionSelectByTicket(tradeResult.deal) && n < 5);
Also I noticed that there is no check OrderCheck(Request, checkResult) and this is bad.
Well, maybe I will insertOrderCheck(Request, checkResult)
Thank you
But I don't particularly like such a loop
Oh, and also in market order - price = 0
Request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID); forget it
{
MqlTradeRequest request={0};
MqlTradeResult result={0};
order_ticket=0;
request.action = TRADE_ACTION_DEAL;
request.magic = 9876543211;
request.symbol = Symbol();
request.volume = 1;
request.price = 0;
request.type=ORDER_TYPE_BUY;
request.comment="Sync mode";
request.type_filling=ORDER_FILLING_FOK;
request.type_time=ORDER_TIME_DAY;
if(OrderSend(request,result))
{
if((result.retcode==TRADE_RETCODE_DONE) || (result.retcode==TRADE_RETCODE_PLACED))
{
if(result.order>0)
{
order_ticket=result.order;
Print(__FUNCTION__," Order sent in sync mode");
return(true);
}
else
{
Print(__FUNCTION__," Error order sent in sync mode! Retcode = ",result.retcode);
}
}
else
{
Print(__FUNCTION__," Error order sent in sync mode! Retcode = ",result.retcode);
}
}
else
{
Print(__FUNCTION__," Order not sent in sync mode.");
}
return(false);
}
const MqlTradeRequest &request,
const MqlTradeResult &result)
{
switch(trans.type)
{
case TRADE_TRANSACTION_HISTORY_ADD:
if((order_ticket > 0) && (trans.order == order_ticket))
{
//Вот здесь и смотрим что произошло
}
break;
}
}
And about the price.... yes, more used to MT4, and there you always have to put the price...
Repeat
Apart from that, you should also check the status of the order and whether it has already appeared in the history or not.
The exchange does not work with positions, only orders.
You would be right if I were to place pending orders, but I work by market, and here the ticket is obtained withResults.deal
I don't understand what your smile means?
I didn't expect to hear you say that. What pauses, or are you joking?
Bad forex legacy.
...
What if the position already existed?
Or is there already a position but the order is not filled completely?