PINE LIBRARY
ema_stoploss_lib

Library "ema_stoploss_lib"
This library derives stop-loss levels from a dynamic list of EMA lengths. It computes each EMA internally (so dynamic lengths are allowed), keeps strict side filtering (long: only EMAs below the source; short: only EMAs above), sorts by distance to the source, and returns the n-th nearest value plus the original index of that EMA length.
get_stop_loss(index)
Initializes (once) a default length list:
21, 50, 100, 200, 250, 500, 750, 1000.
Returns: [sl_buy, sl_sell, nearest_buy_idx, nearest_sell_idx]
sl_buy / sl_sell: selected EMA values
nearest_buy_idx / nearest_sell_idx: 0-based indices in the original lensArr
Parameters & Notes
Index (input in the example; default 2) is 0-based:
0 = nearest, 1 = second nearest, 2 = third, etc.
If there aren’t enough EMAs on the requested side, the value becomes na (plot will skip that bar).
Strict filtering means no fallback to the opposite side.
Performance:
EMA updates are O(n) per bar (n = number of lengths).
Sorting is O(k²) (k = candidates on the chosen side) — negligible for small lists.
This library derives stop-loss levels from a dynamic list of EMA lengths. It computes each EMA internally (so dynamic lengths are allowed), keeps strict side filtering (long: only EMAs below the source; short: only EMAs above), sorts by distance to the source, and returns the n-th nearest value plus the original index of that EMA length.
get_stop_loss(index)
Initializes (once) a default length list:
21, 50, 100, 200, 250, 500, 750, 1000.
Returns: [sl_buy, sl_sell, nearest_buy_idx, nearest_sell_idx]
sl_buy / sl_sell: selected EMA values
nearest_buy_idx / nearest_sell_idx: 0-based indices in the original lensArr
Parameters & Notes
Index (input in the example; default 2) is 0-based:
0 = nearest, 1 = second nearest, 2 = third, etc.
If there aren’t enough EMAs on the requested side, the value becomes na (plot will skip that bar).
Strict filtering means no fallback to the opposite side.
Performance:
EMA updates are O(n) per bar (n = number of lengths).
Sorting is O(k²) (k = candidates on the chosen side) — negligible for small lists.
Biblioteca do Pine
No verdadeiro espirito do TradingView, o autor desse código Pine o publicou como uma biblioteca de código aberto, para que outros programadores Pine da nossa comunidade possam reusa-los. Parabéns ao autor! Você pode usar essa biblioteca privadamente ou em outras publicações de código aberto, mas a reutilização desse código em publicações é regida pelas Regras da Casa.
Aviso legal
As informações e publicações não devem ser e não constituem conselhos ou recomendações financeiras, de investimento, de negociação ou de qualquer outro tipo, fornecidas ou endossadas pela TradingView. Leia mais em Termos de uso.
Biblioteca do Pine
No verdadeiro espirito do TradingView, o autor desse código Pine o publicou como uma biblioteca de código aberto, para que outros programadores Pine da nossa comunidade possam reusa-los. Parabéns ao autor! Você pode usar essa biblioteca privadamente ou em outras publicações de código aberto, mas a reutilização desse código em publicações é regida pelas Regras da Casa.
Aviso legal
As informações e publicações não devem ser e não constituem conselhos ou recomendações financeiras, de investimento, de negociação ou de qualquer outro tipo, fornecidas ou endossadas pela TradingView. Leia mais em Termos de uso.