0

我需要计算今天和上一个日期之间的交易天数。此计数将排除所有假期。

我已经使用下面的脚本实现了计算,但结果只对最后一根柱线和之前的柱线一样正确,柱线减少了。

我的要求是将今天和上一个日期之间的差异定义为常数,这将从第一个栏到最后一个栏起作用。

当前输出:第 1 天:1 条,第 2 天:2 条,第 3 天:3 条,

所需输出:(123 是今天和前一天之间的差异)第 1 天:123 条,第 2 天:123 条,第 3 天:123 条,

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

添加原始完整脚本:

//@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)
4

1 回答 1

0

ashxos,我已经更新了你的时间戳输入和一个函数来获取给定时间的 bar_index。我们将 2 个日期传递给函数,然后减去结果。

//@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

干杯,祝您交易和编码好运

于 2021-11-19T06:09:27.517 回答