用python做個簡單的曾氏通道,先對股價做出迴歸線,再算出標準差,然後分別畫出上下各標準差1倍及兩倍線,就完成了。
股價為台指期(2022.7月往前3.5年,共875個日收盤價)
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
y = np.loadtxt('vvoloi.csv', dtype=int, skiprows=1, usecols=3, delimiter=',')
length = 3.5
toDays = int(length * 250)
y = y[-toDays:]
#y = np.log(y)
x = np.arange(1, len(y)+1)
regressor = LinearRegression()
regressor.fit(x.reshape(-1,1), y)
# y = ax + b
a = regressor.coef_[0] #斜率
b = regressor.intercept_ #截距
TL = [a*i+b for i in x]
sd = (y - TL).std()
plt.figure(figsize=(20,10))
plt.plot(TL)
plt.plot(TL+sd)
plt.plot(TL+sd*2)
plt.plot(TL-sd)
plt.plot(TL-sd*2)
plt.plot(y)
沒有留言:
張貼留言