Trendline Torys
Trendline Torys
Trendline Torys
//
/////////////////////////////////////////////////////
/// © Copyright 2019 to present, Joris Duyck (JD) ///
/////////////////////////////////////////////////////
//{
// This script draws trendlines from the pivot points in the price chart.
//
// The input for the function that draws the trendlines is any calculation or
function
// (here this is pivot points, but the core of the script can be used in other
implementations too),
// that most of the time and occasionally produces a 'non n/a' value.
// The trendline function then connects the non n/a values and extends the
connections of those points in a trendline.
//
// There is an option to draw the lines from the wicks or from the 'real bodies' of
the candles.
// The trendlines can either extend infinitely or only run for an adjustable amount
of bars.
//
// There's an option to display trendline breaks
// And the option to display trendlines in color or monochrome
//
//
// !!! IMPORTANT NOTICE ON THE TRENDLINES !!!
///////////////////////////////////
/// SCHEMATIC OF THE STRUCTURE: ///
///////////////////////////////////
//
////// (header with dimensions info)
//
// [nr_of_sheets, nr_of_rows/records, nr_of_columns,
//
////// (actual array data)
//
// SHEET 0
//
// 0 1 2 3 column_index
// 0 record0_val0, record0_val1, record0_val2, record0_val3, ...,
// 1 record1_val0, record1_val1, record1_val2, record1_val3, ...,
// 2 record2_val0, record2_val1, record2_val2, record2_val3, ...,
// 3 ...
//
// row_index
// SHEET 1
//
// 0 1 2 3
column_index
// 0 record0_val0, record0_val1, record0_val2, record0_val3, ...,
// 1 record1_val0, record1_val1, record1_val2, record1_val3, ...,
// 2 record2_val0, record2_val1, record2_val2, record2_val3, ...,
// 3 ...
//
// row_index
// SHEET 2
//
// 0 1 2 3
column_index
// 0 record0_val0, record0_val1, record0_val2,
record0_val3, ...,
// 1 record1_val0, record1_val1, record1_val2,
record1_val3, ...,
// 2 record2_val0, record2_val1, record2_val2,
record2_val3, ...,
// 3 ...
]
//
// row_index
//
////////////////////////////////////
/// DATA MANIPULATION FUNCTIONS: ///
////////////////////////////////////
//
// A set of functions are implemented to write info to and retrieve info from the
dataframe.
// Of course the possibilities are not limited to the functions here, these were
written because they were needed for the script!
// A whole list of other functions can be easily written for other manipulation
purposes.
//
//
// LIST OF FUNCTIONS contained in this script:
//
// - Dataframe initialisation function, builds 3D matrix with dimensional
metadata/size in first 3 values //
// a new df array is built (cfr. the conventions I propose above) containing a
certain nr of sheets, records and columns
//
// - function to get dimensional info from 3D matrix
// This function returns the nr of sheets, rows and columns in the df
//
// - function to get a sheet start and end index from 3D matrix
// This is mainly used for 1D to 3D index conversions
//
// - function to add a record on the top of a sheet, shift the whole list one down
and remove the oldest record
// (2D version of "unshift" + "pop" but with a whole row at once)
//
// - function to retrieve the values from a record from a certain row/lookback
period
// The values are returned as an array
//
// - function to get/retrieve a single value from a certain sheet, row/lookback
period and column
//
// - function to set a single value in a certain sheet, row/lookback period and
column
//
// - function to print the matrix
// This function is mainly used for debugging purposes and displays the array
as a 2D matrix notation
//
//
// Enjoy!
// JD.
//
// #NotTradingAdvice #DYOR
//
// Disclaimer.
// I AM NOT A FINANCIAL ADVISOR.
// THESE IDEAS ARE NOT ADVICE AND ARE FOR EDUCATION PURPOSES ONLY.
// ALWAYS DO YOUR OWN RESEARCH!
//}
// Function to get the trendline price for X bars ago ("0" = current value)
//{
line_get_price(_start_time, _start_price, _slope, _lookback_period, _log_chart) =>
var float current_price = 0.0
elapsed_time = (time - _start_time)
current_price :=
_log_chart ?
(_start_price * exp((elapsed_time - (_lookback_period * bar_time)) * _slope))
:
(_start_price + (elapsed_time - (_lookback_period * bar_time)) * _slope )
//}
// function to add a record on the top of a sheet and remove the oldest record
("sheet_index" goes from 0 (first) to 1, 2, ...)//
//{
f_df_array_add_new_record(_m, _sheet_index, _record) =>
[_nr_of_sheets, _nr_of_rows, _nr_of_columns] = f_df_array_get_size(_m)
if array.size(_record) == 3
[_sheet_start_index, _sheet_end_index] =
f_df_array_get_sheet_index_range(_m, _sheet_index)
_sheet = array.slice(_m, _sheet_start_index, _sheet_end_index + 1)
for i = (3 - 1) to 0
array.unshift(_sheet, array.get(_record, i))
array.pop(_sheet)
//}
/////////////////////////////////
// Track and store high pivots //
/////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////
// If a new trendline is formed, enter the new trendline info in the df array
//
// high trendline values go in sheet 0
//
// syntax = "f_df_array_add_new_record(array_id, sheet_index,
array_of_record_values_to_insert)"//
///////////////////////////////////////////////////////////////////////////////////
///////////////
float[] high_line = array.new_float(3, 0)
if not na(high_pivot)
array.set(high_line, 0, time[len / 2])
array.set(high_line, 1, high_pivot)
f_df_array_add_new_record(trendlines, high_pivot_data, high_line)
////////////////////////////////
// Track and store low pivots //
////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////
// If a new trendline is formed, enter the new trendline info in the df array
//
// low trendline values go in sheet 1
//
// syntax = "f_df_array_add_new_record(array_id, sheet_index,
array_of_record_values_to_insert)"//
///////////////////////////////////////////////////////////////////////////////////
///////////////
float[] low_line = array.new_float(3, 0)
if not na(low_pivot)
array.set(low_line, 0, time[len / 2])
array.set(low_line, 1, low_pivot)
f_df_array_add_new_record(trendlines, low_pivot_data, low_line)
// plot lines
//{
var line looback_line_high = na, var line trendline_high = na
var line looback_line_low = na, var line trendline_low = na
var bool xtend = limit_extension == 0 ? true : false
low_line_values = f_df_array_get_record(trendlines, 1, 0)
cross_low = 0, low_x = 0.0, low_y = 0.0, low_sl = 0.0
// loop through the high and low trendlines to check for crosses
int[] cross_high_values = array.new_int()
int[] cross_low_values = array.new_int()
for i = 0 to trendline_check_nr
long_break = array.includes(cross_high_values, 1)
short_break = array.includes(cross_low_values, 1)
//}