Lesson 38 of 50

Basic Pine Script Syntax for Absolute Beginners

A line-by-line, simple walkthrough of a genuinely minimal Pine Script example - a taste of syntax, not a full programming course.

What you will learn in this lesson

  • Walk through a genuinely minimal Pine Script example, one line at a time, in plain English
  • Understand the purpose of a version declaration, a script declaration, a variable, and a plot statement
  • Recognize this as a first taste of syntax, not a complete programming education
  • Understand how Pine Script strategies commonly generate the alerts Module 11 builds on
  • Close Module 10 and bridge into Module 11's webhook-focused lessons

Lessons 36-37 covered what Pine Script is and the indicator-vs-strategy distinction, without showing a single line of actual code. This lesson finally looks at a genuinely minimal example - just four lines - explained one at a time, in plain English. This is a taste, not a programming course.

A Genuinely Minimal Example

Here’s a deliberately simple example of what a first Pine Script indicator might look like:

//@version=5
indicator("My First Script")
length = 20
plot(ta.sma(close, length))

That’s the entire script. Four lines. Let’s walk through what each one is doing, in plain English, without worrying about memorizing exact syntax rules.

Line 1: The Version Declaration

//@version=5

This line tells TradingView which version of the Pine Script language the rest of the script is written for. Pine Script has evolved over time, and this declaration ensures the script is interpreted using the correct rules for that specific version - a bit like specifying which edition of a rulebook you’re playing by, before the actual game begins.

Line 2: The Script Declaration

indicator("My First Script")

This line does two things at once: it declares that this script is an indicator (recall Lesson 37’s distinction - purely visual, no simulated trades), and it gives the script a name, “My First Script,” which is what would appear on the chart and in TradingView’s indicator list once applied.

Line 3: A Simple Variable

length = 20

This line stores the number 20 under a name, length. Rather than typing the number 20 repeatedly throughout a script, a variable like this lets you refer to it by name - and, importantly, lets you change it in one place later (say, to 50) if you want to adjust the calculation without hunting through the entire script for every occurrence of the number.

In this example, length is intended to represent the number of periods used in a moving average calculation - a common building block from Module 5’s indicator lessons.

Line 4: The Plot Statement

plot(ta.sma(close, length))

This is where something finally becomes visible on the chart. In plain English, this line says: “calculate a simple moving average (sma) of the closing price (close), using the period length stored in length, and plot that calculation as a line on the chart.” Without this final line, the calculation would simply happen invisibly - plot(...) is what actually draws it where you can see it.

Line 1:  //@version=5              → "Use these language rules"
Line 2:  indicator("My First       → "This is an indicator,
          Script")                    call it this name"
Line 3:  length = 20                → "Remember the number 20
                                        as 'length'"
Line 4:  plot(ta.sma(close,         → "Calculate a moving average
          length))                     and draw it on the chart"

What This Example Deliberately Leaves Out

This lesson is not attempting to teach Pine Script comprehensively - there’s no coverage here of conditional logic, alert triggering from within a script, colors and styling options, or strategy-specific syntax (entry/exit rules from Lesson 37). All of that exists, and genuinely learning it takes continued practice well beyond this course. The goal here was narrower: showing that four understandable lines can produce a real, working result, demystifying Pine Script’s basic shape.

Real-Life Example: Adjusting One Number

Suppose a trader using a script similar to this example finds the 20-period moving average line reacts a little too quickly to short-term price noise for their taste. Because the length is stored as a variable (length = 20), adjusting it to length = 50 in that single spot changes the entire calculation to use a longer historical window - producing a smoother, slower-reacting line - without needing to touch any other part of the script.

Analogy: A Labeled Storage Box

Think of a variable like length = 20 as a labeled storage box: instead of scattering loose scraps of paper reading “20” throughout a workshop, you put the number 20 in one box labeled “length,” and everywhere else in the workshop, you just refer to “the length box.” Need to change it to 50 later? Just update what’s inside that one box - everything that references it automatically uses the new value.

Bridging Into Module 11: Where Alerts Come From

Recall Module 8’s alert lessons: alerts fire when a chart condition is met. A Pine Script - particularly a strategy script (Lesson 37) with defined entry and exit conditions - is commonly exactly what generates those very conditions. A trader’s custom strategy script, once running on a chart, can be configured to trigger an alert whenever its own internal buy or sell condition fires - connecting everything from this module directly into Module 8’s alert system.

Module 11, starting with the next lesson, explains what happens when an alert like that is connected to a webhook - turning a chart-based condition into an automated message sent to another system.

Common Beginner Mistakes

  • Trying to memorize this example instead of understanding what each line type does. The goal is recognizing the PURPOSE of a version line, a declaration line, a variable, and a plot statement - not this exact example verbatim.
  • Expecting this lesson to teach comprehensive syntax rules. This module offers a first taste; genuine fluency takes continued practice well beyond this course’s scope.
  • Assuming a script needs to be complex to be valid. A working script can be genuinely simple, as this four-line example shows.
  • Confusing this indicator-type example with a strategy script. This example only plots visually - Lesson 37’s distinction still applies, and this script does not simulate any trades.

Practical Tips

  • If curious, type this exact four-line example into TradingView’s Pine Editor and apply it to a chart, just to see a moving average line appear - a small, satisfying way to connect this lesson’s explanation to a real result.
  • Try changing just the length variable’s number and observe how the plotted line’s smoothness changes - a low-risk way to build intuition without touching anything else in the script.
  • Carry the “codify your own idea” mindset from Lesson 36 forward into Module 11 - the webhook and automation lessons ahead build directly on the idea that a script’s conditions (however simple or elaborate) are what ultimately triggers an alert.

Practical Exercise

  • Without writing any code yourself, look back at the four-line example in this lesson and, in your own words, explain what would likely change on the chart if the moving average length variable were changed from 20 to 50.
  • Open TradingView's Pine Editor (Lesson 36's first exercise) and look at the default template script it opens with, if any. Try to spot a version declaration line and a declaration line, even without understanding every detail yet.

Mini Quiz

1. What does a line like "//@version=5" typically do at the top of a Pine Script?
  • It adds a visual comment with no functional purpose
  • It declares which version of the Pine Script language the rest of the script is written for
  • It sets the chart's time frame
  • It connects the script to a broker account

A version declaration line tells TradingView which version of the Pine Script language the script is written against, so it's interpreted using the correct rules for that version.

2. What is the general purpose of a line like indicator("My First Script") near the top of a script?
  • It plots a moving average automatically
  • It declares the script as an indicator type and gives it a display name
  • It places a real trade under that name
  • It deletes any existing chart drawings

A declaration line like this establishes the script as an indicator (as opposed to a strategy, per Lesson 37) and assigns it a name that will appear on the chart and in TradingView's indicator list.

3. In this lesson's minimal example, what is the general purpose of defining a simple variable (such as a moving average length)?
  • Variables serve no real purpose in Pine Script
  • It stores a reusable value (like a number of periods) that the rest of the script can refer to, making the script easier to read and adjust
  • It automatically executes a trade
  • It only affects the script's color scheme

A variable stores a value - such as the number of periods used in a moving average calculation - so that value can be reused and easily adjusted in one place, rather than being repeated throughout the script.

4. What does a plot(...) statement generally do in a Pine Script?
  • It saves the script permanently to a broker account
  • It draws the specified calculation visually onto the chart, such as a line
  • It deletes the current chart
  • It sends a webhook message immediately

A plot statement is what actually draws a calculated value onto the chart as a visual element (commonly a line), making an otherwise invisible calculation visible to the trader.

5. Is this lesson intended to teach complete Pine Script syntax rules and programming fundamentals?
  • Yes, this lesson is a complete substitute for a full programming course
  • No - this lesson offers a taste of what a few lines mean in plain English, not comprehensive syntax rules or a programming education
  • Pine Script has no syntax rules to learn at all
  • This lesson only applies to strategy scripts, not indicators

This lesson deliberately stays at the level of explaining what a few representative lines DO, in plain English - genuinely learning Pine Script comprehensively requires considerably more practice and reference material beyond this module's scope.

6. How do Pine Script strategies commonly connect to the topics covered in Module 11 ahead?
  • They are entirely unrelated to Module 11's content
  • Pine Script strategies (and indicators) commonly generate the alert conditions that Module 11 explains can be connected to webhooks
  • Module 11 replaces the need to understand Pine Script at all
  • Webhooks can only be triggered manually, never by a script

A Pine Script strategy's (or indicator's) defined conditions are commonly what triggers the alerts covered in Module 8 - and Module 11, starting next, explains how those same alerts can be connected to webhooks for automated messaging.

Frequently Asked Questions

Do I need to memorize the exact example lines shown in this lesson?

No - the goal is understanding what each TYPE of line generally does (a version declaration, a script declaration, a variable, a plot statement), not memorizing this specific example verbatim.

Is "//@version=5" the only version a Pine Script can use?

No - Pine Script has had multiple versions over time, and TradingView's current documentation reflects whichever versions are currently supported. This lesson uses version 5 purely as a representative, commonly seen example, not as the only option.

What happens if I change the number in a length variable, like from 20 to 50?

Conceptually, a larger number (like 50 periods instead of 20) would generally make a moving average calculation smoother and slower to react to recent price changes, since it's averaging over a longer stretch of historical data - though the exact visual effect depends on the specific calculation involved.

Can a plot statement draw more than just a simple line?

Yes - depending on how it's configured, a plot-type statement can display various visual styles (lines, shapes, colored areas, and more), though this lesson keeps things to the simplest case (a basic line) to stay beginner-friendly.

Do I need to type this example myself to benefit from this lesson?

Typing it into the Pine Editor and observing what appears on the chart can reinforce the concepts, but this lesson is written so the explanation itself is useful even without doing so - re-reading the line-by-line breakdown is a reasonable first step either way.

Is four lines a realistic length for a genuinely useful Pine Script?

No - genuinely useful scripts are typically considerably longer and more elaborate than this deliberately minimal example. Four lines were chosen here purely to keep the very first exposure to syntax approachable, not to represent a typical real-world script.

Why does this course wait until Lesson 38 to show any actual Pine Script lines?

Because understanding WHY Pine Script exists (Lesson 36) and the indicator-vs-strategy distinction (Lesson 37) makes the actual syntax in this lesson considerably easier to place in context, rather than showing code first with no conceptual grounding.

Does this lesson's example create an indicator or a strategy, per Lesson 37's distinction?

The example in this lesson is written as an indicator - it plots a calculation visually, with no simulated trade entries or exits involved, consistent with Lesson 37's definition of an indicator script.

What comes right after this lesson?

Module 11 begins with Lesson 39, covering TradingView webhooks specifically - building directly on the alert concepts from Module 8, and noting that Pine Script strategies (and indicators) are commonly what generates the underlying alert conditions in the first place.

Is it normal to feel like this lesson only scratched the surface?

Yes, entirely - that's the intended feeling. This module's goal was a beginner-friendly taste of Pine Script's purpose and basic shape, not fluency. Genuine comfort with Pine Script syntax typically comes from continued, hands-on practice well beyond this course.

Glossary

Key Takeaways

  • A version declaration line (such as //@version=5) tells TradingView which version of the Pine Script language a script is written against.
  • A declaration line (such as indicator("My First Script")) establishes the script's type and display name.
  • A variable stores a reusable value (like a moving average length), making a script easier to read and adjust in one place.
  • A plot(...) statement is what actually draws a calculated value visually onto the chart.
  • This lesson offered a taste of Pine Script syntax in plain English, not comprehensive rules - genuine fluency takes continued practice beyond this course.
  • Pine Script strategies and indicators commonly generate the alert conditions that Module 11, starting next, connects to webhooks.

Conclusion

Four lines - a version declaration, a script declaration, a variable, and a plot statement - are enough to see what Pine Script actually looks like and to demystify its basic shape, even without becoming fluent in it. This closes Module 10. Module 11 picks up directly from here: the alert conditions that scripts like this one (or considerably more elaborate versions of it) commonly generate are exactly what gets turned into a webhook message, starting with Lesson 39.

Disclaimer:This lesson is for educational purposes only and covers the TradingView platform itself - it is not investment, trading, or financial advice. No indicator, drawing tool, or automation setup guarantees future results. Trading and investing involve risk of loss and are not suitable for every investor. Please do your own research and consult a SEBI-registered investment adviser before making trading or investment decisions.