# Coding a formula

9

How do you code the following formula:

[ (Number of Periods) - (Number of Periods Since Highest High)]     *  100

(Number of Periods)

Any help would be sincerely appreciated.

Moderator
32833

614

Hi

Try this example:

```// Number of periods since last HIGH.
//-----------------------------------
int nMyPeriod      = 300; // Your time period.
double dHigh_Value = 0;  // MAX HIGH value.

// Array for storing the HIGH value of the BARS
double aHigh_Value_Data[];
CopyHigh(Symbol(), Period(), 1, nMyPeriod, aHigh_Value_Data);

// Indexnumber for the time that the HIGH value is updated.
int nIndexNumber_For_High_Time = 0;

for(int i = 0; i < nMyPeriod; i++)
{

// HIGH value is calculated for the first Time in the LOOP.
if(dHigh_Value == 0 || i == 0)
{
dHigh_Value                = aHigh_Value_Data[i]; // Update HIGH Value.
nIndexNumber_For_High_Time = (nMyPeriod - i);     // Update index time value (reverse the index value, LAST bar is 1).
}

// Check if NEW HIGH value is highter then MAX High value.
if(aHigh_Value_Data[i] > dHigh_Value)
{
dHigh_Value                = aHigh_Value_Data[i]; // Update HIGH Value.
nIndexNumber_For_High_Time = (nMyPeriod - i);     // Update index time value (reverse the index value, LAST bar is 1).
}
}

// Ratio time value.
double dMy_Time_Ratio_Value = 0;

// Check if value is greater then 0.
if(nMyPeriod > 0)
{
dMy_Time_Ratio_Value = ((nMyPeriod - nIndexNumber_For_High_Time) * 100) / nMyPeriod;
}

Alert("High Value is = ", dHigh_Value, " Ratio = ", dMy_Time_Ratio_Value); ```