Why is this EA not working properly?

 

I am new to coding and expert advisors.

I was following along with a YouTube video just to learn, this EA is supposed to open a position every time there is an SMA cross for a quick scalp.

I noticed many times there are many times the SMAs cross but no position is opened.

Any ideas why?

#include<Trade\Trade.mqh>
CTrade trade;
void OnTick()
   {
   string entry ="";
   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   double myMovingAverageArray9[],myMovingAverageArray14[];
   
   int MAdef1 = iMA (_Symbol,_Period,9,0,MODE_SMA,PRICE_CLOSE);
   int MAdef2 = iMA (_Symbol,_Period,14,0,MODE_SMA,PRICE_CLOSE);
   
   ArraySetAsSeries(myMovingAverageArray9,true);
   ArraySetAsSeries(myMovingAverageArray14,true);
   
   CopyBuffer(MAdef1,0,0,3,myMovingAverageArray9);
   CopyBuffer(MAdef2,0,0,3,myMovingAverageArray14);
   
   if (
      (myMovingAverageArray9[0]>myMovingAverageArray14[0])
      && (myMovingAverageArray9[1]<myMovingAverageArray14[1])
      )
      {
      entry="buy";
      }
      
    if (
      (myMovingAverageArray9[0]<myMovingAverageArray14[0])
      && (myMovingAverageArray9[1]>myMovingAverageArray14[1])
      )
      {
      entry="sell";
      }
      
     if (entry=="sell" && PositionsTotal()<1)
     trade.Sell(1,NULL,Bid,(Bid-100 * _Point),(Bid-100 * _Point),NULL);
     
     if (entry=="buy" && PositionsTotal()<1)
     trade.Buy(1,NULL,Ask,(Ask-100 * _Point),(Ask+100 * _Point),NULL);
   }
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Marcel:

I am new to coding and expert advisors.

I was following along with a YouTube video just to learn, this EA is supposed to open a position every time there is an SMA cross for a quick scalp.

I noticed many times there are many times the SMAs cross but no position is opened.


Any ideas why?

Check your Journal and Expert tab. There might be some errors to give you clues as to reason. But 1 guess could be that your prices need to be normalised, or that your tp and sl might not be far enuf away from open price, ie you are using bid price for both open and close prices, but you need to use ask price for opening buy trades, and to close buy trades, you use bid. To open sell trades, you use bid, and to close sell trades, ask price.

   int MAdef1 = iMA (_Symbol,_Period,9,0,MODE_SMA,PRICE_CLOSE);

   

   int MAdef2 = iMA (_Symbol,_Period,14,0,MODE_SMA,PRICE_CLOSE);

These lines go into OnInit and remove the int at beginning, instead put 

int MAdef1, MAdef2;

on new line below CTrade