-
-
Save aka-STInG/34a406e72235ce2b59200ab78743e118 to your computer and use it in GitHub Desktop.
Mean-Reversion Swing Trading Strategy v1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@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) | |
has_long = strategy.position_size > 0 | |
has_short = strategy.position_size < 0 | |
enter_long = long_ok[1] and long_ok and crossover(close, trigger) and close > trend and not has_long[1] | |
enter_short = short_ok[1] and short_ok and crossunder(close, trigger) and close < trend and not has_short[1] | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment