Snapshots



How it works
- strategy() sets 10,000 starting capital, 10% of equity per trade, 0.05% commission and 2 ticks of slippage.
- strategy.entry buys on a close back above the trend average, but only when no position is open.
- strategy.exit places the stop and target together; whichever price is hit first closes the trade.
Settings
| Setting | Default | What it does |
|---|---|---|
| Trend average length | 50 | Length of the EMA used as the trend line |
| Stop loss % | 2.0 | Distance of the stop below the entry price |
| Take profit % | 4.0 | Distance of the target above the entry price |
Source code
In TradingView: open the Pine Editor, create a new strategy, paste the code, then click "Add to chart".
//@version=6 strategy("My 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, slippage = 2) // 1. Settings you can change later emaLen = input.int(50, "Trend average length") stopPct = input.float(2.0, "Stop loss %", step = 0.5) takePct = input.float(4.0, "Take profit %", step = 0.5) // 2. The trend line trend = ta.ema(close, emaLen) plot(trend, "Trend", color.orange, 2) // 3. The entry rule: price closes back above the trend line buySignal = ta.crossover(close, trend) // 4. Place the order, but only if we are not already in a trade if buySignal and strategy.position_size == 0 strategy.entry("Long", strategy.long) // 5. Stop loss and take profit, measured from the entry price entryPrice = strategy.position_avg_price stopPrice = entryPrice * (1 - stopPct / 100) takePrice = entryPrice * (1 + takePct / 100) if strategy.position_size > 0 strategy.exit("Exit", "Long", stop = stopPrice, limit = takePrice)
Watch it built
This script is written and explained step by step in the video lesson.
Need a custom indicator or strategy?
Non-repainting Pine Script v6, backtested with real costs, alert and webhook ready. Fixed quote within 24 hours.