1 (edited by johnskin5 2021-12-31 21:08:59)

Topic: EA working on tester but not on chart

please can anyone help me out. i wrote an EA and it works well in strategy tester but not on chart. please what could be wrong with my code?

//+------------------------------------------------------------------+
//|                                                     MA NEW 3.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict




extern int magic = 1234;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
            double SlowMovingAverage = iMA(NULL,0,7,0,MODE_EMA,PRICE_CLOSE,0);
          
            double FastMovingAverage = iMA(NULL,0,21,0,MODE_SMA,PRICE_CLOSE,0);
            
          
            if (FastMovingAverage > SlowMovingAverage)
            {
               CloseSell();     
               if (OrdersTotal()<1)
               {       
                  openBuy(); 
               } 
            }  

            if (FastMovingAverage < SlowMovingAverage)
            {
               openBuy();
               if (OrdersTotal()<1)
               {     
               openSell(); 
               }
            }   
  }
//+------------------------------------------------------------------+


void openBuy()
   {
      int buyticket = OrderSend(Symbol(),OP_BUY,0.01,Ask,3,Ask-500*Point,0,NULL,magic,0,Green); 
   }
   
void openSell()
   {
      int sellticket = OrderSend(Symbol(),OP_SELL,0.01,Bid,3,Bid+500*Point,0,NULL,magic,0,Red);
   }
 
  void CloseSell()
  {
      for (int i=OrdersTotal()-1; i >=0; i--)
      {
          OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
          
          string CurrencyPair=OrderSymbol();
          
          if(_Symbol==CurrencyPair)
          
          if(OrderType()==OP_SELL)
          {
               // Close the current position
               OrderClose(OrderTicket(),OrderLots(),Ask,3,NULL);
          }
          
      } 
      
  }
  
  
    void CloseBuy()
  {
      for (int i=OrdersTotal()-1; i >=0; i--)
      {
          OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
          
          string CurrencyPair=OrderSymbol();
          
          if(_Symbol==CurrencyPair)
          
          if(OrderType()==OP_BUY)
          {
               OrderClose(OrderTicket(),OrderLots(),Bid,3,NULL);
          }
          
      } 
      
  }

Re: EA working on tester but not on chart

Johnskin,

We discuss Expert Advisors created with Forex Strategy Builder or with Expert Advisor Studio in this forum.

It is better to post your general MQL questions in the MQL forums.

However, you may make a trial account in EA Studio here: https://eas.forexsb.com/ and to generate or make some experts. You may then use them as examples and modify them as per your needs.