typeandcastLibrary "typeandcast"
Contains the following methods:
_type() - Returns the type of the variable in the forms "int", "array", "matrix"
_type_item() - Returns the type of the variable or of the element (for array/matrix). (e.g. `arrayFloat._type_item()` returns 'float').
_type_struct() - Returns the type of the structure only (i.e. "array" or "matrix"), for simple types (like e.g. `int`) returns "simple". (e.g. `arrayFloat._type_struct()` returns 'array').
_tona() - Casts na to the type of the parent object. (e.g. for an `int x` calling `x.tona()` returns `int(na)`
(inspired by the works of @kaigouthro and @faiyaz7283)
The _type() / _type_item() / _type_struct() methods are available for the following types:
int
float
bool
string
color
line
label
box
table
linefill
int
float
bool
string
color
line
label
box
table
linefill
matrix
matrix
matrix
matrix
matrix
matrix
matrix
matrix
matrix
matrix
The `tona()' method is available only for the simple types (except `linefill`, coundn't make it work for it)
Please see the descriptions in the script.
Types
WelcomeUDT█ OVERVIEW
This is a simplest example of user-defined types (UDT) or objects , which simplify as alternative to hello world.
█ CREDITS
Tradingview
█ USAGE
These are the types used during initializations, commonly variables.
export type Settings
int bar
float price
string phrase
...
Example of library function to print out label.
export printLabel(Settings setup) =>
if setup.variable
var label lab = na
label.delete(lab)
lab := label.new(setup.bar, setup.price, setup.phrase, color = setup.bg)
else
label.new(setup.bar, setup.price, setup.phrase, color = setup.bg)
Usage of types
Settings setup = Settings.new(bar_index , priceInput, phraseInput, colorInput, variableInput)
Alternative way to write types
Settings setup = Settings.new(
bar = bar_index ,
price = priceInput,
phrase = phraseInput,
variable = variableInput)
Usage of types into custom function / library function.
printLabel(setup)
printLabel(Settings)
Print out label
Parameters:
Settings : types
Returns: Label object
Settings
Initialize type values
Fields:
bar : X position for label
price : Y position for label
phrase : Text for label
bg : Color for label
variable : Boolean for enable new line and delete line
intoLibrary "into"
convert literals by type,
Same-types left in for bulk reasons.
TODO: Expand Types
b(string)
Convert string to bool.
Parameters:
string : val A string value.
Returns: Bool.
b(bool)
Pass Bool/bool
Parameters:
bool :
Returns: Bool.
b(float)
Convert Float (True if exists and not 0)
Parameters:
float : val A float value.
Returns: Bool.
b(int)
Convert integer (True if exists and not 0)
Parameters:
int : val An integer value.
Returns: Bool.
f(bool)
Convert bool to float.
Parameters:
bool : val A boolean value.
Returns: Float.
f(string, int)
Convert with decimal
Parameters:
string : val A string value.
int : decimals Decimal places. def = 6
Returns: Float.
f(float, int)
Convert float bypass with decimals
Parameters:
float : val A float value.
int : decimals Decimal places. def = 6
Returns: Float.
f(int)
Convert integer to float.
Parameters:
int : val An integer value.
Returns: Float.
i(bool)
Convert bool to int.
Parameters:
bool : val A boolean value.
Returns: Int.
i(string)
Convert string number to int.
Parameters:
string : val A string value.
Returns: Int.
i(float)
Convert float to int.
Parameters:
float : val A float value.
Returns: Int.
i(int)
Convert int to int.
Parameters:
int : val An int value.
Returns: Int.
s(bool)
Convert bool value to string.
Parameters:
bool : val A boolean value.
Returns: String.
s(str)
bypass string
Parameters:
str : val A string value.
Returns: String.
s(float)
Convert float value to string.
Parameters:
float : val A float value.
Returns: String.
s(int)
Convert int value to string.
Parameters:
int : val An integer value.
Returns: String.
s(val)
Array Convert Each Item
Parameters:
val : Array Input (Str,Bool,Int,Float)
Returns: String.
s(val)
Array Convert Each Item
Parameters:
val : Array Input (Str,Bool,Int,Float)
Returns: String.
s(val)
Array Convert Each Item
Parameters:
val : Array Input (Str,Bool,Int,Float)
Returns: String.
s(val)
Array Convert Each Item
Parameters:
val : Array Input (Str,Bool,Int,Float)
Returns: String.