calculate the return between selected time points only

1 visualización (últimos 30 días)
Jan Morawietz
Jan Morawietz el 24 de Nov. de 2014
Comentada: Orion el 24 de Nov. de 2014
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

Respuesta aceptada

Orion
Orion el 24 de Nov. de 2014
Editada: Orion el 24 de Nov. de 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 comentarios
Jan Morawietz
Jan Morawietz el 24 de Nov. de 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 el 24 de Nov. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Price and Analyze Financial Instruments en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by