Need help with Volatility Stop

 

Good friends.

This topic has already been asked before. https://www.mql5.com/en/forum/147558

But I seek a different answer.

I have spent a good few days on this, and now I finally seek your help. 

 

Firstly, I am doing this task an exercise for myself to learn MQL4.

Secondly, the original code for that indicator is so elegant that I want to replicate most of it (and learn to code efficient code).

 

The original code is something called pine script:

study("Volatility Stop", shorttitle="VStop", overlay=true)

length = input(20)

mult = input(2)

atr_ = atr(length)

max1 = max(nz(max_[1]), close)

min1 = min(nz(min_[1]), close)

is_uptrend_prev = nz(is_uptrend[1], true)

stop = is_uptrend_prev ? max1 - mult * atr_ : min1 + mult * atr_

vstop_prev = nz(vstop[1])

vstop1 = is_uptrend_prev ? max(vstop_prev, stop) : min(vstop_prev, stop)

is_uptrend = close - vstop1 >= 0

is_trend_changed = is_uptrend != is_uptrend_prev

max_ = is_trend_changed ? close : max1

min_ = is_trend_changed ? close : min1

vstop = is_trend_changed ? is_uptrend ? max_ - mult * atr_ : min_ + mult * atr_ : vstop1

plot(vstop, color = is_uptrend ? green : red, style=cross, linewidth=2)

 

 

Although I understand the general lines, I do not understand all of it to every detail.

 

This is what I have so far, but it is obviously some (or a lot of!) logic errors here. 

 

//+------------------------------------------------------------------+
//|                                                        VStop.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 3;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

input int length = 20;
input int mult = 2;
bool is_trend_changed;
bool is_uptrend;
bool is_uptrend_prev;

double max[];
double min[];
double vstop[];


int init()
  {
   
  SetIndexBuffer(0, max);
  SetIndexBuffer(1, min);
  SetIndexBuffer(2, vstop);

  SetIndexStyle(0, DRAW_NONE);
  SetIndexStyle(1, DRAW_NONE);
  SetIndexStyle(2, DRAW_ARROW);

  SetIndexLabel(0, "MAX");
  SetIndexLabel(1, "MIN");
  SetIndexLabel(2, "VSTOP");
  
  SetIndexEmptyValue(0, 0.0);
  SetIndexEmptyValue(1, 0.0);

  SetIndexArrow(0, 233); // Up arrow
  SetIndexArrow(1, 234); // Down arrow

   return(INIT_SUCCEEDED);
  }

int start()
   {
   
      double max1, min1, atr, stop;
      int   i,nLimit,nCountedBars;
      int vstop1, vstop_prev = 0;
      
      nCountedBars=IndicatorCounted();
      if(nCountedBars>0) nCountedBars--;
      nLimit=Bars-nCountedBars-1;
      
      max[nLimit] = High[nLimit];
      min[nLimit] = Low[nLimit];
      vstop[nLimit] = Open[nLimit];

      

      for (i=nLimit-1; i>=0; i--)
         {
            atr = iATR(NULL,0,length,i);
            max1 = MathMax(max[i+1], Close[i+1]);
            min1 = MathMin(min[i+1], Close[i+1]);

            Print("stop: ",stop);            
            vstop_prev = vstop[i+1];
            vstop1 = is_uptrend_prev ? MathMax(vstop_prev, stop) : MathMin(vstop_prev, stop);

            is_uptrend = Close[i] - vstop1 >= 0;
            is_uptrend_prev=((Close[i+1] - vstop1 >= 0) && true);
            
            
            stop = is_uptrend_prev ? max1 - mult*atr : min1 + mult*atr;
            
            
            is_trend_changed = is_uptrend != is_uptrend_prev;
            
            max[i] = (is_trend_changed ? Close[i] : max1);
            min[i] = (is_trend_changed ? Close[i] : min1);
            
         
            vstop[i] = is_trend_changed ? is_uptrend ? max[i] - mult*atr : min[i] + mult*atr : vstop1;
         }
   
   
   return(0);
   }

 

I am asking if anyone can help me with some pseudo-code or guide me in the right direction.

Should I perhaps have an additional array to store values in?

In the original code they access values of a parameter before declaring it. What should I there?

 

Thanks for any help! 

Reason: