Trading strategy code examples
Open complete Pine Script and Python examples, inspect their entry and exit rules and run a reproducible backtest.
Before you start
Examples are complete starting points for learning the strategy contract, not recommended investments. Opening one creates unsaved work; review the code and settings before running it. The full examples reference explains how existing unsaved work is protected.
Follow the workflow
- Choose a language and read the entry, exit and input definitions.
- Open the example through its own link, then check its recommended timeframe and the market/date window.
- Compile it and inspect Notes before Backtest. Inspect individual trades alongside the conditions.
- Change one declared input, cost or rule at a time and record the result. Save work through the account features if you need to keep it.
Worked example
These links resolve to the current bundled code, rather than a copied or outdated strategy identifier.
- MACD cross, EMA 200 filter (Pine) — pine, 4h. Long only. Buys when the MACD line (12, 26, 9) crosses above its signal line while the close is above the 200-bar EMA; closes the position when the MACD line crosses back below. No stop-loss or take-profit.
- SMA 50/200 cross — python, 4h. Long only. Buys when the 50-bar SMA crosses above the 200-bar SMA and sells when it crosses back below; both lengths are inputs. No stop-loss or take-profit.
- SMA 50/200 cross (Pine) — pine, 4h. The same SMA 50/200 cross in Pine Script: long only, buys when the fast SMA crosses above the slow one and closes when it crosses back below; both lengths are inputs.
- EMA 20/50 cross long/short, 2% stop — python, 4h. Long and short. Goes long when the 20-bar EMA crosses above the 50-bar EMA and short when it crosses below: each cross reverses the open position. A 2% stop-loss guards both sides.
- SMA 50/200 cross, 2% stop / 4% target (Pine) — pine, 1h. Long only. Buys when the 50-bar SMA crosses above the 200-bar SMA and leaves only through strategy.exit: a 2% stop-loss or a 4% take-profit from the entry price, both inputs.
- Bollinger mean reversion, 3% trailing stop — python, 4h. Long only, mean reversion. Buys when the close crosses back above the lower Bollinger Band (20 bars, 2.5 standard deviations) and sells when it falls back below the upper band. A 3% trailing stop protects the position.
- Three green bars in a row (state) — python, 4h. Long only. Counts green bars in a row in a state() variable: buys after three green bars in a row and sells on a red bar that closes below the previous bar's open. No stop-loss or take-profit.
- Fear & Greed contrarian — python, 1d. Long only. Buys when the Fear & Greed Index drops below 25 (extreme fear) and closes the position when it rises above 75 (extreme greed); both levels are inputs. Reads the index as market_data.fear_n_greed: a value counts from 02:00 UTC of its day, when a live robot has it too, so a daily bar trades on the value of the day it opened. No stop-loss or take-profit.
- Breakout, trailing high stop (Pine var) — pine, 1h. Long only, breakout. Buys when the close rises above the highest high of the previous 20 bars, keeps the highest close since in a var, and closes once the close falls 3% below it: a trailing stop written in the script.
Limits and interpretation
The paired SMA examples are a useful language comparison; the persistent-state breakout demonstrates a different coding task. RSI is supplied as a pasteable example in its own guide, not advertised as an existing selectable template. No example's historical performance is evidence that it should be traded live.