Legacy · TradingView
Pine Script Strategies (Legacy)
Legacy path. For the native compiled engine, see RoboQuant strategies.
RoboQuant strategies can be Pine Script v5 scripts declared with strategy() for TradingView backtests and webhook live trading. They run on TradingView for backtesting and live signal generation.
Minimal strategy skeleton
//@version=5
strategy("My Strategy", overlay=true,
default_qty_type=strategy.fixed,
default_qty_value=1,
initial_capital=50000,
currency=currency.USD)
// Inputs
length = input.int(14, "RSI Length", minval=1)
// Logic
rsi = ta.rsi(close, length)
longCondition = ta.crossover(rsi, 30)
shortCondition = ta.crossunder(rsi, 70)
if longCondition
strategy.entry("Long", strategy.long)
if shortCondition
strategy.entry("Short", strategy.short)
Strategy declaration options
Common strategy() parameters used in RoboQuant templates:
| Parameter | Purpose |
|---|---|
overlay=true | Draw on price chart (most strategies) |
default_qty_type | strategy.fixed, strategy.percent_of_equity, etc. |
default_qty_value | Default size per order |
initial_capital | Backtest starting equity |
calc_on_every_tick | false recommended for bar-close logic |
max_bars_back | Increase if script references deep history |
Order API
| Function | Use when |
|---|---|
strategy.entry(id, direction, qty=...) | Open or add to position |
strategy.exit(id, from_entry, profit=..., loss=...) | Attach SL/TP to an entry |
strategy.close(id) | Flatten a specific entry |
strategy.close_all() | Flatten everything (e.g. session end) |
Example: entry with bracket exit
if breakoutUp
strategy.entry("Long", strategy.long, qty=contracts)
strategy.exit("Long Exit", "Long",
profit=tpPoints * syminfo.mintick,
loss=slPoints * syminfo.mintick)
Inputs and organization
RoboQuant templates group inputs for clarity:
contracts = input.int(1, "Contracts", minval=1, group="Risk Management")
session_tz = input.string("America/New_York", "Timezone", group="Session Management")
trade_monday = input.bool(true, "Trade Monday", group="Day of Week Filters")
Use group= so users (and you) can tune risk, session, and visuals separately.
Session and time logic
Most futures strategies anchor to NYSE or London sessions:
inSession = not na(time(timeframe.period, "0930-1600:23456", "America/New_York"))
isNewDay = ta.change(time("1D"))
Common pitfalls:
- Chart timezone ≠ session timezone → entries fire at wrong times
- Using
hour()without explicit timezone when session spans DST - Forgetting
force_session_closelogic → overnight positions
State with var
Persist values across bars:
var float sessionHigh = na
var float sessionLow = na
if isNewDay
sessionHigh := na
sessionLow := na
if inSession
sessionHigh := na(sessionHigh) ? high : math.max(sessionHigh, high)
sessionLow := na(sessionLow) ? low : math.min(sessionLow, low)
Metadata convention (RoboQuant templates)
Official templates and knowledge-base scripts include a metadata header:
// === METADATA ===
// @category: strategy
// @title: Opening Range Breakout
// @timeframes: 5m,15m
// @components: SessionRangeTracker, TradeBoxRenderer
This helps the AI retrieve patterns. Include it when authoring reusable strategies.
From workspace to TradingView
- Save as
my-strategy.pinein your workspace - Copy into TradingView Pine Editor (or use the TradingView extension)
- Add to chart → open Strategy Tester for backtest
- Create alert → connect to a RoboQuant webhook (see TradingView deployment)
Editing AI-generated strategies
When reviewing AI output, check in this order:
- Declaration — qty type, capital, overlay
- Session filters — timezone, day-of-week, session end flatten
- Entry conditions — repainting (
calc_on_every_tick, lookahead) - Risk — SL/TP distance, max trades per day
- Visual noise — disable debug labels before live (
show_*_labels = false)
Related
- Pine indicators — when you need visuals without position management
- TradingView deployment — live alerts and broker routing
- Webhook reference — alert payload fields and variables