calculate the return between selected time points only

1 次查看(过去 30 天)
Hi,
suppose I have a time series with stock prices and this variable is called "price" Now I have a second variable which is called "PSC1". This variable has n different elements which define the position of n stock prices in variable "price".
Now I want to calculate the simple returns between the selected stock prices, e.g. price(n)/price(n-1).
With my code below I always receive the famous error code:
"In an assignment A(I) = B, the number of elements in B and I must be the same."
Thank you for any advice!
%%%%%%%%%%%%%%%%%%%%%%%%%%%
for n = PSC1(1:end)
TF(n)=price(n+1)/price(n)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
* So PSC1 has got twenty numbers, something like 52, 54, 102... Which are single positions in the variable "price" which has a length of 800 or smth. TF is supposed to be a variable which contains 20 returns

采纳的回答

Orion
Orion 2014-11-24
编辑:Orion 2014-11-24
Hi,
in your code, at the first iteration, you're trying to put a value in the 52th position of TF, which only have 20 elements => Problem !
you should do something like
for i = 1:length(PSC1)
TF(i) = price(PSC1(i)+1)/price(PSC1(i));
end
  2 个评论
Jan Morawietz
Jan Morawietz 2014-11-24
Thank you - this code works
In my original code, I did predefine TF as zerovector with same length as price. Now I understand that this pre-format is irrelevant for the iteration steps.
Orion
Orion 2014-11-24
Warning / advice
You always must predefine your arrays when you know the size they will have.
- your algorithm will be faster and more readable.
- this allows you to debug errors like the one you got.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Price and Analyze Financial Instruments 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by