How to know if a bar cross 200 SMA?

 

Hello All,

I am trying to create an EA in that if a bar cross 200 SMA line in either direction, Then based on cross I want to open a trade.


But I am not able to do that, Kindly check my code;


As you can see the blue line, I want to open a trade only on that time, But I am not able to fix this, Please help.


// Code


#include <Trade/Trade.mqh>
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

input ENUM_TIMEFRAMES Timeframe = PERIOD_CURRENT;
input int MAPeriod = 200; // Moving Average Periods
input ENUM_MA_METHOD MA_MODE = MODE_SMA; // Moving Average Mode
input double Lots = 0.01;
input double TPPips = 4; // Take Profit Pips Value (1 = 1 Pips)
input double SLPips = 2;  // Stop Loss Pips Value (1 = 1 Pips)
input int EnableTrailStop = 0; // Enable Trailing Stop (0=Inactive, 1=Active)
int totalBars;
CTrade trade;


int OnInit(){
   totalBars = iBars(_Symbol, Timeframe);
   Comment("EA Bot Working Fine");
   return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason){

}
void OnTick(){
   int bars = iBars(_Symbol, Timeframe);
   if(totalBars != bars){
      totalBars = bars;
      double fastMAArray[];
      static int handleFastMA = iMA(_Symbol,Timeframe, MAPeriod,0, MA_MODE, PRICE_CLOSE);
      CopyBuffer(handleFastMA,0,0,2, fastMAArray);
      
      
      double slowMAArray[];
      static int handleSlowMA = iMA(_Symbol,Timeframe, MAPeriod, 1, MA_MODE, PRICE_CLOSE);
      CopyBuffer(handleSlowMA, 0,0,2, slowMAArray);
      
      int TotalPositions = PositionsTotal();
      double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      double barClosePrice = iClose(_Symbol, Timeframe,0);
      
    if(TotalPositions<=0){
         if(NormalDouble(fastMAArray[0]) > NormalDouble(slowMAArray[0])){
            Print("Buy Trend\n");
            if(barClosePrice>fastMAArray[0]){
               double sl = ask - SLPips*SymbolInfoDouble(_Symbol, SYMBOL_POINT);
               double tp = ask + TPPips*SymbolInfoDouble(_Symbol, SYMBOL_POINT);
               trade.Buy(Lots, _Symbol, ask, sl, tp, "Cross Over Buy Trade");
            }
            
            
         }
         if(NormalDouble(fastMAArray[0]) < NormalDouble(slowMAArray[0])){
            Print("Sell Trend\n");
            if(barClosePrice<fastMAArray[0]){
               double sl = bid + SLPips*SymbolInfoDouble(_Symbol, SYMBOL_POINT);
               double tp = bid - TPPips*SymbolInfoDouble(_Symbol, SYMBOL_POINT);
               trade.Buy(Lots, _Symbol, ask, sl, tp, "Cross Over Buy Trade");
            }
            
         }
      } 
      
      
      
      Comment("fastMAArray[0] : ", NormalDouble(fastMAArray[0]), 
               "\n\n\nslowMAArray[0] : ", NormalDouble(slowMAArray[0]));
               
   }
   
      
               
}

double NormalDouble (double value){
   return NormalizeDouble(value, _Digits);
}
Files:
issue.PNG  46 kb
 
sk2you:

Hello All,

I am trying to create an EA in that if a bar cross 200 SMA line in either direction, Then based on cross I want to open a trade.


But I am not able to do that, Kindly check my code;


As you can see the blue line, I want to open a trade only on that time, But I am not able to fix this, Please help.


// Code


I cannot understand why you have two SMAs(fast and slow) with different shifts and the same period. As you just need to check the bar ohlc with respect to SMA.

Reason: