bilsebub

swinglibrary

bilsebub 已更新   
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:
int SWING_NO_ACTION         = 0
int SWING_FLIP              = 1
int SWING_FLIP_NEW_SWING    = 2
int SWING_FLIP_UPDATED      = 3

int RELATION_HIGHER         = 1
int RELATION_EQUAL          = 0
int RELATION_LOWER          = -1

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)
版本注释:
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
Pine脚本库

本着真正的TradingView精神,作者将此Pine代码以开源脚本库发布,以便我们社区的其他Pine程序员可以重用它。向作者致敬!您可以私下或在其他开源出版物中使用此库,但在出版物中重用此代码受网站规则约束。

免责声明

这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。

想使用这个脚本库吗?

复制以下行并将其粘贴到您的脚本中。