Documentation

Documentation

3. Writing Strategies

Created
Jun 16, 2026
Updated
Jun 16, 2026

A Pine Script strategy uses strategy() instead of indicator() and places trades with strategy.entry, strategy.exit, and strategy.close. Fractal runs it in the Strategy Tester with a full broker simulation.

A basic strategy

//@version=6
strategy("MA Cross", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

fast = ta.sma(close, 10)
slow = ta.sma(close, 30)

if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)

if ta.crossunder(fast, slow)
    strategy.close("Long")

Open the Strategy Tester, go to Compose, choose Pine Script, paste your code, and click Run.

Broker configuration

The strategy() declaration sets broker defaults. You can override most of them from the Settings panel in the Strategy Tester.

ParameterWhat it controls
initial_capitalStarting equity
default_qty_typestrategy.fixed (contracts), strategy.cash (currency), or strategy.percent_of_equity
default_qty_valueQuantity for that type
commission_typestrategy.commission.percent, strategy.commission.cash_per_contract, or strategy.commission.cash_per_order
commission_valueCommission amount
slippageSlippage in ticks per fill
pyramidingMaximum number of same-direction entries
process_orders_on_closeFill orders at bar close instead of next bar's open

Orders

strategy.entry("Long", strategy.long)                        // market entry
strategy.entry("Long", strategy.long, limit=support_level)  // limit entry
strategy.entry("Long", strategy.long, stop=breakout_price)  // stop entry

strategy.close("Long")          // close by id
strategy.close_all()            // close all positions

strategy.order("Scale", strategy.long, qty=0.5)  // direct order

Exit brackets

strategy.exit("Exit", "Long",
    profit   = 200,    // take-profit in ticks
    loss     = 100,    // stop-loss in ticks
    trail_points = 50  // trailing stop in ticks
)

limit= and stop= are also accepted as price values instead of tick offsets.

Cancel pending orders

strategy.cancel("Long")    // cancel a specific pending order
strategy.cancel_all()      // cancel all pending orders

Reading strategy state

Inside your script you can read the live broker state:

VariableWhat it returns
strategy.position_sizeCurrent position size (positive = long, negative = short)
strategy.position_avg_priceAverage entry price of the current position
strategy.equityCurrent equity
strategy.netprofitNet profit to date
strategy.opentradesNumber of open trades
strategy.closedtradesNumber of closed trades

Per-trade data is available via strategy.closedtrades.profit(i), .entry_price(i), .exit_price(i), and similar accessors.

Results

After a run completes the Strategy Tester shows:

  • Performance summary — net profit, win rate, profit factor, max drawdown, Sharpe ratio, and more.
  • Equity curve — cumulative equity over time.
  • Trade list — every entry and exit with timestamps, prices, and P&L.

Results are identical to what you'd get from the same script on TradingView for the same data. See Reading Backtest Results.

Strategy templates

The Compose tab includes ready-to-run Pine strategy templates:

  • Long/Short — simple directional MA cross.
  • Stop & Limit — entry with take-profit and stop-loss brackets.
  • Pyramiding — scaling into a position.
  • Process orders on close — same-bar fills.
  • Exit bracketsstrategy.exit with profit/loss/trail.
  • Config showcase — shows every broker parameter.
  • HTF strategyrequest.security used inside a strategy.

Next steps

Next: Language Reference