Superimpose scatter data points on line charts in loop
조회 수: 4 (최근 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일
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일
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일
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!