Gain Indicator

 

Hi, I wrote the attached indicator but it doesn't work.

The indicator would work as a cumulate sum:


if( Close[0]>High[1] )
{
Gain[i]= "Previous Gain" + (Close[0]-High[1]) ;
}
if( Close[0]<High[1] && High[0]>High[1] )
{
Gain[i]= "Previous Gain" - (High[1]-Close[0]);
}


I see that in the graph the indicator is blank, could you help me to solve the problem? Thank you!

Files:
gain.mq4  2 kb
 

You made quite a lot of mistakes in such short code :-)

1) First of in indicator you cannot adress price arrays High[], Low[], Close[] and so on by constants 0 and 1 like in EA. While writing EA you are interested usually only with current value of an indicator or price and that is why usualy they are adressed by constants of 0 and 1 here you have to use adressing by your indexing variable i. So instead of 0 put i and instead of 1 i+1 while accessing High[] an Close[] arrays.


2) You also have to care for direction of counting, especially in cumulative indicator your loop is indexing from 0 to Bars what means you are counting in wrong direction from present to past while you should move from past to present


3)Your conditions are not complete I mean that there are some situation where none of two condition is met and in this case noting happens. What also menas that cumulative value of Gain indicator is not moved forward. Maybe you shoud make unconditional copying previous value to present and than only add or subtract the rest.


4)Indicator data isn initialized with the value of EMPTY_VALUE which is very big positive number so becouse you add previous value of Gain you have to initialie it sometimies


I do not fully understand how this indicator should work but I corrected some of those bugs.


Good luck

Files:
gain_1.mq4  2 kb
 

Thank you for the answer! As you see I’m a beginner :-)

I’m trying to build an indicator that make a cumulative sum of the following statement:

if( Close[i]>High[i+1] )

{

(Close[i]-High[i+1]) ; // add this quantity to the value of the indicator

}

if( Close[i]<High[i+1] && High[i]>High[i+1] )

{

(High[i+1]-Close[i])/Point; // subctract this quantity to the value of the indicator

}

i--;

is this possible?

Thank you!

 
Alberto_jazz:

Thank you for the answer! As you see I’m a beginner :-)

I’m trying to build an indicator that make a cumulative sum of the following statement:

if( Close[i]>High[i+1] )

{

(Close[i]-High[i+1]) ; // add this quantity to the value of the indicator

}

if( Close[i]<High[i+1] && High[i]>High[i+1] )

{

(High[i+1]-Close[i])/Point; // subctract this quantity to the value of the indicator

}

i--;

is this possible?

Thank you!

It is of course and I belive that now this indicator calculates such sum. Different matter is if it is usefull and how to interpret results

Reason: