Reputation: 19
I have requirement to calculate number of Trading days between today and previous date. This count will exclude all the holidays.
I have achieved the calculation using below script, but the result is correct only for last bar as for previous Bars, bars gets reduced.
My requirement is to have the difference between today and previous date defined as constant which will work from the 1st Bar to last Bar.
Current Output: Day 1 : 1 Bar, Day 2 : 2 Bars, Day 3 : 3 Bars,
Required output : (123 is difference between today and previous date) Day 1 : 123 Bar, Day 2 : 123 Bars, Day 3 : 123 Bars,
i_fromYear = input.int(2021, 'From Year', minval=1900)
i_fromMonth = input.int(5, 'From Month', minval=1, maxval=12)
i_fromDay = input.int(18, 'From Day', minval=1, maxval=31)
fromDate = timestamp(i_fromYear, i_fromMonth, i_fromDay, 00, 00)
isGreen = close > open
gCount = ta.cum(time > fromDate and isGreen ? 1 : 0)
rCount = ta.cum(time > fromDate and not isGreen ? 1 : 0)
total = gCount + rCount
Adding Original Full Script :
//@version=5
indicator('Relative Strength', shorttitle='ARS', max_bars_back=1000, overlay=true)
comparativeTickerId = input.symbol('NSE:NIFTY', title='Comparative Symbol')
startTime = input.time(timestamp("18 May 2021 00:00"), "Start Time")
endTime = input.time(timestamp("20 Nov 2021 00:00"), "End Time")
_barForTime(_t) =>
var int _bar = na
if time_close[1] <= _t and time >= _t
_bar := bar_index
_bar
firstTime = _barForTime(startTime)
secondTime = _barForTime(endTime)
barDelta = secondTime - firstTime
// ************************************
lenght = barDelta // Change this to 100 to see the actual result, which will be plotted as well as visible in Lable
// ************************************
// Checking the value of barDelta
plot(lenght, title='Days', color=color.blue, linewidth = 3, style=plot.style_line)
label.new(bar_index, high, style=label.style_none, text='Bar Count\n' + str.tostring(lenght) + '\n')
baseSymbol = request.security(syminfo.tickerid, timeframe.period, close)
comparativeSymbol = request.security(comparativeTickerId, timeframe.period, close)
hline(0, color=color.black, linestyle=hline.style_solid)
res = baseSymbol / baseSymbol[lenght-1] / (comparativeSymbol / comparativeSymbol[lenght-1]) - 1
plot(res, title='ARS', color=res >= 0.0 ? color.lime : color.orange, linewidth = 2, style=plot.style_area)
Upvotes: 1
Views: 4174
Reputation: 2488
This would be a solution
bars_count(start, end)=>
diff_unix_min = (end - start) / (1000 * 60)
p = str.tonumber(timeframe.period)
if timeframe.isdwm
if timeframe.isdaily
p := timeframe.multiplier * 60 * 24
if timeframe.isweekly
p := timeframe.multiplier * 60 * 24 * 7
if timeframe.ismonthly
p := timeframe.multiplier * 60 * 24 * 7 * 4
math.floor(math.max(diff_unix_min / p, 1))
Upvotes: 0
Reputation: 2300
ashxos, I have updated you with a timestamp input and a function to get the bar_index for a given time. We pass our 2 dates into the functions, and subtract the results.
//@version=5
indicator("My Script")
startTime = input.time(timestamp("01 Nov 2021 00:00"), "Start Time")
endTime = input.time(timestamp("15 Nov 2021 00:00"), "End Time")
_barForTime(_t) =>
var int _bar = na
if time_close[1] <= _t and time >= _t
_bar := bar_index
_bar
firstTime = _barForTime(startTime)
secondTime = _barForTime(endTime)
barDelta = secondTime - firstTime
Cheers, and best of luck with your trading and coding
Upvotes: 0