PINE LIBRARY
Cập nhật swinglibrary

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:
Pine Script®
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:
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)
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)
Phát hành các Ghi chú
v2Added:
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
Phát hành các Ghi chú
v3Added:
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)
Phát hành các Ghi chú
v4Added:
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)
Phát hành các Ghi chú
v5Phát hành các Ghi chú
v6Updated:
swingsLDRTPVCTyped(swingStyleType, swingSizeFloat, dtbStrength, init, ignoreInsideBars, useBreakouts, SWING_HISTORY_NUM, cdHigh, cdLow, useNewSwingCloseConfirmation)
Parameters:
swingStyleType (string)
swingSizeFloat (float)
dtbStrength (int)
init (bool)
ignoreInsideBars (bool)
useBreakouts (bool)
SWING_HISTORY_NUM (int)
cdHigh (float)
cdLow (float)
useNewSwingCloseConfirmation (bool)
Phát hành các Ghi chú
v7fix swing size bug
Phát hành các Ghi chú
v8fix historic buffer issue
Thư viện Pine
Theo tinh thần TradingView thực sự, tác giả đã xuất bản mã Pine này dưới dạng thư viện nguồn mở để các lập trình viên Pine khác trong cộng đồng của chúng tôi có thể sử dụng lại. Xin tri ân tác giả! Bạn có thể sử dụng thư viện này riêng tư hoặc trong các bài đăng nguồn mở khác. Tuy nhiên, bạn cần sử dụng lại mã này theo Nội quy chung.
Thông báo miễn trừ trách nhiệm
Thông tin và ấn phẩm không có nghĩa là và không cấu thành, tài chính, đầu tư, kinh doanh, hoặc các loại lời khuyên hoặc khuyến nghị khác được cung cấp hoặc xác nhận bởi TradingView. Đọc thêm trong Điều khoản sử dụng.
Thư viện Pine
Theo tinh thần TradingView thực sự, tác giả đã xuất bản mã Pine này dưới dạng thư viện nguồn mở để các lập trình viên Pine khác trong cộng đồng của chúng tôi có thể sử dụng lại. Xin tri ân tác giả! Bạn có thể sử dụng thư viện này riêng tư hoặc trong các bài đăng nguồn mở khác. Tuy nhiên, bạn cần sử dụng lại mã này theo Nội quy chung.
Thông báo miễn trừ trách nhiệm
Thông tin và ấn phẩm không có nghĩa là và không cấu thành, tài chính, đầu tư, kinh doanh, hoặc các loại lời khuyên hoặc khuyến nghị khác được cung cấp hoặc xác nhận bởi TradingView. Đọc thêm trong Điều khoản sử dụng.