Arrays And User-Defined Types, A Real Swing Engine | Pine Script Lesson 23

Pine Script course, lesson 23, the second lesson of the advanced arc about Pine Script as a language.

There is a wall in Pine Script and most people never get over it. On one side you write a forty line indicator that plots a moving average. On the other side you write the kind of tool people pay for. The wall is the moment your script has to remember a list of things that each have several properties, and keep that list correct while the market moves.

We get over it by building a swing structure engine with a user defined type and an array of those types, which is the same data model underneath almost every order block and market structure indicator you have ever used.

The beat that matters most: objects in Pine are references, not values. array.get hands you the object itself, not a copy, so assigning to a field of the thing you fetched changes the element still sitting in the array. That is what makes the retire loop work with no array.set anywhere, and it is also the bug behind most confusing behaviour in advanced scripts.

We also cover bounded growth, and a genuine Pine trap: a for loop whose start is above its end counts downwards, so on an empty array the obvious backwards loop runs forwards and asks for element minus one.

FREE CODE + CHART SNAPSHOTS (copy-paste ready)
Swing Engine (this lesson): https://jayadevrana.in/free-pine-script-indicators/swing-engine/
All free Pine Script indicators: https://jayadevrana.in/free-pine-script-indicators/
GitHub: https://github.com/jayadevrana/free-pine-script-indicators

Pine Script course playlist: https://www.youtube.com/playlist?list=PLLXezQvH0Of0

What you’ll learn
– Declaring a type, and why it beats four parallel arrays
– array.new, push, shift, and keeping a list bounded
– Reference semantics, and when they help and when they bite
– The descending for loop trap and the guard it needs
– Rendering a live data structure as a table

Chapters
0: 00 Intro
0: 50 The wrong answer first
1: 35 The declaration
1: 43 A type of your own
2: 13 The inputs
2: 34 The array itself
2: 59 Finding and storing a swing
3: 42 The most important idea in the lesson
4: 15 Which cuts both ways
4: 51 Why the list must be bounded
5: 19 Reading the list backwards, and a trap
5: 59 Drawing without drawings
6: 37 The list, on screen
7: 01 Watching it work
7: 27 What this unlocks
7: 57 Three rules to keep
8: 20 Your homework
8: 50 Recap

Full code: https://jayadevrana.in/free-pine-script-indicators/swing-engine/

Educational content only. Not financial advice.

#PineScript #TradingView #PineScriptCourse