Back to TradingView PineScript for CFD Traders
Part 3

From study to strategy: declaring a strategy

A strategy is just an indicator that can also place orders. The declaration and the constraints you need to know.

Intermediate8 min readBeginner → AdvancedLesson 10 / 17
Try it in TradingView
Indicator → strategy in one word

Same plots as before, but now open the Strategy Tester tab — you have trades. Change default_qty_value and watch the equity curve change.

//@version=5
strategy("Lesson 10 - My first strategy", overlay=true,
     initial_capital=10000,
     default_qty_type=strategy.percent_of_equity,
     default_qty_value=10)

fast = ta.ema(close, 9)
slow = ta.ema(close, 21)

plot(fast, "Fast EMA", color=color.blue, linewidth=2)
plot(slow, "Slow EMA", color=color.orange, linewidth=2)

// Entry on the cross. strategy.entry reverses if already in the other direction.
if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
    strategy.entry("Short", strategy.short)
To run it
  1. 1. Open any chart in TradingView and click Pine Editor at the bottom.
  2. 2. Select everything in the editor and paste this over it, then click Save.
  3. 3. Click Add to chart. Open the Strategy Tester tab if it is a strategy.

Why a strategy is different from an indicator

An indicator is a passive computation — it plots lines and shapes on the chart. A strategy is an indicator that can additionally simulate orders. The strategy tester tab in TradingView is the result: it runs your logic against historical bars and reports profit, drawdown, and trade-by-trade detail.

Declaring a strategy

//@version=5
strategy("My first strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

default_qty_type and default_qty_value control how each entry sizes itself if you do not specify a quantity at order time.

Indicator → strategy migration

Take the SMA cross indicator from Part 1. The only change is the first line — indicator becomes strategy:

//@version=5
strategy("SMA cross", overlay=true)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
plot(fast, color=color.blue)
plot(slow, color=color.orange)

This still plots the lines, but now you can add strategy.entry calls and the strategy tester will start counting trades.

Strategy constraints

A strategy cannot call indicator()-only functions, and it cannot use certain study-only inputs. Most indicator code can be pasted into a strategy with minimal changes.

Backtest tab

Click Strategy Tester at the bottom of the chart. You will see the Overview (net profit, drawdown, win rate), the Performance Summary, and the List of Trades. These three views are how you evaluate the strategy.

What comes next

The next lessons cover the actual entry and exit calls, then how to interpret the numbers the tester reports.

What you just did

Lesson 10 of 17 in TradingView PineScript for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.