Conversion of “AMA filtered” from MT5

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #225603 quote
    wroxwrox
    Participant
    Average

    Goodnight. Would anyone be willing to convert the following code please?



    // https://www.mql5.com/en/code/23756


    // https://forex-station.com/viewtopic.php?p=1295468075#p1295468075


    //——————————————————————


    #property copyright  “© mladen, 2018”


    #property link    “mladenfx@gmail.com”


    #property description “AMA filtered”


    //+——————————————————————


    #property indicator_chart_window


    #property indicator_buffers 3


    #property indicator_plots  1


    #property indicator_label1 “AMA”


    #property indicator_type1  DRAW_COLOR_LINE


    #property indicator_color1 clrDarkGray,clrMediumSeaGreen,clrOrangeRed


    #property indicator_width1 2



    //


    //


    //



    input int        inpPeriod   = 17;     // Period


    input int        inpFastPeriod = 3;     // Fast end period


    input int        inpSlowPeriod = 34;     // Slow end period


    input double       inpFilter   = 5;      // Filter size (<=0 for no filter)


    input ENUM_APPLIED_PRICE inpPrice   = PRICE_CLOSE; // Price



    //


    //—


    //


    //Forex-Station copy & paste code; Button code start 11


    input string       button_note1     = “——————————“;


    input int        btn_Subwindow     = 0;         // What window to put the button on


    input ENUM_BASE_CORNER  btn_corner      = CORNER_LEFT_UPPER; // button corner on chart for anchoring


    input string       btn_text       = “AMA”;       // a button name


    input string       btn_Font       = “Arial”;      // button font name


    input int        btn_FontSize     = 9;         // button font size


    input color       btn_text_ON_color   = clrLime;      // ON color when the button is turned on


    input color       btn_text_OFF_color  = clrRed;      // OFF color when the button is turned off


    input color       btn_background_color = clrDimGray;    // background color of the button


    input color       btn_border_color   = clrBlack;     // border color the button


    input int        button_x       = 20;        // x coordinate of the button


    input int        button_y       = 25;        // y coordinate of the button


    input int        btn_Width       = 80;        // button width


    input int        btn_Height      = 20;        // button height


    input string       soundBT        = “tick.wav”;    // sound file when the button is pressed


    input string       UniqueButtonID    = “AMAfiltered”;  // Unique ID for each button


    input string       button_note2     = “——————————“;



    bool show_data = true, recalc = true;


    string IndicatorName, IndicatorObjPrefix, buttonId;


    //Forex-Station copy & paste code; Button code end 11



    double val[],valc[],ama[],g_fastEnd,g_slowEnd; int g_hlPeriod;


    //+——————————————————————+


    //Forex-Station copy & paste code; Button code start 12


    string GenerateIndicatorName(const string target)


      {


      string name = target;


      int try   = 2;


      while(ChartWindowFind(0, name) != -1)


        {


        name = target + ” #” + IntegerToString(try++);


        }


      return name;


      }


    //+——————————————————————+


    int OnInit(void)


      {


      IndicatorName = GenerateIndicatorName(btn_text);


      IndicatorObjPrefix = “__” + IndicatorName + “__”;


      IndicatorSetString(INDICATOR_SHORTNAME, IndicatorName);


      IndicatorSetInteger(INDICATOR_DIGITS, _Digits);



      double ForexVal;


      if(GlobalVariableGet(IndicatorName + “_visibility”, ForexVal))


        show_data = ForexVal != 0;



      ChartSetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, 1);


      buttonId = IndicatorObjPrefix + UniqueButtonID + btn_text;


      createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color);


      ObjectSetInteger(ChartID(), buttonId, OBJPROP_YDISTANCE, button_y);


      ObjectSetInteger(ChartID(), buttonId, OBJPROP_XDISTANCE, button_x);



      Init2();


      return(INIT_SUCCEEDED);


      }


    //+——————————————————————+


    void OnDeinit(const int reason) {


       ObjectsDeleteAll(ChartID(), IndicatorObjPrefix, -1, -1);


    }


    //+——————————————————————+


    void createButton(string buttonID, string buttonText, int width, int height, string font, int fontSize, color bgColor, color borderColor, color txtColor)


      {


      ObjectDelete  (ChartID(), buttonID);


      ObjectCreate  (ChartID(), buttonID, OBJ_BUTTON, btn_Subwindow, 0, 0);


      ObjectSetInteger(ChartID(), buttonID, OBJPROP_COLOR, txtColor);


      ObjectSetInteger(ChartID(), buttonID, OBJPROP_BGCOLOR, bgColor);


      ObjectSetInteger(ChartID(), buttonID, OBJPROP_BORDER_COLOR, borderColor);


      ObjectSetInteger(ChartID(), buttonID, OBJPROP_BORDER_TYPE, BORDER_RAISED);


      ObjectSetInteger(ChartID(), buttonID, OBJPROP_XSIZE, width);


      ObjectSetInteger(ChartID(), buttonID, OBJPROP_YSIZE, height);


      ObjectSetString (ChartID(), buttonID, OBJPROP_FONT, font);


      ObjectSetString (ChartID(), buttonID, OBJPROP_TEXT, buttonText);


      ObjectSetInteger(ChartID(), buttonID, OBJPROP_FONTSIZE, fontSize);


      ObjectSetInteger(ChartID(), buttonID, OBJPROP_SELECTABLE, 0);


      ObjectSetInteger(ChartID(), buttonID, OBJPROP_CORNER, btn_corner);


      ObjectSetInteger(ChartID(), buttonID, OBJPROP_HIDDEN, 1);


      ObjectSetInteger(ChartID(), buttonID, OBJPROP_XDISTANCE, 9999);


      ObjectSetInteger(ChartID(), buttonID, OBJPROP_YDISTANCE, 9999);


      }


    //+——————————————————————————————————————+


    void handleButtonClicks()


      {


      if(ObjectGetInteger(ChartID(), buttonId, OBJPROP_STATE))


        {


        ObjectSetInteger(ChartID(), buttonId, OBJPROP_STATE, false);


        show_data = !show_data;


        GlobalVariableSet(IndicatorName + “_visibility”, show_data ? 1.0 : 0.0);


        recalc = true;


        }


      }


    //+——————————————————————+


    void OnChartEvent(const int id,


             const long &lparam,


             const double &dparam,


             const string &sparam)


      {


      handleButtonClicks();


      bool ForexStation = ObjectGetInteger(ChartID(),sparam,OBJPROP_TYPE);


      if (id==CHARTEVENT_OBJECT_CLICK && ForexStation==OBJ_BUTTON)


      {


       if (soundBT!=””) PlaySound(soundBT);


      }



      if (show_data)


        {


        ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);


        PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_COLOR_LINE);


        }


      else


        {


        ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);


        PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_NONE);


        }


      }


    //+——————————————————————+


    //Forex-Station copy & paste code; Button code end 12



    //——————————————————————


    // Custom indicator initialization function


    //——————————————————————


    //


    //


    //



    int Init2()


    {


      //


      //— indicator buffers mapping


      //


         SetIndexBuffer(0,val ,INDICATOR_DATA);


         SetIndexBuffer(1,valc,INDICATOR_COLOR_INDEX);


         SetIndexBuffer(2,ama ,INDICATOR_CALCULATIONS);


          g_fastEnd = 2.0/(inpFastPeriod+1.0);


          g_slowEnd = 2.0/(inpSlowPeriod+1.0);


          g_hlPeriod = inpPeriod+1;


            iFilter.init(inpPeriod,inpFilter);


      return (INIT_SUCCEEDED);


    }



    //——————————————————————


    // Custom indicator iteration function


    //——————————————————————


    //


    //—


    //



    #define _setPrice(_priceType,_target,_index) \


      { \


      switch(_priceType) \


      { \


       case PRICE_CLOSE:  _target = close[_index];                       break; \


       case PRICE_OPEN:   _target = open[_index];                        break; \


       case PRICE_HIGH:   _target = high[_index];                        break; \


       case PRICE_LOW:   _target = low[_index];                        break; \


       case PRICE_MEDIAN:  _target = (high[_index]+low[_index])/2.0;               break; \


       case PRICE_TYPICAL: _target = (high[_index]+low[_index]+close[_index])/3.0;        break; \


       case PRICE_WEIGHTED: _target = (high[_index]+low[_index]+close[_index]+close[_index])/4.0; break; \


       default : _target = 0; \


      }}



    //


    //—


    //



    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[])


    {


      static int  prev_i=-1;


      static double prev_max,prev_min;



      //


      //


      //



      int i= prev_calculated-1; if (i<0) i=0; for (; i<rates_total && !_StopFlag; i++)


      {


       double _price; _setPrice(inpPrice,_price,i);


       if (prev_i!=i)


         {


          prev_i = i;


          int start  = i-g_hlPeriod+1; if (start<0) start=0;


            prev_max = high[ArrayMaximum(high,start,g_hlPeriod-1)];


            prev_min = low [ArrayMinimum(low ,start,g_hlPeriod-1)];


         }


         double max = (high[i] > prev_max) ? high[i] : prev_max;


         double min = (low[i] < prev_min) ? low[i] : prev_min;



         //


         //—


         //



         double mltp = (max!=min) ? ((_price-min)-(max-_price))/(max-min) : 1; if (mltp<0) mltp *= -1;


         double ssc = mltp * ( g_fastEnd-g_slowEnd) + g_slowEnd;



         //


         //—


         //



       ama[i] = (i>0) ? ama[i-1]+(ssc*ssc)*(_price-ama[i-1]) : _price;


       val[i] = iFilter.calculate(ama[i],i);


       valc[i]=(i>0) ?(val[i]>val[i-1]) ? 1 :(val[i]<val[i-1]) ? 2 : valc[i-1]: 0;


      }


      return(i);


    }


    //——————————————————————


    // Custom function(s)


    //——————————————————————


    //


    //—


    //



    class CFilter


    {


      private :


       int  m_period;


       int  m_arraySize;


       double m_filter;


       struct sFilterArrayStruct


         {


          double value;


          double change;


          double power;


          double summc;


          double summp;


         };


       sFilterArrayStruct m_array[];



      public :


       CFilter() { init(1,0); }


       ~CFilter() { ArrayFree(m_array); }



       ///


       ///


       ///



       bool init (int period, double filter)


       {


         m_period  = (period>1) ? period : 1;


         m_filter  = (filter>=0) ? filter : 0;


         m_arraySize = m_period + 32;


            if (ArrayResize(m_array,m_arraySize)!=m_arraySize) return(false);


                                     return(true);


       }



       double calculate(double value, int i)


       {


         int _indC = (i )%m_arraySize;


         int _indP = (i-1)%m_arraySize;



          //


          //


          //



          m_array[_indC].value = value; double _change = (i>0) ? m_array[_indC].value-m_array[_indP].value : 0;


          m_array[_indC].change = (_change>0) ? _change : – _change;


          if (i>m_period)


          {


            int _indF = (i-m_period)%m_arraySize;


             #define _power(_val) ((_val)*(_val))


               m_array[_indC].summc = m_array[_indP].summc +m_array[_indC].change-m_array[_indF].change;


               m_array[_indC].power = _power(m_array[_indC].change-m_array[_indC].summc/(double)m_period);


               m_array[_indC].summp = m_array[_indP].summp+m_array[_indC].power-m_array[_indF].power;


          }


          else


          {


               m_array[_indC].summc =


               m_array[_indC].summp = 0;


               for(int k=0; k<m_period && i>=k; k++) m_array[_indC].summc += m_array[_indC-k].change;


                                  m_array[_indC].power = _power(m_array[_indC].change-m_array[_indC].summc/(double)m_period);


               for(int k=0; k<m_period && i>=k; k++) m_array[_indC].summp += m_array[_indC-k].power;


             #undef _power


          }


          if (i>0 && m_filter>0 && m_array[_indC].change<m_filter*MathSqrt(m_array[_indC].summp/(double)m_period)) m_array[_indC].value=m_array[_indP].value;


         return (m_array[_indC].value);


       }


    };


    CFilter iFilter;


    //——————————————————————

    AMAFiltered.png AMAFiltered.png
    #258947 quote
    Iván GonzálezIván González
    Moderator
    Legend

    Here we have the indicator 🙂

    //--------------------------------------------
    // PRC_AMA Filtered (from MT5 by mladen)
    // version = 0
    // 04.03.2026
    // Iván González @ www.prorealcode.com
    // Sharing ProRealTime knowledge
    //--------------------------------------------
    // Parámetros:
    //   period     = ventana para eficiencia y filtro (17)
    //   fastPeriod = extremo rápido AMA (3)
    //   slowPeriod = extremo lento AMA (34)
    //   filterSize = multiplicador filtro (5), <=0 para desactivar
    //--------------------------------------------
    // --- Parámetros ---
    //--------------------------------------------
    period = 17
    fastPeriod = 3
    slowPeriod = 34
    filterSize = 5
    //--------------------------------------------
    // --- Constantes derivadas ---
    //--------------------------------------------
    fastEnd = 2 / (fastPeriod + 1)
    slowEnd = 2 / (slowPeriod + 1)
    hlPeriod = period + 1
    //--------------------------------------------
    // --- Fuente ---
    //--------------------------------------------
    src = close
    //--------------------------------------------
    // --- AMA: Adaptive Moving Average ---
    //--------------------------------------------
    hhVal = highest[hlPeriod](high)
    llVal = lowest[hlPeriod](low)
    
    
    IF hhVal  llVal THEN
    mltp = ABS(((src - llVal) - (hhVal - src)) / (hhVal - llVal))
    ELSE
    mltp = 1
    ENDIF
    
    
    ssc = mltp * (fastEnd - slowEnd) + slowEnd
    
    
    IF barindex < hlPeriod THEN
    amaVal = src
    ELSE
    amaVal = amaVal[1] + (ssc * ssc) * (src - amaVal[1])
    ENDIF
    //--------------------------------------------
    // --- Filtro estadístico ---
    // Suprime movimientos menores que filterSize * stddev(cambios)
    // Warmup: hlPeriod (AMA estable) + period (summation completa)
    //--------------------------------------------
    warmup = hlPeriod + period
    
    
    IF barindex < hlPeriod THEN
    filtVal = src
    chgVal = 0
    pwrVal = 0
    ELSIF barindex  0 AND chgVal < filterSize * SQRT(sumPwr / period) THEN
    filtVal = filtVal[1]
    ELSE
    filtVal = amaVal
    ENDIF
    ENDIF
    //--------------------------------------------
    // --- Colores: verde alcista, rojo bajista, gris neutro ---
    //--------------------------------------------
    IF barindex  filtVal[1] THEN
    // Verde (MediumSeaGreen)
    rr = 60
    gg = 179
    bb = 113
    ELSIF filtVal < filtVal[1] THEN
    // Rojo (OrangeRed)
    rr = 255
    gg = 69
    bb = 0
    ELSE
    rr = rr[1]
    gg = gg[1]
    bb = bb[1]
    ENDIF
    //--------------------------------------------
    RETURN filtVal COLOURED(rr, gg, bb) style(line, 2) AS "AMA Filtered"
    


Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

TradingView to ProRealTime Translation Center

New Reply
Author
author-avatar
wrox @wrox Participant
Summary

This topic contains 1 reply,
has 2 voices, and was last updated by Iván GonzálezIván González
6 months, 2 weeks ago.

Topic Details
Forum: TradingView to ProRealTime Translation Center Forum
Started: 12/22/2023
Status: Active
Attachments: 1 files
ProRealCode ProRealCode
Loading...