wto, 22 kwi 2025, 12:29 CEST, NY 6:29, Londyn 11:29, Tokio 19:29, WIG20 +0.98%
 Symbol np: ^SPX   
 Symbol np: ^SPX   
Login i hasło     zapisz  
How to construct the own indicators?
< Return to the chart
The exercise of the option to create your own indicators is first necessary to activate its service in the application configurator. Turn on the option "Obsługa własnych oscylatorów".

To create, edit or delete your own indicator, click the right mouse button on the menu with a list of indicators in the application of Technical Analysis.

Predefined functions

You can construct your own indicators using JavaScript.
For the construction, use several predefined functions:

Open() - Opening price
High() - Rates of the highest level
Low() - Rates of the lowest level
Close() - Rates of closure or last course
Volume() - Value of the volume
OpenInt() - The number of open positions

Max(array, period) - The highest value from the value of the plate array for the period is the period
Min(array, period) - The lowest value from the value of the plate array for the period is the period
SimpleAvg(array, period) - The arithmetic average from the value of the plate array for the period is the period
ExpAvg(array, period) - The exponential average from the value of the plate array for the period is the period
StdDev(array, period) - The standard deviation from the value of the plate array for the period is the period

CreateArray(length) - Initiates plates with the number of elements equal length
Param(n) - n parameter, the value of the first, second or third parameters field

AddGraph(array, index) - Draw lines from the plate array, starting from the item number index (index is optional)
AddHorizLine(value) - Draw a horizontal line at the height of the value
AddBuySignal(index) - Place a buy signal on the element of the plate number index
AddSellSignal(index) - Place a sell signal on the element of the plate number index


How to construct the own indicators?

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 close = Close();

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.

AddGraph(chng, 1);

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.

AddHorizLine(0);

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,...

var n = Param(1);

...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.


Possible problems

If after creating your own indicator is not showing its course, one ought, in order to refresh data, to change the chart interval, for example, from daily to weekly. After this act the indicator should appear correctly.


© 2000-2025 Stooq