Developer Guides
Write Your First Strategy in 5 Minutes
Starting from zero, write a dual moving-average crossover strategy in Pine Script and run a backtest on A-share data.
Step 1: Open the Script Editor
At the bottom of the main app window, click the "Script Editor" panel tab, or press Ctrl+E to open the editor.
Step 2: Write an Indicator Script
Start with a simple indicator to get familiar with the syntax:
My first indicator
//@version=5 indicator("My dual MA", overlay=true) // Compute the fast and slow moving averages fast = ta.sma(close, 5) slow = ta.sma(close, 20) // Draw them on the chart plot(fast, "Fast MA", color=color.blue, linewidth=2) plot(slow, "Slow MA", color=color.red, linewidth=2) // Mark bullish and bearish crosses if ta.crossover(fast, slow) label.new(bar_index, low, "Golden cross", color=color.green) if ta.crossunder(fast, slow) label.new(bar_index, high, "Death cross", color=color.red)
//@version=5 declares the Pine Script version. indicator() declares an indicator script, and overlay=true draws it on the main chart.Step 3: Convert It into a Strategy
Change indicator() to strategy() and add the entry/exit logic:
Dual MA crossover strategy
//@version=5 strategy("Dual MA strategy", overlay=true) fast = ta.sma(close, 5) slow = ta.sma(close, 20) plot(fast, "Fast MA", color=color.blue) plot(slow, "Slow MA", color=color.red) // Buy on the golden cross if ta.crossover(fast, slow) strategy.entry("Long", strategy.long) // Sell on the death cross if ta.crossunder(fast, slow) strategy.close("Long")
Step 4: Run a Backtest
Click the "Run" button above the editor. The script executes on the current chart's bar data and generates a backtest report automatically. You'll see entry/exit markers, an equity curve, and detailed performance metrics (win rate, Sharpe ratio, max drawdown, and more).
The TideView backtest engine matches TradingView Broker Emulator semantics — strategies copied from the community run as-is, and backtest results can be compared directly against TradingView.
Step 5: Add Take-Profit and Stop-Loss
Complete strategy with take-profit and stop-loss
//@version=5 strategy("MA strategy + TP/SL", overlay=true) fast = ta.sma(close, 5) slow = ta.sma(close, 20) if ta.crossover(fast, slow) strategy.entry("Long", strategy.long) // Take profit at 8%, stop loss at 3% strategy.exit("TP/SL", "Long", profit=close * 0.08, loss=close * 0.03) if ta.crossunder(fast, slow) strategy.close("Long")
Next Steps
Congratulations — you've written a runnable quant strategy. From here you can:
- Read the Strategy Development guide for more order types and position sizing
- Read the Backtest Semantics guide for the TradingView-aligned fill rules
- Browse the API Reference to look up every available function
- Use the editor's built-in "Templates" menu to pick from 80+ strategy templates and customize them