percentage of up days every 12 days

4 次查看(过去 30 天)
Hello I have a data of stock prices and I am trying to write a code that calculate the pecentage of up days for the past 12 day.
equation as follow:
SPY= (number of times the closing price was up/ period)*100
thanks
  2 个评论
dpb
dpb 2020-6-15
Twelve calendar days, trading days, ...???
Moving twelve day window, consecutive 12-day periods, ...???
Data include dates or just closing price???
So many questions, so few details... :)
maha al
maha al 2020-6-15
I have only closing price. I want to take the numbers of days the stock went up for the past 12 days and devide it by 12. For example: if the number of times it went up was 4 times we had green market then 4 devided by 12.
lets say I have 100 closing price = closep.
didx = 2:length(closep)
% if closep(didx) > closep(didx-1)
% up=up+1
% elseif closep(didx) < closep(didx-1)
% up = up
% end
I dont know how to make it does it for 12 days by counting the numbers of up market taking the the first closep(2:13) then (3:14) then (4:15) and so on

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2020-6-15
Assuming your data are daily stock prices, you can use Y = diff(X,1,dim) to compute the difference across the n_th dimention of your data. If your data is a vector, you just need the 1st input.
Y will have 1 less data point than X since Y(1) is the result of X(2)-X(1).
Then look for positive values in Y. If Y(n) is positive that means the stock from X(n+1) increased from the previous day on X(n).
The rest should be straightforward.
  2 个评论
maha al
maha al 2020-6-15
thanks for your reply. How can I do it for specific period. Can you please elaborate. I want to do it for every 12 days.
Adam Danz
Adam Danz 2020-6-15
编辑:Adam Danz 2020-6-15
I have the same questions dpb asked in the comment section under your question.
It sounds like you have a vector of daily stock prices (7 days a week). If you want to compute a 12-day moving average of the daily change, you can use M = movmean(A,k) where A is
A = diff(x)>0
A will be a logical vector of 1s and 0s indicating days that the stock increased. To get the avg number of days in increased within a 12 day period, assuming the input vector is daily values,
movmean(A,12,'endpoints','discard') * 100
See the movmean function for more option to control window size and how to handle the end points.
I really can't give more details without knowing more about your data.

请先登录,再进行评论。

更多回答(2 个)

dpb
dpb 2020-6-15
编辑:dpb 2020-6-15
"counting the numbers of up market taking the the first closep(2:13) then (3:14) then (4:15) and so on..."
OK, so a running number...
One way amongst many...assume variable CP is close price vector...
nD=12; % number days for moving percentage
dCP=diff(CP)>0; % 1 if +, 0 not (following day higher --> T)
pctUp=arrayfun(@(i) sum(dCP(i:i+nD-1)),1:numel(dCP)-nD))/nD*100; % percentage
NB: The above incorporates the difference vector dCP in the anonymous function -- if the data change, then the difference must be recalculated and the new result computed.

dpb
dpb 2020-6-15
However, the neatest solution is probably
nD=12; % number days for moving percentage
dCP=diff(CP)>0; % 1 if +, 0 not (following day higher --> T)
pctUp=conv(dCP,ones(1,nD),'valid')/nD*100; % percentage
  2 个评论
Adam Danz
Adam Danz 2020-6-15
编辑:Adam Danz 2020-6-15
That's not too different from my answer and produces the same results.
nD = 12;
CP = rand(1,1200);
% conv method
dCP=diff(CP)>0; % 1 if +, 0 not (following day higher --> T)
pctUp=conv(dCP,ones(1,nD),'valid')/nD*100; % percentag
% movmean method
dCP=diff(CP)>0;
pctUp2=movmean(dCP,nD,'endpoints','discard') * 100;
% Compare results
figure()
plot(pctUp, 'bo', 'DisplayName', 'conv method')
hold on
plot(pctUp2, 'rs', 'DisplayName', 'movmean method')
legend()
However, the conv method seems to be a bit faster (both methods are extremely fast).
dpb
dpb 2020-6-15
I've not dug into it, but I'd guess the convolution is how movmean is implemented...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by