calculate the return between selected time points only

1 view (last 30 days)
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

Accepted Answer

Orion
Orion on 24 Nov 2014
Edited: Orion on 24 Nov 2014
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 Comments
Jan Morawietz
Jan Morawietz on 24 Nov 2014
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 on 24 Nov 2014
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Price and Analyze Financial Instruments in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!