plotshape 没有绘制在正确的烛台上
plotshape doesn't plot on correct candlestick
我用 pinescript 写了一个指标,但它没有按预期工作。这个想法是当价格在较低的时间范围内穿过移动平均线时,但仅当价格在较高的时间范围内穿过同一时期的移动平均线时,让它在柱上绘制一个形状。关于我试图实现的目标的更好解释:假设价格在 1 小时时间范围内高于 5 周期 SMA,虽然它在上方,但在某个时候价格将在 15 分钟时间范围内超过 5 周期 SMA,并且正确那么它应该在 15 米的时间范围内在该柱上绘制一个形状。谁能帮我?这是代码:
study("MA Cross MTF Alert", shorttitle="MA Cross MTF Alert", overlay=true)
smaPeriod = input(5, title="Period SMA")
pricetype = input(close, title="Price Source For The SMA")
lowerTfRes = input(title="Lower timeframe", type=resolution, defval="15")
higherTfRes = input(title="Higher timeframe", type=resolution, defval="60")
priceLowTf = security(tickerid, lowerTfRes, pricetype)
priceHighTf = security(tickerid, higherTfRes, pricetype)
smaLowTf = sma(priceLowTf, smaPeriod)
smaHighTf = sma(priceHighTf, smaPeriod)
Buy = close > smaLowTf and close > smaHighTf
Sell = close < smaLowTf and close < smaHighTf
plotshape(Buy, style=shape.circle, location=location.abovebar, color=green)
plotshape(Sell, style=shape.circle, location=location.belowbar, color=red)
alertcondition(Buy, title="Buy alert", message="Buy " + syminfo.root + "MA Cross alert")
alertcondition(Sell, title="Sell alert", message="Sell " + syminfo.root + "MA Cross alert")
谢谢
不建议从比当前图表更短的时间范围内获取数据。您可以从两个不同的更高时间范围图表中获取数据,但为了简单起见,我在此示例中使用了当前图表。
//@version=4
study("MA Cross MTF Alert", shorttitle="MA Cross MTF Alert", overlay=true)
smaPeriod = input(5, title="Period SMA")
pricetype = input(close, title="Price Source For The SMA")
higherTfRes = input(title="Higher timeframe", type=input.resolution, defval="60")
priceHighTf = security(syminfo.tickerid, higherTfRes, pricetype[1], lookahead=barmerge.lookahead_on)
smaHighTf = security(syminfo.tickerid, higherTfRes, sma(priceHighTf, smaPeriod)[1], lookahead=barmerge.lookahead_on)
smaLowTf = sma(close, smaPeriod)
Buy = crossover(close, smaLowTf) and close > smaHighTf
Sell = crossunder(close, smaLowTf) and close < smaHighTf
plot(smaLowTf)
plot(smaHighTf, color=color.black, linewidth=2)
plotshape(Buy[1], style=shape.circle, location=location.abovebar, color=color.green, size=size.small)
plotshape(Sell[1], style=shape.circle, location=location.belowbar, color=color.red, size=size.small)
alertcondition(Buy[1], title="Buy alert", message="Buy {{syminfo.root }} MA Cross alert")
alertcondition(Sell[1], title="Sell alert", message="Sell {{ syminfo.root }} MA Cross alert")
此外,您从较高的时间范围内拉出价格,然后在当前时间范围内应用 SMA。所以假设您当前的时间范围是 15 分钟,如果您从 1 小时图表中提取一个价格点,它将显示四个柱的相同价格,这意味着您的当前图表 SMA 正在应用于该点。 1 个 1h 的周期是 4 个 15m 的周期。您想将 SMA 直接插入安全功能,以便从 1 小时图表中获得准确的值。
请注意,我延迟了数据并设置了先行以避免重绘。我还为 plotshapes 和 alertconditions 添加了 [1]
以确保数据得到确认。
我将当前图表上的买入和卖出条件更改为 crossover
、crossunder
以防止再次触发。
未使用 priceHighTf,您可以在 Buy
和 Sell
条件下使用它。
我添加了图表来显示 SMAa。在创建脚本时添加绘图将有助于更快地找出问题。我为 plotshape
设置了一个大小,因为默认情况下它们非常小。
我用 pinescript 写了一个指标,但它没有按预期工作。这个想法是当价格在较低的时间范围内穿过移动平均线时,但仅当价格在较高的时间范围内穿过同一时期的移动平均线时,让它在柱上绘制一个形状。关于我试图实现的目标的更好解释:假设价格在 1 小时时间范围内高于 5 周期 SMA,虽然它在上方,但在某个时候价格将在 15 分钟时间范围内超过 5 周期 SMA,并且正确那么它应该在 15 米的时间范围内在该柱上绘制一个形状。谁能帮我?这是代码:
study("MA Cross MTF Alert", shorttitle="MA Cross MTF Alert", overlay=true)
smaPeriod = input(5, title="Period SMA")
pricetype = input(close, title="Price Source For The SMA")
lowerTfRes = input(title="Lower timeframe", type=resolution, defval="15")
higherTfRes = input(title="Higher timeframe", type=resolution, defval="60")
priceLowTf = security(tickerid, lowerTfRes, pricetype)
priceHighTf = security(tickerid, higherTfRes, pricetype)
smaLowTf = sma(priceLowTf, smaPeriod)
smaHighTf = sma(priceHighTf, smaPeriod)
Buy = close > smaLowTf and close > smaHighTf
Sell = close < smaLowTf and close < smaHighTf
plotshape(Buy, style=shape.circle, location=location.abovebar, color=green)
plotshape(Sell, style=shape.circle, location=location.belowbar, color=red)
alertcondition(Buy, title="Buy alert", message="Buy " + syminfo.root + "MA Cross alert")
alertcondition(Sell, title="Sell alert", message="Sell " + syminfo.root + "MA Cross alert")
谢谢
不建议从比当前图表更短的时间范围内获取数据。您可以从两个不同的更高时间范围图表中获取数据,但为了简单起见,我在此示例中使用了当前图表。
//@version=4
study("MA Cross MTF Alert", shorttitle="MA Cross MTF Alert", overlay=true)
smaPeriod = input(5, title="Period SMA")
pricetype = input(close, title="Price Source For The SMA")
higherTfRes = input(title="Higher timeframe", type=input.resolution, defval="60")
priceHighTf = security(syminfo.tickerid, higherTfRes, pricetype[1], lookahead=barmerge.lookahead_on)
smaHighTf = security(syminfo.tickerid, higherTfRes, sma(priceHighTf, smaPeriod)[1], lookahead=barmerge.lookahead_on)
smaLowTf = sma(close, smaPeriod)
Buy = crossover(close, smaLowTf) and close > smaHighTf
Sell = crossunder(close, smaLowTf) and close < smaHighTf
plot(smaLowTf)
plot(smaHighTf, color=color.black, linewidth=2)
plotshape(Buy[1], style=shape.circle, location=location.abovebar, color=color.green, size=size.small)
plotshape(Sell[1], style=shape.circle, location=location.belowbar, color=color.red, size=size.small)
alertcondition(Buy[1], title="Buy alert", message="Buy {{syminfo.root }} MA Cross alert")
alertcondition(Sell[1], title="Sell alert", message="Sell {{ syminfo.root }} MA Cross alert")
此外,您从较高的时间范围内拉出价格,然后在当前时间范围内应用 SMA。所以假设您当前的时间范围是 15 分钟,如果您从 1 小时图表中提取一个价格点,它将显示四个柱的相同价格,这意味着您的当前图表 SMA 正在应用于该点。 1 个 1h 的周期是 4 个 15m 的周期。您想将 SMA 直接插入安全功能,以便从 1 小时图表中获得准确的值。
请注意,我延迟了数据并设置了先行以避免重绘。我还为 plotshapes 和 alertconditions 添加了 [1]
以确保数据得到确认。
我将当前图表上的买入和卖出条件更改为 crossover
、crossunder
以防止再次触发。
未使用 priceHighTf,您可以在 Buy
和 Sell
条件下使用它。
我添加了图表来显示 SMAa。在创建脚本时添加绘图将有助于更快地找出问题。我为 plotshape
设置了一个大小,因为默认情况下它们非常小。