-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.py
More file actions
62 lines (49 loc) · 1.88 KB
/
Copy pathengine.py
File metadata and controls
62 lines (49 loc) · 1.88 KB
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
import yfinance as yf
import numpy as np
import pandas as pd
def add_indicators(df):
if df.empty:
return df
df['SMA_20'] = df['Close'].rolling(20).mean()
df['EMA_12'] = df['Close'].ewm(span=12).mean()
df['EMA_26'] = df['Close'].ewm(span=26).mean()
df['EMA_50'] = df['Close'].ewm(span=50).mean()
delta = df['Close'].diff()
gain = delta.where(delta > 0, 0).rolling(14).mean()
loss = (-delta.where(delta < 0, 0)).rolling(14).mean()
rs = gain / loss
df['RSI_14'] = 100 - (100 / (1 + rs))
df['MACD'] = df['EMA_12'] - df['EMA_26']
df['BBM'] = df['Close'].rolling(20).mean()
std = df['Close'].rolling(20).std()
df['BBU'] = df['BBM'] + (2 * std)
df['BBL'] = df['BBM'] - (2 * std)
df['BB_width'] = df['BBU'] - df['BBL']
df['TR'] = np.maximum(
df['High'] - df['Low'],
np.maximum(
abs(df['High'] - df['Close'].shift()),
abs(df['Low'] - df['Close'].shift())
)
)
df['ATR'] = df['TR'].rolling(14).mean()
df['OBV'] = (np.sign(df['Close'].diff()) * df['Volume']).cumsum()
df['VWAP'] = (df['Close'] * df['Volume']).cumsum() / df['Volume'].cumsum()
df['Momentum'] = df['Close'] - df['Close'].shift(4)
df['Volatility'] = df['Close'].rolling(10).std()
df['High_Low_Range'] = df['High'] - df['Low']
df['Volume_Change'] = df['Volume'].pct_change()
df['Price_Change'] = df['Close'].pct_change()
df['EMA_diff'] = df['EMA_12'] - df['EMA_26']
df['Close_to_SMA'] = df['Close'] / df['SMA_20'] - 1
df['SMA_diff'] = df['Close'] - df['SMA_20']
df['Hour'] = df.index.hour
df['DayOfWeek'] = df.index.dayofweek
df = df.replace([np.inf, -np.inf], np.nan)
df = df.dropna()
return df
def get_live_data(ticker):
df = yf.download(ticker, period='60d', interval='1h')
df.columns = df.columns.get_level_values(0)
df = add_indicators(df)
return df