本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// MIT License
// (c) 2025 Yuri Lopukhov
//
// This is my (naive) implementation of
//
// "Beat the Market An Effective Intraday Momentum Strategy for S&P500 ETF (SPY)"
// paper by
// Carlo Zarattini, Andrew Aziz, Andrea Barbon
//
// so the credit goes to them.
//
// It is supposed to run on 30-minute timeframe, there may be issues on other timeframes
// Results are good, but not as good as they are stated in the paper (unsurprisingly):
// returns are smaller and Sharpe is very low (which is actually weird), there are also margin calls if you enable margin check
// I have my own ideas of improvements which I will probably implement separately to keep this clean
//
// Version history:
// 2025-06-25: 1.0 - initial implementation
//@version=6
strategy("Out of the Noise Intraday Strategy with VWAP [YuL]", overlay = true, shorttitle = "Noise/VWAP",
commission_type = strategy.commission.cash_per_contract, commission_value = 0.0035, // $0.0035 per share commission
margin_long = 0, margin_short = 0, // you actually need to put 25 here, but original paper doesn't check for margin I believe and it affects results quite a bit
process_orders_on_close = true, // we really don't want to transfer position overnight
slippage = 0) // paper suggests slippage 0.1 per share, but TV doesn't have such option and 1 tick seem too much? It cuts returns by 3 on the long run
n = input.int(14, "Period")
eod_exit = input.bool(true, "Exit at End of Session")
leverage_long = input.float(4, "Leverage Long")
leverage_short = input.float(4, "Leverage Short")
dynamic_position = input.bool(true, "Use Volatility Target:", inline="position") // if false, always use max leverage
vol_target = input.float(0.02, "", inline="position")
var first_day_bar_indices = array.new_int()
var cur_open = 0.0
var open_idx = 0
var float upper_base = na
var float lower_base = na
var float upper_bound = na
var float lower_bound = na
// on first bar of the session initialize data and save open price and bar index
if session.isfirstbar
first_day_bar_indices.push(bar_index)
if first_day_bar_indices.size() > n
first_day_bar_indices.remove(0)
upper_base := math.max(open, close[1])
lower_base := math.min(open, close[1])
cur_open := open
open_idx := bar_index
// when we have enough days passed, we can calculate average moves for each bar across days
if first_day_bar_indices.size() == n
intra_day_idx = bar_index - first_day_bar_indices.get(-1)
moves = array.new_float()
for day_idx = 0 to n-1
first_bar_idx = first_day_bar_indices.get(day_idx)
i = bar_index - first_bar_idx
first_bar_open = open[i]
if i + intra_day_idx >= 0
move = math.abs(close[i + intra_day_idx] / first_bar_open - 1)
moves.push(move)
avg_move = moves.avg()
upper_bound := upper_base * (1 + avg_move)
lower_bound := lower_base * (1 - avg_move)
// we need vwap for trailing stops and daily volatility for dynamic position size
vwap = ta.vwap(close)
vol = request.security(syminfo.tickerid, "D", ta.stdev(close / close[1] - 1, 14))
pos_target = vol_target / vol
// liquidate at end of session
if eod_exit and session.islastbar
strategy.close_all("EOS")
// place entries and close positions intraday
else
// when not long, search for long entries and short exits
if strategy.position_size <= 0
// enter long when price is above upper bound, except for the first bar (if exit at end of session is enabled)
if close > upper_bound and (not session.isfirstbar or not eod_exit)
target = dynamic_position ? math.min(leverage_long, pos_target) : leverage_long
qty = int(strategy.equity * target / close)
if qty > 0
strategy.entry("Long", strategy.long, qty)
// exit short position on VWAP or lower bound cross (whichever is closer)
if close > math.min(vwap, lower_bound)
strategy.close("Short", "VWAP Exit Short")
// when not short, search for short entries and long exits
if strategy.position_size >= 0
// enter short when price is below lower bound, except for the first bar (if exit at end of session is enabled)
if close < lower_bound and (not session.isfirstbar or not eod_exit)
target = dynamic_position ? math.min(leverage_short, pos_target) : leverage_short
qty = int(strategy.equity * target / close)
if qty > 0
strategy.entry("Short", strategy.short, qty)
// exit long position on VWAP or upper bound cross (whichever is closer)
if close < math.max(vwap, upper_bound)
strategy.close("Long", "VWAP Exit Long")
// plot noise area and VWAP
plt_upper = plot(upper_bound, "Upper", color.new(color.green, 75))
plt_open = plot(cur_open, "Open", color.new(color.white, 100))
plt_lower = plot(lower_bound, "Lower", color.new(color.red, 75))
fill(plt_lower, plt_upper, color.new(color.yellow, 90))
plot(vwap, "VWAP", color.blue)