# How to Write a Strategy in Pine Script - Beginner's Guide

*A comprehensive guide on how to write a strategy in Pine Script, tailored for both beginners and seasoned programmers.*

By [The Sense of Value](https://paragraph.com/@sensethevalue) · 2023-09-07

---

Introduction to Pine Script
---------------------------

Pine Script is a domain-specific language designed by TradingView for backtesting trading strategies. It is easy to use and allows traders to create custom technical analysis indicators.

Getting Started with Writing a Strategy
---------------------------------------

The basic structure of a script begins with the annotation \`@version=4\` indicating its version. Then, you describe your strategy using the \`strategy()\` function.

    @version=4 
    strategy("My Strategy", overlay=true)
    

Defining Entry & Exit Points
----------------------------

To define entry points, use the \`entry()\` function. For exit points, the \`exit()\` or \`stoploss\` functions are used.

    longCondition = crossover(sma(close, 14), sma(close, 28))
    if (longCondition)
        strategy.entry("Buy", strategy.long)
    
    shortCondition = crossunder(sma(close, 14), sma(close, 28))
    if (shortCondition)
        strategy.entry("Sell", strategy.short)
    

Backtesting Your Strategy
-------------------------

Once defined, the strategy can be backtested on historical data in TradingView platform to understand its performance.

Conclusion
----------

This article provided a brief overview of writing a strategy in Pine Script. There are many more functions and possibilities to explore. Happy coding!

---

*Originally published on [The Sense of Value](https://paragraph.com/@sensethevalue/how-to-write-a-strategy-in-pine-script-beginners-guide)*
