Skip to main content

Your First Strategies

Complete walkthroughs: RSI strategy, breakout strategy, and pinbar strategy: each with entry logic, exits, and risk management.

RSI Mean Reversion Strategy

The RSI (Relative Strength Index) strategy is the classic starting point for strategy development. The logic is simple: when RSI drops below 30, the asset is oversold and likely to bounce. When RSI rises above 70, it is overbought and likely to pull back. On BTC daily charts, RSI below 30 has historically marked significant buying opportunities during bear market capitulations. On shorter timeframes like the 4H, it generates more frequent but noisier signals.

pine-RSI mean reversion: complete strategy
1//@version=5
2strategy("RSI Mean Reversion", overlay=true,
3 initial_capital=10000,
4 default_qty_type=strategy.percent_of_equity,
5 default_qty_value=95,
6 commission_type=strategy.commission.percent,
7 commission_value=0.075,
8 slippage=2)
9
10// === INPUTS ===
11rsiLen = input.int(14, "RSI Length", minval=2)
12oversold = input.int(30, "Oversold Level", minval=5, maxval=50)
13overbought = input.int(70, "Overbought Level", minval=50, maxval=95)
14useEmaFilt = input.bool(true, "Use EMA Trend Filter")
15emaLen = input.int(200, "EMA Filter Length", minval=10)
16
17// === CALCULATIONS ===
18rsi = ta.rsi(close, rsiLen)
19ema200 = ta.ema(close, emaLen)
20
21// === ENTRY CONDITIONS ===
22// Buy when RSI crosses below oversold AND price is above 200 EMA (uptrend filter)
23longCondition = ta.crossunder(rsi, oversold) and (useEmaFilt ? close > ema200 : true)
24
25// Sell when RSI crosses above overbought
26exitCondition = ta.crossover(rsi, overbought)
27
28// === EXECUTION ===
29if longCondition
30 strategy.entry("RSI Long", strategy.long)
31
32if exitCondition
33 strategy.close("RSI Long", comment="RSI overbought")
34
35// === RISK MANAGEMENT ===
36// 5% stop loss as safety net
37strategy.exit("SL", "RSI Long",
38 loss = close * 0.05 / syminfo.mintick)
39
40// === VISUALS ===
41plot(ema200, "EMA 200", color.yellow, 2)
42bgcolor(longCondition ? color.new(color.green, 90) : na)
43bgcolor(exitCondition ? color.new(color.red, 90) : na)
1

Calculate the RSI

ta.rsi(close, 14) computes the 14-period RSI. This measures the ratio of average gains to average losses over the lookback period, producing a value between 0 and 100. Lower values mean more selling pressure relative to buying.
2

Add the trend filter

The 200 EMA filter ensures we only buy in uptrends. Buying oversold RSI in a downtrend is catching a falling knife. On BTC, the 200 EMA on the daily chart is the single most watched trend indicator by institutional traders.
3

Define entry and exit

ta.crossunder(rsi, 30) fires once when RSI moves from above 30 to below 30. The exit fires when RSI crosses above 70. This gives clear, non-ambiguous signals.
4

Add a stop loss

The 5% stop loss via strategy.exit() protects against the times when oversold becomes more oversold. Without this, a single crash could wipe out months of gains.

N-Bar Breakout Strategy

Breakout strategies capture moves when price pushes through a significant level. The N-bar high breakout buys when price exceeds the highest high of the last N bars. This works well on trending assets like BTC and SOL during strong momentum phases. Adding a volume filter confirms that the breakout has institutional participation rather than being a low-volume wick.

pine-N-bar breakout with volume confirmation
1//@version=5
2strategy("N-Bar Breakout", overlay=true,
3 initial_capital=10000,
4 default_qty_type=strategy.percent_of_equity,
5 default_qty_value=95,
6 commission_value=0.075, slippage=2)
7
8// === INPUTS ===
9lookback = input.int(20, "Breakout Lookback", minval=5)
10volMult = input.float(1.5, "Volume Multiplier", minval=1.0, step=0.1)
11atrMult = input.float(2.0, "ATR Stop Multiplier", minval=0.5, step=0.5)
12atrLen = input.int(14, "ATR Length")
13
14// === CALCULATIONS ===
15highestHigh = ta.highest(high, lookback)
16lowestLow = ta.lowest(low, lookback)
17avgVolume = ta.sma(volume, lookback)
18atr = ta.atr(atrLen)
19
20// === CONDITIONS ===
21breakoutUp = close > highestHigh[1] and volume > avgVolume * volMult
22breakoutDown = close < lowestLow[1] and volume > avgVolume * volMult
23
24// === EXECUTION ===
25if breakoutUp
26 strategy.entry("Breakout Long", strategy.long)
27 strategy.exit("ATR Stop", "Breakout Long",
28 stop = close - atr * atrMult)
29
30if breakoutDown
31 strategy.close("Breakout Long", comment="Breakdown exit")
32
33// === VISUALS ===
34plot(highestHigh[1], "Resistance", color.new(color.green, 40), 1, plot.style_stepline)
35plot(lowestLow[1], "Support", color.new(color.red, 40), 1, plot.style_stepline)

Volume confirmation matters

A breakout without volume is a fake-out. On crypto exchanges, volume spikes of 1.5–2x the 20-bar average typically indicate genuine interest. On low-cap altcoins, use a higher multiplier (2–3x) because volume is noisier. The volume filter alone can eliminate 30–40% of losing trades in a breakout system.

Pin Bar Reversal Strategy

Pin bars (pinocchio bars) are single-candle reversal patterns characterized by a long wick and a small body. A bullish pin bar has a long lower wick (buyers rejected lower prices) and closes near the high. A bearish pin bar has a long upper wick (sellers rejected higher prices) and closes near the low. On ETH and BTC, pin bars at key support/resistance levels are among the highest-probability single-candle setups.

pine-Pin bar reversal with ATR stops
1//@version=5
2strategy("Pin Bar Reversal", overlay=true,
3 initial_capital=10000,
4 default_qty_type=strategy.percent_of_equity,
5 default_qty_value=95,
6 commission_value=0.075, slippage=2)
7
8// === INPUTS ===
9wickRatio = input.float(2.5, "Wick-to-Body Ratio", minval=1.5, step=0.5)
10atrMult = input.float(1.5, "ATR Stop Multiplier", step=0.5)
11tpMult = input.float(2.0, "ATR Take-Profit Multiplier", step=0.5)
12useEmaTrend = input.bool(true, "Require Trend Alignment")
13emaLen = input.int(50, "EMA Length")
14
15// === CALCULATIONS ===
16body = math.abs(close - open)
17upperWick = high - math.max(close, open)
18lowerWick = math.min(close, open) - low
19atr = ta.atr(14)
20ema = ta.ema(close, emaLen)
21
22// === PIN BAR DETECTION ===
23bullPin = lowerWick > body * wickRatio and
24 upperWick < body and
25 body > 0 and
26 (useEmaTrend ? close > ema : true)
27
28bearPin = upperWick > body * wickRatio and
29 lowerWick < body and
30 body > 0 and
31 (useEmaTrend ? close < ema : true)
32
33// === EXECUTION ===
34if bullPin
35 stopPrice = low - atr * atrMult
36 tpPrice = close + atr * tpMult
37 strategy.entry("Pin Long", strategy.long)
38 strategy.exit("Pin TP/SL", "Pin Long",
39 limit = tpPrice,
40 stop = stopPrice)
41
42if bearPin
43 strategy.close("Pin Long", comment="Bear pin exit")
44
45// === VISUALS ===
46plotshape(bullPin, "Bull Pin", shape.triangleup,
47 location.belowbar, color.green, size=size.small)
48plotshape(bearPin, "Bear Pin", shape.triangledown,
49 location.abovebar, color.red, size=size.small)
50plot(ema, "EMA", color.yellow, 1)
1

Calculate body and wicks

The body is math.abs(close - open). The lower wick is math.min(close, open) - low. For a valid bullish pin bar, the lower wick must be at least 2.5x the body size. This ensures genuine rejection.
2

Filter with trend

Bullish pin bars are more reliable when price is above the 50 EMA (uptrend). Bearish pins work better below. Trading pin bars against the trend leads to a lower win rate.
3

Set ATR-based risk

Stop loss is placed below the pin bar's low minus 1.5x ATR as a buffer. Take profit is set at 2x ATR from entry. This gives a 1.33:1 reward-to-risk ratio, which needs roughly a 43% win rate to be profitable after commissions.

Put this lesson to work

Free on Thrive, no account needed.