Create supertrend_agent.py
Browse files- supertrend_agent.py +20 -0
supertrend_agent.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
class SupertrendAgent:
|
| 5 |
+
def run(self, df, period=7, multiplier=3):
|
| 6 |
+
df = df.copy()
|
| 7 |
+
hl2 = (df['High'] + df['Low']) / 2
|
| 8 |
+
atr = df['High'].rolling(period).max() - df['Low'].rolling(period).min()
|
| 9 |
+
upper_band = hl2 + multiplier * atr
|
| 10 |
+
lower_band = hl2 - multiplier * atr
|
| 11 |
+
current_close = df['Close'].iloc[-1]
|
| 12 |
+
|
| 13 |
+
if current_close > upper_band.iloc[-1]:
|
| 14 |
+
signal = "Bullish"
|
| 15 |
+
elif current_close < lower_band.iloc[-1]:
|
| 16 |
+
signal = "Bearish"
|
| 17 |
+
else:
|
| 18 |
+
signal = "Neutral"
|
| 19 |
+
|
| 20 |
+
return {"signal": signal}
|