Skip to content

Commit cce0a0a

Browse files
committed
Add supertrend to readme
1 parent f8d37ee commit cce0a0a

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pip install pyindicators
2929
* [Weighted Moving Average (WMA)](#weighted-moving-average-wma)
3030
* [Simple Moving Average (SMA)](#simple-moving-average-sma)
3131
* [Exponential Moving Average (EMA)](#exponential-moving-average-ema)
32+
* [SuperTrend Clustering](#supertrend-clustering)
33+
* [SuperTrend Basic](#supertrend-basic)
3234
* [Momentum and Oscillators](#momentum-and-oscillators)
3335
* [Moving Average Convergence Divergence (MACD)](#moving-average-convergence-divergence-macd)
3436
* [Relative Strength Index (RSI)](#relative-strength-index-rsi)
@@ -904,6 +906,118 @@ pd_df.tail(10)
904906

905907
![NADARAYA_WATSON_ENVELOPE](https://github.com/coding-kitties/PyIndicators/blob/main/static/images/indicators/nadaraya_watson_envelope.png)
906908

909+
### Trend Following
910+
911+
Indicators that combine trend detection with adaptive trailing stops.
912+
913+
#### SuperTrend Clustering
914+
915+
The SuperTrend Clustering indicator uses K-means clustering to optimize the ATR multiplier factor for the SuperTrend calculation. It computes multiple SuperTrend variations with different factors, evaluates their performance, and clusters them into "best", "average", and "worst" groups. The best-performing factor is then used to generate an adaptive trailing stop with buy/sell signals.
916+
917+
Based on the LuxAlgo SuperTrend AI indicator concept.
918+
919+
```python
920+
def supertrend_clustering(
921+
data: Union[PdDataFrame, PlDataFrame],
922+
atr_length: int = 10,
923+
min_mult: float = 1.0,
924+
max_mult: float = 5.0,
925+
step: float = 0.5,
926+
perf_alpha: float = 10.0,
927+
from_cluster: str = 'best',
928+
max_iter: int = 1000,
929+
max_data: int = 10000
930+
) -> Union[PdDataFrame, PlDataFrame]:
931+
```
932+
933+
Returns the following columns:
934+
- `supertrend`: The optimized SuperTrend trailing stop
935+
- `supertrend_trend`: Current trend (1=bullish, 0=bearish)
936+
- `supertrend_ama`: Adaptive moving average of SuperTrend
937+
- `supertrend_perf_idx`: Performance index (0–1 scale)
938+
- `supertrend_factor`: Currently used ATR factor
939+
- `supertrend_signal`: 1=buy signal, -1=sell signal, 0=no signal
940+
941+
Example
942+
943+
```python
944+
from investing_algorithm_framework import download
945+
946+
from pyindicators import supertrend_clustering, get_supertrend_stats
947+
948+
pd_df = download(
949+
symbol="btc/eur",
950+
market="binance",
951+
time_frame="1d",
952+
start_date="2023-12-01",
953+
end_date="2023-12-25",
954+
pandas=True,
955+
save=True,
956+
storage_path="./data"
957+
)
958+
959+
# Calculate SuperTrend Clustering
960+
pd_df = supertrend_clustering(
961+
pd_df,
962+
atr_length=14,
963+
min_mult=2.0,
964+
max_mult=6.0,
965+
step=0.5,
966+
perf_alpha=14.0,
967+
from_cluster='best',
968+
max_data=500
969+
)
970+
971+
# Get statistics
972+
stats = get_supertrend_stats(pd_df)
973+
print(stats)
974+
pd_df.tail(10)
975+
```
976+
977+
![SUPERTREND_CLUSTERING](https://github.com/coding-kitties/PyIndicators/blob/main/static/images/indicators/supertrend_clustering.png)
978+
979+
#### SuperTrend Basic
980+
981+
The basic SuperTrend indicator uses a fixed ATR multiplier factor to create a trend-following trailing stop. When the price is above the SuperTrend line the trend is bullish; when below, bearish. Trend changes generate buy/sell signals.
982+
983+
```python
984+
def supertrend_basic(
985+
data: Union[PdDataFrame, PlDataFrame],
986+
atr_length: int = 10,
987+
factor: float = 3.0
988+
) -> Union[PdDataFrame, PlDataFrame]:
989+
```
990+
991+
Returns the following columns:
992+
- `supertrend`: The SuperTrend trailing stop value
993+
- `supertrend_trend`: Current trend (1=bullish, 0=bearish)
994+
- `supertrend_upper`: Upper band
995+
- `supertrend_lower`: Lower band
996+
- `supertrend_signal`: 1=buy signal, -1=sell signal, 0=no signal
997+
998+
Example
999+
1000+
```python
1001+
from investing_algorithm_framework import download
1002+
1003+
from pyindicators import supertrend_basic
1004+
1005+
pd_df = download(
1006+
symbol="btc/eur",
1007+
market="binance",
1008+
time_frame="1d",
1009+
start_date="2023-12-01",
1010+
end_date="2023-12-25",
1011+
pandas=True,
1012+
save=True,
1013+
storage_path="./data"
1014+
)
1015+
1016+
# Calculate basic SuperTrend
1017+
pd_df = supertrend_basic(pd_df, atr_length=10, factor=3.0)
1018+
pd_df.tail(10)
1019+
```
1020+
9071021
### Support and Resistance
9081022

9091023
Indicators that help identify potential support and resistance levels in the market.

0 commit comments

Comments
 (0)