Python/Pandas 计算 Ichimoku 图表组件 [英] Python/Pandas calculate Ichimoku chart components
问题描述
我有包含日期、开盘价、收盘价、最低价和最高价每日股票数据的 Pandas DataFrame 对象.我想计算 ,但我很难把它翻译成 Python/Pandas 代码.
Ichimoku 图包含更多组件,但是当我知道如何计算 Pandas 中的 Tenkan-Sen 线时,我将能够计算所有这些(我将分享代码).
我不是金融专家或绘图专家,但以下显示了示例金融数据以及如何使用
I have Pandas DataFrame object with Date, Open, Close, Low and High daily stock data. I want to calculate components of Ichimoku chart. I can get my data using the following code:
high_prices = data['High']
close_prices = data['Close']
low_prices = data['Low']
dates = data['Date'] # contains datetime objects
I need to calculate the following series (Ichimoku calls it Tenkan-Sen line):
(9-period high + 9-period low) / 2
- 9-period high = the highest High value of last 9 days,
- 9-period low = the lowest Low value of last 9 days, so both should begin on 9th day.
I've found a solution in R language here, but it's difficult for me to translate it to Python/Pandas code.
Ichimoku chart contains of more components, but when I will know how to count Tenkan-Sen line in Pandas, I will be able to count all of them (I will share the code).
I'm no financial expert or plotting expert but the following shows sample financial data and how to use rolling_max
and rolling_min
:
In [60]:
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 = df['High'].rolling(window=9).max()
nine_period_low = df['Low'].rolling(window=9).min()
ichimoku = (nine_period_high + nine_period_low) /2
ichimoku
Out[60]:
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
Calling data[['High', 'Low', 'Close', 'ichimoku']].plot()
results in the following plot:
update
After @PedroLobito's comments pointing out the incomplete/incorrect formula I took @chilliq's answer and modified it for pandas versions 0.16.1 and above:
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 = df['High'].rolling(window=9).max()
nine_period_low = df['Low'].rolling(window=9).min()
d['tenkan_sen'] = (nine_period_high + nine_period_low) /2
# Kijun-sen (Base Line): (26-period high + 26-period low)/2))
period26_high = high_prices.rolling(window=26).max()
period26_low = low_prices.rolling(window=26).min()
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 = high_prices.rolling(window=52).max()
period52_low = low_prices.rolling(window=52).min()
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()
results in the following plot, unclear because as stated already I'm not a financial expert:
这篇关于Python/Pandas 计算 Ichimoku 图表组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!