My MQL4 order is sent fine but then nothing happens, and no order was sent?
My MQL4 order is sent fine but then nothing happens, and no order was sent?
Loading saved threads...
Bruse96 · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Bruse96
Original post: https://stackoverflow.com/questions/67525423
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Can this be a latency problem?
Hi! This code sometimes return me nothing after having all condition met for opening an order.
No error, or anything.
Is it possible that in some milliseconds that price has changed and order is automatically discarded?
Here you can see when
PlaceOrder
is called with a BUY or SELL and nothing happen before. In the first case you can see an issue with
USDCAD
, after 9 minutes another signal occurred and the EA opened correctly.
bool PlaceOrder(int type, int magic, int id, bool checkmargin = false)
{
Print("PlaceOrder called.. Type : "+TypeToStr(type));
double entry, lot;
color col;
int typet;
int i = 0;
while(i < NumRetries)
{
i += 1;
while(IsTradeContextBusy())
Sleep(RetryDelayInSec*1000);
RefreshRates();
if(type == OP_BUY)
{
entry = Ask;
col = Blue;
typet = 2;
}
else
{
entry = Bid;
col = Red;
typet = 4;
}
if(MathAbs(entry-Close[1]) > stoplevel)
break; //--Controllo che non ci siano stati spike nel prezzo (es. new impattante)
if(LotCalculation == FixedLots)
lot = FixedLotSize;
else
lot = GetLotSize(type, entry);
Print("Try "+i+" : Open "+TypeToStr(type)+" order. Entry : "+DoubleToStr(entry,Digits)+" Lot :"+lot+" Magic :"+magic);
if(checkmargin)
{
double margin_required = 2 * MarketInfo(Symbol(),MODE_MARGINREQUIRED)*lot;
double initloss = MathCeil((InitialPL() + CommissionPerLot)*lot);
Print("margin_required to open 2 trades : "+margin_required+" initloss : "+initloss+" AccountFreeMargin : "+AccountFreeMargin());
if(AccountFreeMargin() <= margin_required+initloss)
{
Print("Unable to place order due to insufficient margin");
return(false);
}
}
int ticket = OrderSend(Symbol(),type,lot,entry,Slippage,0,0,TradeComment,magic,0,col);
if(ticket <= 0)
{
Print("ERROR opening market order. ErrorCode:"+GetLastError());
if(i == NumRetries)
{
Print("*** Final retry to OPEN ORDER failed ***");
return(false);
}
}
else
{
SetSLandTP(ticket, id);
i = NumRetries;
return(true);
}
}
return(false);
}
Quote
Report
not2qubit · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: not2qubit
Original post: https://stackoverflow.com/a/67612351
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
You need to handle all error codes, and preferably separately from just using GetLastError() once here and there. In you code you're setting TP and SL after having checked the previous operation for error. But your code is likely to fail also here as you probably have too tight SL and/or TP's for the trading conditions under which your algo is running. (See my comments about
slippage, requotes
and
spreads
above.)
Make sure you understand and are handling all of the possible errors here:
https://docs.mql4.com/constants/errorswarnings/errorcodes
https://docs.mql4.com/constants/errorswarnings/enum_trade_return_codes
There are libs that can help you do this.
Quote
Report
Post Reply
Checking account access…