Skip to content

Instantly share code, notes, and snippets.

@sherwind
Created April 15, 2018 03:43
Show Gist options
  • Save sherwind/2d091f18c72c1c1451165b203406be4e to your computer and use it in GitHub Desktop.
Save sherwind/2d091f18c72c1c1451165b203406be4e to your computer and use it in GitHub Desktop.
Ichimoku Cloud Strategy
//@version=3
//
// Based on the trading strategy described at
// http://stockcharts.com/school/doku.php?id=chart_school:trading_strategies:ichimoku_cloud
//
// See Also:
// - Backtesting and forwardtesting (of TradingView Strategies) <https://www.tradingview.com/wiki/Strategies#Backtesting_and_forwardtesting>
// - 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(title="Ichimoku Cloud Strategy", shorttitle="Ichimoku Strategy", overlay=true, pyramiding=3)
conversionPeriods = input(9, minval=1, title="Conversion Line Periods"),
basePeriods = input(26, minval=1, title="Base Line Periods")
laggingSpan2Periods = input(52, minval=1, title="Lagging Span 2 Periods"),
displacement = input(26, minval=1, title="Displacement")
usePSARTrailStop = input(true, title="Use Parabolic SAR for Trailing Stop")
psarStart = input(0.02, title="Parabolic SAR Start")
psarIncrement = input(0.02, title="Parabolic SAR Increment")
psarMaximum = input(0.2, title="Parabolic SAR Maximum")
donchian(len) => avg(lowest(len), highest(len))
conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriods)
leadLine1 = avg(conversionLine, baseLine)
leadLine2 = donchian(laggingSpan2Periods)
leadLineDisp1 = leadLine1[displacement]
leadLineDisp2 = leadLine2[displacement]
psar = sar(psarStart, psarIncrement, psarMaximum)
// BUY Signal:
// close > leading span b and
// leading span a > leading span b and
// close crosses over base line and
// close > parabolic sar
buySignal = close > leadLineDisp2 and
leadLineDisp1 > leadLineDisp2 and
crossover(close, baseLine) and
(usePSARTrailStop ? close > psar : not usePSARTrailStop)
// Sell Signal:
// close < leading span a and
// leading span a < leading span b and
// close crosses under base line and
// close < psar
sellSignal = close < leadLineDisp1 and
leadLineDisp1 < leadLineDisp2 and
crossunder(close, baseLine) and
(usePSARTrailStop ? close < psar : not usePSARTrailStop)
hasLong = strategy.position_size > 0
hasShort = strategy.position_size < 0
strategy.entry("ichimoku-long", strategy.long, when = buySignal)
strategy.entry("ichimoku-short", strategy.short, when = sellSignal)
strategy.exit("trailstop-long", "ichimoku-long", stop = psar, when = hasLong and usePSARTrailStop)
strategy.exit("trailstop-short", "ichimoku-short", stop = psar, when = hasShort and usePSARTrailStop)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment