Pine Script vs Python for automated trading strategies
- Pine Script is TradingView's language. It's quick for charting an idea, but it only runs on TradingView.
- To automate a Pine strategy, TradingView sends an alert to a web address (a webhook), usually a third-party service, which then sends the order to your broker. Every link in that chain can fail or lag, and TradingView's own documentation says webhooks may occasionally fail to arrive.
- Python is a general-purpose language. A strategy written in it runs wherever you choose (your computer, a server or a hosted runner) and talks to any broker that has an API.
- TradeHarbor's Strategy lab builds strategies from blocks and shows the Python they become, so you don't have to start from a blank file.
Once your rules are exact (see turning an idea into rules), they need to become code. Most traders meet two options: Pine Script, because they already chart on TradingView, and Python, because it's what most trading software and broker examples use. Both can express the same strategy. Where they differ is where the strategy can run, and how its orders reach your broker.
Pine Script: built for TradingView
Pine Script is TradingView's own language for indicators and strategies. It's well designed for what it does: you write a few lines, add the script to a chart, and see the trades drawn on the price history with a performance summary. For sketching an idea quickly, that's hard to beat.
The limits come when you want it to trade:
- It only runs on TradingView. There's no other place to run Pine code. Your strategy lives inside one company's platform and its data.
- It doesn't send orders to your broker by itself. Automating a Pine strategy usually means setting up an alert that fires on each signal and sends a webhook (a message to a web address) to a third-party service, which turns it into an order at your broker.
- Webhooks need a paid TradingView plan and two-factor authentication, and the receiving service has three seconds to respond before TradingView cancels the request.
- Backtests and live behaviour can differ. TradingView's own documentation calls this repainting: a script can calculate differently on historical bars than on live, still-forming bars. A strategy that looks great on the chart can trade differently in real time.
The reliability chain
Here's what has to go right for one automated Pine trade:
- TradingView's servers evaluate your script on the new bar.
- The alert fires and joins TradingView's webhook queue.
- The webhook reaches the third-party service within three seconds.
- The service reads your message correctly and finds your broker connection still logged in.
- The broker accepts the order.
TradingView's webhook documentation says plainly that webhooks may occasionally fail to reach the specified address, and alert-routing services document that alerts can be delayed or not sent at all. Many traders running Pine automations report missed or late alerts, and a missed exit is worse than a missed entry. None of this means it never works. It means there are several companies between your rule and your order, and you control none of them.
Python: runs where you choose
Python is a general-purpose programming language, and it's the most common language for trading software. A strategy written in Python is just a program, so it can run:
- on your own computer, during the session;
- on a server or cloud machine that stays on;
- on a hosted runner, like TradeHarbor's automation beta.
It gets prices and sends orders through your broker's API directly, with no alert queue in between, and most futures and stock brokers with an API have Python examples. If you change brokers, you change the connection, not the strategy. And because the same code runs in the backtest and in live trading, what you tested is what trades.
Here's the moving average cross from the overview, with the exits made explicit. It's an example for learning, not a recommendation:
class EmaCross(Strategy):
symbol = "MNQ" # Micro E-mini Nasdaq-100
timeframe = "5m" # 5-minute bars
session = ("09:30", "15:55")
max_contracts = 1
params = {"fast": 9, "slow": 21, "stop_points": 20, "target_points": 40}
def on_bar(self, ctx, bar):
fast = ta.ema(ctx.closes, self.p.fast)
slow = ta.ema(ctx.closes, self.p.slow)
exits = {"stop_points": self.p.stop_points, "target_points": self.p.target_points}
if ta.crossed_above(fast, slow):
if ctx.short:
ctx.close()
ctx.buy(1, **exits)
elif ta.crossed_below(fast, slow):
if ctx.long:
ctx.close()
ctx.sell(1, **exits)
Read it top to bottom: what it trades, when, the settings, then a function that runs on every new bar and decides. That shape is the same for almost any strategy.
Pine Script vs Python at a glance
| Pine Script | Python | |
|---|---|---|
| Where it runs | TradingView only | Anywhere: your computer, a server, a hosted runner |
| How orders reach the broker | Alert, webhook, third-party service, broker | The broker's API, directly |
| Data | TradingView's | Your broker's or any data source |
| Learning curve | Gentle for charting | Gentle for simple strategies, and you can grow into anything |
| Backtest vs live | Can differ (repainting) | Same code in both |
You don't have to start from a blank file
If you've never written code, the hardest part is the blank page. TradeHarbor's Strategy lab starts you somewhere else: build the strategy from blocks (or describe it in words to the strategy assistant, which proposes the blocks), and the summary shows the plain-English rules next to the Python they become. Change a block and watch the code change. It's a practical way to learn what each line does, using your own strategy.
When you outgrow the blocks, the automation beta has a strategy code editor where you write the Python directly, using the same building blocks (Strategy, ctx.buy, ta.ema and so on) that the builder generates.
If you already have a Pine strategy, the rules are the valuable part, not the code. Write them out in plain words (the questions in turning an idea into rules help), rebuild them in the builder, and check the results match before relying on them.
Next steps
Code you can't trust is worse than no code, so test it: how to backtest a trading strategy. Then see how a program talks to your broker in how trading APIs work.