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
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)