使用小时作为日内脚本的条件?

Using hour as a condition for intraday scripting?

我想制定一个非常基本的盘中动量策略。

但是,我一直在想:是否可以使用小时作为交易条件?

例如:

strategy("Strat - Intraday Momentum", overlay=true)

market_open = hour == 9
market_open_hour = hour == 10
market_close = hour == 15

if (market_open)
    strategy.entry("Open", strategy.long)

if (market_open_hour)
    strategy.entry("Open +1 Hour", strategy.long)

strategy.close_all(when= hour == market_close)

看起来很简单,但到目前为止还没有成功。

正在玩弄这个想法:

// Simple Example Code
// Designed to play around with Tradingview's close, close_all and exit functions

strategy("Close Example", overlay=true, pyramiding=2)

monCondition = dayofweek == dayofweek.monday
wedCondition = dayofweek == dayofweek.wednesday

if (monCondition)

    strategy.entry("Monday", strategy.long)

if (wedCondition)
    strategy.entry("Wednesday", strategy.long)

strategy.close_all(when=dayofweek == dayofweek.friday)

来源:https://backtest-rookies.com/2019/03/01/tradingview-strategy-close-strategy-close_all-vs-strategy-exit/

当然可以,但是您的代码的健壮性应该得到提高。最好为过渡建立条件。示例 2 来自 usrman:

//@version=4
study("Time begin", "", true)

// #1
timeOpen1 = hour == 9
timeOpenOk1 = not timeOpen1[1] and timeOpen1
plotchar(timeOpenOk1, "timeOpenOk1", "", location.abovebar, text = "▲")
plotchar(timeOpen1, "timeOpen1", "", location.belowbar, text = "•")

// #2
sessSpec2 = input("0900-0959", type=input.session) + ":1234567"
is_newbar(res, sess) =>
    t = time(res, sess)
    na(t[1]) and not na(t) or t[1] < t
timeOpenOk2 = timeframe.isintraday and is_newbar("1440", sessSpec2)
plotchar(timeOpenOk2, "timeOpenOk2", "", location.abovebar, color.orange, text = "▲\n‎")