我想拿一个股票图表,并创建一个指标,将显示的平均/平均的中华民国(变动率)的每日收盘价,作为一个不断更新长度移动平均(运行平均)从一个固定的日期开始。例如,标准普尔500指数在2022年1月4日/5日之间的变化率为-1.94%,1月5日/6日期间的变化率为-.10%。因此,这一指标将计算(从固定日期1月4日),一个2期移动平均使用-1.94%和-.10% = 1.02%。然后在1月7日,该指标将计算从1月4日开始的3个月移动平均值。NExt,在1月8日,它将计算出从1月4日开始的4个周期移动平均值,等等.我开始了一个代码,但我无法使它正常工作。这个指标有助于在趋势中找到平均值,因为它每天都在进化(概率分布),而不必每天改变移动平均长度(这是你可以做的,但增加了更多的时间)。
//@version=4
study("velocity", shorttitle="vel", overlay=true)
timeYear = input(2022, title="Year", minval=1991, maxval=2100, type=input.integer)
timeMonth = input(1, title="Month", minval=1, maxval=12, type=input.integer)
timeDay = input(04, title="Day", minval=1, maxval=31, type=input.integer)
timeHours = input(9, title="Hours", minval=0, maxval=23, type=input.integer)
timeMinutes = input(30, title="Minutes", minval=0, maxval=59, type=input.integer)
timeSeconds = input(0, title="Seconds", minval=0, maxval=59, type=input.integer)
// Initilization of variables only once
var delta = 0
// start time at 0 from a particular time interval
if(year == timeYear and month == timeMonth and dayofmonth == timeDay and hour == timeHours and minute == timeMinutes and second == timeSeconds)
delta := 0
// Count number of bars
if(year >= timeYear and month >= timeMonth and dayofmonth > timeDay)
delta += 1
plotchar(delta, title="days passed from startdate", color=color.green, char='')
// set to TOP so it doesnt mess up chart scale
delta0 = delta-delta
// rate of change and moving average of ROC
length = input(1, minval=1)
source = input(close, "Source")
roc = (source[delta]-source[delta][1]) / source[delta]
rocma = sma(roc, delta)
plot(rocma)
发布于 2022-10-23 00:46:37
松树脚本的一个限制是有一个限制,你可以引用多少条回来。为了克服这一限制,我将使用一个array
,将其加到10万个元素中,而不是计算这些元素的平均值:
//@version=5
indicator("My script")
var roc_array = array.new_float(0)
float avg_roc_array = array.avg(roc_array)
start = input.time(timestamp("04 Oct 2022 00:00:00"), "Start date")
if time >= start
roc = ((close / close[1]) - 1) * 100
array.push(roc_array, roc)
avg_roc_array := array.avg(roc_array)
plot(avg_roc_array)
或者,您可以在已经编写的代码中使用相同的逻辑。请记住,在进行任何计算之前,您需要检查delta
的值是否高于0
,而不只是创建一系列roc
并计算该系列的sma
:
//@version=4
study("velocity", shorttitle="vel", overlay=true)
timeYear = input(2022, title="Year", minval=1991, maxval=2100, type=input.integer)
timeMonth = input(1, title="Month", minval=1, maxval=12, type=input.integer)
timeDay = input(04, title="Day", minval=1, maxval=31, type=input.integer)
timeHours = input(9, title="Hours", minval=0, maxval=23, type=input.integer)
timeMinutes = input(30, title="Minutes", minval=0, maxval=59, type=input.integer)
timeSeconds = input(0, title="Seconds", minval=0, maxval=59, type=input.integer)
// Initilization of variables only once
var delta = 0
// start time at 0 from a particular time interval
if(year == timeYear and month == timeMonth and dayofmonth == timeDay and hour == timeHours and minute == timeMinutes and second == timeSeconds)
delta := 0
// Count number of bars
if(year >= timeYear and month >= timeMonth and dayofmonth > timeDay)
delta += 1
plotchar(delta, title="days passed from startdate", color=color.green, char='')
// rate of change and moving average of ROC
source = input(close, "Source")
float rocma = na
float roc = 0
if delta >= 1
roc := ((source - source[1]) / source[1]) * 100
rocma := sma(roc, delta)
plot(rocma)
编辑:
考虑过这个问题后,我想到了一个更简单的方法来实现同样的目标:
//@version=5
indicator("My script")
start = input.time(timestamp("04 Oct 2022 00:00:00"), "Start date")
roc = time >= start ? ((close / close[1]) - 1) * 100 : na
bars_since_first_roc = ta.barssince(na(roc))
total_roc = bars_since_first_roc > 0 ? math.sum(roc, bars_since_first_roc) : na
avg_roc = total_roc / bars_since_first_roc
plot(avg_roc)
https://stackoverflow.com/questions/74167874
复制