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
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... :)
采纳的回答
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 个评论
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
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.
0 个评论
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
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).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!