Custom indicator coding dilema

Back to topics list To post a new topic, please log in or register
avatar
18
tenlau 2016.07.31 13:38
 

Hello, first a little introduction: I am newbie to mql4 and coding. I try to understand a simple well known custom indicator to be able to code my own one. But I can not understand it. So this is the code:

#property copyright "Kalenzo"
#property link      "bartlomiej.gorski@gmail.com"
//----
#property indicator_buffers 1
#property indicator_color1 Red
extern int Lb=3;
double ssl[],Hld,Hlv;
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0,ssl);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
   for(int i=Bars-Lb;i>=0;i--)
     {
      if(Close[i]>iMA(Symbol(),0,Lb,0,MODE_SMA,PRICE_HIGH,i+1))
         Hld=1;
      else
        {
         if(Close[i]<iMA(Symbol(),0,Lb,0,MODE_SMA,PRICE_LOW,i+1))
            Hld=-1;
         else
            Hld=0;
        }
      if(Hld!=0)
         Hlv=Hld;
      if(Hlv==1)
         {ssl[i]=iMA(Symbol(),0,Lb,0,MODE_SMA,PRICE_LOW,i+1);
         Print("Hld=1 si ssl(i)=",ssl[i]);}
      else
         {ssl[i]=iMA(Symbol(),0,Lb,0,MODE_SMA,PRICE_HIGH,i+1);
         Print("Hld=-1 si ssl(i)=",ssl[i]);}
      if(Hld==0)
      Print("Hld=0 si ssl(i)=",ssl[i]);
     }
//----
   return(0);
  }
//+------------------------------

 Now my dilema:

Running the code I saw that when Hld==0 the indicator plot the previous value. I mean ssl[i]=ssl[i+1].

But if I read the code I can not see where is this. In my opinion, as I understand the code, the value of ssl[i] when Hld==0 should be EMPTY_VALUE ie 2147483647 (0x7FFFFFFF) because as I understand reading the code ssl takes values only when Hld!=0

So where am I wrong?

Thanks in advance for all answers! 

avatar
13000
WHRoeder 2016.07.31 16:37 #
 
tenlau I understand reading the code ssl takes values only when Hld!=0 So where am I wrong?
  1. if(Hld!=0) Hlv=Hld;
    if(Hlv==1) ssl[i]= ...
    The ssl[i] is set by Hlv not by the Hld and Hlv is never set to zero.
  2.  for(int i=Bars-Lb;i>=0;i--){
          if(Close[i]>iMA(Symbol(),0,Lb,0,MODE_SMA,PRICE_HIGH,i+1))
    The iMA looks back Lb values [i+1 .. i+1+(LB-1)] so to avoid looking past the end the for should be
     for(int i=Bars-1-Lb;i>=0;i--){
avatar
18
tenlau 2016.07.31 17:13 #
 
WHRoeder:
  1. The ssl[i] is set by Hlv not by the Hld and Hlv is never set to zero.
  2. The iMA looks back Lb values [i+1 .. i+1+(LB-1)] so to avoid looking past the end the for should be
What you said is clear for me. Let me put the question some other way. What happens when Hld is set to 0? From the code I understand that Hlv is set to 1 or -1 ONLY when Hld is NOT 0. So for Hld=0 there is no ssl[i] value (BECAUSE there is NO Hlv value). But if you look on a chart that has this indicator attached you will see that, for Hld=0, ssl[i]=ssl[i+1] (in words that is ssl takes its previous value). WHY is my question, how is this possible?
avatar
13000
WHRoeder 2016.07.31 17:32 #
 
tenlau So for Hld=0 there is no ssl[i] value
Wrong. Hld does not set ssl, Hlv does.
if(Hld!=0) Hlv=Hld;
if(Hlv==1) ssl[i]= ...
avatar
18
tenlau 2016.07.31 17:47 #
 
WHRoeder:
Wrong. Hld does not set ssl, Hlv does.
Excuse me, maybe I'm stupid but I want to learn why am I so :). So I insist in other way:There are plenty situations when Hld is zero (from the first if() of the code). WHAT is the value of ssl[i] in this case and please explain WHY.
avatar
13000
WHRoeder 2016.08.01 15:48 #
 
So what if Hld is zero. The code set's ssl from the moving average according to Hlv.
Your Print("Hld=1") may be wrong. Hld could be zero, but Hlv is not.
Change your Print statements to output both variables.
     if(Hlv==1)
         {ssl[i]=iMA(Symbol(),0,Lb,0,MODE_SMA,PRICE_LOW,i+1);
         Print("Hld=1 si ssl(i)=",ssl[i]);}
      else
         {ssl[i]=iMA(Symbol(),0,Lb,0,MODE_SMA,PRICE_HIGH,i+1);
         Print("Hld=-1 si ssl(i)=",ssl[i]);}

avatar
18
tenlau 2016.08.01 21:35 #
 
WHRoeder:
So what if Hld is zero. The code set's ssl from the moving average according to Hlv.
Your Print("Hld=1") may be wrong. Hld could be zero, but Hlv is not.
Change your Print statements to output both variables.

I think i find myself the answer. Hlv is a global variable that is "keeping" its value over for() loop iteration, obviously if it is not changed by code (if(Hld!=0) Hlv=Hld;...). SO; WHEN Hld is 0 THE ACTUAL Hlv TAKES NO NEW VALUE IN THE ACTUAL LOOP (IT ACTUALY REMAINS WITH THE VALUE FROM THE PAST LOOP/LOOPS).

THAT IS THE REASON ssl[i]=ssl[i+1]

Please confirm that I am right in my judgment!

avatar
13000
WHRoeder 2016.08.02 04:01 #
 
tenlau: THAT IS THE REASON ssl[i]=ssl[i+1]
ssl[i]=iMA(Symbol(),0,Lb,0,MODE_SMA,PRICE_LOW,i+1)
ssl[i] is never equal to ssl[i+1]. It is set by the moving average, it may be close but it will never be equal.
avatar
18
tenlau 2016.08.02 08:37 #
 
WHRoeder:
ssl[i] is never equal to ssl[i+1]. It is set by the moving average, it may be close but it will never be equal.

Sorry, my mistake. By "ssl[i] = ssl[i+1]" I wanted to say that they take the same iMA calculation, I mean if ssl[i+1] was from PRICE_HIGH ssl[i] will also be from PRICE_HIGH. Same for PRICE_LOW. Because if Hld=0 Hlv[i]=Hlv[i+1] in the for() loop.

Now, am I right? 

avatar
13000
WHRoeder 2016.08.02 20:53 #
 
Yes
avatar
18
tenlau 2016.08.04 09:19 #
 
WHRoeder:
Yes
Happy that, at last, we find consensus :)
Back to topics list  

To add comments, please log in or register