Superimpose scatter data points on line charts in loop

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

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!
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

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

Thanks Cris. Hold on and off should allow me to add additional stuff to my existing plot great. however I will still need bit help on how I can declare/establish "ind" (or stock purchase dates in my case) outside the loop in one variable. That one variable will have all required dates which need to be plotted as scatter. I might have 200 stocks in the future so I need to have a variable which stores dates for scatters outside the loop.
Example:
Stocks = ["A"; "B"; "C"]
Dates = {{x, y, z}; {x1, y1}; {x2, y2}}
How can I code after Hold on that when the For loop picks stock A for plotting time series, it also picks x, y, and z. In the second iteration the For loop will pick stock B for line chart along with x1 and y1 dates as scatter. (note the total number of dates for scatters changes with each stocks, some stocksmay have 5 dates to plot, some stocks only 2 or 1).
I am struggling to construct Dates variable outside the For loop. I get your point of keeping Dates variable in the datetime format.
Once I know how to create Dates variable then all I will need to do when I purchase a new security is add that stock to variable "Stocks" and then date to variables "Dates" and the rest should be taken care by the For loop. My both variables "Stocks" and "Dates" will continue to grow in size and may look something like this in 2 years. So the codes should be written in such a way that can handle it.
Stocks = ["A"; "B"; "C", "D", "E", "F", "G"]
% total 7 securities
Dates = {{x, y, z}; {x1, y1}; {x2, y2}, {x3, y3, z3, l3, m3, a3, b3};
{x4, y4, z4, l4, m4}; {x5, y5}; {x6, y6, z6, l6, m6, a6, b6}}
% total 7 sets of cells each containns different number of dates.
"Stocks" and "Dates" will be my global variables that needs to stay outside the For loop (nt inside). Cheers.
Am I still unclear?
ind was a variable I created to extract a subset of data for the purpose of plotting points. It was a shortcut approach to creating that data and is not necessarily part of the solution you need.
It's hard to give a specific answer when working with hypothetical data. Could you please share a representative data set? You can either save your variables to a mat file or a spreadhseet and attach it to your post using the paperclip icon.
ok so I have attached my codes. Please note I have removed URL and access token from it because they are sensitive info. you will get the idea though.
When you open my codes you will notice that I am plotting stock price fin in a loop but I don't know how to properly construct Dates variable and plot them on the same plot. Server said cannot attach .docx file so I have attached .pdf file.
% Variables
Stocks = ["ITC.NSE"; "RELIANCE.NSE"; "FLEXI.NSE"; "RPOWER.NSE"];
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'}
}
% Codes
[rows, ~] = size(Stocks);
for_start = 1;
for_end = rows;
for A = for_start : for_end
% Construct url and download individual stock data
URL2 = url2A + company + url2B + dt_str + url2C + token + url2D; % URL
EOD_data = webread(URL2); % Json data
dataStruct = jsondecode(EOD_data); % data convert
EOD_DATA = struct2table(dataStruct); % Create a table
EOD_DATA(:, [2, 3, 4, 5]) = []; % Drop columns
EOD_DATA.Properties.VariableNames = ['Date', Price];
figure;
plot(Date, Price, 'black');
end
It would also be helpful to see what the downloaded data looks like. Could you save your variable Stocks to a mat file and attach that as well?
Also, what are variables Date and Price? They are not defined in the code.you shared.
As for the dates, try this
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'}
};
for c = 1:length(Dates)
D = datetime(Dates{c},'InputFormat','dd/M/yy')
end
D = 1x4 datetime array
15-Feb-2024 18-Nov-2024 22-Jan-2025 01-Feb-2025
D = 1x2 datetime array
15-Feb-2024 22-Jan-2025
D = datetime
22-Jan-2025
D = 1x3 datetime array
18-Nov-2024 22-Jan-2025 01-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 ]}
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

Products

Release

R2024b

Community Treasure Hunt

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

Start Hunting!