It works fine while testing in demo but while testing in strategy tester it doesnot meet my entry criteris and open trades after one is closed
if(!CheckIfOpenOrdersByMagicNB(magicNB))//if no open orders try to enter new position { if(Ask > iMA(NULL,0,maPeriod,0,MODE_EMA,PRICE_CLOSE,0) && iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0 ) < 0 && (iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0 ) > iMACD( NULL, 0,12,26,9, PRICE_CLOSE,MODE_SIGNAL,0)))//buying { Print("Price is above 200 EMA and MACD crossover happened MA period =" + maPeriod ); double stopLossMACDbuy = Ask - 0.0020; double takeProfitMACDbuy = Ask + 0.0060; Print("Entry Price = " + Ask); Print("Stop Loss Price = " + stopLossMACDbuy); Print("Take Profit Price = " + takeProfitMACDbuy); int openOrderID = OrderSend(NULL,OP_BUY,lotSize,Ask,10,stopLossMACDbuy,takeProfitMACDbuy,NULL,magicNB); if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError()); } else if(Bid < iMA(NULL,0,maPeriod,0,MODE_EMA,PRICE_CLOSE,0) && iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0 ) > 0 && (iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0 ) < iMACD( NULL, 0,12,26,9, PRICE_CLOSE,MODE_SIGNAL,0)));//selling { Print("Price is below 200 EMA and MACD crossover happened , Sending sell order"); double stopLossMACDsell = Bid + 0.0020; double takeProfitMACDsell = Bid - 0.0060; Print("Entry Price = " + Bid); Print("Stop Loss Price = " + stopLossMACDsell); Print("Take Profit Price = " + takeProfitMACDsell); int openOrderID = OrderSend(NULL,OP_SELL,lotSize,Bid,10,stopLossMACDsell,takeProfitMACDsell,NULL,magicNB); if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError()); } } else //else if you already have a position, update orders if need too. { Print("Order already placed from your strategy"); }

- 2022.08.07
- www.mql5.com
-
Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
Next time, post in the correct place. The moderators will likely move this thread there soon. -
Are your books one column but two feet wide? No because that is unreadable. They are 6 inches, sometimes two columns, so you can read it easily. So should be your code.
Unreadable if(Ask > iMA(NULL,0,maPeriod,0,MODE_EMA,PRICE_CLOSE,0) && iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0 ) < 0 && (iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0 ) > iMACD( NULL, 0,12,26,9, PRICE_CLOSE,MODE_SIGNAL,0)))//buying
Better double ema0 = iMA(NULL,0,maPeriod,0,MODE_EMA,PRICE_CLOSE,0), macd0 = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0 ), sign0 = iMACD( NULL, 0,12,26,9, PRICE_CLOSE,MODE_SIGNAL,0); if(Ask > ema0 && macd0 < 0 && (macd0 > sign0))//buying
Self-documenting bool isUptrend = Bid > ema0, // Charts are Bid charts; iMA is average Bid Close. bool isValley = macd0 < 0, bool isValleyOver = macd0 > sign0; if(isUptrend && isValley && isValleyOver)//buying
You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.
-
double stopLossMACDbuy = Ask - 0.0020; double takeProfitMACDbuy = Ask + 0.0060;
You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.
-
Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
-
Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 -
The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).
-
-
Don't hard code constants. 0.0020 and 0.0060 fail on JPY pairs and metals. 200*_Point, and 600*_Point works on majors and JPY. 20*PIP, and 60*PIP works everywhere, if you compute PIP correctly.
PIP, Point, or Tick are all different in general.
What is a TICK? - MQL4 programming forum (2014)Unless you manually adjust your SL/TP for each separate symbol, using Point means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points), and metals. Compute what a PIP is and use it, not points.
How to manage JPY pairs with parameters? - MQL4 programming forum (2017)
Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum (2018) -
int openOrderID = OrderSend(NULL,OP_BUY,lotSize,Ask,10,stopLossMACDbuy,takeProfitMACDbuy,NULL,magicNB);
Be careful with NULL.
- On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
- Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
- Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
- MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
- Cloud Protector Bug? - MQL4 programming forum (2020)
- aviyadav321: d open trades after one is closed. please help me.
Of course it does. else if(Bid < iMA(NULL,0,maPeriod,0,MODE_EMA,PRICE_CLOSE,0) && iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0 ) > 0 && (iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0 ) < iMACD( NULL, 0,12,26,9, PRICE_CLOSE,MODE_SIGNAL,0)));//selling { Print("Price is below 200 EMA and MACD crossover happened , Sending sell order");
You have else if(…) /* Do nothing*/; { /* Always open a sell */
You might want to change bool isRetrace = macd0 < 0,
To (to catch more/slow retraces) bool isRetrace = sign0 < 0,
-
Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
Next time, post in the correct place. The moderators will likely move this thread there soon. -
Are your books one column but two feet wide? No because that is unreadable. They are 6 inches, sometimes two columns, so you can read it easily. So should be your code.
Unreadable Better Self-documenting You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.
-
You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.
-
Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
-
Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 -
The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).
-
-
Don't hard code constants. 0.0020 and 0.0060 fail on JPY pairs and metals. 200*_Point, and 600*_Point works on majors and JPY. 20*PIP, and 60*PIP works everywhere, if you compute PIP correctly.
PIP, Point, or Tick are all different in general.
What is a TICK? - MQL4 programming forum (2014)Unless you manually adjust your SL/TP for each separate symbol, using Point means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points), and metals. Compute what a PIP is and use it, not points.
How to manage JPY pairs with parameters? - MQL4 programming forum (2017)
Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum (2018) -
Be careful with NULL.
- On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
- Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
- Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
- MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
- Cloud Protector Bug? - MQL4 programming forum (2020)
-
Of course it does. You have You might want to change To (to catch more/slow retraces)
double PipValuePerLot(string pair=""){ return(DeltaValuePerLot(pair) * pips2dbl); }
Problems with a calculation - Symbols - MQL4 and MetaTrader 4 - MQL4 programming forum #2.2

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello guys, can anyone help me with my code. It works fine while testing in demo but while testing in strategy tester it doesnot meet my entry criteris and open trades after one is closed. please help me.