[Need help] iMAonArray not work on EA?

 

Hello everyone,

I am new to MQL4 programming. I have copied an indicator about Stochatis Smooth - LSMA (using iMAonArray), and the chart displays the calculated information correctly.

However, when I convert it to an EA, there are no errors during compilation, but the EA does not run.

I hope you can check and help me find the wrong at this code.

I have also searched for articles on the same topic, but the solutions are not suitable, or maybe I did not understand (my English is poor) the method of these topic on 4rum.

(I'm still reading Topic by Nikolay Kositsin to find the method convert Indecaotor to EA)


ArraySetAsSeries(sto, true); 
ArrayResize(sto, KPeriod+DPeriod+Slowing);

This my EA code - Stochatic smooth (when change LSMA2 > LSMA 1 -> Sell; when LSMA1>LSMA2 -> Buy)

//+------------------------------------------------------------------+
//|                                              EA_lsma_editing.mq4 |
//|                                                           Mr.Sun |
//|                                                                  |
//+------------------------------------------------------------------+
#property strict


//---- base parameters:
input int                  KPeriod    = 15;   // Stochastic K period (9-26)
input int                  DPeriod    = 3;    // Stochastic D period (3-12)
input int                  Slowing    = 5;    // Stochastic slowing (3-6)
input ENUM_MA_METHOD       MAMethod   = MODE_LWMA; // Stochastic ma method
input ENUM_STO_PRICE       PriceField = STO_LOWHIGH; // Stochastic price   
input int                  LSMAPeriod = 26;   // Smoothing period (9-52)


double lsma_sto[], sto[];
bool     Sell_lsma;
bool     Buy_lsma;


//---- setting for ORDER: 
extern int        pips_SL = 5000;
extern int        pips_TP = 5000;
double lot = 0.01;

int      ticket,i,j;
bool     close,modify;
int      bar_count=0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//Set array sto + lsma
    ArraySetAsSeries(sto, true);
    ArrayResize(sto, KPeriod+DPeriod+Slowing);
    
    ArraySetAsSeries(lsma_sto, true);
    ArrayResize(lsma_sto, LSMAPeriod);
    
   
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//------------------------------------//
//------- LSMA -----------------------//
//------------------------------------//
    int limit = Bars - 1;

    // Tính toán giá trị cho mảng sto
    for(i = KPeriod+DPeriod+Slowing-1; i >= 0; i--)
      {
        sto[i] = iStochastic(NULL, 0, KPeriod, DPeriod, Slowing, MAMethod, PriceField, MODE_MAIN, i);
      }
     
      double lsma_sto1 = 3.0 * iMAOnArray(sto, 0, LSMAPeriod, 0, MODE_LWMA, 1) - 2.0 * iMAOnArray(sto, 0, LSMAPeriod, 0, MODE_SMA, 1);
      double lsma_sto2 = 3.0 * iMAOnArray(sto, 0, LSMAPeriod, 0, MODE_LWMA, 2) - 2.0 * iMAOnArray(sto, 0, LSMAPeriod, 0, MODE_SMA, 2);
   
      
      if (lsma_sto1 > lsma_sto2) 
      {
         Buy_lsma = 1;
         Sell_lsma =0;
      }        
      
      if (lsma_sto1 < lsma_sto2) 
      {
         Buy_lsma  =0; 
         Sell_lsma = 1;

      }


//------------------------------------//
//----------- BUY      ---------------//
//------------------------------------//
   if (Buy_lsma ==1 ) 
   {     
      //---- Close SELL
      for(i=OrdersTotal()-1;i>=0;i--)
      {
         if (OrderSelect(i,SELECT_BY_POS)==true&& OrderSymbol() == Symbol() && OrderType() ==1)
         {
               ticket = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10,clrNONE);
               if (ticket<0&& GetLastError() != 0)
               Alert("Loi close market: ",GetLastError());
         }
      }
   
      //----------------------- BUY -------------------------
      if((Bars - bar_count) >0 )
      {       
         int ID_BUY = OrderSend(NULL,OP_BUY,lot,Ask,10,Ask - pips_SL*Point,Ask + pips_TP*Point);
         bar_count = Bars;
      }
   
   }

//------------------------------------//
//-----  SELL     --------------------//   
//------------------------------------//
   if(Sell_lsma ==1)
   {
      //---- Close BUY 
      for(i=OrdersTotal()-1;i>=0;i--)
      {
         if (OrderSelect(i,SELECT_BY_POS)==true&& OrderSymbol() == Symbol())
         {
            if (OrderType()==0) // Close Buy order
            {
               ticket = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10,clrNONE);
               if (ticket<0&& GetLastError() != 0)
               Alert("Error close market: ",GetLastError());
            }
   
         }
      }
         
      //---------------  SELL -----------------------
      if((Bars - bar_count) > 0)
      {  
         int ID_SELL = OrderSend(NULL,OP_SELL,lot,Bid,10,Bid + pips_SL*Point,Bid - pips_TP*Point);
         bar_count = Bars;
      }
   }

}



How to Order a Trading Robot in MQL5 and MQL4
How to Order a Trading Robot in MQL5 and MQL4
  • www.mql5.com
"Freelance" is the largest freelance service for ordering MQL4/MQL5 trading robots and technical indicators. Hundreds of professional developers are ready to develop a custom trading application for the MetaTrader 4/5 terminal.
Files:
 

The chart same like this:


 
"converting an indicator to an EA" is one of the most useless thing ever heard.

Create the EA from scratch, declare your indicator as resource and use iCustom.