spartan.ts
Member
Hello, I was looking to get more in depth with learning thinkscript and found what appears to be a more popular TV S/R script that is worth learning from and get benefit from.
I can get past most of the basics but need some guidance how to handle array and for loops . I've wrapped the original code in comments under the new for reference. Any guidance would be greatly appreciated.
The script is a dynamic Support and Resistance script.
https://www.tradingview.com/script/va09eWAp-Support-Resistance-Dynamic-v2/
thanks.
I can get past most of the basics but need some guidance how to handle array and for loops . I've wrapped the original code in comments under the new for reference. Any guidance would be greatly appreciated.
The script is a dynamic Support and Resistance script.
https://www.tradingview.com/script/va09eWAp-Support-Resistance-Dynamic-v2/
thanks.
Code:
#https://www.tradingview.com/script/va09eWAp-Support-Resistance-Dynamic-v2/
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © LonesomeTheBlue
#//@version=4
#study("Support Resistance - Dynamic v2", "SRv2", overlay = true)
input prd = 10; # pivot period
input ppsrc = HL2;
input maxnumpp = 20; # max number of pivots
input ChannelW = 10; #max channel width
input maxnumsr = 5; # Maximum Number of S/R
input min_strength = 2; #Minimum Strength
input labelloc = 20 ;# "Label Location" "Positive numbers reference future bars, negative numbers reference histical bars
input linestyle = {default Dashed, Line };
input linewidth = 2;
#resistancecolor = input(defval = color.red, title = "Resistance Color", type = input.color, group = "Colors")
#supportcolor = input(defval = color.lime, title = "Support Color", type = input.color, group = "Colors")
input showpp = yes;
def na = double.nan;
def src1 = if ppsrc == HL2 then high else Max(close, open);
#float src1 = ppsrc == 'High/Low' ? high : max(close, open)
def src2 = if ppsrc == HL2 then low else Min(close, open);
#float src2 = ppsrc == 'High/Low' ? low: min(close, open)
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(prd - 1, lastBar - bn);
def pl = low < Lowest(src1, prd - 1) and low == GetValue(Lowest(src1, prd), -offset);
def ph = high > Highest(src2, prd - 1) and high == GetValue(Highest(src2, prd), -offset);
#float ph = pivothigh(src1, prd, prd)
#float pl = pivotlow(src2, prd, prd)
plot PHLine = ( if showpp then ph else Double.NaN);
#plotshape(ph and showpp, text = "H", style = shape.labeldown, color = na, textcolor = color.red, location = location.abovebar, offset = -prd)
PHLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
plot PlLine = ( if showpp then pl else Double.NaN);
#plotshape(pl and showpp, text = "L", style = shape.labelup, color = na, textcolor = color.lime, location = location.belowbar, offset = -prd)
PlLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#//calculate maximum S/R channel zone width
def prdhighest = Highest(300);
def prdlowest = Lowest(300);
def cwidth = (prdhighest - prdlowest) * ChannelW / 100;
def pivotvals = array.new_float(0);
#var pivotvals= array.new_float(0);
if ph or pl
then{
def ph1 = if ph then ph else pl;
array.unshift(pivotvals,ph1 );
}else {
if{array.size(pivotvals) > maxnumpp ## limit the array size
then array.pop(pivotvals);
else double.nan;
};
};
#if ph or pl
# array.unshift(pivotvals, ph ? ph : pl)
# if array.size(pivotvals) > maxnumpp // limit the array size
# array.pop(pivotvals)
script get_sr_vals{
input ind = 0;
def lo = array.get(pivotvals, ind);
def hi = lo;
def numpp = 0;
for y = 0 to array.size(pivotvals) - 1
def cpp = array.get(pivotvals, y);
def wdth = if cpp <= lo then hi - cpp else cpp - lo;
if wdth <= cwidth ## fits the max channel width?
then {
lo = if cpp <= lo then cpp else lo ;
hi = if cpp > lo then cpp else hi;
numpp= 1;
}
[hi, lo, numpp]
};
#############################
# get_sr_vals(ind)=>
# float lo = array.get(pivotvals, ind)
# float hi = lo
# int numpp = 0
# for y = 0 to array.size(pivotvals) - 1
# float cpp = array.get(pivotvals, y)
# float wdth = cpp <= lo ? hi - cpp : cpp - lo
# if wdth <= cwidth ## fits the max channel width?
# lo := cpp <= lo ? cpp : lo
# hi := cpp > lo ? cpp : hi
# numpp += 1
# [hi, lo, numpp]
#################################
def sr_up_level = array.new_float(0);
def sr_dn_level = array.new_float(0);
def sr_strength = array.new_float(0);
##############################
# find_loc(strength)=>
# ret = array.size(sr_strength)
# for i = (ret > 0 ? array.size(sr_strength) - 1 : na) to 0
# if strength <= array.get(sr_strength, i)
# break
# ret := i
# ret
##############################
script check_sr{
input hi = high;
input lo = low;
input strength = 1;
def ret = 1;
for i = 0 to (if array.size(sr_up_level) > 0 then array.size(sr_up_level) - 1 else na)
//included?
if array.get(sr_up_level, i) >= lo and array.get(sr_up_level, i) <= hi or
array.get(sr_dn_level, i) >= lo and array.get(sr_dn_level, i) <= hi
if strength >= array.get(sr_strength, i)
array.remove(sr_strength, i)
array.remove(sr_up_level, i)
array.remove(sr_dn_level, i)
ret
else
ret = 0;
};
##############################
#check_sr(hi, lo, strength)=>
# ret = true
# for i = 0 to (array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na)
# //included?
# if array.get(sr_up_level, i) >= lo and array.get(sr_up_level, i) <= hi or
# array.get(sr_dn_level, i) >= lo and array.get(sr_dn_level, i) <= hi
# if strength >= array.get(sr_strength, i)
# array.remove(sr_strength, i)
# array.remove(sr_up_level, i)
# array.remove(sr_dn_level, i)
# ret
# else
# ret := false
# break
# ret
##############################
plot sr_lines = array.new_line(11, na);
def sr_labels = array.new_label(11, na);
#var sr_lines = array.new_line(11, na)
#var sr_labels = array.new_label(11, na)
for x = 1 to 10
{
sr_lines.setPaintingStrategy(paintingStrategy.HORIZONTAL);
};
# def rate = 100 * (label.get_y(array.get(sr_labels, x)) - close) / close;
######################################
# for x = 1 to 10
# rate = 100 * (label.get_y(array.get(sr_labels, x)) - close) / close
# label.set_text(array.get(sr_labels, x), text = tostring(label.get_y(array.get(sr_labels, x))) + "(" + tostring(rate,'#.##') + "%)")
# label.set_x(array.get(sr_labels, x), x = bar_index + labelloc)
# label.set_color(array.get(sr_labels, x), color = label.get_y(array.get(sr_labels, x)) >= close ? color.red : color.lime)
# label.set_textcolor(array.get(sr_labels, x), textcolor = label.get_y(array.get(sr_labels, x)) >= close ? color.white : color.black)
# label.set_style(array.get(sr_labels, x), style = label.get_y(array.get(sr_labels, x)) >= close ? label.style_labeldown : label.style_labelup)
# line.set_color(array.get(sr_lines, x), color = line.get_y1(array.get(sr_lines, x)) >= close ? resistancecolor : supportcolor)
########################################################
if ph or pl
then { ##because of new calculation, remove old S/R levels
array.clear(sr_up_level)
array.clear(sr_dn_level)
array.clear(sr_strength)
##find S/R zones
for x = 0 to array.size(pivotvals) - 1
[hi, lo, strength] = get_sr_vals(x)
if check_sr(hi, lo, strength)
loc = find_loc(strength)
## if strength is in first maxnumsr sr then insert it to the arrays
if loc < maxnumsr and strength >= min_strength
array.insert(sr_strength, loc, strength)
array.insert(sr_up_level, loc, hi)
array.insert(sr_dn_level, loc, lo)
##/ keep size of the arrays = 5
if array.size(sr_strength) > maxnumsr
array.pop(sr_strength)
array.pop(sr_up_level)
array.pop(sr_dn_level)
for x = 1 to 10
line.delete(array.get(sr_lines, x))
label.delete(array.get(sr_labels, x))
for x = 0 to (array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na)
float mid = round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
rate = 100 * (mid - close) / close
array.set(sr_labels, x + 1,
label.new( x = bar_index + labelloc,
y = mid, text = tostring(mid) + "(" + tostring(rate,'#.##') + "%)",
color = mid >= close ? color.red : color.lime,
textcolor = mid >= close ? color.white : color.black,
style = mid >= close ? label.style_labeldown : label.style_labelup))
array.set(sr_lines, x + 1,
line.new(x1 = bar_index, y1 = mid, x2 = bar_index - 1, y2 = mid,
extend = extend.both,
color = mid >= close ? resistancecolor : supportcolor,
style = Lstyle,
width = linewidth))
####################################### ALERTS ## LOW priority ##################################
script f_crossed_over{
input ret = 0;
for x = 0 to (array.size(sr_up_level) > 0 then array.size(sr_up_level) - 1 else double.nan)
def mid = round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
if close[1] <= mid and close > mid
ret = 1;
}
############################
#f_crossed_over()=>
# ret = false
# for x = 0 to (array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na)
# float mid = round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
# if close[1] <= mid and close > mid
# ret := true
# ret
#################################
script f_crossed_under{
input ret = 0;
for x = 0 to (array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na)
float mid = round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
if close[1] >= mid and close < mid then
ret = 1 else ret;
};
#####################################
#f_crossed_under()=>
# ret = false
# for x = 0 to (array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na)
# float mid = round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
# if close[1] >= mid and close < mid
# ret := true
# ret
###################################
Alert(f_crossed_over(), "Resistance Broken",Alert.bar,sound.Ding);
Alert(f_crossed_under(), "Support Broken",Alert.bar,sound.Ding);
#alertcondition(f_crossed_over(), title='Resistance Broken', message='Resistance Broken')
#alertcondition(f_crossed_under(), title='Support Broken', message='Support Broken')
Last edited by a moderator: