Strategy · Pine Script v5 · On the chart

Relative Strength Strategy (4h)

by creatidy

A long-only strategy designed for the 4-hour chart.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code64
Settings4inputs
LicenceGPL-3.0
Last update3 years ago10 Aug 2023

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

64 lines of Pine Script v5 from Strategies/Relative Strength Strategy (4h).pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

Relative Strength Strategy (4h).pine
//@version=5
strategy("Relative Strength Strategy (4h)", overlay=true,
     initial_capital=1000000, 
     commission_type=strategy.commission.percent, 
     commission_value=0.075,
     currency = "USD",
     default_qty_type = strategy.percent_of_equity,
     default_qty_value = 100)

// Variables
color col = na

sma_mid_term_length = input.int(35, minval=1, title="Mid-term SMA (periods)")

FromYear  = input.int(defval = 2017, title = "From Year", minval = 2000)
FromMonth = input.int(defval = 1, title = "From Month", minval = 1, maxval = 12)
FromDay   = 1  //input.int(defval = 1, title = "From Day", minval = 1, maxval = 31)
Months    = input.int(defval = 12, title = "Months", minval = 1)
ToYear    = FromYear + int(Months / 12)//input.int(defval = 9999, title = "To Year", minval = 2017)
ToMonth   = (Months + FromMonth) % 12  //input.int(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay     = 1  //input.int(defval = 1, title = "To Day", minval = 1, maxval = 31)
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  // backtest start window
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)        // backtest finish window
// yesterday = timenow - 16 * 60 * 60 * 1000
window() => 
    time >= start and time <= finish ? true : false // create function "within window of time"

avg_price = (low + high + open + close) / 4
sma_mid_term = ta.sma(avg_price, sma_mid_term_length)

relative_strength = (avg_price - sma_mid_term) / sma_mid_term
rs = sma_mid_term + sma_mid_term * ta.ema(relative_strength, sma_mid_term_length)
rs_delta = (rs / rs[1] - 1)
dir = ta.change(rs_delta >= 0 ? 1 : 0)

if not na(rs) and window()
    strategy.cancel_all()
    if dir > 0
        if strategy.position_size == 0
            strategy.order(id="L", direction=strategy.long, limit=avg_price*0.993)
            strategy.exit(id="L1", from_entry="L", stop=avg_price*0.95)
    else if dir[1] > 0 and dir == 0
        if strategy.position_size == 0
            strategy.order(id="L", direction=strategy.long, limit=avg_price*1.03)
            strategy.exit(id="L2", from_entry="L", stop=avg_price*0.95)
    else if dir < 0
        if strategy.position_size > 0 
            strategy.exit(id="L3", from_entry="L", limit=strategy.position_avg_price)
    else if dir[1] < 0 and dir == 0
        if strategy.position_size > 0 
            strategy.exit(id="L4", from_entry="L", stop=strategy.position_avg_price*0.95)
    else if dir == 0
        if strategy.position_size > 0 
            strategy.exit(id="L5", from_entry="L", stop=strategy.position_avg_price*0.95)

if time > finish
    strategy.close_all()
    
col := color.gray
col := rs_delta > 0 ? color.green : col
col := rs_delta < 0 ? color.red : col
plot(rs, color=col, style=plot.style_linebr)
plot(avg_price, style=plot.style_linebr)
plot(sma_mid_term, color=#673AB7)

Overview

Relative Strength Strategy (4h) averages open, high, low and close, smooths that with a mid-term SMA and builds a relative strength line from the gap between price and the SMA. When the slope of that line turns positive it places a limit order slightly below price, with a stop below. Other direction states manage exits using limit or stop orders around the average entry. A date window limits trading, positions close when it ends, and the lines draw on the chart.

Details

Published by

creatidy on GitHub

Draws

On the price chart

Markets

Crypto

Timeframes

H4 and above

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.

Settings

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

SettingTypeDefaultVariable
Mid-term SMA (periods)int35sma_mid_term_length
From Yearint2017FromYear
From Monthint1FromMonth
Monthsint12Months
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

330 stars

2 EMA/SMA + RSI Strategy [Alorse] combines three configurable moving averages with a short-period Relative Strength Index filter. It plots fast, slow, and trend moving averages on the price chart. Long entries trigger when the fast moving average is above the

Strategy v4 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of 2 EMA/SMA + RSI Strategy [Alorse]

Acrypto - Weighted Strategy

by AlbertoCuadra
318 stars

This strategy plots on the price chart and opens long or short positions when a weighted score from MACD, Stochastic RSI, RSI, Supertrend and a moving-average crossover exceeds a user-set trigger. Each component has its own weight and candle delay. Entries can

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · Custom licence GitHub repository of Acrypto - Weighted Strategy
285 stars

Mean Reversion Strategy is an overlay strategy that identifies pullbacks using a long-term EMA, RSI, and candlestick patterns. It generates long signals when a bullish engulfing candle forms below the EMA while RSI is oversold, and short signals when a bearish

Strategy v4 Mean reversionCandlestick patterns
Updated 3 years ago · None stated GitHub repository of Mean Reversion Strategy
228 stars

This strategy generates trading signals based on a selectable momentum engine, supporting MACD, RSI, Stochastic, or Rate of Change. Entries trigger on signal crosses, zero-line crosses, or threshold crosses, with an optional moving average trend filter. Risk m

Strategy v6 Oscillators & momentumTrend
Updated 3 months ago · None stated GitHub repository of Momentum Strategy Template

255 EMA Strategy

by hasnocool
106 stars

255 EMA Strategy combines an exponential moving average with Average True Range bands and standard MACD crossovers to generate entry signals. Drawn directly on the price chart, it plots the central 255 EMA along with upper and lower volatility envelopes. Trade

Strategy v4 Moving averagesOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 255 EMA Strategy

More Trend scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers