ismailcarlik

ottlib

ismailcarlik Zaktualizowano   
Library "ottlib"


█ OVERVIEW

This library contains functions for the calculation of the OTT (Optimized Trend Tracker) and its variants, originally created by Anıl Özekşi (Anil_Ozeksi). Special thanks to him for the concept and to Kıvanç Özbilgiç (KivancOzbilgic) and dg_factor (dg_factor) for adapting them to Pine Script.


█ WHAT IS "OTT"

The OTT (Optimized Trend Tracker) is a highly customizable and very effective trend-following indicator that relies on moving averages and a trailing stop at its core. Moving averages help reduce noise by smoothing out sudden price movements in the markets, while trailing stops assist in detecting trend reversals with precision. Initially developed as a noise-free trailing stop, the current variants of OTT range from rapid trend reversal detection to long-term trend confirmation, thanks to its extensive customizability.

It's well-known variants are:
  • OTT (Optimized Trend Tracker).
  • TOTT (Twin OTT).
  • OTT Channels.
  • RISOTTO (RSI OTT).
  • SOTT (Stochastic OTT).
  • HOTT & LOTT (Highest & Lowest OTT)
  • ROTT (Relative OTT)
  • FT (Original name is Fırsatçı Trend in Turkish which translates to Opportunist Trend)


█ LIBRARY FEATURES

This library has been prepared in accordance with the style, coding, and annotation standards of Pine Script version 5. As a result, explanations and examples will appear when users hover over functions or enter function parameters in the editor.


█ USAGE

Usage of this library is very simple. Just import it to your script with the code below and use its functions.

import ismailcarlik/ottlib/1 as ottlib


█ FUNCTIONS

• f_vidya(source, length, cmoLength)
  Short Definition: Chande's Variable Index Dynamic Average (VIDYA).
  Details: This function computes Chande's Variable Index Dynamic Average (VIDYA), which serves as the original moving average for OTT. The 'length' parameter determines the number of bars used to calculate the average of the given source. Lower values result in less smoothing of prices, while higher values lead to greater smoothing. While primarily used internally in this library, it has been made available for users who wish to utilize it as a moving average or use in custom OTT implementations.

Parameters:
   source (float): (series float) Series of values to process.
   length (simple int): (simple int) Number of bars to lookback.
   cmoLength (simple int): (simple int) Number of bars to lookback for calculating CMO. Default value is `9`.
Returns: (float) Calculated average of `source` for `length` bars back.

  Example:
vidyaValue = ottlib.f_vidya(source = close, length = 20)

plot(vidyaValue, color = color.blue)

• f_mostTrail(source, multiplier)
  Short Definition: Calculates trailing stop value.
  Details: This function calculates the trailing stop value for a given source and the percentage. The 'multiplier' parameter defines the percentage of the trailing stop. Lower values are beneficial for catching short-term reversals, while higher values aid in identifying long-term trends. Although only used once internally in this library, it has been made available for users who wish to utilize it as a traditional trailing stop or use in custom OTT implementations.

  Parameters:
    source (float): (series int/float) Series of values to process.
    multiplier (simple float): (simple float) Percent of trailing stop.

  Returns: (float) Calculated value of trailing stop.

  Example:
emaValue = ta.ema(source = close, length = 14)
mostValue = ottlib.f_mostTrail(source = emaValue, multiplier = 2.0)

plot(mostValue, color = emaValue >= mostValue ? color.green : color.red)

• f_ottTrail(source, multiplier)
  Short Definition: Calculates OTT-specific trailing stop value.
  Details: This function calculates the trailing stop value for a given source in the manner used in OTT. Unlike a traditional trailing stop, this function modifies the traditional trailing stop value from two bars prior by adjusting it further with half the specified percentage. The 'multiplier' parameter defines the percentage of the trailing stop. Lower values are beneficial for catching short-term reversals, while higher values aid in identifying long-term trends. Although primarily used internally in this library, it has been made available for users who wish to utilize it as a trailing stop or use in custom OTT implementations.

  Parameters:
    source (float): (series int/float) Series of values to process.
    multiplier (simple float): (simple float) Percent of trailing stop.

  Returns: (float) Calculated value of OTT-specific trailing stop.

  Example:
vidyaValue = ottlib.f_vidya(source = close, length = 20)
ottValue = ottlib.f_ottTrail(source = vidyaValue, multiplier = 1.5)

plot(ottValue, color = vidyaValue >= ottValue ? color.green : color.red)

• ott(source, length, multiplier)
  Short Definition: Calculates OTT (Optimized Trend Tracker).
  Details: The OTT consists of two lines. The first, known as the "Support Line", is the VIDYA of the given source. The second, called the "OTT Line", is the trailing stop based on the Support Line. The market is considered to be in an uptrend when the Support Line is above the OTT Line, and in a downtrend when it is below.

  Parameters:
    source (float): (series float) Series of values to process. Default value is `close`.
    length (simple int): (simple int) Number of bars to lookback. Default value is `2`.
    multiplier (simple float): (simple float) Percent of trailing stop. Default value is `1.4`.

  Returns: ([float, float]) Tuple of `supportLine` and `ottLine`.

  Example:
[supportLine, ottLine] = ottlib.ott(source = close, length = 2, multiplier = 1.4)

longCondition = ta.crossover(supportLine, ottLine)
shortCondition = ta.crossunder(supportLine, ottLine)

• tott(source, length, multiplier, bandsMultiplier)
  Short Definition: Calculates TOTT (Twin OTT).
  Details: TOTT consists of three lines: the "Support Line," which is the VIDYA of the given source; the "Upper Line," a trailing stop of the Support Line adjusted with an added multiplier; and the "Lower Line," another trailing stop of the Support Line, adjusted with a reduced multiplier. The market is considered in an uptrend if the Support Line is above the Upper Line and in a downtrend if it is below the Lower Line.

  Parameters:
    source (float): (series float) Series of values to process. Default value is `close`.
    length (simple int): (simple int) Number of bars to lookback. Default value is `40`.
    multiplier (simple float): (simple float) Percent of trailing stop. Default value is `0.6`.
    bandsMultiplier (simple float): Multiplier for bands. Default value is `0.0006`.

  Returns: ([float, float, float]) Tuple of `supportLine`, `upperLine` and `lowerLine`.

  Example:
[supportLine, upperLine, lowerLine] = ottlib.tott(source = close, length = 40, multiplier = 0.6, bandsMultiplier = 0.0006)

longCondition = ta.crossover(supportLine, upperLine)
shortCondition = ta.crossunder(supportLine, lowerLine)

• ott_channel(source, length, multiplier, ulMultiplier, llMultiplier)
  Short Definition: Calculates OTT Channels.
  Details: OTT Channels comprise nine lines. The central line, known as the "Mid Line," is the OTT of the given source's VIDYA. The remaining lines are positioned above and below the Mid Line, shifted by specified multipliers.

  Parameters:
    source (float): (series float) Series of values to process. Default value is `close`
    length (simple int): (simple int) Number of bars to lookback. Default value is `2`
    multiplier (simple float): (simple float) Percent of trailing stop. Default value is `1.4`
    ulMultiplier (simple float): (simple float) Multiplier for upper line. Default value is `0.01`
    llMultiplier (simple float): (simple float) Multiplier for lower line. Default value is `0.01`

  Returns: ([float, float, float, float, float, float, float, float, float]) Tuple of `ul4`, `ul3`, `ul2`, `ul1`, `midLine`, `ll1`, `ll2`, `ll3`, `ll4`.

  Example:
[ul4, ul3, ul2, ul1, midLine, ll1, ll2, ll3, ll4] = ottlib.ott_channel(source = close, length = 2, multiplier = 1.4, ulMultiplier = 0.01, llMultiplier = 0.01)

• risotto(source, length, rsiLength, multiplier)
  Short Definition: Calculates RISOTTO (RSI OTT).
  Details: RISOTTO comprised of two lines: the "Support Line," which is the VIDYA of the given source's RSI value, calculated based on the length parameter, and the "RISOTTO Line," a trailing stop of the Support Line. The market is considered in an uptrend when the Support Line is above the RISOTTO Line, and in a downtrend if it is below.

  Parameters:
    source (float): (series float) Series of values to process. Default value is `close`.
    length (simple int): (simple int) Number of bars to lookback. Default value is `50`.
    rsiLength (simple int): (simple int) Number of bars used for RSI calculation. Default value is `100`.
    multiplier (simple float): (simple float) Percent of trailing stop. Default value is `0.2`.

  Returns: ([float, float]) Tuple of `supportLine` and `risottoLine`.

  Example:
[supportLine, risottoLine] = ottlib.risotto(source = close, length = 50, rsiLength = 100, multiplier = 0.2)

longCondition = ta.crossover(supportLine, risottoLine)
shortCondition = ta.crossunder(supportLine, risottoLine)

• sott(source, kLength, dLength, multiplier)
  Short Definition: Calculates SOTT (Stochastic OTT).
  Details: SOTT is comprised of two lines: the "Support Line," which is the VIDYA of the given source's Stochastic value, based on the %K and %D lengths, and the "SOTT Line," serving as the trailing stop of the Support Line. The market is considered in an uptrend when the Support Line is above the SOTT Line, and in a downtrend when it is below.

  Parameters:
    source (float): (series float) Series of values to process. Default value is `close`.
    kLength (simple int): (simple int) Stochastic %K length. Default value is `500`.
    dLength (simple int): (simple int) Stochastic %D length. Default value is `200`.
    multiplier (simple float): (simple float) Percent of trailing stop. Default value is `0.5`.

  Returns: ([float, float]) Tuple of `supportLine` and `sottLine`.

  Example:
[supportLine, sottLine] = ottlib.sott(source = close, kLength = 500, dLength = 200, multiplier = 0.5)

longCondition = ta.crossover(supportLine, sottLine)
shortCondition = ta.crossunder(supportLine, sottLine)

• hottlott(length, multiplier)
  Short Definition: Calculates HOTT & LOTT (Highest & Lowest OTT).
  Details: HOTT & LOTT are composed of two lines: the "HOTT Line", which is the OTT of the highest price's VIDYA, and the "LOTT Line", the OTT of the lowest price's VIDYA. A high price surpassing the HOTT Line can be considered a long signal, while a low price dropping below the LOTT Line may indicate a short signal.

  Parameters:
    length (simple int): (simple int) Number of bars to lookback. Default value is `20`.
    multiplier (simple float): (simple float) Percent of trailing stop. Default value is `0.6`.

  Returns: ([float, float]) Tuple of `hottLine` and `lottLine`.

  Example:
[hottLine, lottLine] = ottlib.hottlott(length = 20, multiplier = 0.6)

longCondition = ta.crossover(high, hottLine)
shortCondition = ta.crossunder(low, lottLine)

• rott(source, length, multiplier)
  Short Definition: Calculates ROTT (Relative OTT).
  Details: ROTT comprises two lines: the "Support Line", which is the VIDYA of the given source, and the "ROTT Line", the OTT of the Support Line's VIDYA. The market is considered in an uptrend if the Support Line is above the ROTT Line, and in a downtrend if it is below. ROTT is similar to OTT, but the key difference is that the ROTT Line is derived from the VIDYA of two bars of Support Line, not directly from it.

  Parameters:
    source (float): (series float) Series of values to process. Default value is `close`.
    length (simple int): (simple int) Number of bars to lookback. Default value is `200`.
    multiplier (simple float): (simple float) Percent of trailing stop. Default value is `0.1`.

  Returns: ([float, float]) Tuple of `supportLine` and `rottLine`.

  Example:
[supportLine, rottLine] = ottlib.rott(source = close, length = 200, multiplier = 0.1)

isUpTrend = supportLine > rottLine
isDownTrend = supportLine < rottLine

• ft(source, length, majorMultiplier, minorMultiplier)
  Short Definition: Calculates Fırsatçı Trend (Opportunist Trend).
  Details: FT is comprised of two lines: the "Support Line", which is the VIDYA of the given source, and the "FT Line", a trailing stop of the Support Line calculated using both minor and major trend values. The market is considered in an uptrend when the Support Line is above the FT Line, and in a downtrend when it is below.

  Parameters:
    source (float): (series float) Series of values to process. Default value is `close`.
    length (simple int): (simple int) Number of bars to lookback. Default value is `30`.
    majorMultiplier (simple float): (simple float) Percent of major trend. Default value is `3.6`.
    minorMultiplier (simple float): (simple float) Percent of minor trend. Default value is `1.8`.

  Returns: ([float, float]) Tuple of `supportLine` and `ftLine`.

  Example:
[supportLine, ftLine] = ottlib.ft(source = close, length = 30, majorMultiplier = 3.6, minorMultiplier = 1.8)

longCondition = ta.crossover(supportLine, ftLine)
shortCondition = ta.crossunder(supportLine, ftLine)


█ CUSTOM OTT CREATION

Users can create custom OTT implementations using f_ottTrail function in this library. The example code which uses EMA of 7 period as moving average and calculates OTT based of it is below.

Source Code:
//@version=5
indicator("Custom OTT", shorttitle = "COTT", overlay = true)

import ismailcarlik/ottlib/1 as ottlib

src = input.source(close, title = "Source")
length = input.int(7, title = "Length", minval = 1)
multiplier = input.float(2.0, title = "Multiplier", minval = 0.1)

support = ta.ema(source = src, length = length)
ott = ottlib.f_ottTrail(source = support, multiplier = multiplier)

pSupport = plot(support, title = "Moving Average Line (Support)", color = color.blue)
pOtt = plot(ott, title = "Custom OTT Line", color = color.orange)

fillColor = support >= ott ? color.new(color.green, 60) : color.new(color.red, 60)

fill(pSupport, pOtt, color = fillColor, title = "Direction")

Result:


█ DISCLAIMER

Trading is risky and most of the day traders lose money eventually. This library and its functions are only for educational purposes and should not be construed as financial advice. Past performances does not guarantee future results.
Informacje o Wersji:
v2


█ UPDATES

  • Fixed `f_vidya()` function for more robust initialization by incorporating a conditional check to set the initial value to the Simple Moving Average (SMA) when previous VIDYA value is not available, ensuring more accurate calculations from the start. This change effectively handles edge cases where the length is set to 1, aligning the function closer to the standard VIDYA methodology and enhancing overall reliability.
  • Fixed an error at `risotto()` function that that involves calculation of `supportLine`.


█ NEW FUNCTIONS

• totto(source, length, multiplier, bandsMultiplier)
  Short Definition: Calculates TOTTO (Another version of Twin OTT).
  Details: TOTTO consists of three lines: the "Support Line," which is the VIDYA of the given source; the "Upper Line," a trailing stop of the Support Line adjusted with an added multiplier; and the "Lower Line," another trailing stop of the Support Line, adjusted with a reduced multiplier. The market is considered in an uptrend if the Support Line is above the Upper Line and in a downtrend if it is below the Lower Line. It's very much like TOTT but difference comes from calculation of VIDYA which uses half of the given length and other lines uses the OTT of the Support Line's VIDYA for 2 bar length.

  Parameters:
    source (float): (series float) Series of values to process. Default value is `close`.
    length (simple int): (simple int) Number of bars to lookback. Default value is `35`.
    multiplier (simple float): (simple float) Percent of trailing stop. Default value is `0.5`.
    bandsMultiplier (simple float): Multiplier for bands. Default value is `0.0006`.

  Returns: ([float, float, float]) Tuple of `supportLine`, `upperLine` and `lowerLine`.

  Example:
[supportLine, upperLine, lowerLine] = ottlib.totto(source = close, length = 35, multiplier = 0.5, bandsMultiplier = 0.0006)

longCondition = ta.crossover(supportLine, upperLine)
shortCondition = ta.crossunder(supportLine, lowerLine)


█ SPECIAL THANKS

Thanks to dg_factor for his precious bug reports and suggestions.

Biblioteka Pine

Działając zgodnie z prawdziwym duchem TradingView, autor opublikował ten kod Pine jako bibliotekę o otwartym kodzie źródłowym, aby inni programiści Pine z naszej społeczności mogli go ponownie wykorzystać. Brawa dla niego! Możesz korzystać z tej biblioteki prywatnie lub w innych publikacjach typu open source, ale ponowne wykorzystanie tego kodu w publikacji podlega Regulaminowi.

Wyłączenie odpowiedzialności

Informacje i publikacje przygotowane przez TradingView lub jego użytkowników, prezentowane na tej stronie, nie stanowią rekomendacji ani porad handlowych, inwestycyjnych i finansowych i nie powinny być w ten sposób traktowane ani wykorzystywane. Więcej informacji na ten temat znajdziesz w naszym Regulaminie.

Chcesz skorzystać z tej biblioteki?

Skopiuj poniższy wiersz i wklej go w swoim skrypcie.