How do I fix this code so that the EA can recognize whether price is above or below the 200 EMA?

132 Views Asked by At

I coded this EA based on the Squeeze Momentum Strategy. Its only supposed to take buy trades when the price is above the 200 EMA and the Bollinger Bands are outside the Keltner Channel and vice versa for sell trades but the EA still takes buy trades when price is below the 200 EMA which its not supposed to do.

Here is the entire code:

//+------------------------------------------------------------------+
//|                                  TrueSqueezeMomentumStrategy.mq5 |
//|                           Copyright 2023, Sauma Galactic Capital |
//|                                    https://www.saumagalactic.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Sauma Galactic Capital"
#property link      "https://www.saumagalactic.com"
#property version   "1.00"

//+------------------------------------------------------------------+
//| Include                                                          |
//+------------------------------------------------------------------+
#include <Trade\trade.mqh>
#include <Math\Stat\Math.mqh>
//+------------------------------------------------------------------+
//| Inputs                                                                 |
//+------------------------------------------------------------------+
static input long    InputMagicNumber = 00001;     //Magic Number For EA
static input double  InputLotSize = 0.01;          //Lot Size
input int            InputBars = 20;               //Number Of Bars For High/Low Range
input int            InputStopLoss = 200;          //Stop Loss In Points (0=off)
input int            InputTakeProfit = 0;          //Take Profit In Points (0=off)  
input string         TimeStart = "08:00";          //Start Time As A String
input string         TimeEnd = "15:00";            //End Time As A String

//+------------------------------------------------------------------+
//| Global Variables                                                 |
//+------------------------------------------------------------------+
CTrade trade;                                      //CTrade Instance

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{  

   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   //Calculate Local CPU Time Data
   datetime LocalTime = TimeLocal();                                          //Calculating The Local Time Zone
   
   //Convert Local CPU Time Data To Show The Hours & Mintues In String Format
   string HoursAndMinutes = TimeToString(LocalTime,TIME_MINUTES);             //Converting The DateTime Time Data Into A String
   
   //Convert Local CPU Time Data To Show The Date In String Format
   string Date = TimeToString(LocalTime,TIME_DATE);                           //Converting The DateTime Date Data Into A tring
   
   //Create A Structure To Put The Time Data In 
   MqlDateTime DateTimeStructure;                                             //Structure To Hold The Time Data
   
   //Put Time Data Into The Structure
   TimeToStruct(LocalTime,DateTimeStructure);                                 //Storing The Time Data In The Structure
   
   //Calculate The Day Of The Week From The Date Time Structure
   int DayOfTheWeek = DateTimeStructure.day_of_week;
   
   //Convert Day Of Week Integer Data Into Text
   string DayOfWeek = "";
   
   if (DayOfTheWeek == 1) DayOfWeek = "Monday";
   if (DayOfTheWeek == 2) DayOfWeek = "Tuesday";
   if (DayOfTheWeek == 3) DayOfWeek = "Wednesday";
   if (DayOfTheWeek == 4) DayOfWeek = "Thursday";
   if (DayOfTheWeek == 5) DayOfWeek = "Friday";
   if (DayOfTheWeek == 6) DayOfWeek = "Saturday";
   if (DayOfTheWeek == 0) DayOfWeek = "Sunday";
   
   //Create Variables For When The Trading Bot Can Start & End Trading
   datetime StartTrading = StringToTime(TimeStart);
   datetime EndTrading = StringToTime(TimeEnd);
          
   //Create A Boolean To Allow Or Disallow Trading
   bool TradingIsAllowed = ((TimeCurrent() >= StartTrading) && (TimeCurrent() <= EndTrading) && (DayOfTheWeek == 1 || 2 || 3 || 4));        

   if(TradingIsAllowed == true)
      Comment("Today's date is ",Date,"\n"
              "Today is a ",DayOfWeek,"\n"
              "The time is ",HoursAndMinutes,"\n"
              "Trading Is Allowed"
              "Engage & Conquer!!!");
   
   if(TradingIsAllowed == false)
      Comment("Today's date is ",Date,"\n"
              "Today is a ",DayOfWeek,"\n"
              "The time is ",HoursAndMinutes,"\n"
              "Trading Is Not Allowed"
              "Wait For The Right Moment"); 
//+------------------------------------------------------------------+
//| Calculate The Current Ask Price                                  |
//+------------------------------------------------------------------+       
   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
//+------------------------------------------------------------------+
//| Calculate The Current Ask Price                                  |
//+------------------------------------------------------------------+    
   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
              
//+------------------------------------------------------------------+
//| Giving The Expert Advisor The Current Trend Using 200 EMA        |
//+------------------------------------------------------------------+              
   //Create An Array For Prices On The Moving Average
   double myMovingAverageArray[];
   
   //Define The Properties Of The Moving Average Indicator
   int movingAverageDefinition = iMA(_Symbol,PERIOD_CURRENT,200,0,MODE_EMA,PRICE_CLOSE);
   
   //Sort Price Data In The Moving Average Array From Current Candle
   ArraySetAsSeries(myMovingAverageArray, true);
   
   //Fill The Moving Average Array With Price Data
   CopyBuffer(movingAverageDefinition,0,0,3,myMovingAverageArray);
   
   //Calculate The MA For The Current Candle
   double myMovingAverageValue = myMovingAverageArray[0];
   
//+------------------------------------------------------------------+
//| Giving The Expert Advisor The Keltner Channels                   |
//+------------------------------------------------------------------+ 
   //Create An Array For Prices On The Keltner Moving Average
   double myKeltnerMovingAverageArray[];
   
   //Define The Properties Of The Keltner Moving Average Indicator
   int keltnerMovingAverageDefinition = iMA(_Symbol,_Period,14,0,MODE_EMA,PRICE_CLOSE);
   
   //Sort Price Data In The Moving Average Array From Current Candle
   ArraySetAsSeries(myKeltnerMovingAverageArray, true);
   
   //Fill The Keltner Moving Average Array With Price Data
   CopyBuffer(keltnerMovingAverageDefinition,0,0,3,myKeltnerMovingAverageArray);
   
   //Calculate The Keltner MA For The Current Candle
   double myKeltnerMovingAverageValue = myKeltnerMovingAverageArray[0];  
   
   //Calculate The Average True Range
   double myAverageTrueRangeArray[];
   
   //Define The Properties Of The Average True Range Indicator
   int AverageTrueRangeDefinition = iATR(_Symbol,_Period,14);
   
   //Sort The ATR Array From The Current Candle Downwards
   ArraySetAsSeries(myAverageTrueRangeArray, true);
  
   //Save ATR Price Info In Array
   CopyBuffer(AverageTrueRangeDefinition,0,0,3,myAverageTrueRangeArray);
   
   //Calculate The Current ATR Value
   double AverageTrueRangeValue = NormalizeDouble(myAverageTrueRangeArray[0],5);
   
   //Calculate The Upper & Lower Bands Of The Keltner Channels
   double upperbandKC = myKeltnerMovingAverageValue + (1.5 * AverageTrueRangeValue);
   double lowerbandKC = myKeltnerMovingAverageValue - (1.5 * AverageTrueRangeValue);
   
//+------------------------------------------------------------------+
//| Giving The Expert Advisor The Bollinger Bands                    |
//+------------------------------------------------------------------+ 
   //Create Arrays For Upper & Lower BBs
   double myUpperBBandArray[];
   double myLowerBBandArray[];
   
   //Sort Array Data From Current Candle Downwards
   ArraySetAsSeries(myUpperBBandArray,true);
   ArraySetAsSeries(myLowerBBandArray,true);
   
   //Define Bollinger Band Properties
   int BollingerBandsDefinition = iBands(_Symbol,PERIOD_CURRENT,20,0,2,PRICE_CLOSE);
   
   //Copy Bollinger Band Price Info Into The Two Arrays
   CopyBuffer(BollingerBandsDefinition,1,0,3,myUpperBBandArray);
   CopyBuffer(BollingerBandsDefinition,2,0,3,myLowerBBandArray);
   
   //Calculate The Bollinger Band Price For The Current Candle
   double myUpperBBandValue = myUpperBBandArray[0];
   double myLowerBBandValue = myLowerBBandArray[0];
  
//+------------------------------------------------------------------+
//| Giving The Expert Advisor The Momentum Of The Market             |
//+------------------------------------------------------------------+ 
   //Create An Array For Momentume Price Data
   double myMomentumArray[];
   
   //Sort Momentume Price Data From Current Candle Downwards
   ArraySetAsSeries(myMomentumArray, true);
   
   //Define Properties Of The Momentum Indicator
   int iMomentumDefinition = iMomentum(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE);
   
   //Copy iMomentum Data Into The Array For One Line, The Current Candle, And 14 Candles Down
   CopyBuffer(iMomentumDefinition,0,0,14,myMomentumArray);
   
   //Calculate The Average Momentum Value AKA "The Zero Line" For The Momentum Indicator
   double AverageMomentumValue = MathMean(myMomentumArray);
   
   
   //Calculate Momentum Value For The Current Candle
   double myMomentumValue = myMomentumArray[0];
   
//+------------------------------------------------------------------+
//| How The Expert Advisor Will Take Buy Positions                   |
//+------------------------------------------------------------------+ 
   //Conditions That Must Be Met In Order For The EA To Open A Buy Position
   if((TradingIsAllowed) && (Ask > myMovingAverageValue) && (PositionsTotal() < 1) && (myUpperBBandValue > upperbandKC) && (myLowerBBandValue < lowerbandKC) && (myMomentumValue > 99.92)){
   //Open Buy Position
      trade.Buy(0.10,NULL,Ask,Ask-15*_Point,Ask+30*_Point,NULL);}

//+------------------------------------------------------------------+
//| How The Expert Advisor Will Take Sell Positions                  |
//+------------------------------------------------------------------+       
   //Conditions That Must Be Met In Order For The EA To Open A Sell Position
   if((TradingIsAllowed) && (Bid < myMovingAverageValue) && (PositionsTotal() < 1) && (myUpperBBandValue > upperbandKC) && (myLowerBBandValue < lowerbandKC) && (myMomentumValue < 99.92)){
   //Open Buy Position
      trade.Buy(0.10,NULL,Bid,Bid-15*_Point,Bid+30*_Point,NULL);}   
      
  
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
{

   
}
//+------------------------------------------------------------------+
//| Trade function                                                   |
//+------------------------------------------------------------------+
void OnTrade()

What could be going wrong in the code?

0

There are 0 best solutions below