Conversion from MT5 Nadaraya-Watson Envelope

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

    //+——————————————————————+
    //| nd.mq5 |
    //| Copyright 2022, MetaQuotes Ltd. |
    //| https://www.mql5.com |
    //+——————————————————————+
    #property copyright “Copyright 2022, Grasco.”
    #property version “1.00”
    #property indicator_chart_window
    #property indicator_buffers 3
    #property indicator_plots 2
    #property indicator_type1 DRAW_LINE
    #property indicator_type2 DRAW_LINE
    #property indicator_width1 2
    #property indicator_width2 2
    #property indicator_color1 Blue
    #property indicator_color2 Red
    #property indicator_applied_price PRICE_WEIGHTED
    //— input parameters
    input int Length=500; // Bars Count
    input int Bandwidth=17; // Bandwidth
    input double Multiplayer=1.5;
    // n = get the bar index

    //— indicator buffers
    double ExtUpBuffer[];
    double ExtDownBuffer[];
    double ExtCABuffer[];
    double y[]; // for calculation
    int ExtBarsHandle;
    int k = 2;
    //+——————————————————————+
    //| Custom indicator initialization function |
    //+——————————————————————+
    int OnInit(){
    //— indicator buffers mapping
    SetIndexBuffer(0,ExtUpBuffer);
    SetIndexBuffer(1,ExtDownBuffer);
    SetIndexBuffer(2,y,INDICATOR_CALCULATIONS);
    //—
    IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
    //— sets first bar from what index will be drawn
    PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,0);
    PlotIndexGetInteger(1,PLOT_DRAW_BEGIN,0);
    PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);
    PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0);
    //— name for DataWindow
    string short_name=StringFormat(“Nadraya”,Length);
    IndicatorSetString(INDICATOR_SHORTNAME,short_name);
    PlotIndexSetString(0,PLOT_LABEL,short_name+” Upper”);
    PlotIndexSetString(1,PLOT_LABEL,short_name+” Lower”);

    ExtBarsHandle = iMA(_Symbol,_Period,Length,0,MODE_EMA,PRICE_WEIGHTED);
    return(INIT_SUCCEEDED);
    }
    //+——————————————————————+
    //| Custom indicator iteration function |
    //+——————————————————————+
    int OnCalculate(const int rates_total,
    const int prev_calculated,
    const int begin,
    const double &price[])
    {
    //—
    if(IsStopped()) return 0;
    int calculated=BarsCalculated(ExtBarsHandle);
    if(rates_total<calculated) return(0);
    if(rates_total<Length)
    return(0);

    int copyBars = 0;
    if (prev_calculated>rates_total || prev_calculated<=0){
    copyBars = rates_total;
    }
    else{
    copyBars = rates_total-prev_calculated;
    if(prev_calculated>0) copyBars++;
    }
    int start=prev_calculated-1;
    if(start<Length)
    start=Length;

    double sum_e = 0.0;
    for(int i=rates_total-Length; i<rates_total && !IsStopped(); i++){
    double sum = 0.0;
    double sumw= 0.0;
    for(int j = rates_total-Length;j<rates_total-1;j++){
    double w = MathExp(-(MathPow(i-j,2)/(Bandwidth*Bandwidth*2)));
    sum += price[j]*w;
    sumw += w;
    }
    double y2 = sum/sumw;
    sum_e += (MathAbs(price[i]-y2));
    y[i] = y2;
    }
    double mae = sum_e/Length*Multiplayer;
    for(int i=rates_total-Length+1; i<rates_total && !IsStopped(); i++){
    double y2 = y[i];
    double y1 = y[i-1];

    ExtUpBuffer[i]=y2+mae;
    ExtDownBuffer[i]=y2-mae;
    //Print(mae);
    //Print(y[i]);
    }
    //— return value of prev_calculated for next call
    return(rates_total);
    }
    //+——————————————————————+

    ND.jpg ND.jpg
    #206793 quote
    JSJS
    Participant
    Master

    Hi @wrox

    This one was among my indicators…

    I don’t know where it originally came from…

    //Nadaraya-Watson Envelope
    defparam drawonlastbaronly = true
    
    length = Min(1000,BarIndex)//Window Size
    hh = 8 //Bandwidth
    mult = 0.3
    src = Close
    n = barindex
    k = 2
    if IsLastBarUpdate then
    y2 = 0
    sume = 0
    for i = 0 to length-1
    sum = 0
    sumw = 0
    for j = 0 to length-1
    w = EXP(-pow(i-j,2)/(hh*hh*2))
    sum = sum+src[j]*w
    sumw = sumw+w
    next
    y2 = sum/sumw
    sume = sume+abs(src[i] - y2)
    $a[barindex-i]=y2
    //DRAWPOINT(barindex-i, y2, 1)
    next
    mae = sume/(length*mult)
    for i=0 to length-1
    DRAWPOINT(barindex-i, $a[barindex-i]-mae, 2) coloured(0,255,0,100)
    DRAWPOINT(barindex-i, $a[barindex-i]+mae, 2) coloured(255,0,0,100)
    next
    endif
    return
    
    coincatcha thanked this post
    Scherm­afbeelding-2023-01-04-om-17.22.08.jpg Scherm­afbeelding-2023-01-04-om-17.22.08.jpg
    #206795 quote
    JC_BywanJC_Bywan
    Moderator
    Master
    Topic of interest, including discussion repainting vs non-repainting:
    Conversion from trading view Nadaraya-Watson Envelope
    code in post #196311 of linked topic
    #206801 quote
    wroxwrox
    Participant
    Average
    Great work! I need to translate to easy language now. Thanks!!!
    #206849 quote
    JSJS
    Participant
    Master

    The code above was the non-repainting version of Nicolas…

    #206915 quote
    SofitechSofitech
    Participant
    Master
    Bonjour @JS et Nicolas, Merci pour ce code. Comment peut-on faire pour qu’une valeur soit renvoyée ? La valeur des points hauts et bas de la bande. Merci d’avance de votre aide.
Viewing 6 posts - 1 through 6 (of 6 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

Conversion from MT5 Nadaraya-Watson Envelope


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
wrox @wrox Participant
Summary

This topic contains 5 replies,
has 4 voices, and was last updated by SofitechSofitech
3 years, 8 months ago.

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