EA places trades in opposite direction

 
//+------------------------------------------------------------------+
//|                                                         NLT2.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>
//--- input parameters
input int      fastMAPeriod=10;
input int      fastMAShift=1;
input int      slowMAPeriod=21;
input int      slowMAShift=1;
//---
int FastExtHandle=0, SlowExtHandle=0;
CTrade ExtTrade;
//+------------------------------------------------------------------+
//| Check for open position conditions                               |
//+------------------------------------------------------------------+
void CheckForOpen(void)
 {
   MqlRates rt[2];
//--- go trading only for the first few ticks of new bar
   if(CopyRates(_Symbol,PERIOD_CURRENT,0,2,rt)!=2) {
    Print ("CopyRates of ",_Symbol," failed. No history");
    return;}
   if(rt[1].tick_volume>1)
      return;
//--- get fast current moving average
   double fma[2];
   if(CopyBuffer(FastExtHandle,0,0,2,fma)!=2) {
    Print ("CopyBuffer from fast iMA failed, no data");
    return;}
//--- get slow current moving average
   double sma[2];
   if(CopyBuffer(SlowExtHandle,0,0,2,sma)!=2) {
    Print ("CopyBuffer from slow iMA failed, no data");
    return;}
//--- check for signals
   double StateMA1=fma[1]-sma[1], StateMA0=fma[0]-sma[0];
   
   ENUM_ORDER_TYPE signal=WRONG_VALUE;
   
   if(StateMA1<0.0 && StateMA0>=0.0) signal=ORDER_TYPE_BUY;
   else{
    if(StateMA1>0.0 && StateMA0<=0.0) signal=ORDER_TYPE_SELL;}
//--- additional checking
   if(signal!=WRONG_VALUE)
   {
    if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) && Bars(_Symbol,PERIOD_CURRENT)>100)
      ExtTrade.PositionOpen(_Symbol,signal,1.0,
                            SymbolInfoDouble(_Symbol,signal==ORDER_TYPE_SELL ? SYMBOL_BID:SYMBOL_ASK)
                            ,0,0);};
 }
//+------------------------------------------------------------------+
//| Check for close position conditions                              |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Fast Moving Average Indicator
   FastExtHandle=iMA(_Symbol,PERIOD_CURRENT,fastMAPeriod,fastMAShift,MODE_SMA,PRICE_CLOSE);
   if(FastExtHandle==INVALID_HANDLE)
      {
       printf("Error creating Fast MA indicator handle");
       return(INIT_FAILED);
      }
//--- Slow Moving Average Indicator
   SlowExtHandle=iMA(_Symbol,PERIOD_CURRENT,slowMAPeriod,slowMAShift,MODE_SMA,PRICE_CLOSE);
   if(SlowExtHandle==INVALID_HANDLE)
      {
       printf("Error creating Slow MA indicator handle");
       return(INIT_FAILED);
      }
//---ok
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CheckForOpen(); 
  }
//+------------------------------------------------------------------+

pls i will be glad if anyone can help. The EA places a buy order when its suppose to place a sell, and places a sell order when its suppose to place a buy order.

 
udoh.jeremiah.emem:

pls i will be glad if anyone can help. The EA places a buy order when its suppose to place a sell, and places a sell order when its suppose to place a buy order.

So, did you try switching your entry type around so that it match the conditions you wrote? Or switching your conditions around so that they match the entry type?

Remember to look at ArraySetAsSeries(). It'll provide you with the part you're missing. 0 is the latest data in the array, 1 is before that. Which, in your code, 0 is current bar, 1 is previous bar.
 

Code:

//+------------------------------------------------------------------+
//| Check for open position conditions                               |
//+------------------------------------------------------------------+
void CheckForOpen(void)
  {
   MqlRates rt[2];
   ArraySetAsSeries(rt,true);
//--- go trading only for the first few ticks of new bar
   if(CopyRates(_Symbol,PERIOD_CURRENT,0,2,rt)!=2)
     {
      Print("CopyRates of ",_Symbol," failed. No history");
      return;
     }
   if(rt[0].tick_volume>1)
      return;
//--- get fast current moving average
   double fma[2];
   ArraySetAsSeries(fma,true);
   if(CopyBuffer(FastExtHandle,0,0,2,fma)!=2)
     {
      Print("CopyBuffer from fast iMA failed, no data");
      return;
     }
//--- get slow current moving average
   double sma[2];
   ArraySetAsSeries(sma,true);
   if(CopyBuffer(SlowExtHandle,0,0,2,sma)!=2)
     {
      Print("CopyBuffer from slow iMA failed, no data");
      return;
     }
//--- check for signals
   double StateMA1=fma[1]-sma[1], StateMA0=fma[0]-sma[0];
   ENUM_ORDER_TYPE signal=WRONG_VALUE;
   if(StateMA1<0.0 && StateMA0>=0.0)
      signal=ORDER_TYPE_BUY;
   else
     {
      if(StateMA1>0.0 && StateMA0<=0.0)
         signal=ORDER_TYPE_SELL;
     }
//--- additional checking
   if(signal!=WRONG_VALUE)
     {
      if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) && Bars(_Symbol,PERIOD_CURRENT)>100)
         ExtTrade.PositionOpen(_Symbol,signal,1.0,
                               SymbolInfoDouble(_Symbol,signal==ORDER_TYPE_SELL ? SYMBOL_BID:SYMBOL_ASK)
                               ,0,0);
     };
  }
 
Vladimir Karputov:

Code:

ok. thanks vladmir. But the strategy does not place any trades now after correcting my code with yours. I will be glad and appreciate if you help me out

 
udoh.jeremiah.emem :

ok. thanks vladmir. But the strategy does not place any trades now after correcting my code with yours. I will be glad and appreciate if you help me out

To determine the event: "a new bar has appeared", you need to remember and compare the bar time

 
MORE: You shouldn't use a static array - you should use a dynamic array.
 

Corrected code

//+------------------------------------------------------------------+
//|                                                         NLT2.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>
//--- input parameters
input int      fastMAPeriod=10;
input int      fastMAShift=1;
input int      slowMAPeriod=21;
input int      slowMAShift=1;
//---
int FastExtHandle=0, SlowExtHandle=0;
CTrade ExtTrade;
//+------------------------------------------------------------------+
//| Check for open position conditions                               |
//+------------------------------------------------------------------+
void CheckForOpen(void)
  {
   MqlRates rt[];
   ArraySetAsSeries(rt,true);
//--- go trading only for the first few ticks of new bar
   if(CopyRates(_Symbol,PERIOD_CURRENT,0,2,rt)!=2)
     {
      Print("CopyRates of ",_Symbol," failed. No history");
      return;
     }
   if(rt[0].tick_volume>1)
      return;
//--- get fast current moving average
   double fma[];
   ArraySetAsSeries(fma,true);
   if(CopyBuffer(FastExtHandle,0,0,2,fma)!=2)
     {
      Print("CopyBuffer from fast iMA failed, no data");
      return;
     }
//--- get slow current moving average
   double sma[];
   ArraySetAsSeries(sma,true);
   if(CopyBuffer(SlowExtHandle,0,0,2,sma)!=2)
     {
      Print("CopyBuffer from slow iMA failed, no data");
      return;
     }
//--- check for signals
   double StateMA1=fma[1]-sma[1], StateMA0=fma[0]-sma[0];
   ENUM_ORDER_TYPE signal=WRONG_VALUE;
   if(StateMA1<0.0 && StateMA0>=0.0)
      signal=ORDER_TYPE_BUY;
   else
     {
      if(StateMA1>0.0 && StateMA0<=0.0)
         signal=ORDER_TYPE_SELL;
     }
//--- additional checking
   if(signal!=WRONG_VALUE)
     {
      if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) && Bars(_Symbol,PERIOD_CURRENT)>100)
         ExtTrade.PositionOpen(_Symbol,signal,1.0,
                               SymbolInfoDouble(_Symbol,signal==ORDER_TYPE_SELL ? SYMBOL_BID:SYMBOL_ASK)
                               ,0,0);
     };
  }
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Fast Moving Average Indicator
   FastExtHandle=iMA(_Symbol,PERIOD_CURRENT,fastMAPeriod,fastMAShift,MODE_SMA,PRICE_CLOSE);
   if(FastExtHandle==INVALID_HANDLE)
     {
      printf("Error creating Fast MA indicator handle");
      return(INIT_FAILED);
     }
//--- Slow Moving Average Indicator
   SlowExtHandle=iMA(_Symbol,PERIOD_CURRENT,slowMAPeriod,slowMAShift,MODE_SMA,PRICE_CLOSE);
   if(SlowExtHandle==INVALID_HANDLE)
     {
      printf("Error creating Slow MA indicator handle");
      return(INIT_FAILED);
     }
//---ok
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CheckForOpen();
  }
//+------------------------------------------------------------------+

and the result is:


Files:
1.mq5  8 kb
 
Vladimir Karputov:

Corrected code

and the result is:


thanks ***. The code now works as it is inended to. I appreciate man

Reason: