demo_deploy_streamlit_app / src /utils /technical_indicators.py
thinh-vu's picture
Upload 12 files
021d91d verified
Raw
History Blame Contribute Delete
1.72 kB
import pandas as pd
import numpy as np
def calculate_sma(data, window):
"""
Tính toán Simple Moving Average (SMA)
Args:
data: Series dữ liệu giá
window: Cửa sổ tính toán
Returns:
Series chứa giá trị SMA
"""
return data.rolling(window=window).mean()
def calculate_ema(data, window):
"""
Tính toán Exponential Moving Average (EMA)
Args:
data: Series dữ liệu giá
window: Cửa sổ tính toán
Returns:
Series chứa giá trị EMA
"""
return data.ewm(span=window).mean()
def calculate_rsi(data, window=14):
"""
Tính toán Relative Strength Index (RSI)
Args:
data: Series dữ liệu giá
window: Cửa sổ tính toán (mặc định 14)
Returns:
Series chứa giá trị RSI
"""
delta = data.diff()
gain = (delta.where(delta > 0, 0)).rolling(window=window).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=window).mean()
rs = gain / loss
return 100 - (100 / (1 + rs))
def add_technical_indicators(price_data):
"""
Thêm các chỉ báo kỹ thuật vào DataFrame giá
Args:
price_data: DataFrame chứa dữ liệu giá
Returns:
DataFrame với các chỉ báo kỹ thuật đã được thêm vào
"""
df = price_data.copy()
# Thêm các chỉ báo
df['SMA_20'] = calculate_sma(df['close'], 20)
df['SMA_50'] = calculate_sma(df['close'], 50)
df['EMA_12'] = calculate_ema(df['close'], 12)
df['EMA_26'] = calculate_ema(df['close'], 26)
df['RSI'] = calculate_rsi(df['close'])
return df