SuperCharting
Sign in Get started Account Open charts

Learn / Strategies

Write your first strategy: strategy() code and the Strategy Tester

Write your first strategy in Pine-compatible script: an EMA crossover with an ATR stop, target, commission and slippage, then read the Strategy Tester report.

A strategy is a script that declares strategy() instead of indicator() and uses strategy.entry, strategy.exit and strategy.close to place simulated orders. When you add one to a SuperCharting chart, the broker emulator fills those orders on the chart's history and the Strategy Tester opens under the chart with the results. This tutorial writes a complete EMA crossover strategy with a stop and a target, then shows where to read the report.

Pine Script is TradingView's language; SuperCharting runs Pine-compatible strategies in the same version 6 syntax. Backtest results describe the past only. Past results do not guarantee future results, and nothing here is a trading recommendation.

From indicator to strategy

If you built the crossover in write your first indicator, you already have the signal. A strategy adds three things:

The complete strategy

//@version=6
strategy("EMA Cross Strategy", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05, slippage=2)

fastLen = input.int(9, "Fast EMA length", minval=1)
slowLen = input.int(21, "Slow EMA length", minval=1)
atrLen  = input.int(14, "ATR length", minval=1)
stopMul = input.float(2.0, "Stop (x ATR)", minval=0.1, step=0.1)
tpMul   = input.float(3.0, "Target (x ATR)", minval=0.1, step=0.1)

fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)
atr  = ta.atr(atrLen)
plot(fast, "Fast EMA", color=color.teal)
plot(slow, "Slow EMA", color=color.orange)

if ta.crossover(fast, slow) and strategy.position_size == 0
    strategy.entry("Long", strategy.long)
    strategy.exit("Long exit", from_entry="Long", stop=close - stopMul * atr, limit=close + tpMul * atr)

if ta.crossunder(fast, slow)
    strategy.close("Long", comment="Cross down")

What each part does

The strategy() line

You can change all of these later on the Properties tab of the strategy's settings without editing the code. Keep commission and slippage realistic for your broker and market; a strategy that only works with zero costs does not work.

Entry, stop and target

The entry fires when the fast EMA crosses above the slow EMA and no position is open (strategy.position_size == 0). On the same bar, strategy.exit attaches a stop two ATRs below the close and a target three ATRs above. The ATR (average true range) scales both to how much the market is moving, so the same rule works on a quiet day and a busy one.

The second exit

If the averages cross back down before the stop or target is reached, strategy.close("Long") closes the trade at market. The comment "Cross down" appears as the label of the exit marker on the chart, so you can tell which exit closed each trade.

How to run a strategy in SuperCharting

  1. Open a chart, for example XAUUSD on 1 hour or NSE:NIFTY on 15 minutes, and press Ctrl + E.
  2. Paste the strategy above. Wait for the console to show "✓ EMA Cross Strategy (strategy)".
  3. Click Add to chart. Entry and exit markers appear on the bars, and the Strategy Tester opens under the chart on the strategy's first result.
  4. Read Key stats at the top: Total PnL, Max drawdown, Profitable trades and Profit factor.
  5. Switch to the table view for the List of trades. Click a trade to jump to it on the chart.
  6. Change an input in the strategy's settings (for example Stop to 1.5) and watch the report update.

If you close the tester, right-click the strategy's name in the chart legend and choose Strategy Tester to open it again. The report is explained section by section in the Strategy Tester guide.

Check the result before you trust it

A first backtest is a starting point, not an answer. Before you draw any conclusion:

Common mistakes in a first strategy

Writing and testing strategies is part of every SuperCharting account, and the site is currently free to use.

Frequently asked questions

Can a strategy go short?

Yes. Use strategy.entry("Short", strategy.short) with its own strategy.exit. A short entry while a long is open reverses the position.

Why do my trades fill one bar after the signal?

That is the default: an order sent at a bar's close fills at the next bar's open. It matches what you could do in real trading.

Can I get an alert when the strategy trades?

Yes. The Strategy Tester has an Add alert on strategy button. The alert fires on each order fill, and the message can include placeholders such as {{strategy.order.action}} and {{strategy.order.price}}.

Does a good backtest mean the strategy will make money?

No. It shows how the rules behaved on past data with the costs you set. Past results do not guarantee future results.

Try it on a live chart

SuperCharting runs in your browser: charts, Pine-compatible scripts, the Strategy Tester, alerts and the screener, with Indian and global markets.

Create a free account More guides