52ky 发表于 2022-5-5 09:11:53

Python/Pandas 计算 Ichimoku 图表组件

问题
我有 Pandas DataFrame 对象,其中包含日期、开盘价、收盘价、每日股价的高低数据。我想计算一个房间图的组成。我可以使用以下代码获取数据:
high_prices = data['High']
close_prices = data['Close']
low_prices = data['Low']
dates = data['Date']# contains datetime objects
我需要计算以下系列(家人称之为 Tenkan-Sen 线):

(9 周期高点 + 9 周期低点)/2

我在 R 中找到了一个解决方案,但我很难将其转换为 Python/Pandas 代码。

Ichimoku 图表包含更多组件,但是当我知道如何在 pandas 中计算 Tenkan Sen 线时,我将能够计算所有这些元素(我将分享代码)。

回答
我不是金融或绘图方面的专家,但这里有一个金融数据示例以及如何使用 rolling_max 和 rolling_min :
In :

import pandas.io.data as web
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2013, 1, 27)
data=web.DataReader("F", 'yahoo', start, end)
high_prices = data['High']
close_prices = data['Close']
low_prices = data['Low']
dates = data.index
nine_period_high = pd.rolling_max(data['High'], window= 9 )
nine_period_low = pd.rolling_min(data['Low'], window= 9 )
ichimoku = (nine_period_high + nine_period_low) /2
ichimoku
Out:
Date
2010-01-04       NaN
2010-01-05       NaN
2010-01-06       NaN
2010-01-07       NaN
2010-01-08       NaN
2010-01-11       NaN
2010-01-12       NaN
2010-01-13       NaN
2010-01-14    11.095
2010-01-15    11.270
2010-01-19    11.635
2010-01-20    11.730
2010-01-21    11.575
2010-01-22    11.275
2010-01-25    11.220
...
2013-01-04    12.585
2013-01-07    12.685
2013-01-08    13.005
2013-01-09    13.030
2013-01-10    13.230
2013-01-11    13.415
2013-01-14    13.540
2013-01-15    13.675
2013-01-16    13.750
2013-01-17    13.750
2013-01-18    13.750
2013-01-22    13.845
2013-01-23    13.990
2013-01-24    14.045
2013-01-25    13.970
Length: 771
调用 data[['High', 'Low', 'Close', 'ichimoku']].plot() 将产生以下情节:

更新

在@PedroLobito 指出不完整/不正确的公式后,我接受了@chilliq 的回答,并对熊猫版本 0.16.1 及更高版本进行了修改:
import pandas as pd
from pandas_datareader import data, wb
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2013, 1, 27)
d=data.DataReader("F", 'yahoo', start, end)
high_prices = d['High']
close_prices = d['Close']
low_prices = d['Low']
dates = d.index
nine_period_high = pd.rolling_max(d['High'], window= 9 )
nine_period_low = pd.rolling_min(d['Low'], window= 9 )
d['tenkan_sen'] = (nine_period_high + nine_period_low) /2

# Kijun-sen (Base Line): (26-period high + 26-period low)/2))
period26_high = pd.rolling_max(high_prices, window=26)
period26_low = pd.rolling_min(low_prices, window=26)
d['kijun_sen'] = (period26_high + period26_low) / 2

# Senkou Span A (Leading Span A): (Conversion Line + Base Line)/2))
d['senkou_span_a'] = ((d['tenkan_sen'] + d['kijun_sen']) / 2).shift(26)

# Senkou Span B (Leading Span B): (52-period high + 52-period low)/2))
period52_high = pd.rolling_max(high_prices, window=52)
period52_low = pd.rolling_min(low_prices, window=52)
d['senkou_span_b'] = ((period52_high + period52_low) / 2).shift(26)

# The most current closing price plotted 22 time periods behind (optional)
d['chikou_span'] = close_prices.shift(-22) # 22 according to investopedia
d.plot()
这是结果,由于我不是金融专家,所以不清楚:



页: [1]
查看完整版本: Python/Pandas 计算 Ichimoku 图表组件