Strategy · Pine Script v4 · On the chart

Stoch RSI Crossover Strat + EMA

by hasnocool

This strategy evaluates trade entries by combining Stochastic RSI crossovers with MACD momentum and three exponential moving averages (8, 15, and 50 EMAs).

View on GitHub

GitHub stars106on the repository
Pine versionv4older, still runs
Lines of code151
Settings31inputs
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

151 lines of Pine Script v4 from 3 EMA Stochastik RSI ATR SL TP only LONG.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

3 EMA Stochastik RSI ATR SL TP only LONG.pine
//@version=4
strategy(title="Stoch RSI Crossover Strat + EMA", shorttitle="Stoch RSI Cross + EMA Strat", overlay = true, pyramiding = 0, max_bars_back = 3000, calc_on_order_fills = false, commission_type =  strategy.commission.percent, commission_value = 0.075, default_qty_type = strategy.percent_of_equity, default_qty_value = 1, initial_capital=10000, currency=currency.USD)
// Time Range
FromMonth=input(defval=1,title="FromMonth",minval=1,maxval=12)
FromDay=input(defval=1,title="FromDay",minval=1,maxval=31)
FromYear=input(defval=2020,title="FromYear",minval=2017)
ToMonth=input(defval=1,title="ToMonth",minval=1,maxval=12)
ToDay=input(defval=1,title="ToDay",minval=1,maxval=31)
ToYear=input(defval=9999,title="ToYear",minval=2017)
start=timestamp(FromYear,FromMonth,FromDay,00,00)
finish=timestamp(ToYear,ToMonth,ToDay,23,59)
window()=>time>=start and time<=finish?true:false
// See if this bar's time happened on/after start date
afterStartDate = time >= start and time<=finish?true:false
enablelong = input(true, title="Enable Long?")
enableshort = input(true, title="Enable Short?")
enableatrexit = input(true, title="Enable ATR Exit?")
enablelowexit = input(false, title="Enable Lowest Low/ Highest High Exit?")
//STOCH RSI
smoothK = input(3, minval=1)
smoothD = input(3, minval=1)
lengthRSI = input(14, minval=1)
lengthStoch = input(14, minval=1)
src = input(close, title="RSI Source")
rsi1 = rsi(src, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
//MACD
fast_length = input(title="Fast Length", type=input.integer, defval=12)
slow_length = input(title="Slow Length", type=input.integer, defval=24)
srcmacd = input(title="MACD Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 8)
sma_source = input(title="Simple MA (Oscillator)", type=input.bool, defval=true)
sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=true)
zeroline = 0
// Plot colors
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350
col_macd = #0094ff
col_signal = #ff6a00
// Calculating
fast_ma = sma_source ? sma(srcmacd, fast_length) : ema(srcmacd, fast_length)
slow_ma = sma_source ? sma(srcmacd, slow_length) : ema(srcmacd, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal
//plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )
//plot(macd, title="MACD", color=col_macd, transp=0)
//plot(signal, title="Signal", color=col_signal, transp=0)
//plot(zeroline, title="Zero Line", color=color.black, transp=0)
///////////////////////////LONG////////////////////////////////////////////////////////////////////
//ATR
lengthATR = input(title="ATR Length", defval=11, minval=1)
atr = atr(lengthATR)
//MULTI EMA
emasrc = close, 
len1 = input(8, minval=1, title="EMA 1")
len2 = input(15, minval=1, title="EMA 2")
len3 = input(50, minval=1, title="EMA 3")
ema1 = ema(emasrc, len1)
ema2 = ema(emasrc, len2)
ema3 = ema(emasrc, len3)
col1 = color.lime
col2 = color.blue
col3 = color.orange
//EMA Plots
plot(ema1, title="EMA 1", linewidth=1, color=col1)
plot(ema2, title="EMA 2", linewidth=1, color=col2)
plot(ema3, title="EMA 3", linewidth=1, color=col3)
//LONG
macdunderhis = macd < zeroline
macdcrossup = macd > signal
crossup = k[0] > d[0] and k[1] <= d[1]
emaup = ema1 > ema2 and ema2 > ema3 and close > ema1
barbuy = crossup and emaup and macdcrossup and macdunderhis
//SHORT
macdoverhis = macd > zeroline
macdcrosunder = macd < signal
crossdown = k[0] < d[0] and k[1] >= d[1]
emadown = ema1 < ema2 and ema2 < ema3 and close < ema1
barsell = crossdown and emadown and macdcrosunder and macdoverhis
//plotshape(crossup, style=shape.triangleup, location=location.belowbar, color=color.white)
plotshape(barbuy, style=shape.triangleup, location=location.belowbar, color=color.green)
plotshape(barsell, style=shape.triangledown, location=location.abovebar, color=color.red)
longloss = sma(open, 1)
//plot(longloss, color=color.red)
//Buy and Sell Factors
profitfactorl = input(title="Profitfactor Long", type=input.float, step=0.1, defval=1.5)
stopfactorl = input(title="ATR Stopfactor Long", type=input.float, step=0.1, defval=2)
profitfactors = input(title="Profitfactor Short", type=input.float, step=0.1, defval=1.5)
stopfactors = input(title="ATR Stopfactor Short", type=input.float, step=0.1, defval=2)
bought = strategy.position_size[1] < strategy.position_size
sold = strategy.position_size[1] > strategy.position_size
longcondition = barbuy
shortcondition = barsell
/////ENTRY/////
//LONG ENTRY
if (enablelong == true) and (longcondition) and (afterStartDate) and strategy.opentrades < 1
    strategy.entry("Long", strategy.long)
    
//SHORT ENTRY
if (enableshort == true) and (shortcondition) and (afterStartDate) and strategy.opentrades < 1
    strategy.entry("Short", strategy.short)
    
    
//PLOTT ATR TP SL//
//Long
plot_profit_level_long = strategy.position_avg_price + (atr*profitfactorl)
plot_stop_level_long = strategy.position_avg_price - (atr*stopfactorl)
plot(plot_profit_level_long, title="PL Long", linewidth=1, color=color.green)
plot(plot_stop_level_long, title="SL Long", linewidth=1, color=color.red)
//Short
plot_profit_level_short = strategy.position_avg_price - (atr*profitfactors)
plot_stop_level_short = strategy.position_avg_price + (atr*stopfactors)
plot(plot_profit_level_short, title="PL Long", linewidth=1, color=color.green)
plot(plot_stop_level_short, title="SL Long", linewidth=1, color=color.red)
////////////////ATR EXIT/////////////////
//LONG EXIT
if (afterStartDate) and (enableatrexit == true) and strategy.opentrades > 0
    barsbought = barssince(bought)
    profit_level = strategy.position_avg_price + (atr*profitfactorl)
    stop_level = strategy.position_avg_price - (atr*stopfactorl)
    strategy.exit("Take Profit/ Stop Loss", "Long", stop=stop_level[barsbought], limit=profit_level[barsbought])
//SHORT EXIT
if (afterStartDate) and (enableatrexit == true) and strategy.opentrades > 0
    barsbought = barssince(sold)
    profit_level = strategy.position_avg_price - (atr*profitfactors)
    stop_level = strategy.position_avg_price + (atr*stopfactors)
    strategy.exit("Take Profit/ Stop Loss", "Short", stop=stop_level[barsbought], limit=profit_level[barsbought])
//Lowest Low / Highest High Exit
//////LOWEST LOW//////
//Lowest Low LONG
loLen = input(title="Lowest Low Lookback", type=input.integer,
  defval=30, minval=2)
stop_level_long = lowest(low, loLen)
barsboughtlong = barssince(bought)
    
if (afterStartDate) and (enablelowexit == true) and strategy.opentrades > 0
    profit_level_long = strategy.position_avg_price + ((strategy.position_avg_price - stop_level_long[barsboughtlong])*profitfactorl)
    strategy.exit(id="TP/ SL", stop=stop_level_long[barsboughtlong], limit=profit_level_long)
//Lowest Low SHORT
highLen = input(title="highest high lookback", type=input.integer,
  defval=26, minval=2)
stop_level_short = highest(high, highLen)
barsboughtshort = barssince(sold)
if (afterStartDate) and (enablelowexit == true) and strategy.opentrades > 0
    profit_level_short = strategy.position_avg_price - ((stop_level_short[barsboughtshort] - strategy.position_avg_price)*profitfactors)
    strategy.exit(id="TP/ SL", stop=stop_level_short[barsboughtshort], limit=profit_level_short)
Expand (182 lines)

Overview

Positions are filtered by directional alignment across indicators and date ranges. Exits are managed via customizable ATR multiplier targets for stop loss and take profit, or optional lookback extreme prices. The strategy plots all three exponential moving averages directly on the main price chart.

Details

Published by

hasnocool on GitHub

Type

Strategy, Pine Script v4

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
FromMonthint1FromMonth
FromDayint1FromDay
FromYearint2020FromYear
ToMonthint1ToMonth
ToDayint1ToDay
ToYearint9999ToYear
Enable Long?booltrueenablelong
Enable Short?booltrueenableshort
Enable ATR Exit?booltrueenableatrexit
Enable Lowest Low/ Highest High Exit?boolfalseenablelowexit
smoothKint3smoothK
smoothDint3smoothD
Show all 31 settings
SettingTypeDefaultVariable
lengthRSIint14lengthRSI
lengthStochint14lengthStoch
RSI Sourcesourceclosesrc
Fast Lengthint12fast_length
Slow Lengthint24slow_length
MACD Sourcesourceclosesrcmacd
Signal Smoothingint8signal_length
Simple MA (Oscillator)booltruesma_source
Simple MA (Signal Line)booltruesma_signal
ATR Lengthint11lengthATR
EMA 1int8len1
EMA 2int15len2
EMA 3int50len3
Profitfactor Longfloat1.5profitfactorl
ATR Stopfactor Longfloat2stopfactorl
Profitfactor Shortfloat1.5profitfactors
ATR Stopfactor Shortfloat2stopfactors
Lowest Low Lookbackint30loLen
highest high lookbackint26highLen
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