PINE LIBRARY

swinglibrary

Updated
Library "swinglibrary"
This library is for calculating non-repainting swings for further calculation on them.
These swings can later be drawn, but drawing is not part of this library, only the calculation.


What do I need to use the library?
You better include the following constants into your script using this library:


Choosing the function, that fits your needs
This library contains 4 functions for calculating swings, the difference between them are the data you get for every swing point and additional average values for length and duration:
  • swings()
  • swingsR()
  • swingsL()
  • swingsLDR()

The naming scheme of these functions is the following:
The base version swings() is only for the swings containing the following swingPoint type:
swingPoint
  Fields:
    x (integer): bar index
    y (float): price
    hilo (integer) 1 -> high, -1 -> low

and the return type:
swingReturn
  Fields:
    swings (array<swingPoint>): array of the last x swing points
    newSwingHigh (integer): flag to detect changes for swing highs see constants (SWING_NO_ACTION, SWING_FLIP_NEW_SWING, SWING_FLIP_UPDATED)
    newSwingLow (integer): flag to detect changes for swing lows see constants (SWING_NO_ACTION, SWING_FLIP_NEW_SWING, SWING_FLIP_UPDATED)

The R in swingsR() stands for relation where the previously shown types do also contain the relation between the swings of the same swing type (highs and lows respectively).
The same goes for L in swingsL() for length containing the price difference between the current and previous swing point in ticks.
And in the following version swingsLDR() there is also the duration between the current and previous point included.
The parameters for the other functions and type definitions include only the ones, that are needed, the "full" version of the function is described here:

swingsLDR(swingSize, dtbStrength, init, SWING_HISTORY_NUM)
  Parameters:
    swingSize (int) This parameter defines the size of the swings to look after, meaning higher values will lead to bigger swings
    dtbStrength (int) Value between 0 and 100 is a factor (%) to the ATR that is used to calculate equal highs/lows (double tops / bottoms).
Higher values will result in a higher tolerance of price difference between the swings.
    init (bool) This value is usually set to false on default.
It has a special use case, where we need to reduce memory usage and calculation time on the script using this library by start calculating at x bars back instead of the beginning of the chart.
In this case, we set init = true on the first bar we start calculating the swings on to perform the correct initialization.
    SWING_HISTORY_NUM (int) This is the max number of swings that are stored in the array, so only the last SWING_HISTORY_NUM swings are stored in the array to reduce the memory usage.
New ones remove the oldest ones like in a ring buffer.
This is also influencing the average duration and average swing length.

swingPointLDR
  Fields:
    x (integer): bar index
    y (float): price
    hilo (integer): 1 -> high, -1 -> low
    length (float): price difference to the previous swing point in ticks
    duration (integer): duration difference to the previous swing point in number of bars
    relation (integer): see constants RELATION_HIGHER, RELATION_EQUAL, RELATION_LOWER: reelation to the previous swing points of the same type (previous high or previous low respectively)

swingReturnLDR
  Fields:
    swings (array<swingPointLDR>): array of the last x swing points
    newSwingHigh (integer): flag to detect changes for swing highs see constants (SWING_NO_ACTION, SWING_FLIP_NEW_SWING, SWING_FLIP_UPDATED)
    newSwingLow (integer): flag to detect changes for swing lows see constants (SWING_NO_ACTION, SWING_FLIP_NEW_SWING, SWING_FLIP_UPDATED)
    avSwLength (float): average swing length for the last x swings (depending on the max number of swings)
    avSwingDuration (float): average swing duration for the last x swings (depending on the max number of swings)
Release Notes
v2

Added:
new swing function that supports different swing types:
swingsRTyped(swingStyleType, swingSizeFloat, dtbStrength, ignoreInsideBars, useBreakouts, SWING_HISTORY_NUM)
  Parameters:
    swingStyleType (string) (Standard, Gann, Ticks, Percent)
    swingSizeFloat (float) See v1 description
    dtbStrength (int) See v1 description
    ignoreInsideBars (bool) Special Setting for Gann Swings
    useBreakouts (bool) Special Setting for Gann Swings
    SWING_HISTORY_NUM (int) See v1 description
Release Notes
v3

Added:
new swing function with more information for each point:
- timeStamp
- percent
- volume
- cvd

swingsLDRTPVCTyped(swingStyleType, swingSizeFloat, dtbStrength, ignoreInsideBars, useBreakouts, SWING_HISTORY_NUM, cdHigh, cdLow)
  Parameters:
    swingStyleType (string)
    swingSizeFloat (float)
    dtbStrength (int)
    ignoreInsideBars (bool)
    useBreakouts (bool)
    SWING_HISTORY_NUM (int)
    cdHigh (float)
    cdLow (float)

swingPointLDRTPVC
  Fields:
    x (series int)
    y (series float)
    hilo (series int)
    relation (series int)
    length (series float)
    duration (series int)
    timeStamp (series int)
    percent (series float)
    vol (series float)
    cumDelta (series float)

swingReturnLDRTPVC
  Fields:
    swings (array<swingPointLDRTPVC>)
    newSwingHigh (series int)
    newSwingLow (series int)
    avSwLength (series float)
    avSwingDuration (series float)
Release Notes
v4

Added:
swingsRTypedCustom(swingStyleType, swingSizeFloat, dtbStrength, ignoreInsideBars, useBreakouts, SWING_HISTORY_NUM, highs, lows)
  Parameters:
    swingStyleType (string)
    swingSizeFloat (float)
    dtbStrength (int)
    ignoreInsideBars (bool)
    useBreakouts (bool)
    SWING_HISTORY_NUM (int)
    highs (float)
    lows (float)
techindicator

Pine library

In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in a publication is governed by House rules.

Disclaimer