Mitch Zanting:
Hello everyone,
I busy making a EA that uses MA5 and MA10.
i want to do a OrderClose function when the cross back so . (PMA5>PMA10 && MA5<MA10) BUY order opens. (PMA5<PMA10 && MA5>MA10) BUY order closes and SELL order opens.
Can you please look at it and help me out
As One of ways:
int MagicNumber=1200; void OnTick() { double LotSize=0.05; int AmountOfOrders=1; int tppoints=15; //10 = 1 pip double MA5=iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,0); double PMA5=iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,1); double MA10=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0); double PMA10=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,1); if((PMA5>PMA10 && MA5<MA10) && !FoundBuyOpenedOrder(_Symbol,MagicNumber)) { for(int cnt5=OrdersTotal()-1;cnt5>=0;cnt5--) { if(!OrderSelect(cnt5,SELECT_BY_POS,MODE_TRADES)) continue; if(OrderType()==OP_SELL && OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNumber) { if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Green)) Print("OrderClose error common profit",GetLastError()); } } int buyticket=OrderSend( Symbol(), OP_BUY, LotSize, Ask, 2, 0*_Point, Ask+tppoints*_Point, NULL, MagicNumber, 0, Red ); } if((PMA5<PMA10 && MA5>MA10) && !FoundSellOpenedOrder(_Symbol,MagicNumber)) { for(int cnt6=OrdersTotal()-1;cnt6>=0;cnt6--) { if(!OrderSelect(cnt6,SELECT_BY_POS,MODE_TRADES)) continue; if(OrderType()==OP_BUY && OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNumber) { if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet)) Print("OrderClose error common profit",GetLastError()); } } int sellticket=OrderSend( Symbol(), OP_SELL, LotSize, Bid, 2, 0*_Point, Bid-tppoints*_Point, NULL, MagicNumber, 0, Blue ); } } // This function returns true if an order with the same Symbol and Magic Number is found. Otherwise it returns false. bool FoundBuyOpenedOrder(const string symbol,const int magicNumber=-1) { bool foundB=false; for(int i=(OrdersTotal()-1);i>=0 && !found;i--) { if(!OrderSelect(i,SELECT_BY_POS)) { printf("OrderSelect failed with error #%d",GetLastError()); continue; } foundB=(magicNumber==-1 || OrderMagicNumber()==magicNumber) && OrderSymbol()==symbol && (OrderType()==OP_BUY); } return(foundB); } bool FoundSellOpenedOrder(const string symbol,const int magicNumber=-1) { bool foundS=false; for(int i=(OrdersTotal()-1);i>=0 && !found;i--) { if(!OrderSelect(i,SELECT_BY_POS)) { printf("OrderSelect failed with error #%d",GetLastError()); continue; } foundS=(magicNumber==-1 || OrderMagicNumber()==magicNumber) && OrderSymbol()==symbol && (OrderType()==OP_SELL); } return(foundS); }
Of course, you can manage order closure block as a function(s), if you preffer. But as for beginner coder this way is the most simple and there is alike code in standard MACD Sample in MT4.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello everyone,
I busy making a EA that uses MA5 and MA10.
i want to do a OrderClose function when the cross back so . (PMA5>PMA10 && MA5<MA10) BUY order opens. (PMA5<PMA10 && MA5>MA10) BUY order closes and SELL order opens.
Can you please look at it and help me out