Reputation: 1
How to add options choice for this code for pine script?
instead of this code :
distance = input.int(250, 'Time range', minval=5, step=10)
to :
distance = input.string(defval = '100', title = 'distance', options = ['100', '200', '300' , '500'], group = 'Settings')
To make supports and resistances by selecting "Time Range" "Sensitivity"
as you see it at picture:
I think the indicator already there at tradingview but i don't find it..
//@version=5
indicator('Classic_Levels', overlay=true)
// number of candles used
distance = input.int(250, 'Time range', minval=5, step=10)
// sensitivity when searching levels
sensitivity = input.float(1, 'Sensitivity', minval=0.1, step=0.1)
// weight coefficients
float wOpenClose = 1
float wHighLow = 0.5
float wCross = 0.5
// number of intervals
int n_intervals = 300
// block size (in intervals)
int block_size = 11
// minimum and maximum values
float min_value = barstate.islast ? low[-ta.lowestbars(distance)] : 0
float max_value = barstate.islast ? high[-ta.highestbars(distance)] : 0
// recalculate sensitivity according to the weights
float sens = barstate.islast ? 2 / (sensitivity * (wOpenClose + wHighLow + wCross)) : 0
// calculate everything on the last bar
if barstate.islast
// number of "key points" falling into each interval
// last element is stored but not used
m = array.new_int(n_intervals + 1, 0)
// sum of the "scores" of the points falling into each interval
// last element is stored but not used
r = array.new_float(n_intervals + 1, 0)
// sums of the interval forming the block
a = array.new_float(block_size, 0)
// declaring variables
int i_close = na
int i_open = na
int i_high = na
int i_low = na
int mn = na
int mx = na
float interval_size = (max_value - min_value) / float(n_intervals)
// populate arrays r, m
for k = 1 to distance - 1 by 1
i_close := math.floor((close[k] - min_value) / interval_size)
i_open := math.floor((open[k] - min_value) / interval_size)
i_high := math.floor((high[k] - min_value) / interval_size)
i_low := math.floor((low[k] - min_value) / interval_size)
array.set(r, i_close, array.get(r, i_close) + wOpenClose)
array.set(r, i_open, array.get(r, i_open) + wOpenClose)
array.set(r, i_high, array.get(r, i_high) + wHighLow)
array.set(r, i_low, array.get(r, i_low) + wHighLow)
array.set(m, i_close, array.get(m, i_close) + 1)
array.set(m, i_open, array.get(m, i_open) + 1)
array.set(m, i_high, array.get(m, i_high) + 1)
array.set(m, i_low, array.get(m, i_low) + 1)
// if current candle crosses the interval
if math.abs(i_open - i_close) >= 2
mn := math.min(i_open, i_close)
mx := math.max(i_open, i_close)
for i = mn + 1 to mx - 1 by 1
array.set(r, i, array.get(r, i) - wCross)
array.set(m, i, array.get(m, i) + 1)
// current block sum
float cur_block = 0
// last extremum
float extr_block = 0
// number of the first interval of extremum block
int extr_val = 0
// level value
float level_val = 0
// initial state: level search
bool state = false
// current interval sum
float cur_sum = 0
// iterate intervals
for i = 0 to n_intervals + block_size - 2 by 1
// shift all the interval sums
array.shift(a)
if i < n_intervals
array.push(a, array.get(r, i) / array.get(m, i))
else
array.push(a, 0)
// current block sum
cur_block := array.sum(a)
// check state
if not state
// searching level: <extr_block> keeps the last maximum
if i == 0 or cur_block > extr_block
extr_block := cur_block
extr_val := i - block_size + 1
extr_val
else
// if we've gone far enough from the maximum downwards
if extr_block - cur_block >= sens or i == n_intervals - 1
// draw level
level_val := min_value + interval_size * (extr_val + float(block_size) / 2)
line.new(bar_index - 1, level_val, bar_index, level_val, style=line.style_solid, extend=extend.both, color=color.blue, width=1)
extr_block := cur_block
extr_val := i - block_size + 1
state := true
state
else
// level is found: <extr_block> keeps the last minimum
if cur_block < extr_block
extr_block := cur_block
extr_val := i - block_size + 1
extr_val
else
// if we've gone far enough from the minimum upwards
if cur_block - extr_block >= sens
extr_block := cur_block
extr_val := i - block_size + 1
state := false
state
plotshape(barstate.islast, style=shape.circle, color=color.new(color.navy, 0), size=size.tiny, offset=-distance)
Upvotes: 0
Views: 623
Reputation: 194
Just remove minval and step.
For example
distance = input.int(250, 'Time range', options=[250,500,1000])
Explanation: No need to paas minval, maxval and step, If you are using options
You can do same thing with input.float()
Upvotes: 1
Reputation: 21139
input.string
will return a string
even if you only use numbers.
So, you need to cast your string to a number type. You can use str.tonumber()
for that. It will return a float
. If you need an int
, you need to cast the return value of str.tonumber()
to int
.
in_distance = input.string(defval = '100', title = 'distance', options = ['100', '200', '300' , '500'], group = 'Settings')
distance_float = str.tonumber(in_distance) // This will return a float
distance_int = int(distance_float) // This will return an int
Upvotes: 1