Making dynamic gridlines for stock data

Question:
Assume that the stock market price for a given period of time fluctuates between low and high. We want to display a y-axis with grid lines so that the entire range of prices is displayed and so that grid lines are drawn at "nice" values. For example, when stock prices range from 7500 to 13010, eight grid lines should be displayed at 7000, 8000, 9000, 10000, 11000, 12000, 13000, and 14000. Yet when a stock price ranges from 160 to 426, seven grid lines should be drawn at values 150, 200, 250, 300, 350, 400, and 450. More formally, we want the following requirements met:
-Grid lines for a plot must be drawn at even increments that are multiples of some fixed M, chosen for the given data set.
-That value of M must be of the form c * 10 ^ k where c is either 1, 2, or 5 and where k can be any integer.
-The lowest grid value must be equal to the greatest multiple of M that is less than or equal to low. The highest grid value must be equal to the lowest multiple of M that is greater than or equal to high.
-There must be at least 5 grid values and at most 10.
Write a function of the form v = computeGrid(low,high) that returns a vector v of the grid values that should be used when plotting stocks that range from low to high. For example, computeGrid(7500, 13010) could return the vector [7000 8000 9000 10000 11000 12000 13000 14000] based on increment of M=1000, whereas computeGrid(160, 426) could return the vector [150 200 250 300 350 400 450] based on an increment of M=50.
For this problem, you may assume that a solution exists for some M 1. That is, you may assume that k 0 in our notation. See extra credit for the more general case.
My answer attempt (updated):
function v = computeGrid(low,high)
format longG;
low = 160; %FOR TESTING ONLY
high = 246; %For testing ONLY
k1 = floor(log10(low));
k2 = floor(log10(high));
k = min([k1 k2]); %Start with smaller of k1 and k2
I1_min = floor(low / (10^(k)));
I2_max = ceil(high / (10^(k)));
if (I2_max - I1_min) <= 10 && (I2_max - I1_min) >= 4%Generate only 10 increments
c = I1_min:1:I2_max;
elseif (I2_max - I1_min) > 10
N = ((I2_max - I1_min)/10)+2;
N2 = round(N);
c = I1_min:N2:I2_max+N2;
%else (I2_max - I1_min) < 4
% I1_min = floor(low / (10^(k-1)));
% I2_max = ceil(high / (10^(k-1)));
% c = (I1_min/10):1:(I2_max/10);
end
v = c .* (10^k)
end
%Not working in function form????
%Do we need to account for the case when max-min <4?? I.e. the case where
%there aren't 5 lines
This attempt works for the data such as (160, 426), (160, 246), (7500, 13010), etc... ONLY if not in function form. Can anyone help troubleshoot how to get this code to work in the function format? I call it as "v = computeGrid(160, 426)".

1 Comment

Probably not the answer wanted, but, one can use subterfuge --
function ytickvals = computeGrid(lo,hi)
hF=figure('visible','off');
hAx=axes(hF,'visible','off');
hL=plot(hAx,[lo;hi]);
ytiickvals=hL.YTick;
end
(Tongue firmly planted in cheek!) <VBG>
It's been a long time since I coded up one of these and I no longer have one in my utilities to refresh memory, but think I'd start out something like
dy=range([lo hi]);
k=fix(log10(ceil(dy)));
v1=floor(lo/10^k);
v2=ceil(hi/10^k);
then test that v1:v2 is within the limits on number of elements allowed; can then step up the multipler from the implied c=1 so far...
I don't think that's giving too much away for homework...I went back and removed the comments so will have to think about what its doing and why... :)

Sign in to comment.

Answers (0)

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Asked:

on 16 Oct 2022

Edited:

dpb
on 17 Oct 2022

Community Treasure Hunt

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

Start Hunting!