calculate the return between selected time points only

1 回表示 (過去 30 日間)
Jan Morawietz
Jan Morawietz 2014 年 11 月 24 日
コメント済み: Orion 2014 年 11 月 24 日
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 Center および File ExchangePrice 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