作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有外汇对的逐笔报价
这是 EURUSD/EURUSD-2012-06.csv
的示例
EUR/USD,20120601 00:00:00.207,1.23618,1.2363
EUR/USD,20120601 00:00:00.209,1.23618,1.23631
EUR/USD,20120601 00:00:00.210,1.23618,1.23631
EUR/USD,20120601 00:00:00.211,1.23623,1.23631
EUR/USD,20120601 00:00:00.240,1.23623,1.23627
EUR/USD,20120601 00:00:00.423,1.23622,1.23627
EUR/USD,20120601 00:00:00.457,1.2362,1.23626
EUR/USD,20120601 00:00:01.537,1.2362,1.23625
EUR/USD,20120601 00:00:03.010,1.2362,1.23624
EUR/USD,20120601 00:00:03.012,1.2362,1.23625
完整的报价数据可以在这里下载 http://dl.free.fr/k4vVF7aOD
列是:
Symbol,Datetime,Bid,Ask
我想将逐笔报价数据转换为 Renko 图表 http://www.investopedia.com/terms/r/renkochart.asp
图表的一个参数是蜡烛高度(收盘价-开盘价):我们称它为 candle_height
我想使用 Python 和 Pandas 库来完成这个任务。
我已经完成了部分工作......逐笔读取数据文件
我想得到这样的数据
Id,Symbol,open_price,close_price
能够画莲子
Id是蜡烛的编号
蜡烛的价格将基于出价列。
这是代码
#!/usr/bin/env python
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.finance import candlestick
def candle_price_ref(price, candle_height):
#return(int(price/candle_height)*candle_height)
return(round(price/candle_height)*candle_height)
print("Reading CSV (please wait)")
df = pd.read_csv('test_EURUSD/EURUSD-2012-07.csv', names=['Symbol', 'Date_Time', 'Bid', 'Ask'], index_col=1)
print("End of reading")
df['Bid'] = df['Bid']
#candle_height = 0.0015
#candle_height = 0.0010
#candle_height = 0.0005
candle_height = 0.0001
#candle_height = 0.000001
price = df.ix[0]['Bid']
price_ref = candle_price_ref(price, candle_height)
ID = 0
#print("ID={0} price_ref={1}".format(ID, price_ref))
candle_price_open = []
candle_price_close = []
candle_price_open.append(price) # price ou price_ref
candle_price_close.append(price)
for i in range(len(df)):
price = df.ix[i]['Bid']
candle_price_close[ID] = price
new_price_ref = candle_price_ref(price, candle_height)
if new_price_ref!=price_ref:
candle_price_close[ID]=new_price_ref
price_ref = new_price_ref
ID += 1
candle_price_open.append(price_ref)
candle_price_close.append(price_ref)
IDs=range(ID+1)
volume=np.zeros(ID+1)
a_price_open=np.array(candle_price_open)
a_price_close=np.array(candle_price_close)
b_green_candle = a_price_open < a_price_close
candle_price_low = np.where(b_green_candle, a_price_open, a_price_close)
candle_price_high = np.where(b_green_candle, a_price_close, a_price_open)
DOCHLV=zip(IDs, candle_price_open, candle_price_close, candle_price_high, candle_price_low, volume)
#print(DOCHLV)
fig = plt.figure()
fig.subplots_adjust(bottom=0.1)
ax = fig.add_subplot(211)
df['Bid'].plot()
plt.title("Price graph")
ax = fig.add_subplot(212)
plt.title("Renko chart")
candlestick(ax, DOCHLV, width=0.6, colorup='g', colordown='r', alpha=1.0)
plt.show()
我的问题是我在这里做的不是真正的 Renko 图表,原因有两个:
我正在寻找一种非常快速的算法,如果可能的话,一个向量化的算法(如果可能的话),因为报价数据可能非常大。
最佳答案
for i,row in enumerate(df.values):
ID = i
sym,timestamp,open_price,close_price = row
print ID,sym,open_price,close_price
但我想我不太明白你想做什么...
关于python - 从逐笔报价数据到 Renko 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12324162/
我是一名优秀的程序员,十分优秀!