简体   繁体   中英

Plotting candlestick chart and executions in mplfinance

I am trying to do the following:

  1. Plot a 5 minute candle stick chart for a particular day.
  2. Plot my executions on top of the candle stick chart as a scatter plot.

Candle stick data is in the conventional format data frame: Datetime Index, Open, Close, High, Low, Volume. Executions are split into two dataframes: byu and sell. Both have similar structure: numeric index, Time, Price. Time is the execution time (datetime format) brought to the start of five minutes period to match the candle stick. The issue is that this time is not unique: it is possible that there are two sells or two buys within the same 5-minutes period. This does not allow me to merge those two dataframes with the candlestick data (initially I thought to add to the candle stick data frame two more columns like sell_price and buy_price and merge these three dataframes together but as there are duplicate time stamps it would require me to add sell_price1, sell_price2 etc which feels stupid).

I tried setting the index to sell Time column and plotting it via addplot as following:

apdict = mpl.make_addplot(sell['Price'], type='scatter')
fig, axlist = mpl.plot(
    df[['open', 'high', 'low', 'close', 'volume']].loc['2022-6-9 6:45':'2022-6-9 15:45'],
    type="candle", 
    title = "Micro E-mini S&P 500 Price",  
    style="yahoo", volume=True, figratio=(12.00, 5.75),
    returnfig=True, show_nontrading=True, addplot=apdict
    )

axlist[0].xaxis.set_major_locator(mdates.MinuteLocator(byminute=[0,30]))

mpl.show()

the attempt failed with the following message:

ValueError: zero-size array to reduction operation maximum which has no identity

What would be the right way to solve this issue? Is there a smart way to plot candle stick chart and transactions on top of it?

Thanks, Dmitry

An important point when adding plots in mplfinance is that the number of time series data must be the same. So the sell data must be extended to the same length as the time series of the stock price data. This code updates the historical data with an index of the historical data to make the length the same.

import pandas as pd
import numpy as np
import io
import mplfinance as mpf
import yfinance as yf

df = yf.download("SPY", start="2022-06-08", end="2022-06-11", interval='5m')
df.index = pd.to_datetime(df.index)
df.index = df.index.tz_localize(None)

data = '''
Time Price
"2022-06-07 11:20:00" 412.66
"2022-06-07 12:30:00" 411.350
"2022-06-07 13:50:00" 413.290
"2022-06-07 15:00:00" 414.109
"2022-06-09 13:25:00" 409.660
"2022-06-10 09:50:00" 394.130
'''

sell_df = pd.read_csv(io.StringIO(data), delim_whitespace=True)

sell_df['Time'] = pd.to_datetime(sell_df['Time'])
sell_df.set_index('Time', inplace=True)
sell_df = sell_df.reindex(df.index, axis='index', fill_value=np.NaN)
apdict = mpf.make_addplot(sell_df['Price'], type='scatter', markersize=200, marker='^')
mpf.plot(df,
         type="candle", 
         title = "Micro E-mini S&P 500 Price",  
         style="yahoo", 
         volume=True, 
         figratio=(12.00, 5.75),
         returnfig=True,
         show_nontrading=False,
         addplot=apdict
    )

在此处输入图像描述

Not sure it is the most optimal way but I reckon I got what I wanted. Only issue setting those 30 mins ticks on x-axis does not seem to be working.

fig, axlist = mpl.plot(
    df[['open', 'high', 'low', 'close', 'volume']].loc['2022-6-9 6:45':'2022-6-9 15:45'],
    type="candle", 
    title = "Micro E-mini S&P 500 Price",  
    style="yahoo", volume=True, figratio=(12.00, 5.75),
    returnfig=True, show_nontrading=True
    )

# axlist[0].yaxis.set_major_formatter(FormatStrFormatter('$%.2f'))
# axlist[0].xaxis.set_major_locator(mdates.MinuteLocator(interval=30))
#axlist[0].xaxis.set_major_locator(mdates.MinuteLocator(byminute=[0,30]))
sell[(sell['Time'] > '2022-6-9 6:45') & (sell['Time'] < '2022-6-9 15:45')].plot(x='Time', y='Price', kind='scatter', ax=axlist[0])
mpl.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM