How to Write a Strategy in Pine Script: A Quick Guide
So you want to automate your trading ideas? I get it - sitting in front of charts all day watching for the perfect setup gets old fast. That's where Pine Script comes in handy. It's like having a friend who never sleeps, watching the markets for you.
What's the Difference Between an Indicator and a Strategy?
Think of it this way: an indicator is like a weather app that tells you it's raining. A strategy is the friend who hands you an umbrella and says "let's go inside."
Indicators just draw lines and shapes on your chart. Strategies actually place pretend trades and tell you how much money you would've made (or lost). It's the difference between saying "this looks like a good buy" and actually pressing the buy button.
Let's Start Simple
Here's the thing - every Pine Script strategy starts with one simple line that tells TradingView "hey, this isn't just another indicator." Instead of indicator(), you use strategy().
Let me show you what I mean. Here's a basic moving average strategy that your cousin could understand:
//@version=5
strategy("Simple Moving Average Strategy", overlay=true)
ma_length = input(20, title="Moving Average Length")
ma = ta.sma(close, ma_length)
if ta.crossover(close, ma)
strategy.entry("Buy", strategy.long)
if ta.crossunder(close, ma)
strategy.entry("Sell", strategy.short)
That's it. No fancy stuff. When price crosses above the moving average, it "buys." When it crosses below, it "sells." Simple as that.
Building Your First Strategy Step by Step
Step 1: Tell TradingView It's a Strategy
Just change indicator() to strategy(). Seriously, that's the first step. It's like changing your relationship status on Facebook - now everyone knows you're serious.
Step 2: Add Some Knobs to Turn
Use input() to let people (including future you) adjust things without digging through code:
fast_ma = input(9, title="Fast MA")
slow_ma = input(21, title="Slow MA")
Step 3: Actually Place Some Trades
This is where the magic happens. Instead of just drawing arrows, you're actually telling TradingView "pretend I bought here":
if buy_condition
strategy.entry("My Buy", strategy.long)
if sell_condition
strategy.entry("My Sell", strategy.short)
Step 4: See How You Did
Add your strategy to any chart, then click the "Strategy Tester" tab. It's like watching a replay of your decisions - sometimes humbling, sometimes exciting.
A Real Example You Can Actually Use
Here's something I actually use on weekends when I'm bored. It buys when RSI is oversold and sells when it's overbought:
//@version=5
strategy("Lazy RSI Strategy", overlay=true)
rsi_length = input(14, title="RSI Length")
rsi_oversold = input(30, title="RSI Oversold")
rsi_overbought = input(70, title="RSI Overbought")
my_rsi = ta.rsi(close, rsi_length)
if my_rsi < rsi_oversold
strategy.entry("Go Long", strategy.long)
if my_rsi > rsi_overbought
strategy.entry("Go Short", strategy.short)
plot(my_rsi, title="RSI", color=color.blue)
Is it perfect? Hell no. But it's a starting point, and starting is what matters.
Some Things I Wish Someone Told Me
- Give your trades obvious names. "Buy123" means nothing to future you. "MA_Cross_Buy" does.
- Don't try to be clever. The best strategies are often the simplest ones.
- Test on different timeframes. What works on 5-minute charts might be garbage on daily charts.
- Keep notes. When you come back to a strategy in 6 months, you'll thank yourself.
Getting Started Right Now
Look, you don't need to build the next big thing today. Just take that simple MA strategy above, change a few numbers, and see what happens. Maybe add an RSI filter. Maybe try different moving averages.
The beauty of Pine Script is that you can test ideas in minutes, not days. Got a theory about Bollinger Bands? Code it up and see. Wondering if that crypto YouTuber's strategy actually works? Build it and find out.
Start small, iterate often, and don't be afraid to break things. The worst that happens is you lose some imaginary money in backtesting.
Happy coding, and may your backtests be ever in your favor.

