#测试talib模块
import talib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = []
close_list = []
f = open("trade_records/contract_data_HOUR.txt",'r')
data = f.readlines()
for item in data:
df.append(eval(item))
f.close()
print(df)
df = pd.DataFrame(df)
print(df)
real = talib.MA(df.close,timeperiod=3)
print(real)
x=range(0,len(real)-2,1)
y= real[2:]
#求x,y的多项式
z1 = np.polyfit(x,list(y),6)
p1 = np.poly1d(z1)# 转换成多项式表达式
yvals = p1(x) #求得拟合函数yvals
#绘制拟合曲线
plt.plot(x,y,'b--',label='original values')
plt.plot(x,yvals,'r',label='polyfit values')
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend(loc=3)
plt.show()
real的值为:
0 NaN
1 NaN
2 3.606000
3 3.584333
4 3.580000
5 3.572333
6 3.585000
7 3.589000
8 3.599667
9 3.593000
10 3.585000
11 3.575667
12 3.557000
13 3.544000
14 3.562000
15 3.590000
16 3.613333
17 3.598333
18 3.590000
19 3.577000
dtype: float64
在上面的例子中,real的前两个值为np.NaN型,因此无法进行plot,进而会出现LinAlgError错误,只需要将非法值去掉后就可以正常运行了;