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.
| Parameter | What it controls |
|---|---|
initial_capital | Starting equity |
default_qty_type | strategy.fixed (contracts), strategy.cash (currency), or strategy.percent_of_equity |
default_qty_value | Quantity for that type |
commission_type | strategy.commission.percent, strategy.commission.cash_per_contract, or strategy.commission.cash_per_order |
commission_value | Commission amount |
slippage | Slippage in ticks per fill |
pyramiding | Maximum number of same-direction entries |
process_orders_on_close | Fill 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:
| Variable | What it returns |
|---|---|
strategy.position_size | Current position size (positive = long, negative = short) |
strategy.position_avg_price | Average entry price of the current position |
strategy.equity | Current equity |
strategy.netprofit | Net profit to date |
strategy.opentrades | Number of open trades |
strategy.closedtrades | Number 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 brackets —
strategy.exitwith profit/loss/trail. - Config showcase — shows every broker parameter.
- HTF strategy —
request.securityused inside a strategy.
Next steps
Next: Language Reference