Jayadev Rana Get a custom script
Free Pine Script Indicators / My First Strategy
Free strategy · Pine Script v6

My First Strategy

A beginner strategy for learning the Strategy Tester: buy when price closes back above a trend average, then a 2% stop loss and a 4% take profit. Commission and slippage are included, so the back test is honest.

StrategyPine Script v6OverlayFree

Get the codeDownload .pine.txt

Snapshots

Entries and exits on SPX500USD 4h with the orange trend line, and the Strategy Tester showing 199 trades, profit factor 1.294 and +7.43%.
Entries and exits on SPX500USD 4h with the orange trend line, and the Strategy Tester showing 199 trades, profit factor 1.294 and +7.43%.
Take profit raised from 4% to 6%: the numbers change, which is exactly how overfitting starts.
Take profit raised from 4% to 6%: the numbers change, which is exactly how overfitting starts.
The complete strategy in the TradingView Pine Editor.
The complete strategy in the TradingView Pine Editor.

How it works

  1. strategy() sets 10,000 starting capital, 10% of equity per trade, 0.05% commission and 2 ticks of slippage.
  2. strategy.entry buys on a close back above the trend average, but only when no position is open.
  3. strategy.exit places the stop and target together; whichever price is hit first closes the trade.

Settings

SettingDefaultWhat it does
Trend average length50Length of the EMA used as the trend line
Stop loss %2.0Distance of the stop below the entry price
Take profit %4.0Distance 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".

first-strategy.pine.txtGitHubDownload
//@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.

Free and open source under the Mozilla Public License 2.0. Educational content only, not financial advice. Backtest results are historical and include the costs stated; past performance does not predict future results. © Jayadev Rana · Privacy · Terms