# How to Build Your First TradingView Strategy in Pine Script v6

AI agents: this is one page from QuantIDE's site, in Markdown.
Canonical HTML: https://www.quantide.io/guides/how-to-build-tradingview-strategy-pine-script-v6
Full index of Markdown pages: https://www.quantide.io/llms.txt
Full text of recent articles: https://www.quantide.io/llms-full.txt

---
*2026-11-16 · QuantIDE Team · 7 min read*

Pine Script v6 is TradingView's current language for systematic strategies. Build a complete moving-average crossover strategy in about 30 lines, run it in the Strategy Tester, and read the result without fooling yourself — including what TradingView's November 2026 rule reversal means for script authors.

## In brief

- Pine Script v6 is the current standard: declare it with //@version=6 and use the ta.* namespace.
- A complete first strategy is about 30 lines: two moving averages, a crossover entry, a stop and target exit, and an early exit on the reverse cross.
- TradingView's November 6, 2026 terms reversed the planned November 1 Marketplace-only rule: authors can sell through their own channels, the Marketplace, or both.
- A Strategy Tester backtest proves nothing until it survives walk-forward validation, realistic costs, and a large enough trade sample.
- Alerts are not automation: a live order needs a bridge from the alert to the broker's API, with risk checks.

## The short answer

Pine Script is TradingView's built-in language for writing indicators and systematic strategies, and version 6 is the current standard. A complete first strategy takes about 30 lines: two moving averages, a crossover rule, an exit rule, and a stop and a target. This guide builds one, runs it in TradingView's Strategy Tester, and shows how to read the result honestly — including what changed on TradingView in November 2026 for anyone who writes or sells scripts.

## What changed on TradingView in November 2026

On September 30, 2026, TradingView announced that selling access to invite-only scripts would move to its Marketplace from November 1, and that selling access anywhere else would no longer be permitted. The reaction from script authors was fast and loud. On October 27, TradingView said nothing would change on November 1 while it finalised new terms — and on November 6 it published them: the November 1 restriction will not take effect. Authors can sell through their own channels, through a Marketplace Paid Space, or both, and the Marketplace fee is currently 0%.

Three dates still matter if you write or sell scripts:

- **November 15, 2026:** authors can start converting private invite-only scripts into public ones.
- **December 1, 2026:** a private invite-only script with more than 5 users cannot take new users or updates until it is converted or cut back to 5 users or fewer.
- **February 1, 2027:** seats start. A Premium or higher plan includes 100 seats; each extra 100 seats costs $300 a month. Access granted before February 1, 2027 is never charged for unless it is extended or a script is added, and customers keep access they already paid for, including lifetime access.

The lesson for a strategy builder is not about the fee schedule. It is that the rules of the platform your strategy lives on can change in five weeks — September 30 to November 6. Own your code, keep it in version 6, and validate it yourself instead of trusting a backtest screenshot from a seller.

## Indicator or strategy? Decide before you type

Every Pine Script file opens with a version line and a declaration, and the declaration decides what the script can do. `indicator()` draws on the chart. `strategy()` does that too, and it also simulates orders — entries, exits, position sizing — so the Strategy Tester can report every simulated trade. If you want to test a system, you want `strategy()`.

The two share most of the language: the same `ta.*` functions for moving averages and crossovers, the same inputs, the same plotting. The difference is the `strategy.*` namespace: `strategy.entry()`, `strategy.exit()`, and `strategy.close()` are only available in a strategy script, and they are what turn a chart drawing into a testable system.

## The complete strategy

This is a teaching example, not a recommended system. It goes long only, enters when a 14-bar simple moving average crosses above a 28-bar one, risks 1% per trade with a stop 1% below entry, takes profit at 2% above entry, and exits early if the averages cross back. The same rules were executed in Python on sample data to confirm the crossover detection and the stop, target, and crossunder exits all fire as written.

```
//@version=6
strategy("SMA Crossover - First 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.05)

fastLen = input.int(14, "Fast SMA length", minval=2)
slowLen = input.int(28, "Slow SMA length", minval=3)

fastMA = ta.sma(close, fastLen)
slowMA = ta.sma(close, slowLen)

longCondition = ta.crossover(fastMA, slowMA)
exitCondition = ta.crossunder(fastMA, slowMA)

if longCondition and strategy.position_size == 0
    strategy.entry("Long", strategy.long)

if strategy.position_size > 0
    entryPrice = strategy.position_avg_price
    stopPrice = entryPrice * 0.99
    targetPrice = entryPrice * 1.02
    strategy.exit("Exit", "Long", stop=stopPrice, limit=targetPrice)

if exitCondition and strategy.position_size > 0
    strategy.close("Long")

plot(fastMA, "Fast SMA", color=color.new(color.blue, 0))
plot(slowMA, "Slow SMA", color=color.new(color.orange, 0))
```

Paste it into the Pine Editor (open a chart, click Pine Editor at the bottom, paste, click Add to chart). The two moving averages appear on the chart, and the Strategy Tester tab opens below it.

## What each line does

- `//@version=6` tells TradingView which version of Pine Script the code is written for. Version 6 is the current standard and the only version receiving new features. Older scripts still run, but new work should start here.
- `strategy(...)` declares a strategy and sets the test conditions: $10,000 starting capital, 10% of equity per trade, and 0.05% commission on every simulated fill. Those assumptions are part of the result — change them and the numbers change.
- `input.int(...)` turns the two lengths into settings you can change from the chart without editing code. That is how you test 14/28 against 9/21 honestly: change the input, re-run, compare.
- `ta.sma(close, fastLen)` is the built-in simple moving average from the `ta.*` technical-analysis namespace. Version 6 requires the namespace prefix: `ta.sma()`, not `sma()`.
- `ta.crossover(fastMA, slowMA)` is true only on the bar where the fast average moves from at or below the slow average to above it. `ta.crossunder()` is the mirror image. A crossover is a single-bar event, not a state — that is why the entry sits inside an `if`.
- `strategy.entry("Long", strategy.long)` opens the long position, but only when flat — the `strategy.position_size == 0` guard stops the script re-entering on every bar while a crossover condition persists.
- `strategy.exit("Exit", "Long", stop=..., limit=...)` attaches the stop and the target to the open position. When both a stop and a limit are given, version 6 uses whichever level is expected to trigger first.
- `strategy.close("Long")` exits on the reverse cross, so a trade that never reaches its stop or target still ends when the trend signal flips.

## Running it in the Strategy Tester

Open the Strategy Tester tab and read three panels before you look at the equity curve. The Overview shows net profit, max drawdown, and profit factor. The Performance Summary breaks the result into longs, win rate, and average trade. The List of Trades shows every simulated fill — and that list is where most first strategies fail the smell test.

Check three things:

1. **Trade count.** Fewer than roughly 30 trades means the result is mostly noise; one or two lucky winners can carry the whole curve. The sample-size guide linked below works through exactly how much history a result needs before it means anything.
2. **Max drawdown against position size.** The code risks 10% of equity per trade with a 1% stop, so a single losing trade costs about 0.1% of equity — small by design. If your own version sizes up to 50% of equity per trade, the same stop costs five times as much, and the drawdown line will show it.
3. **Commission and slippage.** The code sets commission to 0.05%, but slippage is set in the strategy properties, not in the code. A strategy that looks profitable at zero slippage and dies at realistic slippage was never a strategy — it was a rounding error. Costs change verdicts; the transaction-costs guide linked below shows how.

## The three v6 gotchas that change your backtest

Most Pine Script examples online are still version 5, and three version 6 changes alter results silently if you copy old code:

1. **The default margin is 100% in version 6, where it was 0 in version 5.** A version 5 strategy converted to version 6 can behave differently on the same chart, because the margin default changes how much of your capital each position uses. Check the order list before and after converting.
2. **The `when` argument is gone from `strategy.*()` order functions.** Version 5 code that passed `when=` to an entry or exit will not compile. Use an `if` block, as the example above does.
3. **Boolean typing is strict.** A bool is true or false, never `na`, and a number is no longer silently treated as a bool. Conditions that used to evaluate to `na` now evaluate to false, which can quietly remove trades from a converted script.

If you convert an old script, open the Pine Editor's Manage script menu, choose Convert code to v6, and compare the trade list before and after. A conversion that changes the trade count has changed the strategy, not just the syntax.

## What a Pine Script backtest does not prove

A good equity curve on one symbol and one date range is a starting point, not a verdict. Three checks separate a tested idea from a fitted one, and all three are covered in depth in the backtesting guides linked below.

First, **walk-forward validation**: optimise the lengths on one period, then test the chosen lengths on data the optimisation never saw. A crossover pair that only works on the period you tuned it on is curve-fitting with extra steps. Second, **costs**: commission and slippage are real, and the Tester only charges what you tell it to. Third, **sample size**: a 14/28 crossover on a daily chart produces a handful of trades a year, so a two-year test is a small sample dressed as evidence. Run the same rules on more history, more symbols, or a shorter timeframe before you believe the win rate.

The same discipline applies to the MetaTrader side of the world: the [MetaTrader 5 Strategy Tester guide](/guides/how-to-backtest-metatrader-5-strategy-tester) walks through real-tick mode, costs, and forward testing after optimisation — the same three checks, a different platform.

## Alerts are not automation

A TradingView alert can fire a webhook when your crossover happens. That is not a live order. TradingView does not place trades with your broker, so turning an alert into an executed order needs a bridge: a server that receives the webhook, checks risk, and calls the broker's API. That bridge is engineering work — data, signal, risk, execution, monitoring — and it is where most Pine Script strategies die. The backtest was real; the automation was never built.

That gap is exactly what an embedded engineer closes: the strategy you just wrote becomes code that runs without you, with the broker connection and the risk checks built around the rules. The [Python backtesting guide](/guides/how-to-backtest-a-trading-strategy-in-python) shows the same crossover logic end to end in code you own, and the [walk-forward guide](/guides/walk-forward-validation-systematic-strategies) shows how to test it honestly before it ever touches real money.

## What to do next

1. Paste the script into the Pine Editor, add it to a chart, and open the Strategy Tester.
2. Change the two lengths, watch the trade count, and run the walk-forward check from the linked guide before you trust any single setting.
3. If the idea survives validation and you want it trading without you, that is the engineering step: the broker connection, the risk checks, and the monitoring around the rules you just wrote. The [transaction-costs guide](/guides/transaction-costs-backtesting-fees-slippage) is the last check before that step — it prices the part of trading the Tester lets you ignore.


## Sources

- [TradingView Blog — Selling scripts on TradingView: the new terms (November 6, 2026)](https://www.tradingview.com/blog/en/selling-scripts-tradingview-new-terms-62251/)
- [TradingView Blog — Commercial scripts are moving to the TradingView Marketplace (September 30, 2026)](https://www.tradingview.com/blog/en/commercial-scripts-move-to-marketplace-61291/)
- [TradingView Pine Script User Manual — Strategies](https://www.tradingview.com/pine-script-docs/faq/strategies)
- [TradingView Pine Script Language Reference Manual v6](https://www.tradingview.com/pine-script-reference/v6/)

*Educational content, not investment advice. Trading involves risk of loss.*