Superimpose scatter data points on line charts in loop
47 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
KALYAN ACHARJYA
2025 年 2 月 9 日 9:42
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!
採用された回答
Cris LaPierre
2025 年 2 月 9 日 13:53
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 件のコメント
Cris LaPierre
2025 年 2 月 10 日 14:14
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)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!