How to read CSV files(notepad)

Can anyone help me writing a code for this program.

Fields: TimeStamp,TransactionID,SYMBOL,INSTRUMENT,EXPIRY,LTP,LTQ

Write a class to compute the Open, High, Low, Close values of the LTP and the total quantity traded based of the LTQ. The bar size should be variable. You may implement an overloaded constructor / setter method for this information to passed to the instance of your class.

Note that each bar's result should be cached in the class into private data structures with access granted via public getter methods.

Sample Data

"20120327 09:15:01",1,"NIFTY","FUTIDX",20120329,5245,400
"20120327 09:15:01",5,"NIFTY","FUTIDX",20120329,5240.15,50
"20120327 09:15:01",7,"NIFTY","FUTIDX",20120329,5242,50
"20120327 09:15:01",8,"NIFTY","FUTIDX",20120329,5243,50
"20120327 09:15:01",9,"NIFTY","FUTIDX",20120329,5244,50
"20120327 09:15:01",10,"NIFTY","FUTIDX",20120329,5244,50
"20120327 09:15:01",11,"NIFTY","FUTIDX",20120329,5244,200
"20120327 09:15:01",12,"NIFTY","FUTIDX",20120329,5244.7,100
"20120327 09:15:01",13,"NIFTY","FUTIDX",20120329,5245,50
"20120327 09:15:01",14,"NIFTY","FUTIDX",20120329,5245,50
"20120327 09:15:01",15,"NIFTY","FUTIDX",20120329,5247,50
"20120327 09:15:01",16,"NIFTY","FUTIDX",20120329,5247.55,50
"20120327 09:15:01",18,"NIFTY","FUTIDX",20120329,5247.55,50
"20120327 09:15:01",19,"NIFTY","FUTIDX",20120329,5248,50
"20120327 09:15:01",20,"NIFTY","FUTIDX",20120329,5248.95,50
"20120327 09:15:02",26,"NIFTY","FUTIDX",20120329,5246,200
"20120327 09:15:02",27,"NIFTY","FUTIDX",20120329,5240.8,50
"20120327 09:15:02",28,"NIFTY","FUTIDX",20120329,5246,250
"20120327 09:15:02",29,"NIFTY","FUTIDX",20120329,5240.8,50
"20120327 09:15:02",31,"NIFTY","FUTIDX",20120329,5240.8,200
"20120327 09:15:02",49,"NIFTY","FUTIDX",20120329,5240.8,50
"20120327 09:15:02",50,"NIFTY","FUTIDX",20120329,5240.8,50
"20120327 09:15:02",58,"NIFTY","FUTIDX",20120329,5240.8,50
What have you tried?

What is a "bar"?

Bar is basically representation of a collected data which stores Open High Low Close. Likewise here in the above sample data... from the time 09:15:01 to the last 09:15:01, the value in this time frame would create a bar.

Next Time stamp would start from 09:15:02 to last 09:15:02.

So each time stamp would construct a bar. Which would have the following data Open High Low Close. Open would be starting of the time stamp (means the first 09:15:01, and close would be the last timestamp i.e. 09:15:01, High would be the highest number in the time stamp series and low would be the lowest in the timestamp series.)

Now the row we have to look into to find out open high low and close would be the second last comma separated field which starts from 5245, 5240.15, 5242,5243...etc. The last field is called LTQ means last traded quantity...

Example of the data field... of the first row

20120327 09:15:01 = Timestamp
1 = Transaction ID
Nifty = Symbol
FUTIDX = INSTRUMENT
20120329 = EXPIRY (2012 YEAR, 03 MONTH, 29 DAY)
5245 = LTP(Last Traded Price)
400 = LTQ

So in Bar...we have to find out four thing i.e. open high low close with comparison of LTP.

For any further clarification kindly do ask.

I am trying to write the code since yesterday...

right now oly able to create a class

"-----------------------------------------------------------
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

struct Candle
{
std::string tradeTime;
float open;
float high;
float low;
float close;
int totalQty;

Candle(std::string tradeTime, float open, float high, float low, float close,
int totalQty)
{
this->tradeTime = tradeTime;
this->open = open;
this->high = high;
this->low = low;
this->close = close;
this->totalQty = totalQty;
}
};


right now oly able to create a class

Okay so you have a class. Can you show me how you are going to use this class? Show a main() that creates an instance of this class using your constructor. And you may need to show how/where the data you used to construct your class was obtained.


Oh and I really suggest you get used to using the class keyword instead of the struct keyword so you get to understand the difference.


Hi jlb,

Thanks for the suggestion. I will try to implement class keyword instead of the struct keyword. I am still in search of as how i am going to use this class in main(). I need some time for it...thats why i was looking for some help online...

I have a notepad file and from that i have to read the text with comma separated column.
Last edited on
IMO your problem with this assignment is that you really don't understand the problem, which makes it hard to know where to start. So start simple.

Do you know how to declare a class and create an instance of this class in main()?

Do you know how to open a file for reading?

Do you know how to extract items from any type of text file?

jlb, at some extent you are right... The problem i do understand what i have to achieve...

But i don't know how to open a file for reading?

and how to extract items from any type of text file?

I just have started learning this C++...its been some 2 weeks since i have started learning this language...

Can you help..?
Then I suggest you start by studying this tutorial: http://www.cplusplus.com/doc/tutorial/files/

That tutorial should give you the basics, and along with your text book should start you on your way.

Thanks mate... I was looking specific information about it...:-)
What kind of specific information? If you have no idea how to read a text file then you need to start from the basics. Reading CSV files is not difficult, but it is above the basic file reading level. And since you state you don't have a basic understanding of file reading you need to start with the very basics. If you want specific information I really suggest you talk to your instructor or the TAs.
Sorry my mistake... i meant to say i was specifically looking for this kind of link which you just sent me above...
Topic archived. No new replies allowed.