Skip to main content

Examples

Complete, runnable strategies. Copy one and adapt it. Each is a full script — paste it into the strategy editor, pick a universe and timeframe, and run.

Buy and hold​

The simplest possible strategy: buy on the first bar, hold forever.

//@version=1
strategy("Buy and Hold")

if position.size == 0:
strategy.enter_long(size_pct=100)

RSI mean reversion with a trend filter​

Buy oversold, but only in an uptrend; exit when overbought.

//@version=1
strategy("RSI Mean Reversion", timeframe="1D")

length = input.int(14, "RSI Length")
oversold = input.float(30.0, "Oversold")
overbought = input.float(70.0, "Overbought")

rsi = ta.rsi(close, length)
uptrend = close > ta.sma(close, 200)

if position.size == 0 and uptrend and rsi < oversold:
strategy.enter_long(size_pct=100)

if position.size > 0 and rsi > overbought:
strategy.close_all()

Golden cross (manual crossover)​

ta.crossover only works on bare OHLCV series, so to detect a crossover of two moving averages use the previous-difference pattern: remember last bar's difference in a var, and look for the sign flip.

//@version=1
strategy("Golden Cross", timeframe="1D")

fast = ta.sma(close, 50)
slow = ta.sma(close, 200)
diff = fast - slow

var prev_diff = na

cross_up = false
cross_down = false
if not (prev_diff == na):
if prev_diff <= 0 and diff > 0:
cross_up = true
if prev_diff >= 0 and diff < 0:
cross_down = true
prev_diff = diff

if cross_up and position.size == 0:
strategy.enter_long(size_pct=100)

if cross_down and position.size > 0:
strategy.close_all()

Day-of-week strategy​

Buy on Tuesday, sell on Friday. Remember: time.dayofweek is 0=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat.

//@version=1
strategy("Buy Tuesday, Sell Friday", timeframe="1D")

if position.size == 0 and time.dayofweek == 2: # Tuesday
strategy.enter_long(cash=10000, at_open=true)

if position.size > 0 and time.dayofweek == 5: # Friday
strategy.close_all(at_close=true)

Different rules per symbol​

Apply different logic to different tickers in one strategy by branching on syminfo.ticker. Run it over a multi-ticker universe (e.g. SPY, AAPL).

//@version=1
strategy("Per-Symbol Day Rules", timeframe="1D")

if syminfo.ticker == "SPY":
if position.size == 0 and time.dayofweek == 2: # SPY: buy Tuesday
strategy.enter_long(cash=10000, at_close=true)
if position.size > 0 and time.dayofweek == 3: # sell Wednesday
strategy.close_all(at_close=true)
elif syminfo.ticker == "AAPL":
if position.size == 0 and time.dayofweek == 3: # AAPL: buy Wednesday
strategy.enter_long(cash=10000, at_close=true)
if position.size > 0 and time.dayofweek == 5: # sell Friday
strategy.close_all(at_close=true)
warning

Every branch that should trade must call strategy.*. Tracking a position with your own true/false variable places no orders.

Fixed-size, non-compounding​

Trade a constant amount every time so profits don't snowball into bigger positions. Use cash= (fixed dollars) or qty= (fixed shares).

//@version=1
strategy("Fixed $10k per trade", timeframe="1D")

rsi = ta.rsi(close, 14)

if position.size == 0 and rsi < 30:
strategy.enter_long(cash=10000) # always $10,000, never compounds

if position.size > 0 and rsi > 70:
strategy.close_all()

Multi-timeframe trend filter​

Trade daily, but only when the weekly trend agrees. htf.close("1W") is look-ahead safe — it only sees completed weekly bars.

//@version=1
strategy("Daily entries, weekly filter", timeframe="1D")

weekly_uptrend = htf.close("1W") > ta.sma(htf.close("1W"), 30)
rsi = ta.rsi(close, 14)

if position.size == 0 and weekly_uptrend and rsi < 30:
strategy.enter_long(size_pct=100)

if position.size > 0 and rsi > 70:
strategy.close_all()

Trailing stop (persistent state)​

Track the highest price since entry in a var, and exit on a percentage pullback.

//@version=1
strategy("Trailing Stop", timeframe="1D")

trail_pct = input.float(5.0, "Trail %")
rsi = ta.rsi(close, 14)

var stop_high = na

if position.size == 0 and rsi < 30:
strategy.enter_long(size_pct=100)
stop_high = na

if position.size > 0:
stop_high = math.max(stop_high, high) if not (stop_high == na) else high
if close < stop_high * (1 - trail_pct / 100):
strategy.close_all()
stop_high = na