RoboQuant
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:

ParameterPurpose
overlay=trueDraw on price chart (most strategies)
default_qty_typestrategy.fixed, strategy.percent_of_equity, etc.
default_qty_valueDefault size per order
initial_capitalBacktest starting equity
calc_on_every_tickfalse recommended for bar-close logic
max_bars_backIncrease if script references deep history

Order API

FunctionUse 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_close logic → 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

  1. Save as my-strategy.pine in your workspace
  2. Copy into TradingView Pine Editor (or use the TradingView extension)
  3. Add to chart → open Strategy Tester for backtest
  4. Create alert → connect to a RoboQuant webhook (see TradingView deployment)

Editing AI-generated strategies

When reviewing AI output, check in this order:

  1. Declaration — qty type, capital, overlay
  2. Session filters — timezone, day-of-week, session end flatten
  3. Entry conditions — repainting (calc_on_every_tick, lookahead)
  4. Risk — SL/TP distance, max trades per day
  5. Visual noise — disable debug labels before live (show_*_labels = false)