8.3. Volatility Indicators (Chỉ báo biến động)
8.3.1. Bollinger Bands
Bollinger Bands (Dải Bollinger) là một công cụ phân tích kỹ thuật được phát triển bởi John Bollinger vào những năm 1980. Bollinger Bands giúp đo lường sự biến động (volatility) của giá và xác định mức giá cao/thấp tiềm năng trong một xu hướng.
Cấu tạo:
Dải trung tâm (Centerline): Đường trung bình động (thường là đường SMA 20 ngày) của giá.
Dải trên (Upper Band): Số lần chênh lệch chuẩn nhất định (thường là 2) tính trên đường trung tâm, được cộng thêm vào giá trị của đường trung tâm.
Dải dưới (Lower Band): Số lần chênh lệch chuẩn nhất định (thường là 2) tính trên đường trung tâm, được trừ đi khỏi giá trị của đường trung tâm.
Các tính năng thêm:
Độ rộng dải (Bollinger Band Width): Chỉ số đo lường độ biến động (volatility) của giá, được tính từ khoảng cách giữa dải trên và dải dưới của Bollinger Bands và chia cho dải trung tâm.
Tín hiệu "nén" giá (Bollinger Squeeze): Chỉ báo cho biết khi độ rộng của dải Bollinger Bands (Bollinger Band Width) thu hẹp đáng kể, cho thấy biến động thị trường giảm mạnh, và một cú bứt phá (breakout) sắp xảy ra.
def bollinger_mband(column: pandas.core.series.Series, window: int = 20)
def bollinger_hband(column: pandas.core.series.Series, window: int = 20, window_dev: int = 2)
def bollinger_lband(column: pandas.core.series.Series, window: int = 20, window_dev: int = 2)
def bollinger_wband(column: pandas.core.series.Series, window: int = 20, window_dev: int = 2)
def bollinger_squeeze(column: pandas.core.series.Series, window: int = 20, window_dev: int = 2, squeeze_mode: Literal['dynamic', 'percentile'] = "dynamic", dynamic_thresold: float = 0.9, percentile_thresold: int = 10)Tham số
column
Cột dữ liệu chứa các giá trị để tính toán Bollinger Bands.
pandas.Series
window
Số lượng điểm dữ liệu sử dụng trong phép tính Bollinger Bands.
int
20
window_dev
Số lượng độ lệch chuẩn sử dụng để tính toán khoảng cách các dải của Bollinger Bands.
int
2
squeeze_mode
Kiểu "nén giá": percentile (Thị trường đang ở trạng thái squeeze khi BBW rơi xuống nhỏ hơn ngưỡng percentile nhất định trong lịch sử (ví dụ: 10%, 15%)) hoặc dynamic (ngưỡng nén được điều chỉnh linh hoạt theo mức trung bình động của BB Width thay vì cố định bằng percentile)
str
'dynamic'
dynamic_thresold
Hệ số kiểu "nén giá" linh động
float
0.9
percentile_thresold
Hệ số kiểu "nén giá" percentile
int
10
Ví dụ
fi = client.FiinIndicator()
df['bollinger_mband'] = fi.bollinger_mband(df['close'], window=20)
df['bollinger_hband'] = fi.bollinger_hband(df['close'], window=20, window_dev=2)
df['bollinger_lband'] = fi.bollinger_lband(df['close'], window=20, window_dev=2)
df['bollinger_wband'] = fi.bollinger_wband(df['close'], window=20, window_dev=2)
df['bollinger_squeeze'] = fi.bollinger_squeeze(df['close'], window=20, window_dev=2, squeeze_mode='dynamic', dynamic_thresold=0.9)
print(df)8.3.2. Supertrend
Supertrend (siêu xu hướng) là một công cụ phân tích kỹ thuật được phát triển bởi nhà đầu tư Olivier Seban vào năm 2010. Supertrend sử dụng kết hợp các yếu tố như giá cả ATR (Average True Range - Biên độ dao động trung bình thực) và xu hướng hiện tại để xác định xu hướng chính của thị trường và các điểm đảo chiều tiềm năng.
def supertrend(high: pandas.core.series.Series, low: pandas.core.series.Series, close: pandas.core.series.Series, window: int = 14, multiplier: float = 3.0)
def supertrend_hband(high: pandas.core.series.Series, low: pandas.core.series.Series, close: pandas.core.series.Series, window: int = 14, multiplier: float = 3.0)
def supertrend_lband(high: pandas.core.series.Series, low: pandas.core.series.Series, close: pandas.core.series.Series, window: int = 14, multiplier: float = 3.0)Tham số:
high
Cột dữ liệu chứa các giá trị cột giá cao nhất để tính toán Supertrend.
pandas.Series
low
Cột dữ liệu chứa các giá trị cột giá thấp nhất để tính toán Supertrend.
pandas.Series
close
Cột dữ liệu chứa các giá trị cột giá đóng cửa để tính toán Supertrend.
pandas.Series
window
Số lượng điểm dữ liệu sử dụng trong phép tính Supertrend.
int
14
multiplier
Hệ số nhân dùng để điều chỉnh độ rộng của chỉ báo Supertrend so với mức giá.
float
3.0
Ví dụ:
fi = client.FiinIndicator()
df['supertrend'] = fi.supertrend(df['high'], df['low'], df['close'], window=14)
df['supertrend_hband'] = fi.supertrend_hband(df['high'], df['low'], df['close'], window=14)
df['supertrend_lband'] = fi.supertrend_lband(df['high'], df['low'], df['close'], window=14)
print(df)8.3.3. ATR (AverageTrueRange)
ATR là khoảng dao động trung bình thực tế. Đây là chỉ báo để đo lường những biến động của giá trong một khoảng thời gian nhất định.
Chỉ báo được giới thiệu trong cuốn sách “Tư tưởng mới trong Hệ thống kỹ thuật Giao dịch” của J. Welles Wilder Jr vào năm 1978. Thông qua chỉ báo, nhà đầu tư có thể dự đoán mức giá dao động trong tương lai. Nhờ đó, nhà đầu tư có cơ sở để đặt điểm cắt lỗ và chốt lời hợp lý.
def atr(high: pandas.core.series.Series, low: pandas.core.series.Series, close: pandas.core.series.Series, window: int = 14)Tham số:
high
Cột dữ liệu chứa các giá trị cột giá cao nhất để tính toán ATR.
pandas.Series
low
Cột dữ liệu chứa các giá trị cột giá thấp nhất để tính toán ATR.
pandas.Series
close
Cột dữ liệu chứa các giá trị cột giá đóng cửa để tính toán ATR.
pandas.Series
window
Số lượng điểm dữ liệu sử dụng trong phép tính ATR.
int
14
Ví dụ:
fi = client.FiinIndicator()
df['atr'] = fi.atr(df['high'], df['low'], df['close'], window=14)
print(df)8.3.4. Realized Volatility
Realized Volatility (annualized) là chỉ báo đo lường mức độ biến động thực tế của giá dựa trên lợi suất logarit trong một khoảng thời gian nhất định.
Chỉ báo này thường được annualize (quy đổi theo năm) để phản ánh biến động trung bình năm, giúp nhà đầu tư đánh giá mức rủi ro hoặc độ ổn định của tài sản.
def realized_volatility(close: pandas.Series, window: int = 14, trading_days: int = 252)Tham số:
close
Cột dữ liệu chứa các giá trị cột giá đóng cửa để tính toán realized volatility.
pandas.Series
window
Số phiên (ngày) được dùng để tính trung bình phương sai (rolling window).
int
14
trading_days
Số ngày giao dịch trong năm, dùng để annualize biến động.
int
252
Ví dụ:
fi = client.FiinIndicator()
df['rv'] = fi.realized_volatility(df["close"], window=14, trading_days=252)
print(df)8.3.5. Keltner Channels
Kênh Keltner (Keltner Channels) là các đường bao xung quanh dựa trên biến động giá có thể ở trên hoặc dưới đường trung bình động lũy thừa (EMA). Chỉ báo Keltner sử dụng chỉ báo Khoảng dao động trung bình thực tế (ATR) để đặt khoảng cách kênh. Thông thường, chỉ báo kênh Keltner sẽ sử dụng 2 đường ATR ở trên và dưới đường EMA 20 ngày. Đường này sẽ điều khiển hướng trong khi đường ATR sẽ điều khiển độ rộng của kênh.
Chỉ báo kênh Keltner là chỉ báo theo sau xu hướng thường được dùng để tìm tín hiệu đảo chiều xu hướng thông qua việc phá kênh hay dựa vào hướng của kênh. Chỉ báo này cũng được sử dụng để xác định vùng quá mua và quá bán khi thị trường không có xu hướng.
def kc_midline(close: pandas.Series, high: pandas.Series, low: pandas.Series, ema_window: int = 20, atr_window: int = 10, atr_mult: float = 2)
def kc_upper(close: pandas.Series, high: pandas.Series, low: pandas.Series, ema_window: int = 20, atr_window: int = 10, atr_mult: float = 2, band_style: Literal['avg_true_range', 'true_range', 'range'] = "avg_true_range")
def kc_lower(close: pandas.Series, high: pandas.Series, low: pandas.Series, ema_window: int = 20, atr_window: int = 10, atr_mult: float = 2, band_style: Literal['avg_true_range', 'true_range', 'range'] = "avg_true_range")Tham số:
close
Cột dữ liệu chứa các giá trị cột giá đóng cửa để tính toán các kênh Keltner.
pandas.Series
high
Cột dữ liệu chứa các giá trị cột giá cao nhất để tính toán các kênh Keltner.
pandas.Series
low
Cột dữ liệu chứa các giá trị cột giá thấp nhất để tính toán các kênh Keltner.
pandas.Series
ema_window
Số phiên (ngày) được dùng để tính đường trung bình động lũy thừa (EMA).
int
20
atr_window
Số phiên (ngày) được dùng để tính khoảng dao động trung bình thực tế (ATR).
int
10
atr_mult
Số nhân với khoảng dao động trung bình thực tế (ATR) để tính kênh trên và kênh dưới.
float
2.0
band_style
Kiểu dải là 1 trong 3 giá trị 'avg_true_range': Khoảng dao động thực tế trung bình, 'true_range': Phạm vi thực thế, 'range': Phạm vi
str
'avg_true_range'
Ví dụ:
fi = client.FiinIndicator()
df["kc_midline"] = fi.kc_midline(df["close"], df["high"], df["low"], ema_window = 20, atr_window = 10, atr_mult = 2)
df["kc_upper"] = fi.kc_upper(df["close"], df["high"], df["low"], ema_window = 20, atr_window = 10, atr_mult = 2, band_style = 'avg_true_range')
df["kc_lower"] = fi.kc_lower(df["close"], df["high"], df["low"], band_style = 'avg_true_range')
print(df)Trực quan bằng biểu đồ



Source code:
import pandas as pd
import matplotlib.pyplot as plt
import mplfinance as mpf
from FiinQuantX import FiinSession
username = "REPLACE_WITH_YOUR_USERNAME"
password = "REPLACE_WITH_YOUR_PASSWORD"
client = FiinSession(username=username, password=password).login()
df = client.Fetch_Trading_Data(
realtime=False,
tickers=["MWG"],
fields=["open","high","low","close"],
adjusted=True,
by="1d",
from_date="2024-10-20",
lasted=True
).get_data()
fi = client.FiinIndicator()
df["kc_midline"] = fi.kc_midline(df["close"], df["high"], df["low"])
df["kc_upper"] = fi.kc_upper(df["close"], df["high"], df["low"])
df["kc_lower"] = fi.kc_lower(df["close"], df["high"], df["low"])
# ----- 1️⃣ Chuyển đổi timestamp sang datetime -----
df['timestamp'] = pd.to_datetime(df['timestamp'], format='%Y-%m-%d %H:%M')
# Đặt timestamp làm index để phù hợp với mplfinance
df = df.set_index('timestamp')
# ----- 3️⃣ Vẽ biểu đồ -----
# Tạo đối tượng “additional plot” cho đường trend line
add_plots = [
mpf.make_addplot(df["kc_midline"], color='blue', width=1.5, label='Basis'),
mpf.make_addplot(df["kc_upper"], color='blue', width=1.5, label='Upper'),
mpf.make_addplot(df["kc_lower"], color='blue', width=1.5, label='Lower')
]
mpf.plot(
df,
type='candle',
style='yahoo',
addplot=add_plots,
title='Candlestick Chart with Keltner Channels',
ylabel='Price',
ylabel_lower='Volume',
datetime_format='%Y-%m-%d',
tight_layout=True,
figratio=(16, 8)
)
plt.show()
Last updated