4

What is the equivalent of plt.scatter in mplfinance???

I am graphing stock prices using mpl finance.

def graph():
    file = 'prices1.xlsx'
    data = pd.read_excel(file, sheet_name = stockQuote)
    data.Date = pd.to_datetime(data.Date)
    data = data.set_index('Date')
    mpf.plot(data, type = 'candle', mav = (100), tight_layout = True)

This

graph('AAPL')

should get me the graph of AAPL prices in candlestick.
I have another excel sheet with buy and sell prices. it looks like this

myPrices = pd.read_excel('transactions.xlsx')
Date Symbol Action Price
2020-03-20 AAPL Buy 80
2021-03-05 AAPL Sell 120
2020-03-20 TSLA Buy 400

I know that matplotlib has this:

plt.scatter(myPrices.index, myPrices['Buy'], label = 'Buy', market = '^', color = 'green')
plt.scatter(myPrices.index, myPrices['Sell'], label = 'Sell', market = 'v', color = 'red')

Because I want to graph 'AAPL', I want to read the date in transaction.xlsx where Symbol = 'AAPL'. I want to indicate with a green arrow ^ in the MPLFINANCE GRAPH when I buy it, and red arrow v when I sell. However, I only know this method in matplotlib. is there an equivalent for mplfinance? PLEASE HELP T-T

1 Answers1

4

Scatter plots in mplfinace cannot be used alone, but can be used in combination with candlesticks. Your data is modified to monthly based data and used as sample data. One thing to note on the data is that the length of the time series data must be the same or an error will occur. This page is a good reference.

import pandas as pd
import numpy as np
import io

data = '''
Date Symbol Action Price
2020-03-01 AAPL Buy 80
2020-04-01 AAPL Sell 130
2020-05-01 AAPL Buy 90
2020-06-01 AAPL Sell 125
2020-07-01 AAPL Buy 125
2020-08-01 AAPL Sell 110
2020-09-01 AAPL Buy 95
2020-10-01 AAPL Sell 125
2020-11-01 AAPL Buy 125
2020-12-01 AAPL Sell 140
2021-01-01 AAPL Buy 115
2021-02-01 AAPL Sell 135
'''

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

df['Date'] = pd.to_datetime(df['Date'])

buy = df[df['Action'] == 'Buy']
buy2 = df[['Date']].merge(buy,how='outer')
sell = df[df['Action'] == 'Sell']
sell2 = df[['Date']].merge(sell,how='outer')

import mplfinance as mpf
import yfinance as yf

data = yf.download("AAPL", interval='1mo', start="2020-03-01", end="2021-03-01")
data.dropna(how='any', inplace=True)

ap = [mpf.make_addplot(buy2['Price'], type='scatter', marker='^', markersize=200, color='g'),
      mpf.make_addplot(sell2['Price'], type='scatter', marker='v', markersize=200, color='r')
     ]
      
mpf.plot(data, type='candle', ylabel='Candle', addplot=ap, volume=False)

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • Nice explanation. Thanks. For some reason `data = yf.download("AAPL", interval='1mo', start="2020-03-01", end="2021-03-01")` gave me 13 rows. Had to change `end` to `"2021-02-28"` to get it to work. – Daniel Goldfarb Mar 07 '21 at 00:38
  • The same thing happened to me. I don't know the cause, but you may need to do some post-processing after you get it. I learned from your question that this kind of visualization is possible. +1 – r-beginners Mar 07 '21 at 01:15
  • 1
    @r-beginners, does this work even when the `data` argument of `mpf.make_addplot()` is simply one single point, and not a series with an equal length to the original df? This seems to be a very special case when the user wants to plot markers above/below every single candle which is not generally the case. It seems to me that if the `data` argument is a one-element series (a single point), the above solution fails. – lazarea Jun 20 '21 at 17:35
  • @lazarea the trick is to mask out the values you dont want as np.Nan i.e `mask.loc[mask['your_mask_column_name']==???, 'data_col'] = np.NaN` – Kickaha Feb 15 '22 at 23:52