Skip to main content
On this page

How to Create a Strategy in TradingView

· 4 min read
Pineify Team
Pine Script and AI trading workflow research team

A TradingView strategy is a Pine script that specifies simulated orders and reports their historical and realtime test results. Start with a rule that can be stated precisely, then inspect the resulting trades before changing parameters.

How to Create a Strategy in TradingView

Define the Rule and Open the Editor​

For this example, enter long when a 9-bar simple moving average crosses above a 21-bar average; close the position on the reverse cross. The lengths are teaching defaults, not a documented profitable setup.

Open Pine Editor using TradingView's current toolbar control, create a strategy and replace the template with the complete code below. The editor guide covers the available layout controls.

//@version=6
strategy("First SMA Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.1, slippage=1)
fastLength = input.int(9, "Fast MA length", minval=1)
slowLength = input.int(21, "Slow MA length", minval=1)
fastMA = ta.sma(close, fastLength)
slowMA = ta.sma(close, slowLength)
enterLong = ta.crossover(fastMA, slowMA)
exitLong = ta.crossunder(fastMA, slowMA)
if enterLong and strategy.position_size == 0
strategy.entry("Long", strategy.long)
if exitLong
strategy.close("Long")
plot(fastMA, "Fast SMA", color.blue)
plot(slowMA, "Slow SMA", color.orange)

Save and select Add to chart. The script plots both averages and produces a strategy report. It is long-only, has no protective stop and does not place real broker orders. The 10% position allocation is not a 10% loss limit; the commission and slippage are illustrative defaults.

Inspect One Trade Before the Summary​

Use standard candles and record the symbol, interval, data range and settings. Find a crossover and compare its timestamp with the simulated entry. Under default calculation and fill settings, a market order generated after bar close normally fills on the next available tick, often the following bar's open.

Review the trade list, drawdown, net return and the assumptions behind each result. A particular profit factor or win rate is not a universal acceptance threshold. Few trades, concentrated gains or large sensitivity to costs can make an attractive summary misleading. TradingView's strategy documentation explains the report and broker emulator.

Change Inputs Without Fitting the Past​

Compare several plausible settings while keeping the dataset and costs fixed. Record every tested variation, including failures. Choose parameters on one period and evaluate them on a later untouched period. A single split is a holdout test; walk-forward evaluation repeats the train-then-test process over successive windows.

Neither method prevents all overfitting. Repeatedly checking the same holdout and then adjusting rules makes it part of the selection process. Consider different market conditions and realistic position, commission, slippage and margin assumptions.

Add Risk Rules Deliberately​

If you add stop or target orders, specify whether their distances are prices, ticks or account-risk calculations. A trailing stop requires an activation level or distance and a trailing offset; an offset alone is not a complete configuration. Stops can fill worse than their intended level during gaps.

Before using alerts externally, verify the receiver, broker permissions and account rules. A strategy report does not establish a live automation connection. See TradingView's automation limitation.

Pineify Website

Pineify can help assemble strategy conditions and generate Pine source. Inspect the generated logic and run the same testing process; generation does not establish profitability or support every possible indicator combination.

Why is there no trade on every bar?

A crossover is an event, not a continuously true trend state. Check available history, warm-up, conditions and position rules before assuming a runtime problem.

Do these example results apply to options or futures automatically?

No. The strategy uses the chart instrument and its data. Contract value, capital, margin and execution assumptions must match the instrument being tested.

Does a successful backtest prove the strategy will work live?

No. Historical data, fill assumptions, costs and selection bias limit what a backtest demonstrates. Forward evaluation can still differ from actual execution.