Closing orders at the cross of the MA does not work // Fermeture des ordres au croisement des MA ne fonctionne pas
- Are Stop Loss and Take Profit of a Market Order ... limit ordres?
- Finally a REAL ECN MT4 broker?
- Multi Timeframe Indicators
Sorry whroeder1, I made a mistake of manipulation.
hello to all, can anyone help me solve this puzzle please?
First, I present my strategy (to understand). The EA opens a Buy position when the fast MA is higher than the slow MA and Rsi above the 50 level, conversely for Sell. He closes the positions at the TP. Very simple!
The EA works without problems.
By cons, I added 2 MA (trigger) to close open orders at the crossing of these 2 MA.
The function does not work.
I can not find the right code to establish the total closure function.
I tried various codes, but to no avail.
Maybe you can tell me what error is there in my code, please?
I thank you in advance.
bonjour à tous et à toutes, quelqu'un peut-il m'aider pour résoudre cette énigme svp?
Tout d'abord, je vous présente ma stratégie (pour comprendre). L'EA ouvre une position Buy quand la MA rapide est supérieure à la MA lente et Rsi au-dessus du niveau 50, inversement pour Sell. Il ferme les positions au TP. Très simple!
L'EA fonctionne sans problème.
Par contre, j'ai ajouté 2 MA (déclencheur) pour fermer les ordres ouverts au croisement de ces 2 MA.
La fonction ne marche pas.
Je ne trouve pas le bon code pour établir la fonction de fermeture totale.
J'ai essayé divers codes, mais en vain.
Peut-être pouvez-vous m'indiquer quelle erreur y a t-il dans mon code svp?
Je vous remercie d'avance.
//+------------------------------------------------------------------+ //| Stéphane C..mq4 | //| Copyright 2017, MetaQuotes Software Corp. | //| https://www.mql4.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2017, MetaQuotes Software Corp." #property link "chaspierrestephane@gmail.com" #property version "1.00" #property strict input string EAsettings = "[EA settings]"; //Paramètres input double lots = 0; //Lot size input double TakeProfit = 0; //Take profit input double StopLoss=0; //Stop Loss input double distance= 0; //X Pips (Distance) input int MagicNumber = 55545; //Magic Nb input string EA_Comment = ""; // ~~~~ input string Rsi = "RSI Settings"; //RSI input int rsi_period=14; //Rsi period input ENUM_APPLIED_PRICE rsi_applied_price = PRICE_CLOSE; //~~ input string ma1 = "[===MA1===]"; //Fast MA input int ma_period1 = 5; //MA 1 period input ENUM_MA_METHOD ma_type1 = MODE_EMA; //MA 1 type input string ma2 = "[===MA2===]"; //Low MA input int ma_period2=7; //MA 2 period input ENUM_MA_METHOD ma_type2 = MODE_EMA; //MA 2 type input string ma3 = "[===MA3===]"; //MA 3 Trigger input int ma_period3 = 10; //MA 3 period input ENUM_MA_METHOD ma_type3 = MODE_EMA; //MA 3 type input string ma4 = "[===MA4===]"; //MA 4 Trigger input int ma_period4 = 12; //MA 4 period input ENUM_MA_METHOD ma_type4 = MODE_EMA; //MA 4 type datetime time; double _point=1; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- create timer string _sym=Symbol(); double _digits=MarketInfo(_sym,MODE_DIGITS); if(_digits==5||_digits==3) _point=1/MathPow(10,(_digits-1)); if(_digits==4||_digits==2) _point=1/MathPow(10,(_digits)); if(_digits==1) _point=0.1; EventSetTimer(60); time=Time[0]; //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- destroy timer EventKillTimer(); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if ( time==Time[0])return; time=Time[0]; //---fonction stop positions-------- if ((MODE_ASK ==(_ma3(1)>_ma4(1)&&_ma3(0)<_ma4(0))) || (MODE_BID == (_ma3(1)<_ma4(1)&&_ma3(0)>_ma4(0)))) CloseAll(); //---fonctions d'ordres----------- if (OrdersTotalT(OP_BUY)==0&&_ma1(1)>_ma2(1)&&Ask>_ma1()+distance*_point&&_rsi(1)>=50) SendOrder(OP_BUY); if (OrdersTotalT(OP_SELL)==0&&_ma1(1)<_ma2(1)&&Bid<_ma1()-distance*_point&&_rsi(1)<50) SendOrder(OP_SELL); } //+------------------------------------------------------------------+ //| Timer function | //+------------------------------------------------------------------+ void OnTimer() { //--- } //+------------------------------------------------------------------+ //| ChartEvent function | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { //--- } bool SendOrder(int type,double _price=0) { while(IsTradeContextBusy()); int ticket=-1; double SL,TP; if(type==OP_BUY) { if(StopLoss==0) {SL=0;} else {SL=Ask-StopLoss*_point;} if(TakeProfit==0) {TP=0;} else {TP=Ask+TakeProfit*_point;} ticket = OrderSend(Symbol(),OP_BUY,NormalizeLots(LotManage(),Symbol()),Ask,3,SL,TP,EA_Comment,MagicNumber,0,clrBlue); } if(type==OP_SELL) { if(StopLoss==0) {SL=0;} else {SL=Bid+StopLoss*_point;} if(TakeProfit==0) {TP=0;} else {TP=Bid-TakeProfit*_point;} ticket=OrderSend(Symbol(),OP_SELL,NormalizeLots(LotManage(),Symbol()),Bid,3,SL,TP,EA_Comment,MagicNumber,0,clrRed); } if(ticket<0) { Print("OrderSend failed with error #",GetLastError()); return(false); } return(true); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double LotManage() { return (NormalizeDouble(lots,2)); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double NormalizeLots(double _lots,string pair="") { if(pair=="") pair=Symbol(); double lotStep=MarketInfo(pair,MODE_LOTSTEP), minLot=MarketInfo(pair,MODE_MINLOT); _lots=MathRound(_lots/lotStep)*lotStep; if(_lots<MarketInfo(pair,MODE_MINLOT)) _lots=MarketInfo(pair,MODE_MINLOT); if(_lots>MarketInfo(pair,MODE_MAXLOT)) _lots=MarketInfo(pair,MODE_MAXLOT); return(_lots); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double _rsi(int _shift) { return(iRSI(NULL,0,rsi_period,rsi_applied_price,_shift)); } //+------------------------------------------------------------------+ double _ma1(int _shift=0) { return(iMA(NULL,0,ma_period1,0,ma_type1,PRICE_CLOSE,_shift)); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double _ma2(int _shift=0) { return(iMA(NULL,0,ma_period2,0,ma_type2,PRICE_CLOSE,_shift)); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ int OrdersTotalT(int _type) { int _total=0; for(int cnt=OrdersTotal()-1;cnt>=0;cnt--) { bool select=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol() && OrderType()==_type) { _total++; } } return(_total); } //........................................................................................................ void CloseOrders(int type) { bool result=false; int count=0,total=OrdersTotal(); for(count=total-1; count>=0; count--) { if(OrderSelect(count,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==type) { if(OrderType()<2) result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE); else result=OrderDelete(OrderTicket()); } } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double _ma3(int _shift=0) { return(iMA(NULL,0,ma_period3,0,ma_type3,PRICE_CLOSE,_shift)); } double _ma4(int _shift=0) { return(iMA(NULL,0,ma_period4,0,ma_type4,PRICE_CLOSE,_shift)); } //..................................................................... void CloseAll() { bool result=False; for(int _count=OrdersTotal()-1; _count>=0; _count--) { RefreshRates(); if(OrderSelect(_count,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) { if(OrderType()<2) result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrYellow); } } } } //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx FIN xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Sorry whroeder1, I made a mistake of manipulation.
hello to all, can anyone help me solve this puzzle please?
First, I present my strategy (to understand). The EA opens a Buy position when the fast MA is higher than the slow MA and Rsi above the 50 level, conversely for Sell. He closes the positions at the TP. Very simple!
The EA works without problems.
By cons, I added 2 MA (trigger) to close open orders at the crossing of these 2 MA.
The function does not work.
I can not find the right code to establish the total closure function.
I tried various codes, but to no avail.
Maybe you can tell me what error is there in my code, please?
I thank you in advance.
bonjour à tous et à toutes, quelqu'un peut-il m'aider pour résoudre cette énigme svp?
Tout d'abord, je vous présente ma stratégie (pour comprendre). L'EA ouvre une position Buy quand la MA rapide est supérieure à la MA lente et Rsi au-dessus du niveau 50, inversement pour Sell. Il ferme les positions au TP. Très simple!
L'EA fonctionne sans problème.
Par contre, j'ai ajouté 2 MA (déclencheur) pour fermer les ordres ouverts au croisement de ces 2 MA.
La fonction ne marche pas.
Je ne trouve pas le bon code pour établir la fonction de fermeture totale.
J'ai essayé divers codes, mais en vain.
Peut-être pouvez-vous m'indiquer quelle erreur y a t-il dans mon code svp?
Je vous remercie d'avance.
- ma
Why did you post your MT4 question in the
Root /
MT5 EA section
instead of the
MQL4 section, (bottom of the Root page?)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon. ticket = OrderSend(Symbol(),OP_BUY,NormalizeLots(LotManage(),Symbol()),Ask,3,SL,TP,EA_Comment,MagicNumber,0,clrBlue); bool select=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrYellow);
Check your return codes for errors and report them.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles- Stéphane C.: The function does not work."Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here and our crystal balls are cracked.
- True is one and False is zero. So when will 9 or 10 ever equal zero or one?
if ((MODE_ASK ==(_ma3(1)>_ma4(1)&&_ma3(0)<_ma4(0))) || (MODE_BID == (_ma3(1)<_ma4(1)&&_ma3(0)>_ma4(0)))) CloseAll(); if 10 ==( zero or one ) || 9 == ( zero or one ) CloseAll()
Symbol Properties - Environment State - Standard Constants, Enumerations and Structures - MQL4 Reference - Learn to use the debugger or print out your variables, find out why.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use