Mql4 order is not close auto
Mql4 order is not close auto
Loading saved threads...
San San · External communityPost link
External question — Stack Overflow Stack Exchange
Author: San San
Original post: https://stackoverflow.com/questions/66380629
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I want to back test simple EA only BUY with MT4 Simple moving average 20 period
I see a buy signal and up to 3 orders are running
But when the price is below MA20 it does not automatically close the order
Help me to deal with
void OnTick()
{
double sma20 = iMA(Symbol(),PERIOD_H1,20,0,MODE_SMA,PRICE_CLOSE,0);
if(OrdersTotal()<3)
{
if(Low[1]>sma20)
{
bool ticket = OrderSend(Symbol(),OP_BUY,0.01,Ask,3,0,0,NULL,0,0,clrAliceBlue);
}
}
for(int i=0; i<= OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS))
{
if(OrderType()==OP_BUY)
{
if(Close[1] < sma20)
{
bool ticket_close = OrderClose(OrderTicket(),OrderLots(),Ask,3,clrAliceBlue);
}
}
}
}
}
Quote
Report
tomgny · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: tomgny
Original post: https://stackoverflow.com/a/66398533
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
To close order with type
OP_BUY
, you should use
Bid
price insted of
Ask
.
Check why order isn't close by
GetLastError()
, in your case it should looks like:
bool ticket_close = OrderClose(OrderTicket(),OrderLots(),Bid,3,clrAliceBlue);
if(!ticket_close){
Print("Error closing the order id: ", OrderTicket(), ", Error code: ", GetLastError());
}
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: San San Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/66380629 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I want to back test simple EA only BUY with MT4 Simple moving average 20 period I see a buy signal and up to 3 orders are running But when the price is below MA20 it does not automatically close the order Help me to deal with void OnTick() { double sma20 = iMA(Symbol(),PERIOD_H1,20,0,MODE_SMA,PRICE_CLOSE,0); if(OrdersTotal()<3) { if(Low[1]>sma20) { bool ticket = OrderSend(Symbol(),OP_BUY,0.01,Ask,3,0,0,NULL,0,0,clrAliceBlue); } } for(int i=0; i<= OrdersTotal(); i++) { if(OrderSelect(i,SELECT_BY_POS)) { if(OrderType()==OP_BUY) { if(Close[1] < sma20) { bool ticket_close = OrderClose(OrderTicket(),OrderLots(),Ask,3,clrAliceBlue); } } } } }
Checking account access…