2

does anyone know how to add an entry signal onto my graph so that I can see it visually? I want to see an entry signal on my graph when the SPY price hits $411.50.

I've read through everything I can find online regarding this topic including the mplfinance documentation and it seems like there's a way to do it by using addplot() scatter plot but I can't quite figure it out.

Here's what my code looks like so far:

# Libraries:
import pandas as pd
import mplfinance as mpf

# Pulling data:
data = pd.read_csv(r'C:\Users\Viktor\Documents\D2DT\Reformatted_Data\SPY_2021-04-12-2021-04-12.csv')

# Pre-processing data for mpf:
data['Date'] = pd.to_datetime(data['Date'])
data = data.set_index('Date')

# Plotting Data:
mpf.plot(data, type='candle', volume=True, style='yahoo', title="SP500 Data")

data.close()

Inside CSV: enter image description here Thank You for your help with this!

sir_2727
  • 51
  • 4
  • 1
    I had a similar question yesterday. Please refer to [Adding arrows to mpf finance plots](https://stackoverflow.com/questions/71221308/adding-arrows-to-mpf-finance-plots/71223484#71223484). What kind of signals do you want to add? – r-beginners Feb 23 '22 at 09:19
  • can you show the contents of the csv or where the actual signal exists (presumably one of the columns) ? – D.L Feb 23 '22 at 10:28
  • 1
    Does this answer your question? [Adding arrows to mpf finance plots](https://stackoverflow.com/questions/71221308/adding-arrows-to-mpf-finance-plots) – Daniel Goldfarb Feb 23 '22 at 16:18
  • as @r-beginners mentioned, this question has already been answered here: https://stackoverflow.com/questions/71221308/adding-arrows-to-mpf-finance-plot – Daniel Goldfarb Feb 23 '22 at 16:19
  • @r-beginners Sorry for the delayed response. I wanted to make sure I've tried everything before coming back with another question. It doesn't seem like that answer works for my purposes. Basically, what I need is a function where if certain conditions are met, an arrow is displayed on the graph where those conditions are met. For example, If the high price crosses above $441.50, a green arrow is displayed below the candle that crossed. Do you have any idea on how to make this work? – sir_2727 Feb 27 '22 at 01:30
  • @D.L I added the CSV to the body of the question, you can see above. Thanks! – sir_2727 Feb 27 '22 at 01:48
  • If that is your goal, please refer to the [github sample](https://github.com/matplotlib/mplfinance/blob/master/examples/addplot.ipynb) for an actual example. It creates an array equal to the number of candlesticks, and controls the display of markers with Boolean values. – r-beginners Feb 27 '22 at 03:37
  • @r-beginners got it to work! Posted the answer. Thank you! – sir_2727 Feb 27 '22 at 05:34
  • I'm glad you solved the problem. I think the graph you created will make it easier for other people to understand your question. – r-beginners Feb 27 '22 at 06:50

2 Answers2

3

@r-beginners, I messed around with the documentation in the link you provided in the last comment and got it to work. Thank you. I'm posting the code below, hopefully this is helpful to somebody in the future!

import pandas as pd
import numpy as np
import mplfinance as mpf

day = '2021-04-12'
data = pd.read_csv(r'C:\Users\Viktor\Documents\D2DT\Reformatted_Data\SPY_' + str(day) + "-" + str(day) + '.csv')

data['Date'] = pd.to_datetime(data['Date'])
data = data.set_index('Date')

signal = []

for i in data['close']:
    if i > 411.50:
        signal.append(i)
    else:
        signal.append(np.nan)

apd = mpf.make_addplot(signal, type='scatter', markersize=200, marker='v', color='r')

mpf.plot(data[day], type='candle', volume=True, style='yahoo', title="SP500 Data", addplot=apd)
sir_2727
  • 51
  • 4
2

This single line replaces the for loop and is more efficient. It adds the required column (which we called signal) to the dataframe for plotting.

target = 411.50
df.loc[df['close'] >target, 'signal'] = True

For reference, use this site: https://datagy.io/pandas-conditional-column/

basically df.loc[df[‘column’] condition, ‘new column name’] = ‘value if condition is met’

So the full solution looks something like this:

import pandas as pd
import numpy as np
import mplfinance as mpf

day = '2021-04-12'
data = pd.read_csv(r'C:\Users\Viktor\Documents\D2DT\Reformatted_Data\SPY_' + str(day) + "-" + str(day) + '.csv')

data['Date'] = pd.to_datetime(data['Date'])
data = data.set_index('Date')


target = 411.50
df.loc[df['close'] >target, 'signal'] = True

apd = mpf.make_addplot(df['signal'], type='scatter', markersize=200, marker='v', color='r')

mpf.plot(data[day], type='candle', volume=True, style='yahoo', title="SP500 Data", addplot=apd)
D.L
  • 4,339
  • 5
  • 22
  • 45