ADXm .Convertir de MQ4 a PRT

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #71307 quote
    Fr7Fr7
    Participant
    Master

    Hola Nicolás,a ver si puede pasar este indicador a PRT. Creo que es un indicador muy interesante basado en el indicador ADX con canales. Gracias.

     

    //+——————————————————————+
    //| ADXm.mq4 |
    //| Copyright 2018, MetaQuotes Software Corp. |
    //| |
    //+——————————————————————+
    #property copyright “Copyright 2018, MetaQuotes Software Corp.”
    //——————————————————————-

    //——————————————————————-
    #property indicator_separate_window
    #property indicator_buffers 6
    #property indicator_color1 clrDodgerBlue
    #property indicator_color2 clrLightSlateGray
    #property indicator_color3 clrSandyBrown
    #property indicator_color4 clrDodgerBlue
    #property indicator_color5 clrSandyBrown
    #property indicator_color6 clrSandyBrown
    #property indicator_style1 STYLE_DOT
    #property indicator_style2 STYLE_DOT
    #property indicator_style3 STYLE_DOT
    #property indicator_width4 2
    #property indicator_width5 2
    #property indicator_width6 2
    #property strict

    //
    //
    //
    //
    //

    extern ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; // Time frame to use
    extern int period = 25; // ADXm period
    extern double Smooth = 15; // Smoothing period for price filter
    extern int minmaxPeriod = 25; // Period for floating zero
    extern double upLevel = 0.90; // Upper level
    extern double downLevel = 0.10; // Lower level
    extern bool alertsOn = true; // Turn alerts on?
    extern bool alertsOnCurrent = true; // Alerts on current (still opebed) bar?
    extern bool alertsMessage = true; // Alerts should show popup message?
    extern bool alertsSound = true; // Alerts should play alert sound
    extern bool alertsNotify = true; // Alerts should send a push notification?
    extern bool alertsEmail = true; // Alerts should send an emil?
    extern string soundFile = “alert2.wav”; // Sound file to use for alerts
    extern bool Interpolate = true; // Interpolate in multi time frame mode?

    double ADX[],ADXLa[],ADXLb[],DI[],slope[],zero[],up[],down[];
    string indicatorFileName;
    bool returnBars;

    //——————————————————————-
    //
    //——————————————————————-
    //
    //
    //
    //
    //

    int init()
    {
    IndicatorBuffers(8);
    SetIndexBuffer(0,up);
    SetIndexBuffer(1,zero);
    SetIndexBuffer(2,down);
    SetIndexBuffer(3,ADX);
    SetIndexBuffer(4,ADXLa);
    SetIndexBuffer(5,ADXLb);
    SetIndexBuffer(6,DI);
    SetIndexBuffer(7,slope);
    indicatorFileName = WindowExpertName();
    returnBars = (TimeFrame==-99);
    TimeFrame = MathMax(TimeFrame,_Period);
    IndicatorShortName(timeFrameToString(TimeFrame)+” ADXm adaptive (“+(string)period+”)”);
    return(0);
    }

    //——————————————————————-
    //
    //——————————————————————-
    //
    //
    //
    //
    //

    double work[][3];
    #define zdh 0
    #define zdl 1
    #define zdx 2
    int start()
    {
    int counted_bars = IndicatorCounted();
    if(counted_bars < 0) return(-1);
    if(counted_bars > 0) counted_bars–;
    int limit = MathMin(Bars-counted_bars,Bars-1);
    if (returnBars) {up[0] = limit+1; return(0); }
    if (ArrayRange(work,0)!=Bars) ArrayResize(work,Bars);

    //
    //
    //
    //
    //

    if (TimeFrame == Period())
    {
    if (ArrayRange(work,0)!=Bars) ArrayResize(work,Bars);
    if (slope[limit]==-1) CleanPoint(limit,ADXLa,ADXLb);
    for (int i=limit,r=Bars-i-1; i>=0; i–,r++)
    {

    if (r<1) { work[r][zdh] = 0; work[r][zdl] = 0; ADX[i] = 0; continue; }

    //
    //
    //
    //
    //

    double noise = 0, vhf = 0;
    double max = Close[i];
    double min = Close[i];
    for (int k=0; k<period && (i+k+1)<Bars; k++)
    {
    noise += MathAbs(Close[i+k]-Close[i+k+1]);
    max = MathMax(Close[i+k],max);
    min = MathMin(Close[i+k],min);
    }
    if (noise>0) vhf = (max-min)/noise;
    double tperiod = period;
    int fzPeriod = minmaxPeriod;
    if (vhf>0)
    {
    tperiod = -MathLog(vhf)*period;
    fzPeriod = (int)MathCeil(MathMax(-MathLog(vhf)*minmaxPeriod,3));
    }
    double alpha = 2.0/(tperiod+1.0);

    double hc = iSsm(High[i] ,Smooth,i,0);
    double lc = iSsm(Low[i] ,Smooth,i,1);
    double cp = iSsm(Close[i+1],Smooth,i,2);
    double hp = iSsm(High[i+1] ,Smooth,i,3);
    double lp = iSsm(Low[i+1] ,Smooth,i,4);
    double dh = MathMax(hc-hp,0);
    double dl = MathMax(lp-lc,0);

    if(dh==dl) {dh=0; dl=0;} else if(dh<dl) dh=0; else if(dl<dh) dl=0;

    double tr = MathMax(hc,cp)-MathMin(lc,cp);
    double dhk = 0;
    double dlk = 0;

    if(tr!=0) { dhk = 100.0*dh/tr; dlk = 100.0*dl/tr; }

    //
    //
    //
    //
    //

    work[r][zdh] = work[r-1][zdh] + alpha*(dhk-work[r-1][zdh]);
    work[r][zdl] = work[r-1][zdl] + alpha*(dlk-work[r-1][zdl]);
    DI[i] = work[r][zdh] – work[r][zdl];

    double div = MathAbs(work[r][zdh] + work[r][zdl]);
    double temp = 0; if( div != 0.0) temp = 100*DI[i]/div;

    ADXLa[i] = EMPTY_VALUE;
    ADXLb[i] = EMPTY_VALUE;
    ADX[i] = ADX[i+1]+alpha*(temp-ADX[i+1]);
    min = ADX[ArrayMinimum(ADX,fzPeriod,i)];
    max = ADX[ArrayMaximum(ADX,fzPeriod,i)];
    double range = max-min;
    zero[i] = min+range*0.5;
    up[i] = min+range*upLevel;
    down[i] = min+range*downLevel;
    slope[i] = slope[i+1];

    if (ADX[i]>zero[i]) slope[i] = 1;
    if (ADX[i]<zero[i]) slope[i] = -1;
    if (slope[i]==-1) PlotPoint(i,ADXLa,ADXLb,ADX);
    }
    manageAlerts();
    return(0);
    }

    //
    //
    //
    //
    //
    limit = (int)MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
    if (slope[limit]==-1) CleanPoint(limit,ADXLa,ADXLb);
    for(int i=limit; i>=0; i–)
    {
    int y = iBarShift(NULL,TimeFrame,Time[i]);
    up[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,period,Smooth,minmaxPeriod,upLevel,downLevel,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,0,y);
    zero[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,period,Smooth,minmaxPeriod,upLevel,downLevel,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,1,y);
    down[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,period,Smooth,minmaxPeriod,upLevel,downLevel,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,2,y);
    ADX[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,period,Smooth,minmaxPeriod,upLevel,downLevel,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,3,y);
    slope[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,period,Smooth,minmaxPeriod,upLevel,downLevel,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,7,y);
    ADXLa[i] = EMPTY_VALUE;
    ADXLb[i] = EMPTY_VALUE;
    if (!Interpolate || (i>0 && y==iBarShift(NULL,TimeFrame,Time[i-1]))) continue;

    //
    //
    //
    //
    //

    int n,k; datetime time = iTime(NULL,TimeFrame,y);
    for(n = 1; (i+n)<Bars && Time[i+n] >= time; n++) continue;
    for(k = 1; k<n && (i+n)<Bars && (i+k)<Bars; k++)
    {
    ADX[i+k] = ADX[i] + (ADX[i+n] – ADX[i] ) * k/n;
    zero[i+k] = zero[i] + (zero[i+n] – zero[i]) * k/n;
    up[i+k] = up[i] + (up[i+n] – up[i] ) * k/n;
    down[i+k] = down[i] + (down[i+n] – down[i]) * k/n;
    }
    }
    for(int i=limit; i>=0; i–) if (slope[i]==-1) PlotPoint(i,ADXLa,ADXLb,ADX);
    return(0);
    }

    //——————————————————————-
    //
    //——————————————————————-
    //
    //
    //
    //
    //

    void CleanPoint(int i,double& first[],double& second[])
    {
    if (i>=Bars-3) return;
    if ((second[i] != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
    second[i+1] = EMPTY_VALUE;
    else
    if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
    first[i+1] = EMPTY_VALUE;
    }

    void PlotPoint(int i,double& first[],double& second[],double& from[])
    {
    if (i>=Bars-2) return;
    if (first[i+1] == EMPTY_VALUE)
    if (first[i+2] == EMPTY_VALUE)
    { first[i] = from[i]; first[i+1] = from[i+1]; second[i] = EMPTY_VALUE; }
    else { second[i] = from[i]; second[i+1] = from[i+1]; first[i] = EMPTY_VALUE; }
    else { first[i] = from[i]; second[i] = EMPTY_VALUE; }
    }

    //——————————————————————-
    //
    //——————————————————————-
    //
    //
    //
    //
    //

    #define Pi 3.14159265358979323846264338327950288
    double workSsm[][10];
    #define _tprice 0
    #define _ssm 1

    double workSsmCoeffs[][4];
    #define _period 0
    #define _c1 1
    #define _c2 2
    #define _c3 3

    //
    //
    //
    //
    //

    double iSsm(double tprice, double tperiod, int i, int instanceNo=0)
    {
    if (tperiod<=1) return(tprice); i = Bars-i-1;
    if (ArrayRange(workSsm,0) !=Bars) ArrayResize(workSsm,Bars);
    if (ArrayRange(workSsmCoeffs,0) < (instanceNo+1)) ArrayResize(workSsmCoeffs,instanceNo+1);
    if (workSsmCoeffs[instanceNo][_period] != tperiod)
    {
    workSsmCoeffs[instanceNo][_period] = tperiod;
    double a1 = MathExp(-1.414*Pi/tperiod);
    double b1 = 2.0*a1*MathCos(1.414*Pi/tperiod);
    workSsmCoeffs[instanceNo][_c2] = b1;
    workSsmCoeffs[instanceNo][_c3] = -a1*a1;
    workSsmCoeffs[instanceNo][_c1] = 1.0 – workSsmCoeffs[instanceNo][_c2] – workSsmCoeffs[instanceNo][_c3];
    }

    //
    //
    //
    //
    //

    int s = instanceNo*2;
    workSsm[i][s+_tprice] = tprice;
    if (i>1)
    workSsm[i][s+_ssm] = workSsmCoeffs[instanceNo][_c1]*(workSsm[i][s+_tprice]+workSsm[i-1][s+_tprice])/2.0 +
    workSsmCoeffs[instanceNo][_c2]*workSsm[i-1][s+_ssm] +
    workSsmCoeffs[instanceNo][_c3]*workSsm[i-2][s+_ssm];
    else workSsm[i][s+_ssm] = tprice;
    return(workSsm[i][s+_ssm]);
    }

    //——————————————————————-
    //
    //——————————————————————-
    //
    //
    //
    //
    //

    double workSma[][2];
    double iSma(double price, int tperiod, int r, int instanceNo=0)
    {
    if (ArrayRange(workSma,0)!= Bars) ArrayResize(workSma,Bars); instanceNo *= 2; r = Bars-r-1; int k = 0;

    //
    //
    //
    //
    //

    workSma[r][instanceNo] = price;
    if (r>=tperiod)
    workSma[r][instanceNo+1] = workSma[r-1][instanceNo+1]+(workSma[r][instanceNo]-workSma[r-tperiod][instanceNo])/tperiod;
    else { workSma[r][instanceNo+1] = 0; for(k=0; k<tperiod && (r-k)>=0; k++) workSma[r][instanceNo+1] += workSma[r-k][instanceNo];
    workSma[r][instanceNo+1] /= k; }
    return(workSma[r][instanceNo+1]);
    }

    //——————————————————————-
    //
    //——————————————————————-
    //
    //
    //
    //
    //

    string sTfTable[] = {“M1″,”M5″,”M15″,”M30″,”H1″,”H4″,”D1″,”W1″,”MN”};
    int iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

    string timeFrameToString(int tf)
    {
    for (int i=ArraySize(iTfTable)-1; i>=0; i–)
    if (tf==iTfTable[i]) return(sTfTable[i]);
    return(“”);
    }

    //——————————————————————-
    //
    //——————————————————————-
    //
    //
    //
    //
    //

    void manageAlerts()
    {
    if (alertsOn)
    {
    int whichBar = 1; if (alertsOnCurrent) whichBar = 0;
    if (slope[whichBar] != slope[whichBar+1])
    {
    if (slope[whichBar] == 1) doAlert(whichBar,”Buy”);
    if (slope[whichBar] ==-1) doAlert(whichBar,”Sell”);
    }
    }
    }

    //
    //
    //
    //
    //

    void doAlert(int forBar, string doWhat)
    {
    static string previousAlert=”nothing”;
    static datetime previousTime;
    string message;

    if (previousAlert != doWhat || previousTime != Time[forBar]) {
    previousAlert = doWhat;
    previousTime = Time[forBar];

    //
    //
    //
    //
    //

    message = StringConcatenate(Symbol(),” “,timeFrameToString(_Period),” at “,TimeToStr(TimeLocal(),TIME_SECONDS),” ADXm adaptive “,doWhat);
    if (alertsMessage) Alert(message);
    if (alertsNotify) SendNotification(StringConcatenate(Symbol(), Period() ,” ADXm adaptive ” +” “+message));
    if (alertsEmail) SendMail(StringConcatenate(Symbol(), Period(), ” ADXm adaptive “),message);
    if (alertsSound) PlaySound(soundFile);
    }
    }

    ADXm.mq4 ADXM.png ADXM.png
    #71348 quote
    NicolasNicolas
    Keymaster
    Legend

    Hola, hice la traducción del código del indicador. Por una razón desconocida, hay algunos niveles extraños que también están en el código MT4 original … Bueno, dime si es molesto y trataré de solucionarlos.
    El código está adjunto, lo agregaré más adelante en la Biblioteca para que todo el mundo se beneficie de él.

    Fr7 thanked this post
    ADXm.png ADXm.png
    #71351 quote
    Fr7Fr7
    Participant
    Master

    Gracias por el gran trabajo Nicolas,

    ¿Pero creo que los niveles o canales no salen exactamente iguales que en mt4?¿Podría corregirlo?Le adjunto la imagen en MT4.

    ADXM-1.png ADXM-1.png
    #71353 quote
    NicolasNicolas
    Keymaster
    Legend

    los canales son casi idénticos, son diferentes porque los datos del broker son diferentes.

    #71361 quote
    Fr7Fr7
    Participant
    Master

    Gracias Nicolas una vez más.Lo he probado y funciona muy bien.

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

ADXm .Convertir de MQ4 a PRT


ProBuilder: Indicadores y Herramientas

New Reply
Author
author-avatar
Fr7 @fr7 Participant
Summary

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

Topic Details
Forum: ProBuilder: Indicadores y Herramientas
Language: Spanish
Started: 05/24/2018
Status: Active
Attachments: 5 files
ProRealCode ProRealCode
Loading...