唐奇安通道布林带通道
时间: 2023-12-07 15:40:01 浏览: 360
唐奇安通道和布林带通道都是常用的技术分析指标,用于判断股票价格的趋势和波动情况。下面是对唐奇安通道和布林带通道的介绍和Python代码实现:
1. 唐奇安通道
唐奇安通道是由三条轨道线构成的,分别是通道上界、通道下界和中轨道。其中,通道上界等于过去20日内的最高价,通道下界等于过去20日内的最低价,中轨道等于通道上界和通道下界的平均值。唐奇安通道的突破是指股票价格突破了通道上界或通道下界,这通常被视为一个买入或卖出信号。
2. 布林带通道
布林带通道也是由三条轨道线构成的,分别是通道上界、通道下界和中轨道。其中,通道上界等于中轨道加上两倍的标准差,通道下界等于中轨道减去两倍的标准差,中轨道等于股票价格的移动平均线。布林带通道的突破是指股票价格突破了通道上界或通道下界,这通常被视为一个买入或卖出信号。
下面是Python代码实现唐奇安通道和布林带通道:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 计算唐奇安通道
def donchian_channel(high, low, window=20):
channel_up = high.rolling(window=window).max()
channel_down = low.rolling(window=window).min()
channel_mid = (channel_up + channel_down) / 2
return channel_up, channel_down, channel_mid
# 计算布林带通道
def bollinger_band(close, window=20, k=2):
std = close.rolling(window=window).std()
band_up = close.rolling(window=window).mean() + k * std
band_down = close.rolling(window=window).mean() - k * std
band_mid = close.rolling(window=window).mean()
return band_up, band_down, band_mid
# 读取股票数据
df = pd.read_csv('stock.csv', index_col='date', parse_dates=True)
# 计算唐奇安通道和布林带通道
channel_up, channel_down, channel_mid = donchian_channel(df['high'], df['low'])
band_up, band_down, band_mid = bollinger_band(df['close'])
# 绘制K线图和唐奇安通道、布林带通道
fig, ax = plt.subplots(figsize=(16, 8))
ax.set_title('Stock Price')
ax.set_ylabel('Price')
ax.grid(True)
candlestick_ohlc(ax, zip(mdates.date2num(df.index.to_pydatetime()), df['open'], df['high'], df['low'], df['close']), width=0.6, colorup='red', colordown='green')
ax.plot(df.index, channel_up, label='Donchian Channel Up', color='blue')
ax.plot(df.index, channel_down, label='Donchian Channel Down', color='blue')
ax.plot(df.index, band_up, label='Bollinger Band Up', color='orange')
ax.plot(df.index, band_down, label='Bollinger Band Down', color='orange')
ax.legend(loc='upper left')
plt.show()
```
阅读全文