Help with indicator programing from MT4 – Moving average histogram

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #45398 quote
    mikaelmikael
    Participant
    Average

    Hi can anyone help me with translate a code from MT4 to PRT? It’s an Moving average histogram.

    Best Regards Mikael

    Here is the code:

    #property strict

    #include <MovingAverages.mqh>

    //— indicator settings
    #property indicator_separate_window
    #property indicator_buffers 2
    #property indicator_color1 clrDodgerBlue
    #property indicator_color2 clrHotPink
    #property indicator_width1 4
    #property indicator_width2 4
    #property indicator_minimum -0.05
    #property indicator_maximum 1.05

     

    //— indicator parameters
    extern int PMA = 100; // Period Moving Average
    extern ENUM_MA_METHOD MMM = MODE_SMA; // Method Moving Average
    extern ENUM_APPLIED_PRICE MAP = PRICE_CLOSE; // Price for Moving Average
    extern int PSMA = 5; // Period Signal Moving Average

    //— indicator buffers
    double MA1[];
    double MA2[];
    double UP[];
    double DN[];

    //— right input parameters flag
    bool ExtParameters=false;

    //+——————————————————————+
    //+——————————————————————+
    //+——————————————————————+
    int OnInit(void)
    {

    //— 2 additional buffers are used for counting.
    IndicatorBuffers(4);

    //— drawing settings
    SetIndexStyle(0,DRAW_HISTOGRAM);
    SetIndexStyle(1,DRAW_HISTOGRAM);

    SetIndexDrawBegin( 0, PMA + PSMA);
    SetIndexDrawBegin( 1, PMA + PSMA);

    IndicatorDigits(Digits+2);

    //— 4 indicator buffers mapping
    SetIndexBuffer( 0, UP);
    SetIndexBuffer( 1, DN);
    SetIndexBuffer( 2, MA1);
    SetIndexBuffer( 3, MA2);

    //— name for DataWindow and indicator subwindow label
    IndicatorShortName(“MAOMAH(“+IntegerToString(PMA)+”,”+IntegerToString(PSMA)+”)”);

    //— check for input parameters
    if(PMA<=1 || PSMA<=1)
    {
    Print(“Wrong input parameters”);
    ExtParameters=false;
    return(INIT_FAILED);
    }
    else
    ExtParameters=true;

    //— initialization done
    return(INIT_SUCCEEDED);

    }

    //+——————————————————————+
    //+——————————————————————+
    //+——————————————————————+
    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,limit;
    //—
    if(rates_total<=PMA + PSMA || !ExtParameters)
    return(0);
    //— last counted bar will be recounted
    limit=rates_total-prev_calculated-1;
    if(prev_calculated>0)
    limit++;

    for( i = limit; i >= 0; i–)
    MA1[i] = iMA( NULL, 0, PMA, 0, MMM, MAP, i );

    SimpleMAOnBuffer(rates_total,prev_calculated,0,PSMA,MA1,MA2);

    for( i = limit-1; i >= 0; i–)
    {
    UP[i]=0.0;
    DN[i]=0.0;
    if( MA1[i] > MA2[i])
    UP[i]=1.0;
    else
    if( MA1[i] < MA2[i])
    DN[i]=1.0;
    else
    {
    UP[i]=UP[i+1];
    DN[i]=DN[i+1];
    }
    }
    return rates_total;
    }
    //+——————————————————————+

    #45402 quote
    NicolasNicolas
    Keymaster
    Legend

    Sure, could you provide some screenshots? I’d like to know how it looks before starting converting an indicator. Thanks.

    #45461 quote
    mikaelmikael
    Participant
    Average

    Here it is! Thanks for helping me 🙂

    MA-histogram.jpg MA-histogram.jpg
    #45775 quote
    NicolasNicolas
    Keymaster
    Legend

    Fine, here is the code of this indicator converted to probuilder:

    // https://www.prorealcode.com/topic/help-with-indicator-programing-from-mt4/
    
    PMA = 100 // Period Moving Average
    MMM = 0 // Method Moving Average
    MAP = customclose // Price for Moving Average
    PSMA = 5 // Period Signal Moving Average
    
    MA1 = average[PMA,MMM](MAP)
    MA2 = average[PSMA](MA1)
    
    if MA1>MA2 then 
    up = 1
    dn = 0
    elsif MA1<MA2 then 
    dn = 1
    up = 0
    endif
    
    RETURN up coloured(65,105,225) style(histogram) as "up", dn coloured(255,0,255) style(histogram) as "dn"

    Tell us if it is similar to the original one!

    Moving-average-histogram-color.png Moving-average-histogram-color.png
    #45913 quote
    mikaelmikael
    Participant
    Average

    Thank you Nicolas!

    It looks that it gives the same signals as the other one. 🙂

Viewing 5 posts - 1 through 5 (of 5 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

Help with indicator programing from MT4 – Moving average histogram


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
mikael @mikael Participant
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by mikaelmikael
9 years ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 09/04/2017
Status: Active
Attachments: 2 files
ProRealCode ProRealCode
Loading...