The indicator automatically draws pitchfork formations on a recursive zigzag. This algorithm is developed using the latest features of Pinescript of using methods and types. The script has the option to select different categories of pitchforks to be drawn. Popular types include Andrew's pitchfork, Schiff and Modified Schiff pitchforks. These can be further drawn as either regular or inside pitchforks.
Link to the Indicator: Recursive Auto Pitchfork Indicator
Object Oriented Programming In Pine
Say Hi to object-oriented programming with Pinescript using types and methods. This is the beginning of a new era of Pinescript where we are moving from isolated scripts containing indicators and strategies to a whole ecosystem of Object Oriented Programming with libraries of highly reusable components. Those who are familiar with programming would have already realised how big these improvements are and what it brings to the table.
With this script, I am not just providing an indicator for traders but also an introduction for programmers on how to design and build object-oriented components in Pinescript using types and methods. Big thanks to Tradingview and the Pine development team for making this happen. We look forward to many such gifts in the future :)
Architecture - Zigzag Framework
As mentioned before, we are not just building an indicator here. But, an ecosystem of components. Using Types and Methods we can visualise libraries as Classes. Thus, we can build an ecosystem of libraries in a layered approach to enhance effective code reusability.
Generic architecture can be visualised below
Coming to the specific case of the Auto Pitchfork indicator, the indicator code is less than 50 lines for logic and around 100 lines for inputs. But, most of the heavy lifting is done by the libraries underneath. Here is a snapshot of related libraries and how they are connected.
All libraries are divided into two portions.
- Types - Contains only type definitions
- Methods - Contains only method definitions related to the types defined in the Types library
Together, these libraries can be visualised as a Class. Methods are defined in such a way all exported methods are related to Types and no other functions or features are defined. If we need further functionality which does not depend on the types, we need to do this via some other library and use them here. Similarly, we should not define any methods related to these types in other libraries.
The reason for splitting the libraries into types and methods is to enable updating methods without disturbing types. Since libraries create interdependencies due to versioning, it is best if we do fewer updates on the type definitions. Splitting the two enables adding more features while keeping the type definition version intact.
🎲 Base Libraries
Base libraries are those which does not have any dependency. They form basic structures which are later used in other libraries. These libraries need to be crafted carefully so that minimal updates are done later on. Any updates on these libraries will impact all the dependent libraries and scripts.
- DrawingTypes - Defines basic drawing types Point, Line, Label, Box, Linefill and related property types.
- DrawingMethods - All the methods or functionality surrounding Basic types are defined here.
🎲 Layer 1 Libraries
These are the libraries which has a direct dependency on base libraries.
- ZigzagTypes - Types required for defining Zigzag and Divergence
- ZigzagMethods - Methods associated with Zigzag Type definitions.
- PitchforkTypes - Basic and Drawing Types for Pitchfork objects
- PitchforkMethods - Methods associated with Pitchfork-type definitions
Indicator and Settings
The indicator draws a pitchfork based on recursive zigzag configurations. Recursive zigzag is derived with the following logic:
- Base level zigzag is calculated with a regular zigzag algorithm with a given length and depth
- Next level zigzag is calculated based on the base zigzag. And we recursively calculate higher level zigzags until we are left with 4 or fewer pivots or when no further reduction is possible
On every level of the zigzag, we then check the last 3 pivots and draw a pitchfork based on the retracement ratio.
Indicator settings are summarised in the tooltips and are as below.