Conversion OBV MTF code from MT4

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

    Hey guys.
    Could someone please translate the code of the following MT4 indicator? It doesn’t need to be multi-timeframe.
    Thank you very much in advance.

    //+——————————————————————+
    //| OBV2.mq4 |
    //+——————————————————————+
    #property indicator_separate_window
    #property indicator_buffers 2
    #property indicator_color1 DodgerBlue
    #property indicator_color2 Red
    //#property indicator_maximum 1
    //#property indicator_minimum 0

    extern string TimeFrame = “Current time frame”;
    extern int SignalPeriod = 13;
    extern int SignalMaMode = MODE_LWMA;

    extern bool alertsOn = false;
    extern bool alertsOnCurrent = true;
    extern bool alertsMessage = true;
    extern bool alertsSound = false;
    extern bool alertsEmail = false;
    input bool alertsNotify = false;

    double gadblOBV[];
    double ma[];
    double hu[];
    double hd[];
    double trend[];

    string indicatorFileName;
    bool returnBars;
    bool calculateValue;
    int timeFrame;

    int init()
    {
    IndicatorBuffers(5);
    SetIndexBuffer( 0, hu ); SetIndexStyle(0,DRAW_LINE);
    SetIndexBuffer( 1, hd ); SetIndexStyle(1,DRAW_LINE);
    SetIndexBuffer( 2, gadblOBV );
    SetIndexBuffer( 3, ma );
    SetIndexBuffer( 4, trend );
    IndicatorDigits( 0 );

    //
    //
    //
    //
    //

    indicatorFileName = WindowExpertName();
    calculateValue = TimeFrame==”calculateValue”; if (calculateValue) { return(0); }
    returnBars = TimeFrame==”returnBars”; if (returnBars) { return(0); }
    timeFrame = stringToTimeFrame(TimeFrame);

    //
    //
    //
    //
    //

    IndicatorShortName( timeFrameToString(timeFrame)+” O” );
    SetIndexLabel( 0, “OBV2″ );
    return( 0 );
    }

    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) { hu[0] = MathMin(limit+1,Bars-1); return(0); }

    //
    //
    //
    //
    //

    if (calculateValue || timeFrame == Period())
    {
    for( int inx = limit; inx >=0; inx– )
    {
    if ( inx == ( Bars – 1 ) )
    {
    gadblOBV[inx] = Volume[inx];
    }
    else
    {
    if ( ( High[inx] == Low[inx] ) || ( Open[inx] == Close[inx] ) || ( Close[inx] == Close[inx+1] ) )
    {
    gadblOBV[inx] = gadblOBV[inx+1];
    }
    else
    {
    if ( Close[inx] > Open[inx] ) gadblOBV[inx] = gadblOBV[inx+1] + ( Volume[inx] * ( Close[inx] – Open[inx] ) / ( High[inx] – Low[inx] ) );
    else gadblOBV[inx] = gadblOBV[inx+1] – ( Volume[inx] * ( Open[inx] – Close[inx] ) / ( High[inx] – Low[inx] ) );
    }
    }
    }
    for(inx=limit; inx>=0; inx–)
    {
    ma[inx]=iMAOnArray(gadblOBV,0,SignalPeriod,0,SignalMaMode,inx);
    // hu[inx] = EMPTY_VALUE;
    // hd[inx] = EMPTY_VALUE;
    trend[inx] = trend[inx+1];
    // if (gadblOBV[inx] > ma[inx]) { trend[inx] = 1; hu[inx] = 1; }
    // if (gadblOBV[inx] < ma[inx]) { trend[inx] = -1; hd[inx] = 1; }

    if (ma[inx]>ma[inx+1]) { trend[inx] = 1; hu[inx] = ma[inx]; }
    if (ma[inx]<ma[inx+1]) { trend[inx] = -1; hd[inx] = ma[inx]; }
    }
    manageAlerts();
    return( 0 );
    }

    limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,”returnBars”,0,0)*timeFrame/Period()));
    for (int i=limit; i>=0; i–)
    {
    int y = iBarShift(NULL,timeFrame,Time[i]);
    hu[i] = iCustom(NULL,timeFrame,indicatorFileName,”calculateValue”,SignalPeriod,SignalMaMode,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,0,y);
    hd[i] = iCustom(NULL,timeFrame,indicatorFileName,”calculateValue”,SignalPeriod,SignalMaMode,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,1,y);
    }
    return(0);
    }

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

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

    //
    //
    //
    //
    //

    int stringToTimeFrame(string tfs)
    {
    StringToUpper(tfs);
    for (int i=ArraySize(iTfTable)-1; i>=0; i–)
    if (tfs==sTfTable[i] || tfs==””+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
    return(Period());
    }
    string timeFrameToString(int tf)
    {
    for (int i=ArraySize(iTfTable)-1; i>=0; i–)
    if (tf==iTfTable[i]) return(sTfTable[i]);
    return(“”);
    }

    //+——————————————————————
    //|
    //+——————————————————————
    //
    //
    //
    //
    //

    void manageAlerts()
    {
    if (!calculateValue && alertsOn)
    {
    if (alertsOnCurrent)
    int whichBar = 0;
    else whichBar = 1;
    if (trend[whichBar] != trend[whichBar+1])
    {
    if (trend[whichBar] == 1) doAlert(whichBar,” buy “);
    if (trend[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(),” at “,TimeToStr(TimeLocal(),TIME_SECONDS),” “,timeFrameToString(_Period),” OBV2 “,doWhat);
    if (alertsMessage) Alert(message);
    if (alertsEmail) SendMail(StringConcatenate(Symbol(),” OBV2 “),message);
    if (alertsSound) PlaySound(“alert2.wav”);
    if (alertsNotify) SendNotification(message);
    }
    }

    OBV-MTF-MT4.jpg OBV-MTF-MT4.jpg
    #217707 quote
    JS
    Participant
    Master

    Hi,

    I think it is no more than the “normal” On Balance Volume but with an average (MA)…

    maPeriod = 200
    useFill = 1
     
    If Close = Close[1] then
    xOBV = xOBV
    EndIf
     
    If Close > Close[1] then
    xOBV = xOBV + Volume
    ElsIf Close < Close[1] then
    xOBV = xOBV - Volume
    EndIf
     
    MAOBV = Average[maPeriod](xOBV)
     
    If MAOBV > MAOBV[1] then
    R = 0
    G = 255
    B = 0
    Else
    R = 255
    G = 0
    B = 0
    EndIf
     
    Return MAOBV Coloured(R,G,B) as "MA On Balance Volume"
    Nicolas thanked this post
    Scherm­afbeelding-2023-07-15-om-20.24.51.png Scherm­afbeelding-2023-07-15-om-20.24.51.png
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

Conversion OBV MTF code from MT4


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
wrox @wrox Participant
Summary

This topic contains 1 reply,
has 2 voices, and was last updated by JS
3 years, 2 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 07/15/2023
Status: Active
Attachments: 2 files
Logo Logo
Loading...