gpt4 book ai didi

python - 从逐笔报价数据到 Renko 图表

转载 作者:行者123 更新时间:2023-11-28 19:26:03 30 4
gpt4 key购买 nike

我有外汇对的逐笔报价

这是 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 图表,原因有两个:

  1. 在 Renko 图表中,您不能有开盘价和收盘价相同的绿色蜡烛和红色蜡烛(在我的代码中不是这种情况)...
  2. 每根蜡烛必须有一个固定的高度(在我的代码中,一些蜡烛的高度是 candle_height 的 2 或 3 倍......这是一个问题!)

我正在寻找一种非常快速的算法,如果可能的话,一个向量化的算法(如果可能的话),因为报价数据可能非常大。

最佳答案

 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/

30 4 0
文章推荐: python - App Engine 中实体的最佳大小是多少
文章推荐: Python urllib2 : How to eliminate urllib2 add it's own headers
文章推荐: python - 尝试从 C 调用 Python
文章推荐: python - 如何从