File size: 1,715 Bytes
021d91d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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