Superimpose scatter data points on line charts in loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Amit Patel
am 9 Feb. 2025
Kommentiert: Amit Patel
am 11 Feb. 2025
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 Kommentare
KALYAN ACHARJYA
am 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!
Akzeptierte Antwort
Cris LaPierre
am 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.
load SimulatedStock.mat
TMW
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 Kommentare
Cris LaPierre
am 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)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!