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.
Hello there!
I'm wondering how to close a position in condition?
For example When EMA 20 cross over EMA 50 then start buy position
and when EMA 20 cross under EMA 50 then close this buy position
I only using trade.buy()
if( (myMAfast[0] > myMAslow[0]) && (myMAfast[1] < myMAslow[1]) ) //cross over
{ signal = "buy"; }
if( (myMAfast[0] < myMAslow[0]) && (myMAfast[1] > myMAslow[1]) ) //cross under
{ signal = "sell"; }
if(signal =="sell" && PositionsTotal()<1)
trade.Sell(0.50,NULL,Bid,Ask+100*_Point,(Bid-150*_Point),NULL);
if(signal =="buy" && PositionsTotal()<1)
trade.Buy(0.50,NULL,Ask,Bid - 100*_Point,(Ask+150*_Point),NULL);
I'm thinking open another function ?
like
if((myMAfast[0] < myMAslow[0]) && (myMAfast[1] > myMAslow[1])) //cross under
{
ClosebuyPositions();
}
but I don't know how to define ClosebuyPositions()
Can anyone give me a hand?
thank a lot!