0% found this document useful (0 votes)
29 views4 pages

EMA Crossover Strategy in Pine Script

The document provides two Pine Script strategies for trading: the first is an EMA Crossover with a 200 SMA filter for swing trading, defining conditions for long and short entries and exits. The second strategy is a customizable volatility breakout strategy that uses ATR for breakout levels and RSI for momentum confirmation, including user-defined trading session times and dynamic stop-loss/take-profit levels. Both strategies are designed for use on TradingView with specific settings for NVDA and a 5-minute chart.

Uploaded by

Jailson Dantas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

EMA Crossover Strategy in Pine Script

The document provides two Pine Script strategies for trading: the first is an EMA Crossover with a 200 SMA filter for swing trading, defining conditions for long and short entries and exits. The second strategy is a customizable volatility breakout strategy that uses ATR for breakout levels and RSI for momentum confirmation, including user-defined trading session times and dynamic stop-loss/take-profit levels. Both strategies are designed for use on TradingView with specific settings for NVDA and a 5-minute chart.

Uploaded by

Jailson Dantas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link]

usp=sharin
g

Here's the long/short version of the EMA Crossover + Trend Regime system for
pinescript

//@version=5
strategy("HTBS - Swing Trading: EMA Cross with 200 SMA Filter (Long and Short)",
overlay=true)

// Define inputs for the moving averages


fastLength = [Link](9, title="Fast EMA Length", minval=1)
slowLength = [Link](21, title="Slow EMA Length", minval=1)
smaLength = [Link](200, title="SMA Length", minval=1)

// Calculate the EMAs and SMA


fastEMA = [Link](close, fastLength)
slowEMA = [Link](close, slowLength)
sma200 = [Link](close, smaLength)

// Plot the moving averages on the chart


plot(fastEMA, color=[Link], linewidth=2, title="Fast EMA")
plot(slowEMA, color=[Link], linewidth=2, title="Slow EMA")
plot(sma200, color=[Link], linewidth=2, title="200 SMA")

// Define long entry and exit conditions


longCondition = [Link](fastEMA, slowEMA) and close > sma200
exitLongCondition = [Link](fastEMA, slowEMA)

// Define short entry and exit conditions


shortCondition = [Link](fastEMA, slowEMA) and close < sma200
exitShortCondition = [Link](fastEMA, slowEMA)

// Execute long strategy


if (longCondition)
[Link]("Long", [Link])

if (exitLongCondition)
[Link]("Long")
// Execute short strategy
if (shortCondition)
[Link]("Short", [Link])

if (exitShortCondition)
[Link]("Short")

========================================================================

Here's the code for the tradingview strategy we looked at here

Settings:

NVDA, 5 minute, Regular Trading Hours


100% of equity
--------------------

//@version=5
strategy("Customizable Volatility Breakout Strategy", overlay=true)

// User Inputs
atrMultiplier = [Link](2.0, title="ATR Multiplier", minval=0.1,
step=0.1) // ATR multiplier for breakout levels
startHour = [Link](9, title="Start Hour", minval=0, maxval=23) //
Start hour for trading session
startMinute = [Link](30, title="Start Minute", minval=0,
maxval=59) // Start minute for trading session
endHour = [Link](16, title="End Hour", minval=0, maxval=23) // End
hour for trading session
endMinute = [Link](0, title="End Minute", minval=0, maxval=59) //
End minute for trading session
exitAtEndOfDay = [Link](true, title="Exit at End of Day?",
tooltip="Force exit at the end of the trading day")
rsiOverboughtInput = [Link](80, title="RSI Overbought Level",
minval=50, maxval=100) // RSI overbought threshold
rsiOversoldInput = [Link](20, title="RSI Oversold Level",
minval=0, maxval=50) // RSI oversold threshold

// Define the trading session based on user inputs


isInTradingSession = (hour > startHour or (hour == startHour and
minute >= startMinute)) and (hour < endHour or (hour == endHour and
minute < endMinute))
// ATR for volatility-based breakout levels
atrLength = 14
atrValue = [Link](atrLength)

// Use previous day's high/low and add a configurable ATR buffer for
breakouts
yesterdayHigh = [Link](high[1], 1)
yesterdayLow = [Link](low[1], 1)
upperBreakout = yesterdayHigh + atrValue * atrMultiplier // ATR
multiplier for breakouts
lowerBreakout = yesterdayLow - atrValue * atrMultiplier // ATR
multiplier for breakouts

// Plot breakout levels


plot(upperBreakout, color=[Link], linewidth=2, title="Upper
Breakout Level")
plot(lowerBreakout, color=[Link], linewidth=2, title="Lower
Breakout Level")

// Use RSI for momentum confirmation with customizable overbought and


oversold levels
rsi = [Link](close, 14)
rsiOverbought = rsiOverboughtInput
rsiOversold = rsiOversoldInput

// Entry logic for long and short


enterLong = [Link](close, upperBreakout) and (rsi <
rsiOverbought)
enterShort = [Link](close, lowerBreakout) and (rsi >
rsiOversold)

// Time filter for trade sessions based on user-defined start and end
time
tradeSession = isInTradingSession

// Minimum time between trades (30 bars delay)


var float lastTradeTime = na
tradeDelay = 30
canTrade = na(lastTradeTime) or (bar_index - lastTradeTime >
tradeDelay)

if (enterLong and tradeSession and canTrade)


[Link]("Long", [Link])
lastTradeTime := bar_index
if (enterShort and tradeSession and canTrade)
[Link]("Short", [Link])
lastTradeTime := bar_index

// Dynamic stop-loss and take-profit based on ATR


atrStopLoss = atrValue * 2.5 // 2.5x ATR for stop-loss
atrTakeProfit = atrValue * 3.0 // 3.0x ATR for take-profit

[Link]("Exit Long", "Long", stop=close - atrStopLoss,


limit=close + atrTakeProfit)
[Link]("Exit Short", "Short", stop=close + atrStopLoss,
limit=close - atrTakeProfit)

// Force close positions 10 minutes before market close if the


exitAtEndOfDay option is enabled
if (exitAtEndOfDay and hour == 15 and minute >= 50)
[Link]("Long")
[Link]("Short")

// Plot RSI for visibility


plot(rsi, title="RSI", color=[Link])
hline(rsiOverbought, "RSI Overbought", color=[Link])
hline(rsiOversold, "RSI Oversold", color=[Link])
========================================================================

You might also like