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.
Added:
stdev(source, length)
Parameters:
source (float)
length (simple int)
bb(source, length, mult)
Parameters:
source (float)
length (simple int)
mult (simple float)
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
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).
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
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
- removed label that caused 'side effects' error on request security call
- added HTF example
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
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).
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.
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).
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)
Libreria Pine
In pieno spirito TradingView, l'autore ha pubblicato questo codice Pine come libreria open-source in modo che altri programmatori Pine della nostra comunità possano riutilizzarlo. Complimenti all'autore! È possibile utilizzare questa libreria privatamente o in altre pubblicazioni open-source, ma il riutilizzo di questo codice in una pubblicazione è regolato dal nostro Regolamento.
Declinazione di responsabilità
Libreria Pine
In pieno spirito TradingView, l'autore ha pubblicato questo codice Pine come libreria open-source in modo che altri programmatori Pine della nostra comunità possano riutilizzarlo. Complimenti all'autore! È possibile utilizzare questa libreria privatamente o in altre pubblicazioni open-source, ma il riutilizzo di questo codice in una pubblicazione è regolato dal nostro Regolamento.