我有两个趋势线,即自动生成与烛台日期(高,低,关闭,开放)。当生成此值时,我将存储趋势线值。当我加载这些数据时,我需要这些行在未来继续。例如:我有一个Dataframe:
在10:00 -> df.iloc[0]['TrendLine'] = 100在11:00 -> df.iloc[1]['TrendLine'] = 130在12:00 -> df.iloc[2]['TrendLine'] = ? (我需要一个趋势的连续地块值)。
遵循更清晰的图像:

发布于 2022-04-01 11:39:56
import matplotlib.pyplot as plt
import numpy as np
up = []
dn = []
up.append(1.00)
dn.append(1.25)
for i in range(1, 25):
dn.append(dn[i-1] / 1.0015)
up.append(up[i-1] * 1.003)
#We absolutely do not know what the trend lines should be in the future.
koef_up = up[len(up)-1] / up[len(up)-2]#get coeficents up
koef_dn = dn[len(up)-2] / dn[len(up)-1]#get coeficents dn
print('koef_up', koef_up, 'koef_dn', koef_dn)
for i in range(25, 30):
up.append(up[i-1] * koef_up)#calculate line to the future up
dn.append(dn[i-1] / koef_dn)#calculate line to the future dn
ind = np.arange(30)
fig, ax = plt.subplots()
ax.plot(ind, up, color="green")
ax.plot(ind, dn, color="red")
ax.plot(ind[25:], up[25:], color="yellow")
ax.plot(ind[25:], dn[25:], color="yellow")
fig.autofmt_xdate()
plt.show()发布于 2022-04-01 04:19:51
你要寻找的是找到这些线的斜率,并继续这个趋势。要做到这一点,一个简单的方法是使用NumPy polyval方法在给定的值下计算一个多项式,在这里您将传递一个poly1d对象。看起来会像:
import numpy as np
np.polyval(np.polyfit(x, y, 1), x)您很可能不得不使用OHLC数据从dataframe中提取这些列,并将它们放入自己的数据中,如果您要将这些趋势线预测到您还没有数据的未来日期。
关于numpy.polyval()的更多信息:https://numpy.org/doc/stable/reference/generated/numpy.polyval.html
https://stackoverflow.com/questions/71701506
复制相似问题