gpt4 book ai didi

time - Tradingview Pine-Script : How to plot only the last x periods

转载 作者:行者123 更新时间:2023-12-03 21:16:20 24 4
gpt4 key购买 nike

我只想为最后 x 个周期绘制一个指标。
我怎么做?

如果我可以进行时间运算(从 plotStartDate 中减去 x * period),也许我可以使用以下代码:

period = timeframe.ismonthly or timeframe.isweekly ? "12M" : "M"
plotStartDate = timestamp(year(timenow), month(timenow), dayofmonth(timenow), 00, 00)
isPlotDate = time >= plotStartDate
plot(isPlotDate ? mydata : na, color=mydata != mydata[1]:na, style=plot.style_line, linewidth=2)

最佳答案

版本 1

不确定这就是你要找的。它使用 plot()show_last=用于限制在您的 isPlotDate 之后绘制的最后一个柱线数量的参数约束已满足:

//@version=4
study("", "", true)
xPeriods = input(10)
plotStartDate = timestamp(year(timenow), month(timenow), dayofmonth(timenow), 00, 00)
isPlotDate = time >= plotStartDate
plot(isPlotDate ? close : na, show_last = xPeriods)

版本 2
//@version=4
study("Plot starting n months back", "", true)
monthsBack = input(3, minval = 0)
monthsExtra = monthsBack % 12
monthsExcedent = month(timenow) - monthsExtra
yearsBack = floor(monthsBack / 12) + (monthsExcedent <= 0 ? 1 : 0)
targetMonth = monthsExcedent <= 0 ? 12 + monthsExcedent : monthsExcedent
targetYearMonth = year == year(timenow) - yearsBack and month == targetMonth
beginMonth = not targetYearMonth[1] and targetYearMonth

var float valueToPlot = na
if beginMonth
valueToPlot := high
plot(valueToPlot)
bgcolor(beginMonth ? color.green : na)

enter image description here

版本 3

更简单:
//@version=4
study("Plot starting n months back", "", true)
monthsBack = input(3, minval = 0)

targetDate = time >= timestamp(year(timenow), month(timenow) - monthsBack, 1, 0, 0, 0)
beginMonth = not targetDate[1] and targetDate

var float valueToPlot = na
if beginMonth
valueToPlot := high
plot(valueToPlot)
bgcolor(beginMonth ? color.green : na)

关于time - Tradingview Pine-Script : How to plot only the last x periods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60367084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com