Superimpose scatter data points on line charts in loop

2 views (last 30 days)
Hi team. I have a simple script which takes in stock ticker from the my list and downloads histrorical data and then plots it on line chart (X axis is date, and Y axis is close price). I want to add scatter points on the dates when I bought securities. Example - If I bought stock A on 1/2/25 and 5/2/25 then I want my codes to show two dots on the line chart on these two dates. I know scatter needs second dimension so the x value will be the dates and y value should be what is there already on the line chart. I have attached a figure of what I want to achieve.
I have a list of securities (as shown in the image column "Name"), I would like to know best way to create a list for the dates that I want as scatters (as shown in image column "Purchased (scatter)"). Please advise how to create a list of dates which can have single or multiple dates for each securities (as shown in the image below). So when loop runs for security A it should then check for all dates for A and plot them as scatter before moving to security B in second iteraton. So total number of main loop iterations would be equal to number of securities I have in Name column.
My current code takes name from the list, downloads historical prices and then plots them on line chart in Loop.
Any help would be appreciated.
kind regards
Amit
  2 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 9 Feb 2025
Could you clarify what you mean by "purchase date sheet table"? Are you looking to generate a table from purchase data, plot a graph based on it, or something else? Let me know what you need, and I'll try to help accordingly!
Amit Patel
Amit Patel on 9 Feb 2025
Hi Kalyan. Hope below is more clear.
Stocks = ["A"; "B"; "C"]
For loop
Codes to plot A, B and C historical prices
End
So currently I have purchased stocks A, B and C. My codes fetches the historical price of these stocks and plots them Simple. I purchased stock A on three different occassions, B on 2 occassions and C on 2 occassions. I want to plot the purchase dates of these stocks as shown in my previous image. Something like this:
Stocks = ["A"; "B"; "C"]
Dates = {[x, y, z]; [x1, y1]: [x2, y2]}
For loop
Codes to plot A, B and C historical prices
Codes to add Dates for A on A plot, for B on B plot and for C on C plot
End
x, y, z = three purchase dates of stock A
x1, y1 = two purchase dates of stock B
x2, y2 = two purchase dates of stock C
Please also tell me how to construct Dates array in such a way that it will be compatible within the loop

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 9 Feb 2025
You just need to have your x data match the datatype used to plot your stock data.
My suggestion would be to use datatimes for both. Then it's just a matter of plotting 2 data series on the same axis, which you can do using hold.
Here's an example where the data is in a timetable and Time is a datetime.
load SimulatedStock.mat
TMW
TMW = 1000x5 timetable
Time Open High Low Close Volume ___________ ______ ______ ______ ______ __________ 04-Sep-2012 100 102.19 98.57 100.25 7.4792e+06 05-Sep-2012 100.15 101.05 98.45 100.43 6.5051e+06 06-Sep-2012 100.4 102.38 100.34 101.81 6.0469e+06 07-Sep-2012 101.74 102.37 98.97 99.51 7.2479e+06 10-Sep-2012 99.72 101.55 98.05 98.36 5.7329e+06 11-Sep-2012 98.48 98.66 96.63 96.9 5.7566e+06 12-Sep-2012 96.9 99.18 96.54 96.78 6.2104e+06 13-Sep-2012 96.9 98.79 96.52 97.57 5.2657e+06 14-Sep-2012 97.65 98.92 96.58 97.52 5.5528e+06 17-Sep-2012 97.35 97.52 94.51 94.69 5.8096e+06 18-Sep-2012 94.59 95.49 92.81 93.42 6.2436e+06 19-Sep-2012 93.36 94.7 92.78 93.26 5.5614e+06 20-Sep-2012 93.16 95.87 92.6 95.47 6.3724e+06 21-Sep-2012 95.52 95.68 93.15 93.73 5.1637e+06 24-Sep-2012 93.55 95.18 92.25 92.46 7.6349e+06 25-Sep-2012 92.57 95.54 92.26 95.49 5.7268e+06
plot(TMW.Time,TMW.High,'g-')
ind = 900:30:1000;
hold on
plot(TMW.Time(ind),TMW.High(ind),'ro','MarkerFaceColor','r')
hold off
  6 Comments
Cris LaPierre
Cris LaPierre on 10 Feb 2025
You can also convert your date strings to datetimes with cellfun (avoids using a for loop if that is desirable).
Dates = {
{'15/2/24', '18/11/24', '22/1/25', '1/2/25'},
{'15/2/24', '22/1/25'},
{'22/1/25'},
{'18/11/24', '22/1/25', '1/2/25'}
};
D = cellfun(@(x) datetime(x,'InputFormat','dd/M/yy'),Dates,'UniformOutput',false)
D = 4x1 cell array
{[15-Feb-2024 18-Nov-2024 22-Jan-2025 01-Feb-2025]} {[15-Feb-2024 22-Jan-2025 ]} {[22-Jan-2025 ]} {[18-Nov-2024 22-Jan-2025 01-Feb-2025 ]}
Amit Patel
Amit Patel on 11 Feb 2025
Thank you very much Cris, I think your codes for D will solve my problem. You guys are awesome... Cheers.

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!