MathComplexTrignometryLibrary "MathComplexTrignometry"
Methods for complex number trignometry operations.
sinh(complex) Hyperbolic Sine of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
cosh(complex) Hyperbolic cosine of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
tanh(complex) Hyperbolic tangent of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
coth(complex) Hyperbolic cotangent of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
sech(complex) Hyperbolic Secant of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
csch(complex) Hyperbolic Cosecant of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
sin(complex) Trigonometric Sine of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
cos(complex) Trigonometric cosine of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
tan(complex) Trigonometric tangent of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
cot(complex) Trigonometric cotangent of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
sec(complex) Trigonometric Secant of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
csc(complex) Trigonometric Cosecant of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
asin(complex) Trigonometric Arc Sine of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
acos(complex) Trigonometric Arc Cosine of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
atan(complex) Trigonometric Arc Tangent of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
acot(complex) Trigonometric Arc Cotangent of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
asec(complex) Trigonometric Arc Secant of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
acsc(complex) Trigonometric Arc Cosecant of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
asinh(complex) Hyperbolic Arc Sine of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
acosh(complex) Hyperbolic Arc Cosine of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
atanh(complex) Hyperbolic Arc Tangent of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
acoth(complex) Hyperbolic Arc Cotangent of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
asech(complex) Hyperbolic Arc Secant of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
acsch(complex) Hyperbolic Arc Cosecant of complex number.
Parameters:
complex : float array, complex number.
Returns: float array.
Arrays
MathComplexExtensionLibrary "MathComplexExtension"
A set of utility functions to handle complex numbers.
get_phase(complex_number, in_radians) The phase value of complex number complex_number.
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
in_radians : boolean, value for the type of angle value, default=true, options=(true: radians, false: degrees)
Returns: float value with phase.
natural_logarithm(complex_number) Natural logarithm of complex number (base E).
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: float array, complex number.
common_logarithm(complex_number) Common logarithm of complex number (base 10).
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: float array, complex number.
logarithm(complex_number, base) Common logarithm of complex number (custom base).
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
base : float, base value.
Returns: float array, complex number.
power(complex_number, complex_exponent) Raise complex_number with complex_exponent.
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
complex_exponent : float array, pseudo complex number in the form of a array .
Returns: float array, pseudo complex number in the form of a array
root(complex_number, complex_exponent) Raise complex_number with inverse of complex_exponent.
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
complex_exponent : float array, pseudo complex number in the form of a array .
Returns: float array, pseudo complex number in the form of a array
square(complex_number) Square of complex_number (power 2).
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: float array, pseudo complex number in the form of a array
square_root(complex_number) Square root of complex_number (power 1/2).
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: float array, pseudo complex number in the form of a array
square_roots(complex_number) Square root of complex_number (power 1/2).
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: tuple with 2 complex numbers.
cubic_roots(complex_number) Square root of complex_number (power 1/2).
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: tuple with 2 complex numbers.
to_polar_form(complex_number, in_radians) The polar form value of complex_number.
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
in_radians : boolean, value for the type of angle value, default=true, options=(true: radians, false: degrees)
Returns: float array, pseudo complex number in the form of a array
** returns a array
MathComplexOperatorLibrary "MathComplexOperator"
A set of utility functions to handle complex numbers.
conjugate(complex_number) Computes the conjugate of complex_number by reversing the sign of the imaginary part.
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: float array, pseudo complex number in the form of a array
add(complex_number_a, complex_number_b) Adds complex number complex_number_b to complex_number_a, in the form:
.
Parameters:
complex_number_a : pseudo complex number in the form of a array .
complex_number_b : pseudo complex number in the form of a array .
Returns: float array, pseudo complex number in the form of a array
subtract(complex_number_a, complex_number_b) Subtract complex_number_b from complex_number_a, in the form:
.
Parameters:
complex_number_a : float array, pseudo complex number in the form of a array .
complex_number_b : float array, pseudo complex number in the form of a array .
Returns: float array, pseudo complex number in the form of a array
multiply(complex_number_a, complex_number_b) Multiply complex_number_a with complex_number_b, in the form:
Parameters:
complex_number_a : float array, pseudo complex number in the form of a array .
complex_number_b : float array, pseudo complex number in the form of a array .
Returns: float array, pseudo complex number in the form of a array
divide(complex_number_a, complex_number_b) Divide complex_number _a with _b, in the form:
Parameters:
complex_number_a : float array, pseudo complex number in the form of a array .
complex_number_b : float array, pseudo complex number in the form of a array .
Returns: float array, pseudo complex number in the form of a array
reciprocal(complex_number) Computes the reciprocal or inverse of complex_number.
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: float array, pseudo complex number in the form of a array
negative(complex_number) Negative of complex_number, in the form:
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: float array, pseudo complex number in the form of a array
inverse(complex_number) Inverse of complex_number, in the form:
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: float array, pseudo complex number in the form of a array
exponential(complex_number) Exponential of complex_number.
Parameters:
complex_number : pseudo complex number in the form of a array .
Returns: float array, pseudo complex number in the form of a array
ceil(complex_number, digits) Ceils complex_number.
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
digits : int, digits to use as ceiling.
Returns: _complex: pseudo complex number in the form of a array
radius(complex_number) Radius(magnitude) of complex_number, in the form:
This is defined as its distance from the origin (0,0) of the complex plane.
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: float value with radius.
magnitude(complex_number) magnitude(absolute value) of complex_number, should be the same as the radius.
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: float.
magnitude_squared(complex_number) magnitude(absolute value) of complex_number, should be the same as the radius.
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: float.
sign(complex_number) Unity of complex numbers.
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
Returns: float array, complex number.
MathComplexArrayLibrary "MathComplexArray"
Array methods to handle complex number arrays.
new(size, initial_complex) Prototype to initialize a array of complex numbers.
Parameters:
size : size of the array.
initial_complex : Complex number to be used as default value, in the form of array .
Returns: float array, pseudo complex Array in the form of a array
get(id, index) Get the complex number in a array, in the form of a array
Parameters:
id : float array, ID of the array.
index : int, Index of the complex number.
Returns: float array, pseudo complex number in the form of a array
set(id, index, complex_number) Sets the values complex number in a array.
Parameters:
id : float array, ID of the array.
index : int, Index of the complex number.
complex_number : float array, Complex number, in the form: .
Returns: Void, updates array id.
push(id, complex_number) Push the values into a complex number array.
Parameters:
id : float array, ID of the array.
complex_number : float array, Complex number, in the form: .
Returns: Void, updates array id.
pop(id, complex_number) Pop the values from a complex number array.
Parameters:
id : float array, ID of the array.
complex_number : float array, Complex number, in the form: .
Returns: Void, updates array id.
to_string(id, format) Reads a array of complex numbers into a string, of the form: " [ , ... ]""
Parameters:
id : float array, ID of the array.
format : string, format of the number conversion, default='#.##########'.
Returns: string, translated complex array into string.
MathComplexCoreLibrary "MathComplexCore"
Core functions to handle complex numbers.
set_real(complex_number, real) Set the real part of complex_number.
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
real : float, value to replace real value of complex_number.
Returns: Void, Modifies complex_number.
set_imaginary(complex_number, imaginary) Set the imaginary part of complex_number.
Parameters:
complex_number : float array, pseudo complex number in the form of a array .
imaginary : float, value to replace imaginary value of complex_number.
Returns: Void, Modifies complex_number.
new(real, imaginary) Creates a prototype array to handle complex numbers.
Parameters:
real : float, real value of the complex number. default=0.
imaginary : float, imaginary number of the complex number. default=0.
@return float array, pseudo complex number in the form of a array .
zero() complex number "0+0i".
@return float array, pseudo complex number in the form of a array .
one() complex number "1+0i".
@return float array, pseudo complex number in the form of a array .
imaginary_one() complex number "0+1i".
@return float array, pseudo complex number in the form of a array .
nan() complex number "0+1i".
@return float array, pseudo complex number in the form of a array .
from_polar_coordinates(magnitude, phase) Create a complex number from a point's polar coordinates.
Parameters:
magnitude : float, default=0.0, The magnitude, which is the distance from the origin (the intersection of the x-axis and the y-axis) to the number.
phase : float, default=0.0, The phase, which is the angle from the line to the horizontal axis, measured in radians.
@return float array, pseudo complex number in the form of a array .
get_real(complex_number) Get the real part of complex_number.
Parameters:
complex_number : pseudo complex number in the form of a array .
Returns: float, Real part of the complex_number.
get_imaginary(complex_number) Get the imaginary part of complex_number.
Parameters:
complex_number : pseudo complex number in the form of a array .
Returns: float, Imaginary part of the complex number.
is_complex(complex_number) Checks that its a valid complex_number.
Parameters:
complex_number : pseudo complex number in the form of a array .
Returns: bool.
is_nan(complex_number) Checks that its empty "na" complex_number.
Parameters:
complex_number : pseudo complex number in the form of a array .
Returns: bool.
is_real(complex_number) Checks that the complex_number is real.
Parameters:
complex_number : pseudo complex number in the form of a array .
Returns: bool.
is_real_non_negative(complex_number) Checks that the complex_number is real and not negative.
Parameters:
complex_number : pseudo complex number in the form of a array .
Returns: bool.
is_zero(complex_number) Checks that the complex_number is zero.
Parameters:
complex_number : pseudo complex number in the form of a array .
Returns: bool.
equals(complex_number_a, complex_number_b) Compares two complex numbers:
Parameters:
complex_number_a : float array, pseudo complex number in the form of a array .
complex_number_b : float array, pseudo complex number in the form of a array .
Returns: boolean value representing the equality.
to_string(complex, format) Converts complex_number to a string format, in the form: "a+bi"
Parameters:
complex : pseudo complex number in the form of a array .
format : string, formating to apply.
Returns: a string in "a+bi" format
FunctionBestFitFrequencyLibrary "FunctionBestFitFrequency"
TODO: add library description here
array_moving_average(sample, length, ommit_initial, fillna) Moving Average values for selected data.
Parameters:
sample : float array, sample data values.
length : int, length to smooth the data.
ommit_initial : bool, default=true, ommit values at the start of the data under the length.
fillna : string, default='na', options='na', '0', 'avg'
Returns: float array
errors:
length > sample size "Canot call array methods when id of array is na."
best_fit_frequency(sample, start, end) Search a frequency range for the fairest moving average frequency.
Parameters:
sample : float array, sample data to based the moving averages.
start : int lowest frequency.
end : int highest frequency.
Returns: tuple with (int frequency, float percentage)
ArrayStatisticsLibrary "ArrayStatistics"
Statistic Functions using arrays.
rms(sample) Root Mean Squared
Parameters:
sample : float array, data sample points.
Returns: float
skewness_pearson1(sample) Pearson's 1st Coefficient of Skewness.
Parameters:
sample : float array, data sample.
Returns: float
skewness_pearson2(sample) Pearson's 2nd Coefficient of Skewness.
Parameters:
sample : float array, data sample.
Returns: float
pearsonr(sample_a, sample_b) Pearson correlation coefficient measures the linear relationship between two datasets.
Parameters:
sample_a : float array, sample with data.
sample_b : float array, sample with data.
Returns: float p
kurtosis(sample) Kurtosis of distribution.
Parameters:
sample : float array, data sample.
Returns: float
range_int(sample, percent) Get range around median containing specified percentage of values.
Parameters:
sample : int array, Histogram array.
percent : float, Values percentage around median.
Returns: tuple with , Returns the range which containes specifies percentage of values.
ArrayOperationsIntLibrary "ArrayOperationsInt"
Array Basic Operations for Integers
add(sample_a, sample_b) Adds sample_b to sample_a and returns a new array.
Parameters:
sample_a : values to be added to.
sample_b : values to add.
Returns: int array with added results.
subtract(sample_a, sample_b) subtracts sample_b from sample_a and returns a new array.
Parameters:
sample_a : values to be subtracted from.
sample_b : values to subtract.
Returns: int array with subtracted results.
multiply(sample_a, sample_b) multiply sample_a with sample_b and returns a new array.
Parameters:
sample_a : values to multiply.
sample_b : values to multiply with.
Returns: int array with multiplied results.
divide(sample_a, sample_b) divide sample_a with sample_b and returns a new array.
Parameters:
sample_a : values to divide.
sample_b : values to divide with.
Returns: int array with divided results.
power(sample_a, sample_b) rise sample_a to the power of sample_b and returns a new array.
Parameters:
sample_a : base values to raise.
sample_b : values of exponents.
Returns: int array with raised results.
remainder(sample_a, sample_b) integer remainder of sample_a under the dividend sample_b and returns a new array.
Parameters:
sample_a : values of quotients.
sample_b : values of dividends.
Returns: int array with remainder results.
ArrayOperationsFloatLibrary "ArrayOperationsFloat"
Array Basic Operations for Integers
add(sample_a, sample_b) Adds sample_b to sample_a and returns a new array.
Parameters:
sample_a : values to be added to.
sample_b : values to add.
Returns: float array with added results.
subtract(sample_a, sample_b) subtracts sample_b from sample_a and returns a new array.
Parameters:
sample_a : values to be subtracted from.
sample_b : values to subtract.
Returns: float array with subtracted results.
multiply(sample_a, sample_b) multiply sample_a with sample_b and returns a new array.
Parameters:
sample_a : values to multiply.
sample_b : values to multiply with.
Returns: float array with multiplied results.
divide(sample_a, sample_b) divide sample_a with sample_b and returns a new array.
Parameters:
sample_a : values to divide.
sample_b : values to divide with.
Returns: float array with divided results.
power(sample_a, sample_b) rise sample_a to the power of sample_b and returns a new array.
Parameters:
sample_a : base values to raise.
sample_b : values of exponents.
Returns: float array with raised results.
remainder(sample_a, sample_b) float remainder of sample_a under the dividend sample_b and returns a new array.
Parameters:
sample_a : values of quotients.
sample_b : values of dividends.
Returns: float array with remainder results.
bursamalaysianonshariahLibrary "bursamalaysianonshariah"
List of non-Shariah stock for Bursa Malaysia as of Oct 2021
No parameter required
status() will return 1 if ticker in the list, 0 if ticker not in the list and 2 if ticker not from Bursa Malaysia
Example usage :
//@version=5
indicator("My Script", overlay = true)
import BURSATRENDBANDCHART/bursamalaysianonshariah/1 as b
bgcolor(status() == 1 ? color.new(color.red, 90) : status() == 0 ? color.new(color.green, 90) : color.new(color.blue, 90))
Special thanks to
wmsafwan
RozaniGhani-RG
Vector2OperationsLibrary "Vector2Operations"
functions to handle vector2 operations.
math_fractional(_value) computes the fractional part of the argument value.
Parameters:
_value : float, value to compute.
Returns: float, fractional part.
atan2(_a) Approximation to atan2 calculation, arc tangent of y/ x in the range radians.
Parameters:
_a : vector2 in the form of a array .
Returns: float, value with angle in radians. (negative if quadrante 3 or 4)
set_x(_a, _value) Set the x value of vector _a.
Parameters:
_a : vector2 in the form of a array .
_value : value to replace x value of _a.
Returns: void Modifies vector _a.
set_y(_a, _value) Set the y value of vector _a.
Parameters:
_a : vector in the form of a array .
_value : value to replace y value of _a.
Returns: void Modifies vector _a.
get_x(_a) Get the x value of vector _a.
Parameters:
_a : vector in the form of a array .
Returns: float, x value of the vector _a.
get_y(_a) Get the y value of vector _a.
Parameters:
_a : vector in the form of a array .
Returns: float, y value of the vector _a.
get_xy(_a) Return the tuple of vector _a in the form
Parameters:
_a : vector2 in the form of a array .
Returns:
length_squared(_a) Length of vector _a in the form. , for comparing vectors this is computationaly lighter.
Parameters:
_a : vector in the form of a array .
Returns: float, squared length of vector.
length(_a) Magnitude of vector _a in the form.
Parameters:
_a : vector in the form of a array .
Returns: float, Squared length of vector.
vmin(_a) Lowest element of vector.
Parameters:
_a : vector in the form of a array .
Returns: float
vmax(_a) Highest element of vector.
Parameters:
_a : vector in the form of a array .
Returns: float
from(_value) Assigns value to a new vector x,y elements.
Parameters:
_value : x and y value of the vector. optional.
Returns: float vector.
new(_x, _y) Creates a prototype array to handle vectors.
Parameters:
_x : float, x value of the vector. optional.
_y : float, y number of the vector. optional.
Returns: float vector.
down() Vector in the form . Returns: float vector.
left() Vector in the form . Returns: float vector.
one() Vector in the form . Returns: float vector.
right() Vector in the form . Returns: float vector
up() Vector in the form . Returns: float vector
zero() Vector in the form . Returns: float vector
add(_a, _b) Adds vector _b to _a, in the form
.
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
Returns:
subtract(_a, _b) Subtract vector _b from _a, in the form
.
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
Returns:
multiply(_a, _b) Multiply vector _a with _b, in the form
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
Returns:
divide(_a, _b) Divide vector _a with _b, in the form
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
Returns:
negate(_a) Negative of vector _a, in the form
Parameters:
_a : vector in the form of a array .
Returns:
perp(_a) Perpendicular Vector of _a.
Parameters:
_a : vector in the form of a array .
Returns:
vfloor(_a) Compute the floor of argument vector _a.
Parameters:
_a : vector in the form of a array .
Returns:
fractional(_a) Compute the fractional part of the elements from vector _a.
Parameters:
_a : vector in the form of a array .
Returns:
vsin(_a) Compute the sine of argument vector _a.
Parameters:
_a : vector in the form of a array .
Returns:
equals(_a, _b) Compares two vectors
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
Returns: boolean value representing the equality.
dot(_a, _b) Dot product of 2 vectors, in the form
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
Returns: float
cross_product(_a, _b) cross product of 2 vectors, in the form
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
Returns: float
scale(_a, _scalar) Multiply a vector by a scalar.
Parameters:
_a : vector in the form of a array .
_scalar : value to multiply vector elements by.
Returns: float vector
normalize(_a) Vector _a normalized with a magnitude of 1, in the form.
Parameters:
_a : vector in the form of a array .
Returns: float vector
rescale(_a) Rescale a vector to a new Magnitude.
Parameters:
_a : vector in the form of a array .
Returns:
rotate(_a, _radians) Rotates vector _a by angle value
Parameters:
_a : vector in the form of a array .
_radians : Angle value.
Returns:
rotate_degree(_a, _degree) Rotates vector _a by angle value
Parameters:
_a : vector in the form of a array .
_degree : Angle value.
Returns:
rotate_around(_center, _target, _degree) Rotates vector _target around _origin by angle value
Parameters:
_center : vector in the form of a array .
_target : vector in the form of a array .
_degree : Angle value.
Returns:
vceil(_a, _digits) Ceils vector _a
Parameters:
_a : vector in the form of a array .
_digits : digits to use as ceiling.
Returns:
vpow(_a) Raise both vector elements by a exponent.
Parameters:
_a : vector in the form of a array .
Returns:
distance(_a, _b) vector distance between 2 vectors.
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
Returns: float, distance.
project(_a, _axis) Project a vector onto another.
Parameters:
_a : vector in the form of a array .
_axis : float vector2
Returns: float vector
projectN(_a, _axis) Project a vector onto a vector of unit length.
Parameters:
_a : vector in the form of a array .
_axis : vector in the form of a array .
Returns: float vector
reflect(_a, _b) Reflect a vector on another.
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
Returns: float vector
reflectN(_a, _b) Reflect a vector to a arbitrary axis.
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
Returns: float vector
angle(_a) Angle in radians of a vector.
Parameters:
_a : vector in the form of a array .
Returns: float
angle_unsigned(_a, _b) unsigned degree angle between 0 and +180 by given two vectors.
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
Returns: float
angle_signed(_a, _b) Signed degree angle between -180 and +180 by given two vectors.
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
Returns: float
angle_360(_a, _b) Degree angle between 0 and 360 by given two vectors
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
Returns: float
clamp(_a, _vmin, _vmax) Restricts a vector between a min and max value.
Parameters:
_a : vector in the form of a array .
_vmin : vector in the form of a array .
_vmax : vector in the form of a array .
Returns: float vector
lerp(_a, _b, _rate_of_move) Linearly interpolates between vectors a and b by _rate_of_move.
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
_rate_of_move : float value between (a:-infinity -> b:1.0), negative values will move away from b.
Returns: vector in the form of a array
herp(_a, _b, _rate_of_move) Hermite curve interpolation between vectors a and b by _rate_of_move.
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
_rate_of_move : float value between (a-infinity -> b1.0), negative values will move away from b.
Returns: vector in the form of a array
area_triangle(_a, _b, _c) Find the area in a triangle of vectors.
Parameters:
_a : vector in the form of a array .
_b : vector in the form of a array .
_c : vector in the form of a array .
Returns: float
to_string(_a) Converts vector _a to a string format, in the form "(x, y)"
Parameters:
_a : vector in the form of a array .
Returns: string in "(x, y)" format
vrandom(_max) 2D random value
Parameters:
_max : float vector, vector upper bound
Returns: vector in the form of a array
noise(_a) 2D Noise based on Morgan McGuire @morgan3d
thebookofshaders.com
www.shadertoy.com
Parameters:
_a : vector in the form of a array .
Returns: vector in the form of a array
array_new(_size, _initial_vector) Prototype to initialize a array of vectors.
Parameters:
_size : size of the array.
_initial_vector : vector to be used as default value, in the form of array .
Returns: _vector_array complex Array in the form of a array
array_size(_id) number of vector elements in array.
Parameters:
_id : ID of the array.
Returns: int
array_get(_id, _index) Get the vector in a array, in the form of a array
Parameters:
_id : ID of the array.
_index : Index of the vector.
Returns: vector in the form of a array
array_set(_id, _index, _a) Sets the values vector in a array.
Parameters:
_id : ID of the array.
_index : Index of the vector.
_a : vector, in the form .
Returns: Void, updates array _id.
array_push(_id, _a) inserts the vector at the end of array.
Parameters:
_id : ID of the array.
_a : vector, in the form .
Returns: Void, updates array _id.
array_unshift(_id, _a) inserts the vector at the begining of array.
Parameters:
_id : ID of the array.
_a : vector, in the form .
Returns: Void, updates array _id.
array_pop(_id, _a) removes the last vector of array and returns it.
Parameters:
_id : ID of the array.
_a : vector, in the form .
Returns: vector2, updates array _id.
array_shift(_id, _a) removes the first vector of array and returns it.
Parameters:
_id : ID of the array.
_a : vector, in the form .
Returns: vector2, updates array _id.
array_sum(_id) Total sum of all vectors.
Parameters:
_id : ID of the array.
Returns: vector in the form of a array
array_center(_id) Finds the vector center of the array.
Parameters:
_id : ID of the array.
Returns: vector in the form of a array
array_rotate_points(_id) Rotate Array vectors around origin vector by a angle.
Parameters:
_id : ID of the array.
Returns: rotated points array.
array_scale_points(_id) Scale Array vectors based on a origin vector perspective.
Parameters:
_id : ID of the array.
Returns: rotated points array.
array_tostring(_id, _separator) Reads a array of vectors into a string, of the form " ""
Parameters:
_id : ID of the array.
_separator : string separator for cell splitting.
Returns: string Translated complex array into string.
line_new(_a, _b) 2 vector line in the form.
Parameters:
_a : vector, in the form .
_b : vector, in the form .
Returns:
line_get_a(_line) Start vector of a line.
Parameters:
_line : vector4, in the form .
Returns: float vector2
line_get_b(_line) End vector of a line.
Parameters:
_line : vector4, in the form .
Returns: float vector2
line_intersect(_line1, _line2) Find the intersection vector of 2 lines.
Parameters:
_line1 : line of 2 vectors in the form of a array .
_line2 : line of 2 vectors in the form of a array .
Returns: vector in the form of a array .
draw_line(_line, _xloc, _extend, _color, _style, _width) Draws a line using line prototype.
Parameters:
_line : vector4, in the form .
_xloc : string
_extend : string
_color : color
_style : string
_width : int
Returns: draw line object
draw_triangle(_v1, _v2, _v3, _xloc, _color, _style, _width) Draws a triangle using line prototype.
Parameters:
_v1 : vector4, in the form .
_v2 : vector4, in the form .
_v3 : vector4, in the form .
_xloc : string
_color : color
_style : string
_width : int
Returns: tuple with 3 line objects.
draw_rect(_v1, _size, _angle, _xloc, _color, _style, _width) Draws a square using vector2 line prototype.
Parameters:
_v1 : vector4, in the form .
_size : float
_angle : float
_xloc : string
_color : color
_style : string
_width : int
Returns: tuple with 3 line objects.
SignalProcessingClusteringKMeansLibrary "SignalProcessingClusteringKMeans"
K-Means Clustering Method.
nearest(point_x, point_y, centers_x, centers_y) finds the nearest center to a point and returns its distance and center index.
Parameters:
point_x : float, x coordinate of point.
point_y : float, y coordinate of point.
centers_x : float array, x coordinates of cluster centers.
centers_y : float array, y coordinates of cluster centers.
@ returns tuple of int, float.
bisection_search(samples, value) Bissection Search
Parameters:
samples : float array, weights to compare.
value : float array, weights to compare.
Returns: int.
label_points(points_x, points_y, centers_x, centers_y) labels each point index with cluster index and distance.
Parameters:
points_x : float array, x coordinates of points.
points_y : float array, y coordinates of points.
centers_x : float array, x coordinates of points.
centers_y : float array, y coordinates of points.
Returns: tuple with int array, float array.
kpp(points_x, points_y, n_clusters) K-Means++ Clustering adapted from Andy Allinger.
Parameters:
points_x : float array, x coordinates of the points.
points_y : float array, y coordinates of the points.
n_clusters : int, number of clusters.
Returns: tuple with 2 arrays, float array, int array.
AnalysisInterpolationLoessLibrary "AnalysisInterpolationLoess"
LOESS, local weighted Smoothing function.
loess(sample_x, sample_y, point_span) LOESS, local weighted Smoothing function.
Parameters:
sample_x : int array, x values.
sample_y : float array, y values.
point_span : int, local point interval span.
aloess(sample_x, sample_y, point_span) aLOESS, adaptive local weighted Smoothing function.
Parameters:
sample_x : int array, x values.
sample_y : float array, y values.
point_span : int, local point interval span.
ArrayExtensionLibrary "ArrayExtension"
Functions to extend Arrays.
index_2d_to_1d(dimension_x, dimension_y, index_x, index_y) returns the flatened one dimension index of a two dimension array.
Parameters:
dimension_x : int, dimension of X.
dimension_y : int, dimension of Y.
index_x : int, index of X.
index_y : int, index of Y.
Returns: int, index in 1 dimension
index_3d_to_1d(dimension_x, dimension_y, dimension_z, index_x, index_y, index_z) returns the flatened one dimension index of a three dimension array.
Parameters:
dimension_x : int, dimension of X.
dimension_y : int, dimension of Y.
dimension_z : int, dimension of Z.
index_x : int, index of X.
index_y : int, index of Y.
index_z : int, index of Z.
Returns: int, index in 1 dimension
down_sample(sample, new_size) Down samples a array to a specified size.
Parameters:
sample : float array, array with source data.
new_size : new size of down sampled array.
Returns: float array with down sampled data.
sort_indices_float(sample, order) Sorts array and returns a extra array with sorting indices.
Parameters:
sample : float array with values to be sorted.
order : string, default='forward', options='forward', 'backward'.
Returns: _indices int array with indices.
_ordered float array with ordered values.
sort_indices_int(sample, order) Sorts array and returns a extra array with sorting indices.
Parameters:
sample : int array with values to be sorted.
order : string, default='forward', options='forward', 'backward'.
Returns: _indices int array with indices.
_ordered float array with ordered values.
sort_bool_from_indices(indices, sample) Sorts sample array using a array with indices.
Parameters:
indices : int array with positional indices.
sample : bool array with data sample to be sorted.
Returns: bool array
sort_box_from_indices(indices, sample) Sorts sample array using a array with indices.
Parameters:
indices : int array with positional indices.
sample : box array with data sample to be sorted.
Returns: box array
sort_color_from_indices(indices, sample) Sorts sample array using a array with indices.
Parameters:
indices : int array with positional indices.
sample : color array with data sample to be sorted.
Returns: color array
sort_float_from_indices(indices, sample) Sorts sample array using a array with indices.
Parameters:
indices : int array with positional indices.
sample : float array with data sample to be sorted.
Returns: float array
sort_int_from_indices(indices, sample) Sorts sample array using a array with indices.
Parameters:
indices : int array with positional indices.
sample : int array with data sample to be sorted.
Returns: int array
sort_label_from_indices(indices, sample) Sorts sample array using a array with indices.
Parameters:
indices : int array with positional indices.
sample : label array with data sample to be sorted.
Returns: label array
sort_line_from_indices(indices, sample) Sorts sample array using a array with indices.
Parameters:
indices : int array with positional indices.
sample : line array with data sample to be sorted.
Returns: line array
sort_string_from_indices(indices, sample) Sorts sample array using a array with indices.
Parameters:
indices : int array with positional indices.
sample : string array with data sample to be sorted.
Returns: string array
sort_table_from_indices(indices, sample) Sorts sample array using a array with indices.
Parameters:
indices : int array with positional indices.
sample : table array with data sample to be sorted.
Returns: table array
sort_bool_inplace_from_indices(indices, sample) Sorts sample array inplace using a array with indices.
Parameters:
indices : int array with positional indices.
sample : bool array with data sample to be sorted.
Returns: void updates sample array.
sort_box_inplace_from_indices(indices, sample) Sorts sample array inplace using a array with indices.
Parameters:
indices : int array with positional indices.
sample : box array with data sample to be sorted.
Returns: void updates sample
sort_color_inplace_from_indices(indices, sample) Sorts sample array inplace using a array with indices.
Parameters:
indices : int array with positional indices.
sample : color array with data sample to be sorted.
Returns: void updates sample
sort_float_inplace_from_indices(indices, sample) Sorts sample array inplace using a array with indices.
Parameters:
indices : int array with positional indices.
sample : float array with data sample to be sorted.
Returns: void updates sample
sort_int_inplace_from_indices(indices, sample) Sorts sample array inplace using a array with indices.
Parameters:
indices : int array with positional indices.
sample : int array with data sample to be sorted.
Returns: void updates sample
sort_label_inplace_from_indices(indices, sample) Sorts sample array inplace using a array with indices.
Parameters:
indices : int array with positional indices.
sample : label array with data sample to be sorted.
Returns: void updates sample
sort_line_inplace_from_indices(indices, sample) Sorts sample array inplace using a array with indices.
Parameters:
indices : int array with positional indices.
sample : line array with data sample to be sorted.
Returns: void updates sample
sort_string_inplace_from_indices(indices, sample) Sorts sample inplace array using a array with indices.
Parameters:
indices : int array with positional indices.
sample : string array with data sample to be sorted.
Returns: void updates sample
sort_table_inplace_from_indices(indices, sample) Sorts sample array inplace using a array with indices.
Parameters:
indices : int array with positional indices.
sample : table array with data sample to be sorted.
Returns: void updates sample
to_float(sample) Transform a integer array into a float array
Parameters:
sample : int array, sample data to transform.
Returns: float array
to_int(sample, method) Transform a float array into a int array
Parameters:
sample : float array, sample data to transform.
method : string, default="round", options= , aproximation method.
Returns: int array
ArrayGenerateLibrary "ArrayGenerate"
Functions to generate arrays.
sequence_int(start, end, step) returns a sequence of int numbers.
Parameters:
start : int, begining of sequence range.
end : int, end of sequence range.
step : int, step, default=1 .
Returns: int , array.
sequence_float(start, end, step) returns a sequence of float numbers.
Parameters:
start : float, begining of sequence range.
end : float, end of sequence range.
step : float, step, default=1.0 .
Returns: float , array.
sequence_from_series(src, length, shift, direction_forward) Creates a array from a series sample range.
Parameters:
src : series, any kind.
length : int, window period in bars to sample series.
shift : int, window period in bars to shift backwards the data sample, default=0.
direction_forward : bool, sample from start to end or end to start order, default=true.
Returns: float array
normal_distribution(size, mean, dev) Generate normal distribution random sample.
Parameters:
size : int, size of array
mean : float, mean of the sample, (default=0.0).
dev : float, deviation of the sample from the mean, (default=1.0).
Returns: float array.
log_spaced(length, start_exp, stop_exp) Generate a base 10 logarithmically spaced sample sequence.
Parameters:
length : int, length of the sequence.
start_exp : float, start exponent.
stop_exp : float, stop exponent.
Returns: float array.
linear_range(stop, start) Generate a linearly spaced sample vector within the inclusive interval (start, stop) and step 1.
Parameters:
stop : float, stop value.
start : float, start value, (default=0.0).
Returns: float array.
periodic_wave(length, sampling_rate, frequency, amplitude, phase, delay) Create a periodic wave.
Parameters:
length : int, the number of samples to generate.
sampling_rate : float, samples per time unit (Hz). Must be larger than twice the frequency to satisfy the Nyquist criterion.
frequency : float, frequency in periods per time unit (Hz).
amplitude : float, the length of the period when sampled at one sample per time unit. This is the interval of the periodic domain, a typical value is 1.0, or 2*Pi for angular functions.
phase : float, optional phase offset.
delay : int, optional delay, relative to the phase.
Returns: float array.
sinusoidal(length, sampling_rate, frequency, amplitude, mean, phase, delay) Create a Sine wave.
Parameters:
length : int, The number of samples to generate.
sampling_rate : float, Samples per time unit (Hz). Must be larger than twice the frequency to satisfy the Nyquist criterion.
frequency : float, Frequency in periods per time unit (Hz).
amplitude : float, The maximal reached peak.
mean : float, The mean, or DC part, of the signal.
phase : float, Optional phase offset.
delay : int, Optional delay, relative to the phase.
Returns: float array.
periodic_impulse(length, period, amplitude, delay) Create a periodic Kronecker Delta impulse sample array.
Parameters:
length : int, The number of samples to generate.
period : int, impulse sequence period.
amplitude : float, The maximal reached peak.
delay : int, Offset to the time axis. Zero or positive.
Returns: float array.
Matrix_Functions_Lib_JDLibrary "Matrix_Functions_Lib_JD"
This is a library to add matrix / 2D array functionality to Pinescript.
once you import the library at the beginning of your script, you can add all the functions described below just by calling them like you do any other built'in function.
Enjoy,
Gr, JD.
PS. if you find functionality or calculation errors in the functions, please let me know, so I can fix them.
There are quite a lot of functions, so little mishaps may have slipped in! ;-)
get_nr_of_rows() Returns the number of rows from a 2D matrix
get_nr_of_columns() Returns the number of columns from a 2D matrix
get_size() Returns a tuple with the total number of rows and columns from a 2D matrix
init() 2D matrix init function, builds a 2D matrix with dimensional metadata in first two values and fills it with a default value, the body of the actual matrix data starts at index 2.
from_list() 2D matrix init function, builds a 2D matrix from an existing array by adding dimensional metadata in first two values, the body of the actual matrix data consists of the data of the source array and starts at index 2.
set() Sets values in 2D matrix with (row index, column index) (index for rows and columns both starts at 0 !!)
fill_val() Fills all elements in a 2D matrix with a value
randomize() Fills a 2D matrix with random values//
get() Gets values from 2D matrix with (row index, column index) (index for rows and columns both starts at 0 !!)
copy_slice_body() Cuts off the metadata header and returns the array body, WITHOUT THE DIMENSIONAL METADATA!!
do_slice This variable should be set as: - 'false' to only make a copy, changes to the new array copy will NOT ALTER the ORIGINAL - 'true' to make a slice, changes to the new array slice WILL(!) ALTER the ORIGINAL
get_record() Gets /retrieve the values from a ROW/RECORD from a certain row/lookback period, the values are returned as an array
get_row_index() Gets the row nr. in a 2D matrix from 1D index (index for rows and columns both starts at 0 !!)
get_column_index() Gets the column nr. in a 2D matrix from 1D index (index for rows and columns both starts at 0 !!)
get_row_column_index() Gets a tuple with the (row, column) coordinates in 2D matrix from 1D index (index starts at 0 and does not include the header!!)
get_array_index() Gets the 1D index from (row, column) coordinates in 2D matrix (index for row and column both starts at 0 !! Index starts at 0 and does not include the header!!)
remove_rows() Removes one or more rows/records from a 2D matrix (if from_row = to_row, only this row is removed)
remove_columns() Remove one or more columns from a 2D matrix (if from_column = to_column, only this column is removed)
insert_array_of_rows() Insert an array of rows/records at a certain row number in a 2D matrix
add_row() ADDS a ROW/RECORD on the TOP of a sheet, shift the whole list one down and gives the option to REMOVE the OLDEST row/record. (2D version of "unshift" + "pop" but with a whole row at once)
insert_array_of_columns() Insert an array of columns at a certain column number in a 2D matrix
append_array_of_rows() Appends/adds an array of rows/records to the bottom of a 2D matrix
append_array_of_columns() Appends/adds an array of columns to the right side of a 2D matrix
pop_row() Removes / pops and returns the last row/record from a 2D matrix.
pop_column() Removes / pops and returns the last (most right) column from a 2D matrix.
replace()
abs()
add_value() Returns a new matrix with the same value added to all the elements of the source matrix.
addition() Returns a new matrix with the of the elements of one 2D matrix added to every corresponding element of a source 2D matrix.
subtract_value() Returns a new matrix with the same value subtracted from every element of a 2D matrix
subtraction() Returns a new matrix with the values of the elements of one 2D matrix subtracted from every corresponding element of a source 2D matrix.
scalar_multipy() Returns a new matrix with all the elements of the source matrix scaled/multiplied by a scalar value.
transpose() Returns a new matrix with the elements of the source matrix transposed.
multiply_elem() Performs ELEMENT WISE MULTIPLICATION of 2D matrices, returns a new matrix c.
multiply() Performs DOT PROCUCT MULTIPLICATION of 2D matrices, returns a new matrix c.
determinant_2x2() Calculates the determinant of 2x2 matrices.
determinant_3x3() Calculates the determinant of 3x3 matrices.
determinant_4x4() Calculates the determinant of 4x4 matrices.
print() displays a 2D matrix in a table layout.
Hx MTF Sorted MAs Panel with Freeze WarningThis script displays the close price and 4 sorted moving averages of your choice in a small repositionable panel and, when used on a higher timeframe, warns you when values may be different from actual values in the higher timeframe, inciting you to double check the actual values of the moving averages in the higher timeframe the panel is supposed to reflect.
The 4 moving averages and close are sorted together, providing you with a bird’s-eye view of their relative positions, the same way moving averages and last price values are displayed on the right scale.
The black header reminds of:
(1) the timeframe (resolution) used in the panel
(2) the remaining time before a new bar is created in the panel timeframe. Note that this remaining time is different from the one on the right scale, since it is only updated when a new transaction occurs.
Below, price and moving averages are sorted, color coded and followed by:
(1) a trend indicator ↗ or ↘ meaning that last change is up or down
(2) the number of bars since the moving average is above or below close (0 means current bar). This is obviously not displayed after the close price line (white background color).
Use
This panel was basically developed to display higher timeframe data but it can also be used with the same timeframe as chart for example if you do not want to plot moving averages on your chart but are still interested in their trends and relative positions vs price.
If you see something strange (like header is not black and displays NaN), it just means you requested moving averages that are not available in the panel timeframe. This may happen with newly introduced cryptos and “long” MA timeframes.
Different Timeframe
If you choose to use the panel on a different timeframe than the current one, be aware that you should only use timeframes higher than the current one, as per Tradingview recommendations.
If you select a lower timeframe than the current one, the panel timeframe header cell will turn to the alert color you set (fuchsia by default).
After tinkering for a while with the security function, I noticed that sometimes indicator values “freeze” (i.e. stop udating) and I have found no workaround.
What I mean is that when you look at a sma on a 5 minutes timeframe (the reference) and look at this same sma on a 5 minutes timeframe but from a lower timeframe through the security function set with a timeframe of 5 minutes, values returned by the security function are not always up to date and “freeze”. That’s the bad news.
Freeze warning
The better news is that this unexpected behaviour seems to be predictable, at least on minutes timeframes and I implemented an indicator that endeavors to detecting such situations. When the panel believes data may be frozen, the ‘Remaining Time’ header cell will turn to the alert color.
This feature is only implemented on minutes timeframes and can be switched on or off.
Other points of interest in this script
If you code, this function may also interest you:
sortWithIndexes (arrayToSort) returns a tuple (sortedArray, sortedIndexes) and therefore allows multi-dimensional arrays sorting without actually implementing a sorting algorithm 😉.
Default Settings
The default settings provide an example of commonly used moving averages with associated colors ranked from Hot (more nervous) to Cold (less nervous).
These settings are just an example and are NOT meant to be used as a trading system! DYOR!
Hope it will be useful.
Does the Freeze warning work for you? What do you think of my pseudo sorting algorithm?
Enjoy and please let me know what you think in the comments.
2 Dimensional Array using one floating point entry This is an attempt to create a two dimensional floating point array from the pine single dimension array.
It enables some useful array functions like sorting, when you are trying to keep track of price and location in time or bars and you would like to sort the array.
Other array functions on this array will not work, like average and other statistical functions - they will provide bad results. I would suggest continuing to use a single dimensional array for each element where that kind of array function is required.
I wrote this simply as a mean of using the sort functions when I had to sort price and try to keep the bar location or time in synch.
Other array functions could be written to manipulate this kind of array, leave that to others. The goal here was to avoid using for loops which would be a performance impact on large arrays.
The basic concept is to create one floating point number from two, put that into an array, and then be able to pull the compound value out and parse out the individual components.
I imagine it could also accommodate a 3 or 4 dimensional array with some work, as long as you had some idea of how many digits are consumed by each element. For example you may be interested in storing price, RSI, x_loc values and then be able to sort and parse them out.
Pinescript - Common Label & Line Array Functions Library by RRBPinescript - Common Label & Line Array Functions Library by RagingRocketBull 2021
Version 1.0
This script provides a library of common array functions for arrays of label and line objects with live testing of all functions.
Using this library you can easily create, update, delete, join label/line object arrays, and get/set properties of individual label/line object array items.
You can find the full list of supported label/line array functions below.
There are several libraries:
- Common String Functions Library
- Standard Array Functions Library
- Common Fixed Type Array Functions Library
- Common Label & Line Array Functions Library
- Common Variable Type Array Functions Library
Features:
- 30 array functions in categories create/update/delete/join/get/set with support for both label/line objects (45+ including all implementations)
- Create, Update label/line object arrays from list/array params
- GET/SET properties of individual label/line array items by index
- Join label/line objects/arrays into a single string for output
- Supports User Input of x,y coords of 5 different types: abs/rel/rel%/inc/inc% list/array, auto transforms x,y input into list/array based on type, base and xloc, translates rel into abs bar indexes
- Supports User Input of lists with shortened names of string properties, auto expands all standard string properties to their full names for use in functions
- Live Output for all/selected functions based on User Input. Test any function for possible errors you may encounter before using in script.
- Output filters: hide all excluded and show only allowed functions using a list of function names
- Output Panel customization options: set custom style, color, text size, and line spacing
Usage:
- select create function - create label/line arrays from lists or arrays (optional). Doesn't affect the update functions. The only change in output should be function name regardless of the selected implementation.
- specify num_objects for both label/line arrays (default is 7)
- specify common anchor point settings x,y base/type for both label/line arrays and GET/SET items in Common Settings
- fill lists with items to use as inputs for create label/line array functions in Create Label/Line Arrays section
- specify label/line array item index and properties to SET in corresponding sections
- select label/line SET function to see the changes applied live
Code Structure:
- translate x,y depending on x,y type, base and xloc as specified in UI (required for all functions)
- expand all shortened standard property names to full names (required for create/update* from arrays and set* functions, not needed for create/update* from lists) to prevent errors in label.new and line.new
- create param arrays from string lists (required for create/update* from arrays and set* functions, not needed for create/update* from lists)
- create label/line array from string lists (property names are auto expanded) or param arrays (requires already expanded properties)
- update entire label/line array or
- get/set label/line array item properties by index
Transforming/Expanding Input values:
- for this script to work on any chart regardless of price/scale, all x*,y* are specified as % increase relative to x0,y0 base levels by default, but user can enter abs x,price values specific for that chart if necessary.
- all lists can be empty, contain 1 or several items, have the same/different lengths. Array Length = min(min(len(list*)), mum_objects) is used to create label/line objects. Missing list items are replaced with default property values.
- when a list contains only 1 item it is duplicated (label name/tooltip is also auto incremented) to match the calculated Array Length
- since this script processes user input, all x,y values must be translated to abs bar indexes before passing them to functions. Your script may provide all data internally and doesn't require this step.
- at first int x, float y arrays are created from user string lists, transformed as described below and returned as x,y arrays.
- translated x,y arrays can then be passed to create from arrays function or can be converted back to x,y string lists for the create from lists function if necessary.
- all translation logic is separated from create/update/set functions for the following reasons:
- to avoid redundant code/dependency on ext functions/reduce local scopes and to be able to translate everything only once in one place - should be faster
- to simplify internal logic of all functions
- because your script may provide all data internally without user input and won't need the translation step
- there are 5 types available for both x,y: abs, rel, rel%, inc, inc%. In addition to that, x can be: bar index or time, y is always price.
- abs - absolute bar index/time from start bar0 (x) or price (y) from 0, is >= 0
- rel - relative bar index/time from cur bar n (x) or price from y0 base level, is >= 0
- rel% - relative % increase of bar index/time (x) or price (y) from corresponding base level (x0 or y0), can be <=> 0
- inc - relative increment (step) for each new level of bar index/time (x) or price (y) from corresponding base level (x0 or y0), can be <=> 0
- inc% - relative % increment (% step) for each new level of bar index/time (x) or price (y) from corresponding base level (x0 or y0), can be <=> 0
- x base level >= 0
- y base level can be 0 (empty) or open, close, high, low of cur bar
- single item x1_list = "50" translates into:
- for x type abs: "50, 50, 50 ..." num_objects times regardless of xloc => x = 50
- for x type rel: "50, 50, 50 ... " num_objects times => x = x_base + 50
- for x type rel%: "50%, 50%, 50% ... " num_objects times => x_base * (1 + 0.5)
- for x type inc: "0, 50, 100 ... " num_objects times => x_base + 50 * i
- for x type inc%: "0%, 50%, 100% ... " num_objects times => x_base * (1 + 0.5 * i)
- when xloc = xloc.bar_index each rel*/inc* value in the above list is then subtracted from n: n - x to convert rel to abs bar index, values of abs type are not affected
- x1_list = "0, 50, 100, ..." of type rel is the same as "50" of type inc
- x1_list = "50, 50, 50, ..." of type abs/rel/rel% produces a sequence of the same values and can be shortened to just "50"
- single item y1_list = "2" translates into (ragardless of yloc):
- for y type abs: "2, 2, 2 ..." num_objects times => y = 2
- for y type rel: "2, 2, 2 ... " num_objects times => y = y_base + 2
- for y type rel%: "2%, 2%, 2% ... " num_objects times => y = y_base * (1 + 0.02)
- for y type inc: "0, 2, 4 ... " num_objects times => y = y_base + 2 * i
- for y type inc%: "0%, 2%, 4% ... " num_objects times => y = y_base * (1 + 0.02 * i)
- when yloc != yloc.price all calculated values above are simply ignored
- y1_list = "0, 2, 4" of type rel% is the same as "2" with type inc%
- y1_list = "2, 2, 2" of type abs/rel/rel% produces a sequence of the same values and can be shortened to just "2"
- you can enter shortened property names in lists. To lookup supported shortened names use corresponding dropdowns in Set Label/Line Array Item Properties sections
- all shortened standard property names must be expanded to full names (required for create/update* from arrays and set* functions, not needed for create/update* from lists) to prevent errors in label.new and line.new
- examples of shortened property names that can be used in lists: bar_index, large, solid, label_right, white, left, left, price
- expanded to their corresponding full names: xloc.bar_index, size.large, line.style_solid, label.style_label_right, color.white, text.align_left, extend.left, yloc.price
- all expanding logic is separated from create/update* from arrays and set* functions for the same reasons as above, and because param arrays already have different types, implying the use of final values.
- all expanding logic is included in the create/update* from lists functions because it seemed more natural to process string lists from user input directly inside the function, since they are already strings.
Creating Label/Line Objects:
- use study max_lines_count and max_labels_count params to increase the max number of label/line objects to 500 (+3) if necessary. Default number of label/line objects is 50 (+3)
- all functions use standard param sequence from methods in reference, except style always comes before colors.
- standard label/line.get* functions only return a few properties, you can't read style, color, width etc.
- label.new(na, na, "") will still create a label with x = n-301, y = NaN, text = "" because max default scope for a var is 300 bars back.
- there are 2 types of color na, label color requires color(na) instead of color_na to prevent error. text_color and line_color can be color_na
- for line to be visible both x1, x2 ends must be visible on screen, also when y1 == y2 => abs(x1 - x2) >= 2 bars => line is visible
- xloc.bar_index line uses abs x1, x2 indexes and can only be within 0 and n ends, where n <= 5000 bars (free accounts) or 10000 bars (paid accounts) limit, can't be plotted into the future
- xloc.bar_time line uses abs x1, x2 times, can't go past bar0 time but can continue past cur bar time into the future, doesn't have a length limit in bars.
- xloc.bar_time line with length = exact number of bars can be plotted only within bar0 and cur bar, can't be plotted into the future reliably because of future gaps due to sessions on some charts
- xloc.bar_index line can't be created on bar 0 with fixed length value because there's only 1 bar of horiz length
- it can be created on cur bar using fixed length x < n <= 5000 or
- created on bar0 using na and then assigned final x* values on cur bar using set_x*
- created on bar0 using n - fixed_length x and then updated on cur bar using set_x*, where n <= 5000
- default orientation of lines (for style_arrow* and extend) is from left to right (from bar 50 to bar 0), it reverses when x1 and x2 are swapped
- price is a function, not a line object property
Variable Type Arrays:
- you can't create an if/function that returns var type value/array - compiler uses strict types and doesn't allow that
- however you can assign array of any type to another array of any type creating an arr pointer of invalid type that must be reassigned to a matching array type before used in any expression to prevent error
- create_any_array2 uses this loophole to return an int_arr pointer of a var type array
- this works for all array types defined with/without var keyword and doesn't work for string arrays defined with var keyword for some reason
- you can't do this with var type vars, only var type arrays because arrays are pointers passed by reference, while vars are actual values passed by value.
- you can only pass a var type value/array param to a function if all functions inside support every type - otherwise error
- alternatively values of every type must be passed simultaneously and processed separately by corresponding if branches/functions supporting these particular types returning a common single type result
- get_var_types solves this problem by generating a list of dummy values of every possible type including the source type, tricking the compiler into allowing a single valid branch to execute without error, while ignoring all dummy results
Notes:
- uses Pinescript v3 Compatibility Framework
- uses Common String Functions Library, Common Fixed Type Array Functions Library, Common Variable Type Array Functions Library
- has to be a separate script to reduce the number of local scopes/compiled file size, can't be merged with another library.
- lets you live test all label/line array functions for errors. If you see an error - change params in UI
- if you see "Loop too long" error - hide/unhide or reattach the script
- if you see "Chart references too many candles" error - change x type or value between abs/rel*. This can happen on charts with 5000+ bars when a rel bar index x is passed to label.new or line.new instead of abs bar index n - x
- create/update_label/line_array* use string lists, while create/update_label/line_array_from_arrays* use array params to create label/line arrays. "from_lists" is dropped to shorten the names of the most commonly used functions.
- create_label/line_array2,4 are preferable, 5,6 are listed for pure demonstration purposes only - don't use them, they don't improve anything but dramatically increase local scopes/compiled file size
- for this reason you would mainly be using create/update_label/line_array2,4 for list params or create/update_label/line_array_from_arrays2 for array params
- all update functions are executed after each create as proof of work and can be disabled. Only create functions are required. Use update functions when necessary - when list/array params are changed by your script.
- both lists and array item properties use the same x,y_type, x,y_base from common settings
- doesn't use pagination, a single str contains all output
- why is this so complicated? What are all these functions for?
- this script merges standard label/line object methods with standard array functions to create a powerful set of label/line object array functions to simplify manipulation of these arrays.
- this library also extends the functionality of Common Variable Type Array Functions Library providing support for label/line types in var type array functions (any_to_str6, join_any_array5)
- creating arrays from either lists or arrays adds a level of flexibility that comes with complexity. It's very likely that in your script you'd have to deal with both string lists as input, and arrays internally, once everything is converted.
- processing user input, allowing customization and targeting for any chart adds a whole new layer of complexity, all inputs must be translated and expanded before used in functions.
- different function implementations can increase/reduce local scopes and compiled file size. Select a version that best suits your needs. Creating complex scripts often requires rewriting your code multiple times to fit the limits, every line matters.
P.S. Don't rely too much on labels, for too often they are fables.
List of functions*:
* - functions from other libraries are not listed
1. Join Functions
Labels
- join_label_object(label_, d1, d2)
- join_label_array(arr, d1, d2)
- join_label_array2(arr, d1, d2, d3)
Lines
- join_line_object(line_, d1, d2)
- join_line_array(arr, d1, d2)
- join_line_array2(arr, d1, d2, d3)
Any Type
- any_to_str6(arr, index, type)
- join_any_array4(arr, d1, d2, type)
- join_any_array5(arr, d, type)
2. GET/SET Functions
Labels
- label_array_get_text(arr, index)
- label_array_get_xy(arr, index)
- label_array_get_fields(arr, index)
- label_array_set_text(arr, index, str)
- label_array_set_xy(arr, index, x, y)
- label_array_set_fields(arr, index, x, y, str)
- label_array_set_all_fields(arr, index, x, y, str, xloc, yloc, label_style, label_color, text_color, text_size, text_align, tooltip)
- label_array_set_all_fields2(arr, index, x, y, str, xloc, yloc, label_style, label_color, text_color, text_size, text_align, tooltip)
Lines
- line_array_get_price(arr, index, bar)
- line_array_get_xy(arr, index)
- line_array_get_fields(arr, index)
- line_array_set_text(arr, index, width)
- line_array_set_xy(arr, index, x1, y1, x2, y2)
- line_array_set_fields(arr, index, x1, y1, x2, y2, width)
- line_array_set_all_fields(arr, index, x1, y1, x2, y2, xloc, extend, line_style, line_color, width)
- line_array_set_all_fields2(arr, index, x1, y1, x2, y2, xloc, extend, line_style, line_color, width)
3. Create/Update/Delete Functions
Labels
- delete_label_array(label_arr)
- create_label_array(list1, list2, list3, list4, list5, d)
- create_label_array2(x_list, y_list, str_list, xloc_list, yloc_list, style_list, color1_list, color2_list, size_list, align_list, tooltip_list, d)
- create_label_array3(x_list, y_list, str_list, xloc_list, yloc_list, style_list, color1_list, color2_list, size_list, align_list, tooltip_list, d)
- create_label_array4(x_list, y_list, str_list, xloc_list, yloc_list, style_list, color1_list, color2_list, size_list, align_list, tooltip_list, d)
- create_label_array5(x_list, y_list, str_list, xloc_list, yloc_list, style_list, color1_list, color2_list, size_list, align_list, tooltip_list, d)
- create_label_array6(x_list, y_list, str_list, xloc_list, yloc_list, style_list, color1_list, color2_list, size_list, align_list, tooltip_list, d)
- update_label_array2(label_arr, x_list, y_list, str_list, xloc_list, yloc_list, style_list, color1_list, color2_list, size_list, align_list, tooltip_list, d)
- update_label_array4(label_arr, x_list, y_list, str_list, xloc_list, yloc_list, style_list, color1_list, color2_list, size_list, align_list, tooltip_list, d)
- create_label_array_from_arrays2(x_arr, y_arr, str_arr, xloc_arr, yloc_arr, style_arr, color1_arr, color2_arr, size_arr, align_arr, tooltip_arr, d)
- create_label_array_from_arrays4(x_arr, y_arr, str_arr, xloc_arr, yloc_arr, style_arr, color1_arr, color2_arr, size_arr, align_arr, tooltip_arr, d)
- update_label_array_from_arrays2(label_arr, x_arr, y_arr, str_arr, xloc_arr, yloc_arr, style_arr, color1_arr, color2_arr, size_arr, align_arr, tooltip_arr, d)
Lines
- delete_line_array(line_arr)
- create_line_array(list1, list2, list3, list4, list5, list6, d)
- create_line_array2(x1_list, y1_list, x2_list, y2_list, xloc_list, extend_list, style_list, color_list, width_list, d)
- create_line_array3(x1_list, y1_list, x2_list, y2_list, xloc_list, extend_list, style_list, color_list, width_list, d)
- create_line_array4(x1_list, y1_list, x2_list, y2_list, xloc_list, extend_list, style_list, color_list, width_list, d)
- create_line_array5(x1_list, y1_list, x2_list, y2_list, xloc_list, extend_list, style_list, color_list, width_list, d)
- create_line_array6(x1_list, y1_list, x2_list, y2_list, xloc_list, extend_list, style_list, color_list, width_list, d)
- update_line_array2(line_arr, x1_list, y1_list, x2_list, y2_list, xloc_list, extend_list, style_list, color_list, width_list, d)
- update_line_array4(line_arr, x1_list, y1_list, x2_list, y2_list, xloc_list, extend_list, style_list, color_list, width_list, d)
- create_line_array_from_arrays2(x1_arr, y1_arr, x2_arr, y2_arr, xloc_arr, extend_arr, style_arr, color_arr, width_arr, d)
- update_line_array_from_arrays2(line_arr, x1_arr, y1_arr, x2_arr, y2_arr, xloc_arr, extend_arr, style_arr, color_arr, width_arr, d)
Product function for ArraysHello,
This code may be useful for Pine script coders only.
Arrays were welcomed in Pine script, as they are a foundation for coding properly many useful things, like studies and strategies here in TradingView :)
TradingView recently released new functions for arrays, like array.median and more, but the "PRODUCT" function was still missing, so I created it. I've no doubt that array.product() will soon be proposed by the platform, but for the time being, one can use mine.
It does the same thing as the PRODUCT() function in Excel : it multiplies all the source values included in the specified lookback, and returns the product. This function can accept Series or Arrays as source input.
Tick Data DetailedHello All,
After Tick Chart and Tick Chart RSI scripts, this is Tick Data Detailed script. Like other tick scrips this one only works on real-time bars too. it creates two tables: the table at the right shows the detailed data for Current Bar and the table at the left shows the detailed data for all calculated bars (cumulative). the script checks the volume on each tick and add the tick and volume to the specified level (you can set/change levels)
The volume is multiplied by close price to calculate real volume .There are 7 levels/zones and the default levels are:
0 - 10.000
10.000 - 20.000
20.000 - 50.000
50.000 - 100.000
100.000 - 200.000
200.000 - 400.000
> 400.000
With this info, you will get number of ticks and total volumes on each levels. The idea to separate this levels is in order to know which type of traders trade at that moment. for example volume of whale moves are probably greater than 400.000 or at least 100.000. Or volume of small traders is less than 10.000 or between 20.000-50.000.
You will get info if there is anomaly on each candle as well. what is anomaly definition? Current candle is green but Sell volume is greater than Buy volume or current candle is red but Buy volume is greater than Sell volume . it is shown as (!). you should think/search why/how this anomaly occurs. You can see screenshot about it below.
also "TOTAL" text color changes automatically. if Buy volume is greater than Sell volume then its color becomes Green, if Sell volume is greater than Buy volume then its color becomes Red (or any color you set)
Optionally you can change background and text colors as shown in the example below.
Explanation:
How anomaly is shown:
You can enable coloring background and set the colors as you wish:
And Thanks to @Duyck for letting me use the special characters from his great script.
Enjoy!
Pinescript - Standard Array Functions Library by RRBStandard Array Functions Library by RagingRocketBull 2021
Version 1.0
This script provides a library of every standard Pinescript array function for live testing with all supported array types.
You can find the full list of supported standard array functions below.
There are several libraries:
- Common String Functions Library
- Common Array Functions Library
- Standard Array Functions Library
Features:
- Supports all standard array functions (30+) with all possible array types* (* - except array.new* functions and label, line array types)
- Live Output for all/selected functions based on User Input. Test any function for possible errors you may encounter before using in script.
- Output filters: show errors, hide all excluded and show only allowed functions using a list of function names
- Console customization options: set custom text size, color, page length, line spacing
Notes:
- uses Pinescript v3 Compatibility Framework
- uses Common String Functions Library
- has to be a separate script to reduce the number of local scopes in Common Array Function Library, there's no way to merge these scripts into a single library.
- lets you live test all standard array functions for errors. If you see an error - change params in UI
- array types that are not supported by certain functions and producing a compilation error were disabled with "error" showing up as result
- if you see "Loop too long" error - hide/unhide or reattach the script
- doesn't use pagination, a single str contains all output
- for most array functions to work (except push), an array must be defined with at least 1 pre-existing dummy element 0.
- array.slice and array.fill require from_index < to_index otherwise error
- array.join only supports string arrays, and delimiter must be a const string, can't be var/input. Use join_any_array to join any array type into string. You can also use tostring() to join int, float arrays.
- array.sort only supports int, float arrays. Use sort_any_array from the Common Array Function Library to sort any array type.
- array.sort only sorts values, doesn't preserve indexes. Use sort_any_array from the Common Array Function Library to sort any array while preserving indexes.
- array.concat appends string arrays in reverse order, other array types are appended correctly
- array.covariance requires 2 int, float arrays of the same size
- tostring(flag) works only for internal bool vars, flag expression can't depend on any inputs of any type, use bool_to_str instead
- you can't create an if/function that returns var type value/array - compiler uses strict types and doesn't allow that
- however you can assign array of any type to another array of any type creating an arr pointer of invalid type that must be reassigned to a matching array type before used in any expression to prevent error
- source_array and create_any_array2 use this loophole to return an int_arr pointer of a var type array
- this works for all array types defined with/without var keyword. This doesn't work for string arrays defined with var keyword for some reason
- you can't do this with var type vars, this can be done only with var type arrays because they are pointers passed by reference, while vars are the actual values passed by value.
- wrapper functions solve the problem of returning var array types. This is the only way of doing it when the top level arr type is undefined.
- you can only pass a var type value/array param to a function if all functions inside support every type - otherwise error
- alternatively values of every type must be passed simultaneously and processed separately by corresponding if branches/functions supporting these particular types returning a common single result type
- get_var_types solves this problem by generating a list of dummy values of every possible type including the source type, allowing a single valid branch to execute without error
- examples of functions supporting all array types: array.size, array.get, array.push. Examples of functions with limited type support: array.sort, array.join, array.max, tostring
- unlike var params/global vars, you can modify array params and global arrays directly from inside functions using standard array functions, but you can't use := (it only works for local arrays)
- inside function always work with array.copy to prevent accidental array modification
- you can't compare arrays
- there's no na equivalent for arrays, na(arr) doesn't work
P.S. A wide array of skills calls for an even wider array of responsibilities
List of functions:
- array.avg(arr)
- array.clear(arr)
- array.concat(arr1, arr2)
- array.copy(arr)
- array.covariance(arr1, arr2)
- array.fill(arr, value, index_from, index_to)
- array.get(arr, index)
- array.includes(arr, value)
- array.indexof(arr, value)
- array.insert(arr, index, value)
- array.join(arr, delimiter)
- array.lastindexof(arr, value)
- array.max(arr)
- array.median(arr)
- array.min(arr)
- array.mode(arr)
- array.pop(arr)
- array.push(arr, value)
- array.range(arr)
- array.remove(arr, index)
- array.reverse(arr)
- array.set(arr, index, value)
- array.shift(arr)
- array.size(arr)
- array.slice(arr, index_from, index_to)
- array.sort(arr, order)
- array.standardize()
- array.stdev(arr)
- array.sum(arr)
- array.unshift(arr, value)
- array.variance(arr)
FCMS - Arrays Exercise - High Low with Arrays - StudyThis is just a script to exercise the use of arrays on pine script.
I think we could say that every for loop we had in pine script before the arrays, is eligible to become an array.
Our script will get more efficient and more reliable.
As every "if" case is elegible to became a function
I confess I was addicted to use if, else if, else in my codes, but recently i've been updating my scripts and it's became more efficient
I couldn't find an array function that insert an value removing the oldest one, so I'm using this condition to "shift" the first value.
I'll update as soon as I find a better way to do it.
In any case, for this specific goal, we already have an built in function, as I let on script