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

 

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.

 
What code?
 
 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");
      }
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2022.08.07
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Eleni Anna Branou #:
What code?

Thank You so much for your concern. i am really having trouble in backtesting. it works fine when testion live on demo.

 
aviyadav321 #:

Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

 
  1. 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.

  2. 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.


  3.          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.

    1. 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?

    2. 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

    3. 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).

  4. 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)


  5. int openOrderID = OrderSend(NULL,OP_BUY,lotSize,Ask,10,stopLossMACDbuy,takeProfitMACDbuy,NULL,magicNB);
    

    Be careful with NULL.

    1. 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.
    2. 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.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020)

  6. 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 */
    
  7. You might want to change
    bool   isRetrace     = macd0 < 0,
    To (to catch more/slow retraces)
    bool   isRetrace     = sign0 < 0,

 
  Thank you so much William. This was a lot helpful. i am just wondering how do I fix the problem of always opening a trade. What should i use if i don't use else if. Thank You in advance.
 
aviyadav321 #: What should i use if i don't use else if. Thank You in advance.

You completely missed the point. Look at №6 again, closer. Drop the semicolon, and then you have else if(…) { open }

 
William Roeder #:

You completely missed the point. Look at №6 again, closer. Drop the semicolon, and then you have else if(…) { open }

OMG, such a small mistake and i couldn't figure it out last 2 days. Thank You so much William. Can we be friends?, i have sent you request.
 
William Roeder #:
  1. 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.

  2. 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.


  3. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. 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?

    2. 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

    3. 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).

  4. 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)


  5. Be careful with NULL.

    1. 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.
    2. 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.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020)

  6. Of course it does.
    You have
  7. You might want to change
    To (to catch more/slow retraces)

Hello! 
Followed your above links. What is pips2dbl? 
double  PipValuePerLot(string pair=""){ return(DeltaValuePerLot(pair) * pips2dbl); }
Reason: