Effacer les filtres
Effacer les filtres

How do I count the total number of conditions from .CSV file

2 vues (au cours des 30 derniers jours)
I would like to count when certain conditions in a .csv file are true, for example: these data are from .csv file:
Date,Open,High,Low,Close,Volume,Adj Close
2014-07-21,358.10,361.71,356.72,359.76,2278400,359.76
2014-07-18,354.40,359.68,352.08,358.66,3407400,358.66
2014-07-17,353.44,356.96,351.38,352.45,3636000,352.45
2014-07-16,355.62,359.32,353.00,355.90,3503600,355.90
From these data you can draw candlestick chart, I would like to check if first date open price is less than second date open price and also check if first date close price is more than second date close price and 3rd date open price is more than close price. if all these 3 conditions are true then +1 in .txt file. Let me give you a small example: Take first 2 lines:
2014-07-21,358.10,361.71,356.72,359.76,2278400,359.76
2014-07-18,354.40,359.68,352.08,358.66,3407400,358.66
Compare 358.10<354.40 && 359.76>358.66 Now take the next day
2014-07-17,353.44,356.96,351.38,352.45,3636000,352.45
so overall: 358.10<354.40 && 359.76>358.66 && (353.44<352.45) if this condition is true add 1 to the file. Now restart the process take 2nd and 3rd lines: Note first time take 1st and 2nd lines then in second check take 2nd and 3rd line then 4th time check 3rd and 4th line etc… like this:
2014-07-18,354.40,359.68,352.08,358.66,3407400,358.66 <--this time 2nd line 2014-07-17,353.44,356.96,351.38,352.45,3636000,352.45 <--and 3rd line
This is what I have done but not sure if its right:
str = urlread('http://ichart.yahoo.com/table.csv?s=AMZN&a=0&b=1&c=2008&d=0&e=31&n=2014&g=d&ignore=.csv');
i = 0;
[dates,o,h,l,c] = dataread('string',str,'%s%f%f%f%f%*f%*f','delimiter',',','headerlines',1);
dates = datenum(dates);
numElems = length(dates);
for k=numElems:-1:2
if o(k)<o(k-1) && c(k)>c(k-1)
i = i+1;
save('output.txt', 'i', '-ASCII');
end
end
In a nutshell if you understand candlesticks chart this is what I am looking for:
If these pattern is detected then check if next day is positive if yes record that into file.

Réponse acceptée

Matz Johansson Bergström
Matz Johansson Bergström le 22 Juil 2014
Modifié(e) : Matz Johansson Bergström le 22 Juil 2014
Edit: I first thought you wanted to store the values of 'i' when the conditions are true. So, I would use a vector for that. If you don't want that, just sum(tricond) in the end. Also, changed loop from 1
In your code you save 'i' to a file repeatedly, which is unnecessary. File access is slow. Always store data in variables like matrices of vectors first, then after the loop, store the variable in a file.
I also would like to work from "today" to "yesterday" by stepping in the variables o and c from i=1 to numElems-2.
After everything is stored in tricond I save the results in a file. here is my take on it:
str = urlread('http://ichart.yahoo.com/table.csv?s=AMZN&a=0&b=1&c=2008&d=0&e=31&n=2014&g=d&ignore=.csv');
i = 0;
[dates, o, h, l, c] = dataread('string',str,'%s%f%f%f%f%*f%*f','delimiter',',','headerlines',1);
dates = datenum(dates);
numElems = length(dates);
tricond = zeros(numElems, 1, 'uint8'); %binary vector
fd = fopen('output.txt','w');
for i=1:numElems-2
%Say we think of today as 'i', the yesterday will be i+1
%The condition can be phrased as:
%"Opening price today is larger than open pice for yesterday and...
%the closing price for today is larger than yesterday and...
%the opening price for day before yesterday is smaller than the closing
%price for that day"
if o(i) < o(i+1) & c(i) > c(i+1) & o(i+2) < c(i+2)
tricond(i) = 1;
end
end
fprintf(fd, '% g\n', tricond); %save the integers
  4 commentaires
Dipesh Ramesh
Dipesh Ramesh le 22 Juil 2014
At the moment the program reads last two lines from the file, and it builds its way up. Can i not make it so that it reads the line from top
Matz Johansson Bergström
Matz Johansson Bergström le 22 Juil 2014
My solution actually starts from i=1, so it starts from the top of the file. If you want it so iterate in the other way you could instead write
i = numElems-2:-1:1, similar to what you do in your question.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by