Trying to use if to create a table

1 vue (au cours des 30 derniers jours)
Emil Petersen
Emil Petersen le 5 Juin 2022
Réponse apportée : Jan le 5 Juin 2022
I have to do this tick test which is classified as:
I have no problem with classifying uptick, downtick and zero tick. But find it quite hard to classify zero-downtick and zero-uptick. I have tried following:
Rows=height(data);
Uptick=sum(data.price(1:end-1)<data.price(2:end));
downtick=sum(data.price(1:end-1)>data.price(2:end));
Zerotick=sum(data.price(1:end-1)==data.price(2:end));
if data.price(1:end-1)<data.price(2:end)
ZeroUptick=sum(data.price(2:end-1)==data.price(3:end))
end
if data.price(1:end-1)>data.price(2:end)
ZeroDowntick=sum(data.price(2:end-1)==data.price(3:end))
end
PrcUptick=Uptick./Rows*100;
PrcDowntick=downtick./Rows*100;
PrcZerotick=Zerotick./Rows*100;
PrcZeroUptick=ZeroUptick./Rows*100;
PrcZeroDowntick=ZeroDowntick./Rows*100;
Isn't it possible to use the if funtion to create this data? Any help to solve this would be very helpfull..
Kind Regard, Emil Petersen.

Réponses (1)

Jan
Jan le 5 Juin 2022
Remember, that the if condition must be a scalar. You provide a comparison of two vectors:
if data.price(1:end-1)<data.price(2:end)
Then Matlab executes this internally:
if all(data.price(1:end-1) < data.price(2:end)) && ...
~isempty(data.price(1:end-1) < data.price(2:end))
I assume you want something else.
Either use logical indexing. Or create a loop over all "trades" like:
for iTrade = 1:numel(data.price) - 1
if data.price(iTrade) < data.price(iTrade + 1)
...
end
end

Catégories

En savoir plus sur Time Series Objects dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by