使指标在特定时间段内工作
make indicator work during a specific period of time
我有一个警报指示器,我希望它在周一到周五的一段时间内工作。时间段应在上午 8 点到晚上 7 点之间。我怎样才能做到这一点?
这是我目前所拥有的:
//@version=4
study("My Script")
// 1 hour is 3600000 milliseconds
// to convert to 8 hours, multiply 8 with 3600000 = 28800000
startTime = 28800000 // 08.00
endTime = 68400000 // 19.00
currentTime = timenow
if (currentTime >= startTime and currentTime <= endTime)
plot(close)
保存时出现以下错误:
第 16 行:不匹配的输入 'plot' 期望 'end of line without line continuation'。
我找到了解决办法。我不确定我应该在 plotshape()
和 alertcondition()
中使用 t[1]
还是只使用 t
,但这两种方法似乎都有效。如果有人对代码感兴趣,请看这里:
//@version=4
//Shows buy-signal when price crosses over SMA on lower timeframe, while price is above SMA on higher timeframe.
//The opposite is true for sell signals.
//For this indicator to work properly, present chart needs to be lower than the selected higher timeframe.
//You can also set it to work only during a specific time.
//For more information on how to use time: https://www.tradingview.com/pine-script-docs/en/v4/essential/Sessions_and_time_functions.html
study("MA Cross MTF Alert", shorttitle="MA Cross MTF Alert", overlay=true)
timeInput = input('0200-1330', title="Indicator will work during following time (has to be according to the exchange's timezone):")
t = time(timeframe.period, timeInput)
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(pricetype, 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] and t, style=shape.circle, location=location.abovebar, color=color.green, size=size.tiny)
plotshape(Sell[1] and t, style=shape.circle, location=location.belowbar, color=color.red, size=size.tiny)
alertcondition(Buy[1] and t, title="Buy alert", message="Buy {{ syminfo.root }} - MA Cross alert")
alertcondition(Sell[1] and t, title="Sell alert", message="Sell {{ syminfo.root }} - MA Cross alert")
我有一个警报指示器,我希望它在周一到周五的一段时间内工作。时间段应在上午 8 点到晚上 7 点之间。我怎样才能做到这一点? 这是我目前所拥有的:
//@version=4
study("My Script")
// 1 hour is 3600000 milliseconds
// to convert to 8 hours, multiply 8 with 3600000 = 28800000
startTime = 28800000 // 08.00
endTime = 68400000 // 19.00
currentTime = timenow
if (currentTime >= startTime and currentTime <= endTime)
plot(close)
保存时出现以下错误: 第 16 行:不匹配的输入 'plot' 期望 'end of line without line continuation'。
我找到了解决办法。我不确定我应该在 plotshape()
和 alertcondition()
中使用 t[1]
还是只使用 t
,但这两种方法似乎都有效。如果有人对代码感兴趣,请看这里:
//@version=4
//Shows buy-signal when price crosses over SMA on lower timeframe, while price is above SMA on higher timeframe.
//The opposite is true for sell signals.
//For this indicator to work properly, present chart needs to be lower than the selected higher timeframe.
//You can also set it to work only during a specific time.
//For more information on how to use time: https://www.tradingview.com/pine-script-docs/en/v4/essential/Sessions_and_time_functions.html
study("MA Cross MTF Alert", shorttitle="MA Cross MTF Alert", overlay=true)
timeInput = input('0200-1330', title="Indicator will work during following time (has to be according to the exchange's timezone):")
t = time(timeframe.period, timeInput)
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(pricetype, 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] and t, style=shape.circle, location=location.abovebar, color=color.green, size=size.tiny)
plotshape(Sell[1] and t, style=shape.circle, location=location.belowbar, color=color.red, size=size.tiny)
alertcondition(Buy[1] and t, title="Buy alert", message="Buy {{ syminfo.root }} - MA Cross alert")
alertcondition(Sell[1] and t, title="Sell alert", message="Sell {{ syminfo.root }} - MA Cross alert")