PINE LIBRARY

lib_no_delay

تم تحديثه
Library "lib_no_delay"
This library contains modifications to standard functions that return na before reaching the bar of their 'length' parameter.

That is because they do not compromise speed at current time for correct results in the past. This is good for live trading in short timeframes but killing applications on Monthly / Weekly timeframes if instruments, like in crypto, do not have extensive history (why would you even trade the monthly on a meme coin ... not my decision).

Also, some functions rely on source[1] (value at previous bar), which is not available on bar 1 and therefore cascading to a na value up to the last bar ... which in turn leads to a non displaying indicator and waste of time debugging this)

Anyway ... there you go, let me know if I should add more functions.

sma(source, length)
  Parameters:
    source (float): Series of values to process.
    length (simple int): Number of bars (length).
  Returns: Simple moving average of source for length bars back.

ema(source, length)
  Parameters:
    source (float): Series of values to process.
    length (simple int): Number of bars (length).
  Returns: (float) The exponentially weighted moving average of the source.

rma(source, length)
  Parameters:
    source (float): Series of values to process.
    length (simple int): Number of bars (length).
  Returns: Exponential moving average of source with alpha = 1 / length.

atr(length)
  Function atr (average true range) returns the RMA of true range. True range is max(high - low, abs(high - close[1]), abs(low - close[1])). This adapted version extends ta.atr to start without delay at first bar and deliver usable data instead of na by averaging ta.tr(true) via manual SMA.
  Parameters:
    length (simple int): Number of bars back (length).
  Returns: Average true range.

rsi(source, length)
  Relative strength index. It is calculated using the ta.rma() of upward and downward changes of source over the last length bars. This adapted version extends ta.rsi to start without delay at first bar and deliver usable data instead of na.
  Parameters:
    source (float): Series of values to process.
    length (simple int): Number of bars back (length).
  Returns: Relative Strength Index.
ملاحظات الأخبار
v2

Added:
stdev(source, length)
  Parameters:
    source (float)
    length (simple int)

bb(source, length, mult)
  Parameters:
    source (float)
    length (simple int)
    mult (simple float)
ملاحظات الأخبار
v3

Added:
wma(source, length)
  Parameters:
    source (float)
    length (simple int)

vwma(source, length)
  Parameters:
    source (float)
    length (simple int)

get_ma(select_ma, source, length)
  Common Moving Average Selection. This function uses only adapted no-delay versions that start without delay at first bar and deliver usable data instead of na.
  Parameters:
    select_ma (simple string): function selector, one of SMA/EMA/RMA/WMA/VWMA
    source (float): Series of values to process.
    length (simple int): Number of bars back (length).
  Returns: selected Moving Average of source

Updated:
stdev(source, length)
  Standard deviation. It is calculated using the ta.stdev(). This adapted version extends ta.stdev to start without delay at first bar and deliver usable data instead of na.
  Parameters:
    source (float): Series of values to process.
    length (simple int): Number of bars back (length).
  Returns: Standard deviation

bb(source, length, mult)
  Bollinger Bands. A Bollinger Band is a technical analysis tool defined by a set of lines plotted two standard deviations (positively and negatively) away from a simple moving average (SMA) of the security's price, but can be adjusted to user preferences. This adapted version extends ta.stdev to start without delay at first bar and deliver usable data instead of na.
  Parameters:
    source (float): Series of values to process.
    length (simple int): Number of bars back (length).
    mult (simple float): Standard deviation factor.
  Returns: Standard deviation
ملاحظات الأخبار
v4

Added:
get_signal_once_per_bar(condition)
  Parameters:
    condition (bool): a condition that should only return true once per bar, (e.g. ta.crossover(ema7, ema200))
  Returns: a tuple of [bool triggered_this_bar, bool triggered_this_tick], to allow for accurate plotting (triggered this bar) as well as alert triggering (triggered this tick).
ملاحظات الأخبار
v5 added Hull Moving Average Functions

Added:
hma(source, length)
  The hma function returns the Hull Moving Average.
  Parameters:
    source (float): Series of values to process.
    length (simple int): Number of bars back (length).

ehma(source, length)
  The ehma function returns the Exponential Hull Moving Average.
  Parameters:
    source (float): Series of values to process.
    length (simple int): Number of bars back (length).

thma(source, length)
  The thma function returns the Triple Exponential Hull Moving Average.
  Parameters:
    source (float): Series of values to process.
    length (simple int): Number of bars back (length).

Updated:
get_ma(select_ma, source, length)
  Common Moving Average Selection. This function uses only adapted no-delay versions that start without delay at first bar and deliver usable data instead of na.
  Parameters:
    select_ma (simple string): function selector, one of SMA/EMA/RMA/WMA/VWMA/HMA/EHMA/THMA
    source (float): Series of values to process.
    length (simple int): Number of bars back (length).
  Returns: selected Moving Average of source
ملاحظات الأخبار
v6 fixed bug in THMA calculation
ملاحظات الأخبار
v7 performance improvement of HMA functions
ملاحظات الأخبار
v8 using enums for get_ma

Updated:
get_ma(select_ma, source, length)
  Common Moving Average Selection. This function uses only adapted no-delay versions that start without delay at first bar and deliver usable data instead of na.
  Parameters:
    select_ma (simple MovingAverage): function selector, one of SMA/EMA/RMA/WMA/VWMA/HMA/EHMA/THMA
    source (float): Series of values to process.
    length (simple int): Number of bars back (length).
  Returns: selected Moving Average of source
ملاحظات الأخبار
v9
  • removed label that caused 'side effects' error on request security call
  • added HTF example
ملاحظات الأخبار
v10 update to pine v6
ملاحظات الأخبار
v11 added no delay versions of ta.stoch, ta.macd, ta.stc and a speed optimized version of ta.stdev

Added:
stdev_fast(src, length)
  Parameters:
    src (float)
    length (simple int)

macd_custom(source, fast, slow, signal, osc_ma, sig_ma)
  MACD (moving average convergence/divergence). It is supposed to reveal changes in the strength, direction, momentum, and duration of a trend in a stock's price.\n\nReturns\nTuple of three MACD series: MACD line, signal line and histogram line. This adapted version extends ta.macd to start without delay at first bar and deliver usable data instead of na. It also allows to use custom moving average algorithms
  Parameters:
    source (float): (simple int) optional
    fast (simple int): (simple int) optional
    slow (simple int): (simple int) optional
    signal (simple int): (simple int) optional
    osc_ma (simple MovingAverage): (simple MovingAverage) optional select a custom function for the MACD calculation
    sig_ma (simple MovingAverage): (simple MovingAverage) optional select a custom function for the MACD signal calculation

macd(source, fast, slow, signal)
  MACD (moving average convergence/divergence). It is supposed to reveal changes in the strength, direction, momentum, and duration of a trend in a stock's price.\n\nReturns\nTuple of three MACD series: MACD line, signal line and histogram line. This adapted version extends ta.macd to start without delay at first bar and deliver usable data instead of na.
  Parameters:
    source (float): (simple int) optional
    fast (simple int): (simple int) optional
    slow (simple int): (simple int) optional
    signal (simple int): (simple int) optional

stoch(source, h, l, length)
  Stochastic. It is calculated by a formula: 100 * (close - lowest(low, length)) / (highest(high, length) - lowest(low, length)). This adapted version extends ta.stoch to start without delay at first bar and deliver usable data instead of na.
  Parameters:
    source (float)
    h (float)
    l (float)
    length (int)

stc(source, fast, slow, cycle, d1, d2)
  Calculates the value of the Schaff Trend Cycle indicator. This adapted version extends ta.stc to start without delay at first bar and deliver usable data instead of na.
  Parameters:
    source (float): (series int/float) optional Series of values to process. (default: close)
    fast (simple int): (simple int) optional Length for the MACD fast smoothing parameter calculation. (default: 26)
    slow (simple int): (simple int) optional Length for the MACD slow smoothing parameter calculation. (default: 50)
    cycle (simple int): (simple int) optional Number of bars for the Stochastic values (length). (default: 12)
    d1 (simple int): (simple int) optional Length for the initial %D smoothing parameter calculation. (default: 3)
    d2 (simple int): (simple int) optional Length for the final %D smoothing parameter calculation. (default: 3)
  Returns: (float) The oscillator value.

stc_fast(source, fast, slow, cycle, scale)
  Returns the Schaff Trend Cycle (STC) (similar to MACD, but faster). This adapted version extends ta.stc to start without delay at first bar and deliver usable data instead of na.
  Parameters:
    source (float): (series int/float) optional allows to calculate STC for another symbol or timeframe by passing its close series (obtain with request.security) (default: close)
    fast (simple int): (simple int) optional fast moving average length (default: 23)
    slow (simple int): (simple int) optional slow moving average length (default: 50)
    cycle (simple int): (simple int) optional stochastic length (default: 10)
    scale (simple float): (simple float) optional if you want to tweak the speed the trend changes. (default: 0.5)
  Returns: float stc
ملاحظات الأخبار
v12 added directional movement index

Added:
dmi(di_length, adx_smoothing)
  The dmi function returns the directional movement index. This adapted version extends ta.dmi to start without delay at first bar and deliver usable data instead of na (will converge with builtin version at difference of max 0.1 amongst all values).
  Parameters:
    di_length (simple int)
    adx_smoothing (simple int)
  Returns: Tuple of three DMI series: Positive Directional Movement (+DI), Negative Directional Movement (-DI) and Average Directional Movement Index (ADX).
ملاحظات الأخبار
v13 improved sma and wma on first bar(s) , added mean deviation, fixed stdev on first bar(s), added missing display toggle for DMI

Added:
dev(source, length)
  Measure of difference (averaged) between the series and it's ta.sma. This adapted version extends ta.dev to start without delay at first bar and deliver usable data instead of na.
  Parameters:
    source (float): Series of values to process.
    length (simple int): Number of bars back (length).
  Returns: Deviation of source for length bars back.
ملاحظات الأخبار
v14 fixed bug in dev, allowing for float length in ema
ملاحظات الأخبار
v15 added Directional Volume Index

Added:
dvi(di_length, advx_smoothing)
  The dvi function returns the directional volume index (DVI). This adapted version uses volume as base of the directional index (DI), unlike the Directional Movement Index (DMI) which uses price.
  Parameters:
    di_length (simple int): lookback period for the average volume for comparison with the directional volume (DV) to calculate the directional volume index (DVI)
    advx_smoothing (simple int): smoothing distance for the average directional volume index (ADVX)
  Returns: Tuple of three DVI series: Positive Directional Movement (+DVI), Negative Directional Movement (-DVI) and Average Directional Movement Index (ADVX).

Updated:
dmi(di_length, adx_smoothing)
  The dmi function returns the directional movement index (DMI). This adapted version extends ta.dmi to start without delay at first bar and deliver usable data instead of na (will converge with builtin version at difference of max 0.1 amongst all values).
  Parameters:
    di_length (simple int): lookback period for the average true range (ATR) for comparison with the current true range (TR) to calculate the directional index (DI)
    adx_smoothing (simple int): smoothing distance for the average directional index (ADX)
  Returns: Tuple of three DMI series: Positive Directional Movement (+DI), Negative Directional Movement (-DI) and Average Directional Movement Index (ADX).
ملاحظات الأخبار
v16 fixed a rounding error in thma for length < 6
ملاحظات الأخبار
v17 added linreg and linreg_values (including slope and intercept to draw a regression line over last 'length' bars (with optional offset)

Added:
linreg_values(source, length, offset)
  Linear regression curve. A line that best fits the prices specified over a user-defined time period. It is calculated using the least squares method. The result of this function is calculated using the formula: linreg = intercept + slope * (length - 1 - offset), where intercept and slope are the values calculated with the least squares method on source series. This adapted version extends ta.linreg to start without delay at first bar and deliver usable data instead of na.
  Parameters:
    source (float): Series of values to process.
    length (simple int): Number of bars back (length)
    offset (simple int): Offset.
  Returns: a tuple of [regression_line, slope, intercept] (for a straight regression line over a measured amount of data (bars) use x1: bar_index[length+offset] y1: intercept + slope * bar_index[length+offset] x2: bar_index[offset] y2: regression_line)

linreg(source, length, offset)
  Linear regression curve. A line that best fits the prices specified over a user-defined time period. It is calculated using the least squares method. The result of this function is calculated using the formula: linreg = intercept + slope * (length - 1 - offset), where intercept and slope are the values calculated with the least squares method on source series. This adapted version extends ta.linreg to start without delay at first bar and deliver usable data instead of na.
  Parameters:
    source (float): Series of values to process.
    length (simple int): Number of bars back (length).
    offset (simple int): Offset. (according to default implementation this using offset bar_indexes for current value calculation ... not sure that's intended, expected behavior would be an x shift so I can use it to calculate a regression line for a past range of candles -> use linreg_values for that)
  Returns: The regression line (to receive slope and intercept values as well, call linreg_values)
ملاحظات الأخبار
v18 updated examples
ATRatr2MATHmoving_averagerma2rsi2sma2statisticstechindicator

مكتبة باين

كمثال للقيم التي تتبناها TradingView، نشر المؤلف شيفرة باين كمكتبة مفتوحة المصدر بحيث يمكن لمبرمجي باين الآخرين من مجتمعنا استخدامه بحرية. تحياتنا للمؤلف! يمكنك استخدام هذه المكتبة بشكل خاص أو في منشورات أخرى مفتوحة المصدر، ولكن إعادة استخدام هذا الرمز في المنشور تحكمه قوانين الموقع.

إخلاء المسؤولية