-
Notifications
You must be signed in to change notification settings - Fork 2
/
technical-indicator.py
130 lines (101 loc) · 4.02 KB
/
technical-indicator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import yfinance as yf
import pandas as pd
"""
RSI:
Buy: RSI < 30
Sell: RSI > 70
MACD:
Buy: MACD crosses above the signal line.
Sell: MACD crosses below the signal line.
Stochastic Oscillator:
Buy: %K < 20 (oversold).
Sell: %K > 80 (overbought).
ATR (Average True Range):
ATR doesn’t provide direct buy/sell signals but indicates volatility. A high ATR suggests high volatility.
"""
# RSI Calculation
def calculate_rsi(data, period=14):
delta = data['Close'].diff(1)
gain = delta.where(delta > 0, 0)
loss = -delta.where(delta < 0, 0)
avg_gain = gain.rolling(window=period).mean()
avg_loss = loss.rolling(window=period).mean()
rs = avg_gain / avg_loss
rsi = 100 - (100 / (1 + rs))
return rsi
# MACD Calculation
def calculate_macd(data, short_window=12, long_window=26, signal_window=9):
short_ema = data['Close'].ewm(span=short_window, adjust=False).mean()
long_ema = data['Close'].ewm(span=long_window, adjust=False).mean()
macd = short_ema - long_ema
signal_line = macd.ewm(span=signal_window, adjust=False).mean()
return macd, signal_line
# Stochastic Oscillator Calculation
def calculate_stochastic(data, period=14, smooth_k=3, smooth_d=3):
low_min = data['Low'].rolling(window=period).min()
high_max = data['High'].rolling(window=period).max()
stochastic_k = 100 * (data['Close'] - low_min) / (high_max - low_min)
stochastic_d = stochastic_k.rolling(window=smooth_d).mean()
return stochastic_k, stochastic_d
# ATR Calculation
def calculate_atr(data, period=14):
high_low = data['High'] - data['Low']
high_close = (data['High'] - data['Close'].shift()).abs()
low_close = (data['Low'] - data['Close'].shift()).abs()
tr = pd.concat([high_low, high_close, low_close], axis=1).max(axis=1)
atr = tr.rolling(window=period).mean()
return atr
# Stock Data Fetching
def get_stock_data(ticker, period='3mo', interval='1d'):
stock_data = yf.download(ticker, period=period, interval=interval)
return stock_data
# Signal Check for RSI
def check_rsi_signal(rsi):
if rsi.iloc[-1] > 70:
return "RSI Signal: Sell (Overbought)"
elif rsi.iloc[-1] < 30:
return "RSI Signal: Buy (Oversold)"
else:
return "RSI Signal: No clear signal"
# Signal Check for MACD
def check_macd_signal(macd, signal_line):
if macd.iloc[-1] > signal_line.iloc[-1] and macd.iloc[-2] < signal_line.iloc[-2]:
return "MACD Signal: Buy"
elif macd.iloc[-1] < signal_line.iloc[-1] and macd.iloc[-2] > signal_line.iloc[-2]:
return "MACD Signal: Sell"
else:
return "MACD Signal: No clear signal"
# Signal Check for Stochastic Oscillator
def check_stochastic_signal(stochastic_k, stochastic_d):
if stochastic_k.iloc[-1] > 80:
return "Stochastic Oscillator: Sell (Overbought)"
elif stochastic_k.iloc[-1] < 20:
return "Stochastic Oscillator: Buy (Oversold)"
else:
return "Stochastic Oscillator: No clear signal"
# Signal Check for ATR
def check_atr_signal(atr):
# ATR is often used as a volatility measure, so we do not generate direct buy/sell signals
# We can print the ATR value and suggest that high ATR indicates high volatility
return f"ATR (Average True Range) Value: {atr.iloc[-1]:.2f} (High ATR implies high volatility)"
# Main function
def analyze_stock(ticker):
data = get_stock_data(ticker)
# Calculate indicators
rsi = calculate_rsi(data)
macd, signal_line = calculate_macd(data)
stochastic_k, stochastic_d = calculate_stochastic(data)
atr = calculate_atr(data)
# Get individual signals
rsi_signal = check_rsi_signal(rsi)
macd_signal = check_macd_signal(macd, signal_line)
stochastic_signal = check_stochastic_signal(stochastic_k, stochastic_d)
atr_signal = check_atr_signal(atr)
# Print signals
print(f"Stock: {ticker}")
print(rsi_signal)
print(macd_signal)
print(stochastic_signal)
print(atr_signal)
# Example usage
analyze_stock("MSFT") # You can replace 'AAPL' with any stock ticker