Pine Script course, lesson 25, the fourth lesson of the advanced arc about Pine Script as a language.
Which session makes the high of the day on gold: Asia, London or New York? Almost every trader has an opinion, and almost nobody has counted. In this lesson we build an indicator that counts it for you, live, on any symbol, and along the way we use the three Pine v6 features that separate a hobby script from a professional one: enums, methods and maps.
An enum gives us a closed list of sessions, and input.enum turns that list into a settings dropdown for free. A user defined type with default field values holds each session’s statistics, and a method called update keeps the behaviour right next to the data it changes. A map from the Session enum to those records replaces twelve loose variables with one loop.
The beat that matters most: key your maps with an enum, not a string. A string key with a typo silently creates a new entry and splits your statistics in two with no error anywhere. An enum key with a typo does not compile. We also hit a genuine keyword trap (range is reserved in Pine), meet reference semantics again through map.get, and design the table so it checks itself: every finished day hands its high to exactly one session, so each percentage column adds up to a hundred.
FREE CODE + CHART SNAPSHOTS (copy-paste ready)
Session Stats (this lesson): https://jayadevrana.in/free-pine-script-indicators/enum-method-map/
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 an enum, and using input.enum to get a dropdown for free
– User defined types with default field values
– Writing methods, and calling them with the dot
– A map keyed by an enum, and the silent string-key bug it prevents
– Iterating a map with key and value, and why its order is predictable
– Scoring only finished days, and building a statistic that checks itself
Chapters
0: 00 Intro
0: 45 The problem
1: 15 The declaration
1: 29 Feature one, the enum
1: 54 A dropdown for free
2: 15 Feature two, the type
2: 35 Feature three, the method
3: 03 A keyword trap
3: 27 A second method
3: 42 Which session is this bar in
4: 23 Shading the sessions
4: 40 What the shading shows
4: 57 Feature four, the map
5: 23 Why the key is an enum
5: 52 What we track
6: 09 Closing a session, closing a day
6: 37 References, again
6: 57 Running highs and lows
7: 23 The table
8: 09 Reading the table
8: 37 A built-in sanity check
8: 59 Why this is how pros write Pine
9: 22 Your homework
9: 35 Recap
Full code: https://jayadevrana.in/free-pine-script-indicators/enum-method-map/
Educational content only. Not financial advice.
#PineScript #TradingView #PineScriptCourse