Strategy · Pine Script v5 · On the chart

My Strategy

by hasnocool· code credited to Romedius

This overlay strategy combines a Schaff Trend Cycle, an ATR trailing stop, and a fixed 200-period simple moving average.

View on GitHub

GitHub stars106on the repository
Pine versionv5runs as is
Lines of code84
Settings9inputs
LicenceGPL-3.0
Last update2 years ago18 Sep 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

84 lines of Pine Script v5 from ATR + STC + MA.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

ATR + STC + MA.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Romedius
//@version=5
strategy("My Strategy", overlay=true, margin_long=100, margin_short=100)
// STC
EEEEEE=input(12,"Length",group="STC")
BBBB=input(26,"FastLength",group="STC")
BBBBB=input(50,"SlowLength",group="STC")
AAAA(BBB, BBBB, BBBBB) =>
    fastMA = ta.ema(BBB, BBBB)
    slowMA = ta.ema(BBB, BBBBB)
    AAAA = fastMA - slowMA
    AAAA
    
AAAAA(EEEEEE, BBBB, BBBBB) => 
    //AAA=input(0.5)
    var AAA = 0.5
    var CCCCC = 0.0
    var DDD = 0.0
    var DDDDDD = 0.0
    var EEEEE = 0.0
    BBBBBB = AAAA(close,BBBB,BBBBB)     
    CCC = ta.lowest(BBBBBB, EEEEEE)
    CCCC = ta.highest(BBBBBB, EEEEEE) - CCC    
    CCCCC := (CCCC > 0 ? ((BBBBBB - CCC) / CCCC) * 100 : nz(CCCCC[1])) 
    DDD := (na(DDD[1]) ? CCCCC : DDD[1] + (AAA * (CCCCC - DDD[1]))) 
    DDDD = ta.lowest(DDD, EEEEEE) 
    DDDDD = ta.highest(DDD, EEEEEE) - DDDD     
    DDDDDD := (DDDDD > 0 ? ((DDD - DDDD) / DDDDD) * 100 : nz(DDDDDD[1])) 
    EEEEE := (na(EEEEE[1]) ? DDDDDD : EEEEE[1] + (AAA * (DDDDDD - EEEEE[1])))
    EEEEE
mAAAAA = AAAAA(EEEEEE,BBBB,BBBBB)
stc = mAAAAA > mAAAAA[1] ? true : false
stc_sig = stc == true and stc[1] == false ? 1 : stc == false and stc[1] == true ? -1 : 0
stc_long = stc_sig == 1
stc_short = stc_sig == -1
// STC end
// ATR stops
nATRPeriod = input(5,group="ATR Stops")
nATRMultip = input(3.5,group="ATR Stops")
xATR = ta.atr(nATRPeriod)
nLoss = nATRMultip * xATR
xATRTrailingStop = 0.0
xATRTrailingStop := close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), close - nLoss) : close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), close + nLoss) : close > nz(xATRTrailingStop[1], 0) ? close - nLoss : close + nLoss
pos = 0
pos := close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0) ? 1 : close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
atr_sig = pos == -1 ? false : true
// ATR stops end
// ma
ma_len = input(200, title="MA Length", group="Moving Average")
ma = ta.sma(close, 200)
ma_sig = close < ma ? false : true
// ma end
// strategy entries
tp_mult = input(2, title="Take Profit ATR Multiplier", group="Strategy")
sl_mult = input(1, title="Stop Loss ATR Multiplier", group="Strategy")
early_stop = input(true, title="Close position when ATR changes color")
atr_stop = if close < xATRTrailingStop
    close - (close - xATRTrailingStop) * sl_mult
else
    close + (xATRTrailingStop - close) * sl_mult
longCondition = atr_sig == true and stc_sig == 1 and ma_sig == true
shortCondition = atr_sig == false and stc_sig == -1 and ma_sig == false
if (longCondition)
    strategy.entry("Long", strategy.long)
    strategy.exit("Exit Long", "Long", limit=close + xATR * tp_mult, stop=atr_stop)
else if atr_sig == false and early_stop
    strategy.close("Long")
if (shortCondition)
    strategy.entry("Short", strategy.short)
    strategy.exit("Exit Short", "Short", limit=close - xATR * tp_mult, stop=atr_stop)
else if atr_sig == true and early_stop
    strategy.close("Short")
    
// plot stuff
atr_color = pos == -1 ? color.red: pos == 1 ? color.green : color.blue
plot(atr_stop, title="ATR Stop", color=atr_color)
ma_color = ma_sig ? color.green : color.red
plot(ma, title="Moving Average", color=ma_color)
stc_color = stc_long ? color.green : color.red
plotshape(stc_long, style=shape.triangleup, color=stc_color, title="STC Long Signal", size=size.tiny)
plotshape(stc_short, style=shape.triangledown, color=stc_color, title="STC Short Signal", size=size.tiny)
// plot stuff end
Expand (100 lines)

Overview

My Strategy enters long when the cycle turns upward, the ATR bias is bullish, and price is at or above the average, and short on the opposite alignment. Exits place an ATR-multiple take-profit limit and a stop scaled from the trailing ATR distance. Inputs change the cycle lengths, ATR period and multiplier, profit and stop multiples, and whether positions close when the ATR color flips. The declared moving-average length is not used in the calculation.

Details

Published by

hasnocool on GitHub

Code credit

Romedius, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Trend, Oscillators & momentum, Moving averages, Volatility & bands

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

analysisbtcusdtcryptocurrencydatapandaspythonsimulationtradingtradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Lengthint12EEEEEE
FastLengthint26BBBB
SlowLengthint50BBBBB
nATRPeriodint5nATRPeriod
nATRMultipfloat3.5nATRMultip
MA Lengthint200ma_len
Take Profit ATR Multiplierint2tp_mult
Stop Loss ATR Multiplierint1sl_mult
Close position when ATR changes colorbooltrueearly_stop
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Momentum Rider DAILY FINAL

by greenpeas007
58 stars

This strategy combines exponential moving averages (EMA 9, 21, 50), RSI, MACD, and Supertrend indicators to enter long positions. It triggers trades using six dynamic setups based on momentum breakouts, EMA crossovers, and pullbacks. Position sizing is compute

Strategy v6 TrendOscillators & momentum Repaints
Updated 4 months ago · Custom licence GitHub repository of Momentum Rider DAILY FINAL

CombinedBinHAndClucV6

by viktors1982
36 stars

CombinedBinHAndClucV6 is a trading strategy based on the popular BinHV45 and ClucMay72018 algorithms. It calculates dual Bollinger Bands, Exponential Moving Averages, Relative Strength Index, Average True Range, and volume metrics across primary and one-hour h

Strategy v4 Volatility & bandsOscillators & momentum Repaints
Updated 2 years ago · None stated GitHub repository of CombinedBinHAndClucV6

BB Swing

by benso87
24 stars

This strategy calculates Bollinger Bands, a configurable moving average slope, Stochastic RSI, ATR, and ADX directly on the price chart. It evaluates trend bias and momentum conditions to generate long and short swing entries using percent-of-equity sizing. Th

Strategy v5 Volatility & bandsOscillators & momentum
Updated 4 years ago · MPL-2.0 GitHub repository of BB Swing

This strategy trades long and short on the price chart using a multi-timeframe TradingLatino setup. It requires EMA 10 to cross or sit above or below EMA 55 on the one-hour, four-hour, and daily charts, together with a positive ADX slope, a directional monitor

Strategy v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of Ruckard ABCBitcoin TradingLatino A010

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers