用python绘制日内烛台

时间:2017-04-19 22:42:00

标签: python candlestick-chart

我开始创建一个有趣的程序,用于绘制日内烛台,但我很努力。如果有人可以帮助我。

import numpy as np
import pandas as pd

def main(filename):
    df = pd.read_csv(filename, sep = ',', parse_dates = ['DateTime'])

    del df['Tid']
    del df['Dealable']
    del df['Pair']
    del df['Ask']

    df = df.set_index(['DateTime'])

    df.index = pd.to_datetime(df.index, unit='s')

    # group every 1 minutes and create OHLC
    data = df.resample('1Min').ohlc()

    # groupe our ohlc data
    ohlc_data = data.reset_index()
    ohlc_data['DateTime'] = ohlc_data['DateTime'].apply(mdates.date2num)

    plt.close('all')
    fig = plt.figure()
    ax = plt.subplot2grid((1,1), (0,0))
    ax.xaxis_date()
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d %H:%M:%S'))
    plt.xticks(rotation=45)
    plt.xlabel("Date")
    plt.ylabel("Price")
    plt.title("EURUSD")
    candlestick_ohlc(ax, ohlc_data.values, width=.6, colorup='#53c156', colordown='#ff1717')
    plt.show()

if __name__ == "__main__":
    main("EUR_USD.csv")

表单的EUR_USD.csv文件: TID,Dealable,对,日期时间,买价,卖价 5803730773,D,EUR / USD,2017-04-09 17:00:04.343000000,1.058550,1.059050 5803730842,D,EUR / USD,2017-04-09 17:00:38.593000000,1.058660,1.058960 5803730879,D,EUR / USD,2017-04-09 17:00:39.343000000,1.058680,1.058980 5803730894,D,EUR / USD,2017-04-09 17:00:41.593000000,1.058710,1.058970 5803731109,D,EUR / USD,2017-04-09 17:01:47.843000000,1.058680,1.058980

1 个答案:

答案 0 :(得分:1)

您的width设置似乎有误,地图中的width应该是fraction of day。在您的情况下,您将其设置为.6(51840秒),但您的数据每1分钟(60秒)采样一次。以下是使用不同宽度修改的代码,请参见下文。

from matplotlib.finance import candlestick_ohlc
import matplotlib.dates as mdates
import numpy as np
import pandas as pd

def main(filename):
    df = pd.read_csv(filename, sep = ',', parse_dates = True, index_col = ['DateTime'])
    print 'raw data'
    print df

    """Creating open high low and close from  bid price at every 1 min. in your case, you will get different values for the first minute
    but for second minute high, low, close and open and are all same"""

    ohlc_data = df.resample('1Min')['Bid'].ohlc()
    print 'ohlc data'
    print ohlc_data
    ohlc_data = ohlc_data.reset_index()
    ohlc_data['DateTime'] = ohlc_data['DateTime'].apply(mdates.date2num)

    #plot
    plt.close('all')
    fig = plt.figure()
    ax = plt.subplot2grid((1,1), (0,0))
    ax.xaxis_date()
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d %H:%M:%S'))
    plt.xticks(rotation=45)
    plt.xlabel("Date")
    plt.ylabel("Price")
    plt.title("EURUSD")
    # width should in terms of fraction of day, here i choose width of 0.25 min
    candlestick_ohlc(ax, ohlc_data.values,width = 0.25/(24*60), colorup='#53c156', colordown='#ff1717')
    plt.show()

if __name__ == "__main__":
    main("EUR_USD.csv")

这会产生

raw data
                                Tid Dealable     Pair      Bid      Ask
DateTime                                                               
2017-04-09 17:00:04.343  5803730773        D  EUR/USD  1.05855  1.05905
2017-04-09 17:00:38.593  5803730842        D  EUR/USD  1.05866  1.05896
2017-04-09 17:00:39.343  5803730879        D  EUR/USD  1.05868  1.05898
2017-04-09 17:00:41.593  5803730894        D  EUR/USD  1.05871  1.05897
2017-04-09 17:01:47.843  5803731109        D  EUR/USD  1.05868  1.05898

ohlc data

                        open     high      low    close
DateTime                                               
2017-04-09 17:00:00  1.05855  1.05871  1.05855  1.05871
2017-04-09 17:01:00  1.05868  1.05868  1.05868  1.05868

enter image description here