使用小时作为日间脚本编写的条件吗?

问题描述 投票:0回答:1

我想为日内动量制定一个非常基本的策略。

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

例如:

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/

time pine-script hour
1个回答
1
投票

可以肯定,但是代码的健壮性应该得到改善。在过渡上建立条件会更好。示例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‎")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.