OPEN-SOURCE SCRIPT

Seasonality Monthly v2.0

127
//version=6
indicator("Seasonality Monthly v2.0", overlay=false)

if not (timeframe.ismonthly or timeframe.isdaily)
alert("Switch to Daily or Monthly timeframe", alert.freq_once_per_bar)

// Input Settings
i_year_start = input(2000, "Start Year")
i_text_size = input.string(size.auto, "Text Size", [size.auto, size.tiny, size.small, size.normal, size.large, size.huge])

// Function for calculating statistics
f_array_stats(array_) =>
count_pos_ = 0
count_neg_ = 0
count_ = 0
sum_ = 0.0
if not na(array_) and array.size(array_) > 0
for i_ = 0 to array.size(array_) - 1
elem_ = array.get(array_, i_)
if not na(elem_)
sum_ += elem_
count_ += 1
count_pos_ += elem_ > 0 ? 1 : 0
count_neg_ += elem_ < 0 ? 1 : 0
avg_ = count_ > 0 ? sum_ / count_ : 0.0
[sum_, avg_, count_, count_pos_, count_neg_]

// Request historical data
year_ = request.security(syminfo.tickerid, "M", year(time_close), gaps = barmerge.gaps_on, lookahead = barmerge.lookahead_on)
month_ = request.security(syminfo.tickerid, "M", month(time_close), gaps = barmerge.gaps_on, lookahead = barmerge.lookahead_on)
chg_pct_ = request.security(syminfo.tickerid, "M", nz(close / close[1] - 1), gaps = barmerge.gaps_on, lookahead = barmerge.lookahead_on)

// Initialize variables
var year_start_ = math.max(year_, i_year_start)
var no_years_ = year(timenow) - year_start_ + 1
var matrix<float> data_ = matrix.new<float>(no_years_, 13, na)
var table table_ = na
var text_color_ = color.white
var bg_color_ = color.gray

if year_ >= year_start_
cur_val_ = nz(matrix.get(data_, year_ - year_start_, month_ - 1))
matrix.set(data_, year_ - year_start_, month_ - 1, cur_val_ + chg_pct_)

if barstate.islast
table_ := table.new(position.middle_center, 13, no_years_ + 7, border_width = 1)
table.cell(table_, 0, 0, str.format("Seasonality Monthly Performance - {0}:{1}", syminfo.prefix, syminfo.ticker), text_color = text_color_, bgcolor = color.blue, text_size = i_text_size)
table.merge_cells(table_, 0, 0, 12, 0)
row = 1

// Header row
months_ ="Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
for col = 0 to 12
table.cell(table_, col, row, months_[col], text_color = text_color_, bgcolor = bg_color_, text_size = i_text_size)

// Data rows
for row_ = 0 to no_years_ - 1
table.cell(table_, 0, 2 + row_, str.tostring(row_ + year_start_), text_color = text_color_, bgcolor = bg_color_, text_size = i_text_size)
for col_ = 0 to 11
val_ = nz(matrix.get(data_, row_, col_), 0.0)
val_color_ = val_ > 0.0 ? color.green : val_ < 0.0 ? color.red : color.gray
table.cell(table_, col_ + 1, 2 + row_, str.format("{0,number,###.##%}", val_), bgcolor = color.new(val_color_, 80), text_color = val_color_, text_size = i_text_size)

// Aggregates
row_ = no_years_ + 2
labels = "AVG", "SUM", "+ive", "WR"
for i = 0 to 3
table.cell(table_, 0, row_ + i, labels, text_color = text_color_, bgcolor = bg_color_, text_size = i_text_size)

for col_ = 0 to 11
arr_ = matrix.col(data_, col_)
[sum_, avg_, count_, count_pos_, count_neg_] = f_array_stats(arr_)
val_color_ = sum_ > 0 ? color.green : sum_ < 0 ? color.red : color.gray

table.cell(table_, col_ + 1, row_, str.format("{0,number,###.##%}", avg_), bgcolor = color.new(val_color_, 50), text_color = val_color_, text_size = i_text_size)
table.cell(table_, col_ + 1, row_ + 1, str.format("{0,number,###.##%}", sum_), bgcolor = color.new(val_color_, 50), text_color = val_color_, text_size = i_text_size)
table.cell(table_, col_ + 1, row_ + 2, str.format("{0}/{1}", count_pos_, count_), bgcolor = color.new(val_color_, 50), text_color = color.new(color.white, 50), text_size = i_text_size)
table.cell(table_, col_ + 1, row_ + 3, str.format("{0,number,#.##%}", count_pos_ / count_), bgcolor = color.new(val_color_, 50), text_color = color.new(color.white, 50), text_size = i_text_size)

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.