PINE LIBRARY
更新済 ema_stoploss

Library "ema_stoploss"
What it does
A small library that builds stop-loss levels from dynamically computed EMAs. It finds EMAs strictly on the desired side of price (long: below; short: above), sorts them by distance to price, and returns the n-th nearest as your stop.
How it works
sortEMAsByDistanceStrictDyn(signal, lensArr, src, ascending)
Computes each EMA internally with the alpha formula (alpha = 2/(len+1)), so you can pass a dynamic array of lengths.
Strict side filter:
signal = 1 → only EMAs < src (below)
signal = -1 → only EMAs > src (above)
Sorts candidates by distance to src (default: nearest → farthest) and returns two arrays: EMA values and their lengths.
get_stop_loss(index) (exported)
Builds a default length array: 21, 50, 100, 200, 250, 500, 750, 1000.
Long side uses low to find the index-th nearest lower EMA.
Short side uses high to find the index-th nearest upper EMA.
Returns [sl_buy, sl_sell].
Plots
Stop-Loss Long (green): the selected lower EMA (based on low).
Stop-Loss Short (red): the selected upper EMA (based on high).
Input
Index (default 2): 0-based.
0 = nearest, 1 = second nearest, 2 = third, etc.
If there aren’t enough EMAs on the required side, the function returns na (no plot).
Why internal EMA calc?
ta.ema() doesn’t accept a series length; by updating each EMA with its alpha step every bar, the library supports arbitrary dynamic length arrays and stays bar-consistent.
Customize
Edit the list in get_stop_loss() to use your own EMA lengths.
Change ascending in sortEMAsByDistanceStrictDyn if you prefer farthest → nearest.
Use a different src if needed (e.g., close, hlc3, etc.).
The example intentionally uses low for long stops and high for short stops.
Notes
Strict side filtering: EMAs on the wrong side are ignored (no fallback).
If no EMA qualifies on a side, you’ll get na for that side.
Complexity is O(n²) for sorting, which is negligible for small EMA lists.
What it does
A small library that builds stop-loss levels from dynamically computed EMAs. It finds EMAs strictly on the desired side of price (long: below; short: above), sorts them by distance to price, and returns the n-th nearest as your stop.
How it works
sortEMAsByDistanceStrictDyn(signal, lensArr, src, ascending)
Computes each EMA internally with the alpha formula (alpha = 2/(len+1)), so you can pass a dynamic array of lengths.
Strict side filter:
signal = 1 → only EMAs < src (below)
signal = -1 → only EMAs > src (above)
Sorts candidates by distance to src (default: nearest → farthest) and returns two arrays: EMA values and their lengths.
get_stop_loss(index) (exported)
Builds a default length array: 21, 50, 100, 200, 250, 500, 750, 1000.
Long side uses low to find the index-th nearest lower EMA.
Short side uses high to find the index-th nearest upper EMA.
Returns [sl_buy, sl_sell].
Plots
Stop-Loss Long (green): the selected lower EMA (based on low).
Stop-Loss Short (red): the selected upper EMA (based on high).
Input
Index (default 2): 0-based.
0 = nearest, 1 = second nearest, 2 = third, etc.
If there aren’t enough EMAs on the required side, the function returns na (no plot).
Why internal EMA calc?
ta.ema() doesn’t accept a series length; by updating each EMA with its alpha step every bar, the library supports arbitrary dynamic length arrays and stays bar-consistent.
Customize
Edit the list in get_stop_loss() to use your own EMA lengths.
Change ascending in sortEMAsByDistanceStrictDyn if you prefer farthest → nearest.
Use a different src if needed (e.g., close, hlc3, etc.).
The example intentionally uses low for long stops and high for short stops.
Notes
Strict side filtering: EMAs on the wrong side are ignored (no fallback).
If no EMA qualifies on a side, you’ll get na for that side.
Complexity is O(n²) for sorting, which is negligible for small EMA lists.
リリースノート
v2fix na
Pineライブラリ
TradingViewの精神に則り、作者はPineコードをオープンソースライブラリとして公開し、コミュニティ内の他のPineプログラマーが再利用できるようにしました。作者に敬意を表します!このライブラリを個人的に、または他のオープンソースの投稿で使用することができますが、このコードを投稿で再利用するには、ハウスルールに準拠する必要があります。
免責事項
これらの情報および投稿は、TradingViewが提供または保証する金融、投資、取引、またはその他の種類のアドバイスや推奨を意図したものではなく、またそのようなものでもありません。詳しくは利用規約をご覧ください。
Pineライブラリ
TradingViewの精神に則り、作者はPineコードをオープンソースライブラリとして公開し、コミュニティ内の他のPineプログラマーが再利用できるようにしました。作者に敬意を表します!このライブラリを個人的に、または他のオープンソースの投稿で使用することができますが、このコードを投稿で再利用するには、ハウスルールに準拠する必要があります。
免責事項
これらの情報および投稿は、TradingViewが提供または保証する金融、投資、取引、またはその他の種類のアドバイスや推奨を意図したものではなく、またそのようなものでもありません。詳しくは利用規約をご覧ください。