Ea dosnt follow rules specified for order open function

 

please for help Ea dosnt follow rules specified for order open,

its not opening trades in regard to correct site of SMMAValue? and dosnt fillter trades by engulfing pattern candle as its stated here: if(IsBearishEngulfing() && MFITrend==-1 && Close[1]<SMMAValue) ?

I dont have a better idea how to specify SMMAValue and Engulfing pattern ..


Thanks For Help !!

//--- input parameters
input int SMAPeriod = 100;           // Period for the Smoothed Moving Average
input int MFIPeriod = 14;            // Period for the MFI indicator
input double OverboughtLevel = 80;   // Overbought level for MFI
input double OversoldLevel = 20;     // Oversold level for MFI
input double RiskPercentage = 2.0;   // Risk percentage of account balance per trade
input int SLIPPAGE = 3;             // Slippage value (adjust as needed)
input int TakeProfitPips = 15;      // Take Profit in pips
input int StopLossPips = 6;         // Stop Loss in pips
//--- global variables
int MFILevel;
int MFITrend;
//--- array for SMMA calculation
double SMMAArray[];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   //--- create the MFI indicator
   MFILevel=iMFI(Symbol(),Period(),MFIPeriod,0);
   //--- initialize the SMMA array
   ArrayResize(SMMAArray, 0);
   //---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   //--- deinitialize and remove any objects if needed
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   //--- calculate MFI value
   double MFIValue=iMFI(Symbol(),Period(),MFIPeriod,0);
   //--- calculate SMMA value
   double SMMAValue=CalculateSMMA(SMAPeriod,PRICE_CLOSE);
   //--- check for MFI conditions (overbought or oversold)
   if(MFIValue>OverboughtLevel)
     {
      MFITrend=-1;  // Overbought
     }
   else if(MFIValue<OversoldLevel)
     {
      MFITrend=1; // Oversold
     }
   else
     {
      MFITrend=0;  // No trend
     }
   //--- calculate the position size based on risk percentage
   double riskAmount=AccountBalance()*RiskPercentage/100.0;
   double lotSize=riskAmount/MarketInfo(Symbol(),MODE_MARGINREQUIRED);
   double takeProfit,stopLoss;
   int ticket=0;  // Define 'ticket' variable outside the conditional blocks
   //--- Check for a bullish engulfing candlestick pattern
   if(IsBullishEngulfing() && MFITrend==1 && Close[1]>SMMAValue)
     {
      // Place a Buy (Long) trade with Take Profit and Stop Loss
      takeProfit=Bid+TakeProfitPips*MarketInfo(Symbol(),MODE_POINT);
      stopLoss=Ask-StopLossPips*MarketInfo(Symbol(),MODE_POINT);
      ticket=OrderSend(Symbol(),OP_BUY,lotSize,Ask,SLIPPAGE,stopLoss,takeProfit,"",0,0,clrNONE);
     }
   //--- Check for a bearish engulfing candlestick pattern
   if(IsBearishEngulfing() && MFITrend==-1 && Close[1]<SMMAValue)
     {
      // Place a Sell (Short) trade with Take Profit and Stop Loss
      takeProfit=Ask-TakeProfitPips*MarketInfo(Symbol(),MODE_POINT);
      stopLoss=Bid+StopLossPips*MarketInfo(Symbol(),MODE_POINT);
      ticket=OrderSend(Symbol(),OP_SELL,lotSize,Bid,SLIPPAGE,stopLoss,takeProfit,"",0,0,clrNONE);
     }
   if(ticket>0)
     {
      Print("Order placed successfully. Ticket: ",ticket);
     }
   else if(ticket==-1)
     {
      int error_code=GetLastError();
      Print("Error placing order. Error code: ",error_code);
     }
  }
//+------------------------------------------------------------------+
//| Function to check for a bullish engulfing candlestick pattern     |
//+------------------------------------------------------------------+
bool IsBullishEngulfing()
  {
   double currentOpen=iOpen(Symbol(),Period(),0);
   double currentClose=iClose(Symbol(),Period(),0);
   double previousOpen=iOpen(Symbol(),Period(),1);
   double previousClose=iClose(Symbol(),Period(),1);
   //--- Check for a bullish engulfing pattern
   if(currentOpen<previousOpen && currentClose>previousClose && currentClose>currentOpen && previousOpen>previousClose)
     {
      return(true);
     }
   return(false);
  }
//+------------------------------------------------------------------+
//| Function to check for a bearish engulfing candlestick pattern     |
//+------------------------------------------------------------------+
bool IsBearishEngulfing()
  {
   double currentOpen=iOpen(Symbol(),Period(),0);
   double currentClose=iClose(Symbol(),Period(),0);
   double previousOpen=iOpen(Symbol(),Period(),1);
   double previousClose=iClose(Symbol(),Period(),1);
   //--- Check for a bearish engulfing pattern
   if(currentOpen>previousOpen && currentClose<previousClose && currentClose<currentOpen && previousOpen<previousClose)
     {
      return(true);
     }
   return(false);
  }
//+------------------------------------------------------------------+
//| Function to calculate Smoothed Moving Average (SMMA)              |
//+------------------------------------------------------------------+
double CalculateSMMA(int period, int applied_price)
{
   double alpha = 2.0 / (period + 1);
   double smma = 0.0;
   for (int i = 0; i < period; i++)
   {
      smma += iMA(Symbol(), Period(), period, 0, MODE_SMA, applied_price, i);
   }
   smma /= period;

   for (int i = period; i < Bars; i++)
   {
      smma = (1 - alpha) * smma + alpha * iMA(Symbol(), Period(), period, 0, MODE_SMA, applied_price, i);
   }

   return smma;
}
 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
  1.    double MFIValue=iMFI(Symbol(),Period(),MFIPeriod,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.


  2.       takeProfit=Bid+TakeProfitPips*MarketInfo(Symbol(),MODE_POINT);
          stopLoss=Ask-StopLossPips*MarketInfo(Symbol(),MODE_POINT);
          ticket=OrderSend(Symbol(),OP_BUY,lotSize,Ask,SLIPPAGE,stopLoss,takeProfit,"",0,0,clrNONE);
    

    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 at 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, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  3. Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5 / MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014)

  4. Joseph Fakhoury: I dont have a better idea how to specify SMMAValue and Engulfing pattern .. 
    bool IsBullishEngulfing()
      {
       double currentOpen=iOpen(Symbol(),Period(),0);
       double currentClose=iClose(Symbol(),Period(),0);
       double previousOpen=iOpen(Symbol(),Period(),1);
       double previousClose=iClose(Symbol(),Period(),1);

    You don't have an Engulfing pattern on the forming candle. Only when the candle completes, can you check. Wait for a new bar and check one (1) and two (2).

  5. Trading View's RMA is the same as MT's SMMA. Never any reason to use the SMMA(L) or RMA(L). They are equivalent to the EMA(2L-1).
              The Smoothed Moving Average or SMMA - How to Avoid It - NinjaTrader Programming | futures.io (2019)

    Drop your function (it does not compute SMMA) and use ima.

 
May you please show your correct implementation of those corrections please?
 
Joseph Fakhoury #: May you please show your correct implementation of those corrections please?

We are not here to code things for you, only to guide you in your own research to resolve the issues yourself.

If you wish for someone to code it for you, then please use the Freelance section instead.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2023.10.31
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
  • Usually people who can't code don't receive free help on this forum.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community. Use the CODE button (Alt-S) when inserting code.
  • To learn MQL programming, you can research the many available Articles on the subject, or examples in the Codebase, as well as reference the online Documentation.
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free). However, recommendations or suggestions for Market products are not allowed on the forum, so you will have to do your own research.
  • Finally, you also have the option to hire a programmer in the Freelance section.
 
Ok thanks for replying. I will keep it in my mind.