Starting Point for C++ Program

Discussions about custom-built testing platforms written in C, C++, or Java.
Chris Murphy
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Thu May 29, 2003 12:11 pm
Location: Eugene, OR
Contact:

Starting Point for C++ Program

Post by Chris Murphy »

I am going to code my program in c++ but don't know where to start. I took an intro c++ class in college two years back so I know some basics. However my main problem is that I don't know how to write the code to bring in the data (commodity prices) and then how to spit out the results. Would anyone be willing to email me the c++ code to one of their programs? I don't care how basic it is I just need the structure. I will be able to read it and see whats going on so that I can then add my part to make it do what I want. I just need a launch pad because I really don't know how to begin. I am using CodeWarrior which is what I used for the college course. Is this adequate? Any help is appreciated.

Chris
Chris Murphy
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Thu May 29, 2003 12:11 pm
Location: Eugene, OR
Contact:

Post by Chris Murphy »

I should add that I will be using the system for a custom course in the Masters of Accounting program at the University of Oregon. I will be in my final term this coming fall and will be in a self designed trading system course. Most of the academics do not believe in technical systems and I would like to prove them wrong. The system is similar to the turtle money management scheme but with a trend identification filter that is used to decide the amount of capital to risk rather than a set percent, ie. 1 or 2 percent. Also the breakout number in days is varied based on the trend identification value. Other twists will be added later. I am going to show how money management plays a role in system design to both increase profit and reduce drawdowns. I do not believe that in academics money management is given enough consideration. Just to add, my undergraduate major was both accounting and finance with a minor in mathematics, so no I'm not strictly an accounting guy :D
Last edited by Chris Murphy on Wed Jul 02, 2003 3:10 pm, edited 1 time in total.
Chris Murphy
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Thu May 29, 2003 12:11 pm
Location: Eugene, OR
Contact:

Post by Chris Murphy »

Does anyone know how to read data from a file into a c++ program? I'm trying to do it and the code is not working.
Jason Czech
Roundtable Fellow
Roundtable Fellow
Posts: 68
Joined: Fri May 16, 2003 8:28 am
Location: Atlanta, GA

Post by Jason Czech »

Sounds like a pretty ambitious project given that you have to have it done by the end of the year.

I can't really help you with the file handling myself, but maybe you could look in a C++ text under 'File Handling', or go to google and search 'C++ file handling'.

Good Luck,

Jason
Forum Mgmnt
Roundtable Knight
Roundtable Knight
Posts: 1842
Joined: Tue Apr 15, 2003 11:02 am
Contact:

Post by Forum Mgmnt »

Chris,

I've attached some code that should read a file if you pass it the path to the file. (NOTE: I changed the code around to simplify it so there is a possibility I made a slight error).

There are lots of things that can go wrong when you first start this sort of thing. My first suggestion is not to make any assumptions. Check the errors at every step.

Code: Select all

#include <iostream.h>
#include <fstream.h>

void ReadFromSampleFile( char* filePath )
{
    char              lineBuffer[4096];
    int                openMode;
    ifstream	        sampleFile;
    int                count = 0;

    // Open the sample file.
    openMode = ios::in | ios::nocreate;
    sampleFile.open( filePath , openMode );

    // Check for errors opening.
    if ( !sampleFile.good() )
        {
        // Report the error here.
       }

    // Loop over the file reading in a line at a time.
    while ( !sampleFile.eof() )
        {
        // Get a line from the input file.
        sampleFile.getline( (char *) &lineBuffer, 4096 );
      
        // If we have not had a problem reading the data.
        if ( sampleFile.good() )
            {
            // process the new line here.
            }
	
        // Increment the count.
        count++;
        }

    // Close the sample file.
    sampleFile.close();

} // ReadFromSampleFile
Chris Murphy
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Thu May 29, 2003 12:11 pm
Location: Eugene, OR
Contact:

Post by Chris Murphy »

Thank you for the response c.f.. I was able to get the input (open, high, low, close) inputed into a multidimensional array last night. I did it differently than your code. Mine went something like this:

///////////////////////Code/////////////////////////////////////////////
int Prices[DaysOfData][4];
int InFile Data;
InFile Data("CommodityData.dat");

for (loopcounter=0; loopcounter<DaysOfData; loopcounter++);
{
InFile >> Open >> High >> Low >> Close;
Prices[loopcounter][0]=Open;
Prices[loopcounter][1]=High;
Prices[loopcounter][2]=Low;
Prices[loopcounter][3]=Close;
}

InFile close();
//////////////////////End of Code//////////////////////////////////////

How does my code compare? Yours seems much more complicated, I will try to run it tonight and figure out how it works. Could you please explain why your code is more desireable.

Thanks,
Chris
Chris Murphy
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Thu May 29, 2003 12:11 pm
Location: Eugene, OR
Contact:

Post by Chris Murphy »

FYI, I'm using the book "The C++ Coach" written by Jeff Salvage. It was the book used for an intro to C++ class at the University of Oregon. Pretty basic but it has the information needed for me to start my journey. Last night I was able to write code to read open,high,low,close data from a file and then insert them into an array and calculate the atr. The only problem is that the data could only be read if it was one space apart, since my data is comma delimited this means that I had to manually edit it. I assume this is because I used the following command:
InFile >> Open >> High >> Low >> Close;
Is their a way to have it read a file that is:
100, 101, 98, 99
instead of having to convert it to:
100 101 98 99 (which is how it currently has to be)?

Chris
Forum Mgmnt
Roundtable Knight
Roundtable Knight
Posts: 1842
Joined: Tue Apr 15, 2003 11:02 am
Contact:

Post by Forum Mgmnt »

Chris,

You're running into the "simpler is harder" phenomenon of programming in general and C++. There is a lot of hidden behavior in C++ that is difficult to undersand and often hard to control.

I don't like the stream input and output of C++, meaning the >> and << operators. These operators do different things depending on the type of the operand (in your case int). They will do something different if you use a float or char.

My feeling is that stream operators are great for sample programs but since you don't have any control over what is actually loading the data and saving it you don't have very much flexibility and it makes it hard to know what is not working if you run into a problem. For robust commercial programs this is more trouble that it is worth.

In your case, the problem is that stream operators don't understand comma delimited formats. They just read information from the file. You'll run into problems if the item that it is expecting to read is either not there or of the wrong format.

My code just reads in a single line of characters. You'd need to write even more code to parse out the pieces into the component open, high, low and close. I find the little bit of extra work is worth it.

I also prefer to see what's going on rather than to rely on a bunch of code that gets executed when you reach a } or >> character.

There are many, many different ways to approach a problem like this, or any problem for that matter. The right solution depends on many factors which are probably beyond your ability to appreciate until you write a bunch more code. This is part of the learning process.

The other problem with your code is that there is no error checking. What happens if the file doesn't contain DaysOfData days? Your code assumes you know ahead of time how much data is in the file.

What happens when the file is already open in another program?

What happens when you don't have the correct file name?

Your code will probably create the file and then fill your array with zeros without giving you any indication that there was a problem finding the file. This may not show up until much later when you tried to use the data, making it much harder to find out what the problem is.

This would be especially true if this code worked fine and was used as part of a large system and then one day someone changed the name of a file causing the above behavior. It might take a while to figure out where the problems were coming from.
How does my code compare? Yours seems much more complicated
Actually, neither of these is complicated. These routines would be considered almost trivial by experienced programmers. If you take out the error checking code from my example it is even simpler.
Last edited by Forum Mgmnt on Wed Jul 02, 2003 5:04 pm, edited 1 time in total.
redbullpeter
Roundtable Fellow
Roundtable Fellow
Posts: 82
Joined: Mon Apr 28, 2003 9:12 am
Location: London, UK

Post by redbullpeter »

Take a look at:

www.ta-lib.org

It's an open source C++ project for technical analysis. Included are various modules for importing and handling data.

red
Chris Murphy
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Thu May 29, 2003 12:11 pm
Location: Eugene, OR
Contact:

Post by Chris Murphy »

Thanks c.f.. You will have saved me a great deal of trouble. I'll implement your code this evening and see how it goes.

Chris
Chris Murphy
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Thu May 29, 2003 12:11 pm
Location: Eugene, OR
Contact:

Post by Chris Murphy »

Thanks redbull. I'll check it out!
Chris Murphy
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Thu May 29, 2003 12:11 pm
Location: Eugene, OR
Contact:

Post by Chris Murphy »

Forum Mgmnt wrote:
// Get a line from the input file.
sampleFile.getline( (char *) &lineBuffer, 4096 );
Right after this is where I parse to get the open, high, low, and close and then input them into the PriceData Array, is that correct?
Forum Mgmnt
Roundtable Knight
Roundtable Knight
Posts: 1842
Joined: Tue Apr 15, 2003 11:02 am
Contact:

Post by Forum Mgmnt »

Chris, yes, that's where you'd do it.
Chris Murphy
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Thu May 29, 2003 12:11 pm
Location: Eugene, OR
Contact:

Post by Chris Murphy »

I've downloaded the file from ta-lib and am now trying to use it. The website says:

"To use the library in C/C++ project, you just need to #include "ta_libc.h" and link to the static library corresponding to your type of application."

What does it mean to link to the static library and how do I go about doing this?
Chris Murphy
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Thu May 29, 2003 12:11 pm
Location: Eugene, OR
Contact:

Post by Chris Murphy »

I'm getting the following error message:

Code: Select all

Error   : undefined identifier 'std::basic_ios<char, std::char_traits<char>>::nocreate'
TrendProgression.cpp line 26       openMode = ios::in | ios::nocreate;

Error   : illegal implicit conversion from 'int' to
'std::ios_base::openmode'
TrendProgression.cpp line 27       sampleFile.open( filePath, openMode ); 
From the following code:

Code: Select all

// Libraries
#include <iostream.h> 
#include <fstream.h> 
#include <stdlib.h>

// Prototypes
void ReadFromSampleFile( char* filePath );

// Main Routine
void main()
{
	
ReadFromSampleFile("StockData.dat");

}


void ReadFromSampleFile( char* filePath ) 
{ 
    char               lineBuffer[4096];
    int                openMode; 
    ifstream           sampleFile; 
    int                count = 0; 

    // Open the sample file. 
    openMode = ios::in | ios::nocreate;
    sampleFile.open( filePath, openMode ); 

    // Check for errors opening. 
    if ( !sampleFile.good() ) 
       { 
        // Report the error here.
        cout<<"Error opening file."<<endl; 
       } 

    // Loop over the file reading in a line at a time. 
    while ( !sampleFile.eof() ) 
        { 
        // Get a line from the input file. 
        cout<<sampleFile.getline( (char *) &lineBuffer, 4096 )<<endl; 
      
        // If we have not had a problem reading the data. 
        if ( sampleFile.good() ) 
            { 
            // process the new line here. 
            } 
    
        // Increment the count. 
        count++; 
        } 

    // Close the sample file. 
    sampleFile.close(); 

} // ReadFromSampleFile
Any thoughts as to why? I've looked over this for 3 hours and have no idea.
redbullpeter
Roundtable Fellow
Roundtable Fellow
Posts: 82
Joined: Mon Apr 28, 2003 9:12 am
Location: London, UK

Post by redbullpeter »

Some instructions on linking the libraries are here:

How to start using TA-LIB

The GNU C Programming Tutorial: Putting a program together

ta-lib specific problems would best be directed to:

ta-lib yahoo mailing list or support@ta-lib.org

red
Chris Murphy
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Thu May 29, 2003 12:11 pm
Location: Eugene, OR
Contact:

Post by Chris Murphy »

Thanks redbull, I've found the gnu site very helpful.
Forum Mgmnt
Roundtable Knight
Roundtable Knight
Posts: 1842
Joined: Tue Apr 15, 2003 11:02 am
Contact:

Post by Forum Mgmnt »

Are you including the headers as I listed?

The error you are getting is one that indicates it can't find the definition for ios::nocreate.

The header file iostream.h should include the file ios.h which defines the ios class which has the following enum:

Code: Select all

class _CRTIMP ios {

... some code....

    enum open_mode { in        = 0x01,
                     out       = 0x02,
                     ate       = 0x04,
                     app       = 0x08,
                     trunc     = 0x10,
                     nocreate  = 0x20,
                     noreplace = 0x40,
                     binary    = 0x80 };

... more code....

}
You may have a different version of the compiler and the headers than I do (I'm using Visual C++ 6.0). If so you may have to use different headers to find the right definition.
Forum Mgmnt
Roundtable Knight
Roundtable Knight
Posts: 1842
Joined: Tue Apr 15, 2003 11:02 am
Contact:

Post by Forum Mgmnt »

trendprogression,

You are new enough to C++, based on your questions, that you will run into a lot of these sorts of problems until you start to recognize them and understand how your code relates to the errors. This takes a while. Everyone goes through this.

There are many places that are better equipped to handle your general questions about C++ and programming problems. This really isn't intended to be a C++ programming forum, we are not set up for it.

The answers to almost all your questions can be found on the web if you know how to find them. Developing an efficient way to get answers to questions you have is part of learning to be an efficient developer whether using C++ or any other language.

Most developers, myself included, don't mind helping. We do however, expect you to have done your homework. When you are new, you may not know what this means. You may not be able to tell the difference between a complete newbie problem and one that is more specific and troublesome.

For example: When I get an error I don't understand, I usually copy the text of the error or parts of the error and paste it into google (in between quotes so google searches for the exact phrase). I'll then sift through the answers to see if there are any suggestions that help.

Failiing that, I'll look for similar questions on google, sometimes hitting the "groups" tab on google to pick up questions asked on the newsgroups in addition to those posted on the web.

If I still can't find an answer, I'll make a post somewhere outlining the problem and the approaches I have tried in an attempt to find the solution. This last part is important, it will save a lot of 1) Try this, 2) Already tried that it didn't work, 3) Try that, 4) Already tried that, etc. wasted effort.

You should spend a bit more time trying to develop an efficient approach to finding answers to your questions. Focus as much on your approach as on the specific problem at hand.

I hope this helps. I and others here are happy to answer questions that relate to the intersection of C++ and trading, specific C++ language or compiler questions would be better answered in other places. You'll get your answers quicker too.
Last edited by Forum Mgmnt on Thu Oct 16, 2003 8:27 pm, edited 1 time in total.
Chris Murphy
Roundtable Fellow
Roundtable Fellow
Posts: 84
Joined: Thu May 29, 2003 12:11 pm
Location: Eugene, OR
Contact:

Post by Chris Murphy »

Yes, I am very new. The course was very basic and really only taught you how to loop, use arrays, functions, and a bit on structures. I can write simple code but when it comes to input and output we did NOTHING. We never spoke about creating programs, using libraries, linking and the sort. We made files that would act as a class, bank account, and then ask for input and store the information. Then when a bank account number is entered the data from the class array would be spit out. The course is not enough to do this project. I have just signed up for a course on codewarrioru.com to learn how to create programs in the code warrior development software. After this I'm going to take their c++ courses. I believe that I need to get a better understanding of c++ before I try to create a fully functional program. Thanks for the guidance, much appreciated.
Post Reply