Trying to use if to create a table

1 view (last 30 days)
Emil Petersen
Emil Petersen on 5 Jun 2022
Answered: Jan on 5 Jun 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.

Answers (1)

Jan
Jan on 5 Jun 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

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!