Technical Articles

Pine Script Part 4: Strategy Building and Deep Strategy Tester Usage

Nov 30, 202520
#Pine Script#strategy#backtest#Strategy Tester#algo trading

Pine Script Part 4: Strategy Building and Deep Strategy Tester

This chapter focuses on building full trading strategies using Pine Script and learning how to properly analyze Strategy Tester performance.


1. Strategy() Basics

strategy("Simple Strategy", overlay=true, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.05)

2. strategy.entry()

strategy.entry("Long", strategy.long)
strategy.entry("Short", strategy.short)

Conditional:

longSignal = ta.crossover(ta.sma(close, 20), ta.sma(close, 50))
if longSignal
    strategy.entry("Long", strategy.long)

3. strategy.exit()

strategy.exit("Exit", "Long", stop=close*0.98, limit=close*1.03)

4. Position Controls

strategy.position_size

5. Strategy Tester Metrics

  • Net profit
  • Win rate
  • Max drawdown
  • Sharpe & Sortino
  • Average trade duration

Open tester:
https://sancoqhub.com/go/tradingview


6. Slippage & Commission

strategy.slippage(3)
strategy.commission.percent

7. Full Strategy Example

//@version=5
strategy("EMA Crossover", overlay=true)

fast = ta.ema(close, 20)
slow = ta.ema(close, 50)

longSignal = ta.crossover(fast, slow)
shortSignal = ta.crossunder(fast, slow)

if longSignal
    strategy.entry("Long", strategy.long)

if shortSignal
    strategy.entry("Short", strategy.short)

strategy.exit("TP/SL", "Long", stop=close*0.97, limit=close*1.03)
strategy.exit("TP/SL", "Short", stop=close*1.03, limit=close*0.97)

plot(fast)
plot(slow)

8. Optimization via Inputs

len1 = input.int(20, "Fast MA")
len2 = input.int(50, "Slow MA")

9. Premium Benefits

https://sancoqhub.com/go/tradingview


10. Conclusion

You now know how to write strategies and analyze performance.
Next chapter: Alerts & Webhook Automation.