A project for C/C++ Programming by Josh Cavaluzzi during the Spring 2012.
This project was begun on DATE and is anticipated to take two days at most to complete. Project was completed on MONTH DAY, YEAR.
State the purpose of this project. What is the point of this project? What do we hope to accomplish by undertaking it?
In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:
State the idea or purpose of the project. What are you attempting to pursue?
You'll want to fill this section out with more detailed background information. DO NOT JUST PROVIDE A LINK.
Providing any links to original source material, such as from a project page, is a good idea.
You'll want to give a general overview of what is going to be accomplished (for example, if your project is about installing a web server, do a little write-up on web servers. What is it, why do we need one, how does it work, etc.)
Time to theme our exploration of programming in a different context- the financial market.
While you may not be familiar with the stock market and the “technicals” that are in the thoughts of traders, they are deeply rooted in algorithms, and need to be implemented into programs to aid traders in visualization, tracking, and predicting trends.
One such unit of measurement is the stock chart candle, which can be used on a number of time-specific stock charts (yearly, monthly, weekly, daily, hourly, minutely, etc.)
Regardless of the time period, the candle can be used to derive all sorts of information:
And many other more subtle points of analysis.
There are many great resources out on the internet covering the history and background of the candle, so please go and familiarize yourself with this before proceeding with actual program implementation. If you do not understand the why and how of a candle, you cannot successfully write a program to utilize them. You also are not eligible for receiving any help on program implementation for this project unless and until you can demonstrate familiarity with the concept of stock chart candles. Multiple violations of this will result in automatic deductions from your final project assessment; if you do not read this or do the necessary background research, it will not be spoon fed to you.
For this project, our task will be to implement a program the can read stock data from a text file, and textually render a single candle from that data.
I will be providing a few different data files, and your program must flexibly work with all of them (and any other file of similar format that may be introduced).
So, please implement a program that does the following:
I would also like each person to do their own post-visualization candle analysis. What type of candle is it?
Data file for use with the project can be found on Lab46 in: /var/public/cprog/data/spring2012/project4/
If you'd like an extra challenge, implement logic that “formats” the visualized candle to fit on a standard terminal display (assume 80×25).
The C code:
#include<stdio.h> #include<stdlib.h> int main( int argc, char **argv ) { if( argc != 2 ) { fprintf( stdout, "You need to include a stock chart file in the command line, please run the program again." ); return 0; } FILE *sfile = fopen( *( argv + 1 ) , "r" ); if( sfile == 0 ) { fprintf( stdout, "Something went wrong, try again.\n" ); return 0; } // Array for holding the different stock values float *stocks; stocks = ( float * ) malloc( sizeof( float ) * 255 ); // Array for the stock symbol char *abrev; abrev = (char *) malloc(sizeof(char) * 10 ); // Integer for assigning the values to the stocks array and the abrev array int i = 0; // Accept and assign the first value in the file, which is the stock symbol, to the abrev array fscanf( sfile, "%s", abrev); // Assign the rest of the values to the stocks array fscanf( sfile, "%f", (stocks + i)); while( *( stocks + i ) != -1.0 ) { i++; fscanf( sfile, "%f", ( stocks + i )); } // Have to assign the first value, the open value, to the open variable float open = 0.0; open = *(stocks); // Same thing goes for the close value i--; float close = 0.0; close = *(stocks + i); // Have to create a variable to determine which of the values is the largest float high = 0.0; int counti; for( counti = 0; counti < i; counti++ ) { if( high < *(stocks + counti) ); { high = *(stocks + counti); } } // Do the same thing for the lowest value float low = *(stocks); for( counti = 0; counti < i; counti++ ) { if( low > *(stocks + counti) ) { low = *(stocks + counti); } } // Now, all of the data must be printed printf("Stock Symbol: %s", abrev); printf("High: %.2f\tOpen: %.2f\tClose: %.2f\tLow: %.2f\n\n", high, open, close, low); printf("High: %.2f\t*", high ); // Now, I must print the candlestick chart int count; for( count = 0; count <= ( high - close ); ++count ) { printf("\n%17s", "*"); } printf("\n%19s", "*****"); for( count = 0; count <= (close - open); ++count) { printf("\n%19s", "* *"); } printf("\n%19s", "*****"); for( count = 0; count < (open - low); ++count) { printf("\n%17s", "*"); } printf("\nLow: %.2f\t*\n\n", low); return 0; }
An example run of your code (be sure to show off all operations):
lab46:~/src/cprog/Projects/project4$ ./project4 stock1.csv Stock Symbol: GLW High: 22.33 Open: 13.92 Close: 22.54 Low: 11.64 High: 22.33 * ***** * * * * * * * * * * * * * * * * * * ***** * * * Low: 11.64 * lab46:~/src/cprog/Projects/project4$
I ran into a big problem with printing the Stock Symbol. For some reason, the value in the array would change after the 67th time running the while loop to assign all of the stock values, which is very weird. I couldn't seem to find out how to fix it, so I just printed it when it wasn't screwed up. The rest of the project went along pretty smoothly.
EDIT:
I fixed the problem, thanks to Saad!
In performing this project, the following resources were referenced: