Change%Indicator example imaging percentage changes in the prices of the previous session
var close = Close(); var chng = CreateArray(close.length); for(var i = 0;i < close.length; i++) chng[i] = (close[i] - close[i-1]) / close[i-1] * 100; AddGraph(chng, 1); AddHorizLine(0);
|
... after another...
var place before any defined variable, which ensures that there will be no global variable type, which will not interfere with the definitions of other indicators.
close is the name of our variable, which assign tables of values of closing rates (Close()).
The semicolon at the end of the line marks is an end of lines of code.
var chng = CreateArray(close.length);
|
Variable chng assign with the function CreateArray the type ,,table" (array) and identify the number of its elements. The number that we obtain from the length (length) of the plate close, which is defined previously. All the words close.length return the amount of plate close.
for(var i=0; i < close.length; i++) chng[i] = (close[i] - close[i-1]) / close[i-1] * 100;
|
for is a loop, which will perform as many times until it met the condition "i < close.length". At the beginning of the loop, we assign the value 0 for the variable i. Along with each loop jump, the variable is incremented (increased) by one ( "i++" expression is synonymous with saving "i+=1").
Each loop transition pursues the orders, which are described with the formula "chng[i] = (close[i] - close[i-1]) / close[i-1] * 100", it means those of the already before formed variable chng of the array type, together with another index i, the result of the difference quotient of the current value close (close[i]) and of the previous value close (close[i-1]) and of the product of the previous value close (close[i-1]) and of the number 100 will be attributable.
The whole operation will return the plate chng corresponding daily percentage change in prices.
Time to draw a line with the elements of the plate chng using the predefined function AddGraph. 1 reported as a second function argument means that we will begin drawing from the second element.
At the end we will draw a horizontal line at a height of 0 points with the predefined function AddHorizLine.
Rate of Change
var close = Close(); var roc = CreateArray(close.length); var n = Param(1); for(var i = 0; i < close.length; i++) roc[i] = (close[i] - close[i-n]) / close[i-n] * 100; AddGraph(roc, n); AddHorizLine(0); AddHorizLine(10); AddHorizLine(-10);
|
The above example is a modification of the already previously described Change% indicator. Here, with the first parameter field,...
...we can at the function entry determine the number of sessions that will be taken into account when calculating the percentage change in prices (using the parameters fields should be sure to enter a default value for the proper field in the right upper part of the indicators edition window).
Furthermore, uploads are two horizontal lines at a height of 10 and -10 points.
AddHorizLine(10); AddHorizLine(-10);
|
MACD
var close = Close(); var avg1 = ExpAvg(close, Param(1)); var avg2 = ExpAvg(close, Param(2)); var macd = CreateArray(avg1.length); for(var i = 0; i < avg1.length; i++) macd[i] = avg1[i] - avg2[i]; AddGraph(macd, Param(2)); var signal = ExpAvg(macd, Param(3)); AddGraph(signal, Param(2) + Param(3)); AddHorizLine(0);
|
The above function builds the MACD indicator, which is required to use the exponential averages.
var avg1 = ExpAvg(close, Param(1)); var avg2 = ExpAvg(close, Param(2));
|
The function ExpAvg creates an array with the values of the exponential average of the rates close for a period corresponding to the value of the fields Param(1) and Param(2).
var signal = ExpAvg(macd, Param(3));
|
The variable signal assign an array with the values of the exponential average of mileage macd (two lines previously calculated) for the period corresponding to the value of the field Param(3).
MACD with buy/sell signals
var close = Close(); var avg1 = ExpAvg(close, Param(1)); var avg2 = ExpAvg(close, Param(2)); var macd = CreateArray(avg1.length); for(var i = 0; i < avg1.length; i++) macd[i] = avg1[i] - avg2[i]; AddGraph(macd, Param(2)); var signal = ExpAvg(macd, Param(3)); AddGraph(signal, Param(2) + Param(3)); AddHorizLine(0); for(var i = Param(2) + Param(3); i < macd.length; i++) { if((macd[i-1] < signal[i-1]) && (macd[i] > signal[i])) AddBuySignal(i); else if((macd[i-1] > signal[i-1]) && (macd[i] < signal[i])) AddSellSignal(i); }
|
In the above example has been added sequence, in which the loop for ...
for(var i = Param(2) + Param(3); i < macd.length; i++) { ... }
|
... checks the position of the line macd and its mean, that is line signal, relative to one another.
if((macd[i-1] < signal[i-1]) && (macd[i] > signal[i])) AddBuySignal(i); else if((macd[i-1] > signal[i-1]) && (macd[i] < signal[i])) AddSellSignal(i);
|
If the previous value macd (macd[i-1]) is less than the previous value of signal (signal[i-1]) and the current value of macd (macd[i]) is greater than the current value of signal (signal[i]),buy signal is generated with the function AddBuySignal and it is placed on the mileage of the plate macd in the place where the index i meets these conditions.
It is analogous with sell signal AddSellSignal, but of course to reverse conditions.