Download MetaTrader 5

Slow visual test on MT5 of MQL4 AMA indicator converted to MQL5

To add comments, please log in or register
EUGENE ANDRY
875
EUGENE ANDRY 2013.03.03 02:37 

My problem is <video link deleted>

 All standard indicators run very fast in visual testing mode,

but my converted from mql4 to mql5 ama custom indicator is very slow during visual testing even if I use 1 minute input not ticks

What to do? 

//+------------------------------------------------------------------+
//|                                                          AMA.mq5 |
//|     Converted from AMA.mq4 ("Copyright © 2004, by konKop,wellx") |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, eurgbp"
#property link      "https://www.mql5.com/en/forum/10942"
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 3

#property indicator_color1 Sienna
#property indicator_type1   DRAW_LINE
#property indicator_width1  2

#property indicator_color2 DeepSkyBlue
#property indicator_type2   DRAW_ARROW

#property indicator_color3 Gold
#property indicator_type3   DRAW_ARROW




//---- input parameters
input int periodAMA=20;
input int nfast=3;
input int nslow=30;
input double G=2.0;
input double dK=2.0;

//---- buffers
double kAMAbuffer[];
double kAMAupsig[];
double kAMAdownsig[];

//+------------------------------------------------------------------+

int cbars=0, prevbars=0, prevtime=0;
double slowSC, fastSC;


#property indicator_plots   3

//--- indicator buffers

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

//--- indicator buffers mapping
   SetIndexBuffer(0,kAMAbuffer,INDICATOR_DATA);
   SetIndexBuffer(1,kAMAupsig,INDICATOR_DATA);
   SetIndexBuffer(2,kAMAdownsig,INDICATOR_DATA);
   
   PlotIndexSetInteger(1, PLOT_ARROW, 159);
   PlotIndexSetInteger(2, PLOT_ARROW, 159);
   
   
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
      int i, pos=0;
      double noise=0.000000001, AMA, AMA0, signal, ER;
      double dSC, ERSC, SSC, ddK;
  
      //if (prevbars==rates_total) return (0);
      
      //---- TODO: add your code here
      slowSC=(2.0 /(nslow+1));
      fastSC=(2.0 /(nfast+1));

      //cbars=IndicatorCounted(prev_calculated);
      if(prev_calculated>0) cbars = prev_calculated-1;
      if(prev_calculated==0) cbars = 0;
      cbars=0;
      //Print(cbars);
      
      if (rates_total<=(periodAMA+2)) return (0);
      //---- check for possible errors
      if (cbars<0) return (-1);
      //---- last counted bar will be recounted
      if (cbars>0) cbars--;
      pos=periodAMA+2;
      AMA0=close[pos-1];
      
      while (pos<rates_total)
      {
         if (pos==periodAMA-2) AMA0=close[pos-1];
         signal=MathAbs (close[pos]-close[pos-periodAMA]);
         
         
         noise=0.000000001;
         for (i=0; i< periodAMA; i++)
         {
            noise=noise+MathAbs (close[pos-i]-close[pos-i-1]);
         }
         ER =signal/noise;
         dSC=(fastSC-slowSC);
         ERSC=ER*dSC;
         SSC=ERSC+slowSC;
         AMA=AMA0+(MathPow (SSC, G)*(close[pos]-AMA0));
         kAMAbuffer[pos]=AMA;
   
         ddK=(AMA-AMA0);
         if ((MathAbs (ddK)) > (dK*_Point) &&(ddK > 0)) kAMAupsig[pos] =AMA; else kAMAupsig[pos]=0;
         if ((MathAbs (ddK)) > (dK*_Point) &&(ddK < 0)) kAMAdownsig[pos]=AMA; else kAMAdownsig[pos]=0;
   
   
         AMA0=AMA;
         pos++;
      }
      
      

     
     prevbars=rates_total;
     
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
//---
   
  }
//+------------------------------------------------------------------+

 

//+------------------------------------------------------------------+
//|                                                          AMA.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, by konKop,wellx"
#property link      "http://www.metaquotes.net"
 
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Sienna
#property indicator_color2 DeepSkyBlue
#property indicator_color3 Gold
 
//---- input parameters
extern int       periodAMA=9;
extern int       nfast=2;
extern int       nslow=30;
extern double    G=2.0;
extern double    dK=2.0; 
 
//---- buffers
double kAMAbuffer[];
double kAMAupsig[];
double kAMAdownsig[];
 
//+------------------------------------------------------------------+
 
int    cbars=0, prevbars=0, prevtime=0;
 
double slowSC,fastSC;
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE,0,2);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,159);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,159);
   //SetIndexDrawBegin(0,nslow+nfast);
   SetIndexBuffer(0,kAMAbuffer);
   SetIndexBuffer(1,kAMAupsig);
   SetIndexBuffer(2,kAMAdownsig);
   
   
   IndicatorDigits(4);
   
   //slowSC=0.064516;
   //fastSC=0.2;
   //cbars=IndicatorCounted();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    i,pos=0;
   double noise=0.000000001,AMA,AMA0,signal,ER;
   double dSC,ERSC,SSC,ddK;
   
   if (prevbars==Bars) return(0);
    
//---- TODO: add your code here
   slowSC=(2.0 /(nslow+1));
   fastSC=(2.0 /(nfast+1));
   cbars=IndicatorCounted();
   if (Bars<=(periodAMA+2)) return(0);
//---- check for possible errors
   if (cbars<0) return(-1);
//---- last counted bar will be recounted
   if (cbars>0) cbars--;
   pos=Bars-periodAMA-2;
   AMA0=Close[pos+1];
   while (pos>=0)
     {
      if(pos==Bars-periodAMA-2) AMA0=Close[pos+1];
      signal=MathAbs(Close[pos]-Close[pos+periodAMA]);
      noise=0.000000001;
      for(i=0;i<periodAMA;i++)
       {
        noise=noise+MathAbs(Close[pos+i]-Close[pos+i+1]);
       }
      ER =signal/noise;
      dSC=(fastSC-slowSC);
      ERSC=ER*dSC;
      SSC=ERSC+slowSC;
      AMA=AMA0+(MathPow(SSC,G)*(Close[pos]-AMA0));
      kAMAbuffer[pos]=AMA;
 
      ddK=(AMA-AMA0);
      if ((MathAbs(ddK)) > (dK*Point) && (ddK > 0)) kAMAupsig[pos] =AMA; else kAMAupsig[pos]=0;
      if ((MathAbs(ddK)) > (dK*Point) && (ddK < 0)) kAMAdownsig[pos]=AMA; else kAMAdownsig[pos]=0; 
 
     
      AMA0=AMA;
      pos--;
     }
//----
   prevbars=Bars;
   return(0);
  }
Step on New Rails: Custom Indicators in MQL5
Step on New Rails: Custom Indicators in MQL5
  • 2009.11.23
  • Андрей
  • www.mql5.com
I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.
phi nuts
2184
phi nuts 2013.03.04 10:00  
eurgbp:

My problem is here http://imagicon.info/howto/mql5%20slow%20test%20ama.htm

 All standard indicators run very fast in visual testing mode,

but my converted from mql4 to mql5 ama custom indicator is very slow during visual testing even if I use 1 minute input not ticks

What to do? 

I haven't study the entire code, but I guess this is the problem

 if (cbars>0) cbars--;
      pos=periodAMA+2;    //<<-- pos is always equal to periodAMA+2
      AMA0=close[pos-1];
      
 while (pos<rates_total)   //<<-- on every tick, the indicator have to re-calculate all over again
     {

     ...
      
     pos ++;
     }

I gonna delete your video link later, I hope you don't mind.

Pasi Hakamaki
4966
Pasi Hakamaki 2013.03.05 09:32  

I will try to help you soon as I get into the TRAIN :) Like phi.nuts said the problem is likely in the loop.

Br, Candles


Pasi Hakamaki
4966
Pasi Hakamaki 2013.03.05 21:28  
Candles:

I will try to help you soon as I get into the TRAIN :) Like phi.nuts said the problem is likely in the loop.

Br, Candles


I think I got it to work faster now. The below code seems ok:

If it does not work or has bugs report to me, thx :)

Br, Candles

//+------------------------------------------------------------------+
//|                                                          AMA.mq5 |
//|     Converted from AMA.mq4 ("Copyright © 2004, by konKop,wellx") |
//|                                                                  |
//+------------------------------------------------------------------+
//#property copyright "Copyright 2013, eurgbp"
//#property link      "https://www.mql5.com/en/forum/10942"
#property version   "1.00"

//+------------------------------------------------------------------+
//|                                                          AMA.mq5 |
//|                                             rewritten by Candles |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, Candles"
#property link      "https://www.mql5.com/en/forum/10942#comment_442248"
#property version   "1.00"


#property indicator_chart_window
#property indicator_buffers 3

#property indicator_color1 Sienna
#property indicator_type1   DRAW_LINE
#property indicator_width1  2

#property indicator_color2 DeepSkyBlue
#property indicator_type2   DRAW_ARROW

#property indicator_color3 Gold
#property indicator_type3   DRAW_ARROW




//---- input parameters
input int periodAMA=20;
input int nfast=3;
input int nslow=30;
input double G=2.0;
input double dK=2.0;

//---- buffers
double kAMAbuffer[];
double kAMAupsig[];
double kAMAdownsig[];

//+------------------------------------------------------------------+

int cbars=0,prevbars=0,prevtime=0;
double slowSC,fastSC;

#property indicator_plots   3
//--- indicator buffers

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

//--- indicator buffers mapping
   SetIndexBuffer(0,kAMAbuffer,INDICATOR_DATA);
   SetIndexBuffer(1,kAMAupsig,INDICATOR_DATA);
   SetIndexBuffer(2,kAMAdownsig,INDICATOR_DATA);

   PlotIndexSetInteger(1,PLOT_ARROW,159);
   PlotIndexSetInteger(2,PLOT_ARROW,159);

//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int i,pos=0;
   double noise=0.000000001,AMA,AMA0,signal,ER;
   double dSC,ERSC,SSC,ddK;

//if (prevbars==rates_total) return (0);

//---- TODO: add your code here
   slowSC=(2.0 /(nslow+1));
   fastSC=(2.0 /(nfast+1));

//cbars=IndicatorCounted(prev_calculated);
   if(prev_calculated>0) cbars=prev_calculated-1;
   if(prev_calculated==0) cbars=0;
   cbars=0;
//Print(cbars);

   if(rates_total<=(periodAMA+2)) return(0);
//---- check for possible errors
   if(cbars<0) return(-1);
//---- last counted bar will be recounted
   if(cbars>0) cbars--;
   pos=periodAMA+2;
   AMA0=close[pos-1];
   if(rates_total>prev_calculated)
     {
      while(pos<rates_total)
        {
         if(pos==periodAMA-2) AMA0=close[pos-1];
         signal=MathAbs (close[pos]-close[pos-periodAMA]);


         noise=0.000000001;
         for(i=0; i< periodAMA; i++)
           {
            noise=noise+MathAbs(close[pos-i]-close[pos-i-1]);
           }
         ER =signal/noise;
         dSC=(fastSC-slowSC);
         ERSC=ER*dSC;
         SSC=ERSC+slowSC;
         AMA=AMA0+(MathPow (SSC, G)*(close[pos]-AMA0));
         kAMAbuffer[pos]=AMA;

         ddK=(AMA-AMA0);
         if((MathAbs(ddK))>(dK*_Point)  && (ddK>0)) kAMAupsig[pos]=AMA; else kAMAupsig[pos]=0;
         if((MathAbs(ddK))>(dK*_Point) &&(ddK<0)) kAMAdownsig[pos]=AMA; else kAMAdownsig[pos]=0;


         AMA0=AMA;
         pos++;


        }
     }



   prevbars=rates_total;

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//---

  }
//+------------------------------------------------------------------+

 

phi nuts
2184
phi nuts 2013.03.06 02:16  

You know guys, there's AMA in MetaEditor. All we have to do is add a dots in it.

;D 

Pasi Hakamaki
4966
Pasi Hakamaki 2013.03.06 07:55  
phi.nuts:

You know guys, there's AMA in MetaEditor. All we have to do is add a dots in it.

;D 

Lol, we do? I did not know that :)
phi nuts
2184
phi nuts 2013.03.06 09:26  
Candles:
Lol, we do? I did not know that :)

I did not know about that either until this morning when I checked several codes from forum in MetaEditor, and ... it's there.

Its just ... there.

;D 

To add comments, please log in or register