Mean-Reversion Swing Trading Strategy v1

sherwind · strategy · 93 行 · 点赞 519 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=3
//
// A port of the TradeStation EasyLanguage code for a mean-revision strategy described at
//     http://traders.com/Documentation/FEEDbk_docs/2017/01/TradersTips.html
//
// "In “Mean-Reversion Swing Trading,” which appeared in the December 2016 issue of STOCKS & COMMODITIES, author Ken Calhoun
//  describes a trading methodology where the trader attempts to enter an existing trend after there has been a pullback. 
//  He suggests looking for 50% pullbacks in strong trends and waiting for price to move back in the direction of the trend
//  before entering the trade."
//
//  See Also:
//    - 9 Mistakes Quants Make that Cause Backtests to Lie (https://blog.quantopian.com/9-mistakes-quants-make-that-cause-backtests-to-lie-by-tucker-balch-ph-d/)
//    - When Backtests Meet Reality (http://financial-hacker.com/Backtest.pdf)
//    - Why MT4 backtesting does not work (http://www.stevehopwoodforex.com/phpBB3/viewtopic.php?f=28&t=4020)
//
// 
// -----------------------------------------------------------------------------
// Copyright 2018 sherwind
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// The GNU General Public License can be found here
// <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
 
strategy("Mean-Reversion Swing Trading Strategy v1", shorttitle="MRST Strategy v1", overlay=true)
channel_len  = input(defval=20, title="Channel Period", minval=1)
pullback_pct = input(defval=0.5, title="Percent Pull Back Trigger", minval=0.01, maxval=1, step=0.01)
trend_filter_len = input(defval=50, title="Trend MA Period", minval=1)
 
 
upper_band = highest(high, channel_len)
lower_band = lowest(low, channel_len)
trend      = sma(close, trend_filter_len)
 
low_ref  = 0.0
low_ref  :=  nz(low_ref[1])
high_ref = 0.0
high_ref := nz(high_ref[1])
long_ok  = false
long_ok  := nz(long_ok[1])
short_ok = false
short_ok := nz(short_ok[1])
long_ok2 = false
long_ok2  := nz(long_ok2[1])
 
if (low == lower_band)
    low_ref  := low
    long_ok  := false
    short_ok := true
    long_ok2 := false
 
if (high == upper_band)
    high_ref := high
    long_ok  := true
    short_ok := false
    long_ok2  := true
 
// Pull Back Level
trigger = long_ok2 ? high_ref - pullback_pct * (high_ref - low_ref) : low_ref + pullback_pct * (high_ref - low_ref)
 
plot(upper_band, title="Upper Band", color=long_ok2?green:red)
plot(lower_band, title="Lower Band", color=long_ok2?green:red)
plot(trigger, title="Trigger", color=purple)
plot(trend, title="Trend", color=orange)
 
enter_long = long_ok[1] and long_ok and crossover(close, trigger) and close > trend and strategy.position_size <= 0
enter_short = short_ok[1] and short_ok and crossunder(close, trigger) and close < trend and strategy.position_size >= 0
 
if (enter_long)
    long_ok := false
    strategy.entry("pullback-long", strategy.long, stop=close, comment="pullback-long")
else
    strategy.cancel("pullback-long")
 
if (enter_short)
	short_ok := false
    strategy.entry("pullback-short", strategy.short, stop=close, comment="pullback-short")
else
    strategy.cancel("pullback-short")
 
strategy.exit("exit-long", "pullback-long", limit=upper_band, stop=lower_band)
strategy.exit("exit-short", "pullback-short", limit=lower_band, stop=upper_band)

← 返回列表