User Tools

Site Tools


user:dmckinn2:portfolio:cprogproject4

Project: STOCK CHART CANDLES

A project for C/C++ by Daniel McKinney during the spring 2012.

This project was begun on 4/27 and is anticipated to take a few days to complete. Project was completed on 4/29/2012.

Objectives

The purpose of this project is to read different types of data from a file and to display high, low, beginning, and end values, using command line arguments. Also to be able to make a visualization of the data in a file using an array.

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • successful completion of projects up to this point
  • familiarity with file I/O
  • familiarity with arrays

Background

For this project, we are working with some stock market items, Specifically a candle graph. A candle shows the high, low, open, and close of a certain stock. In a candle chart the “wick” represents the highest and lowest price, wile the “body” represents the open and closing price.

Scope

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:

  • direction of trade price
  • magnitude of trade price
  • high of day with respect to opening/closing price
  • low of day with respect to opening/closing price

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:

  • opens a specified data file for reading (command-line arguments might be particularly useful)
  • reads the data to determine the open, close, high, and low
  • displays the stock symbol of the data file in question
  • displays the determined open, close, high, and low to STDOUT
  • utilizes an array for processing of candle visualization
  • textually visualizes the candle to STDOUT

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).

Code

The C code:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <cmath>
using namespace std;
 
int main (int argc, char *argv[]) {
 
float max=0;
float nmax=0;
float min=0;
float nmin=1000;
float candletop = 0;
float candlemiddle = 0;
float candlebottom = 0;
string  candlestick [3] = {"  *  ", "*****","*   *"};
 
if (argc <= 1)
    {
        cout << "ERROR! Must provide CSV file as argument!\n"<< endl;
        exit(1);
    }
 
  int temp=0;
  string stockarray[100];
  string line;
  ifstream myfile (argv[1]);
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
        temp = temp + 1;
      stockarray[temp] = line;
    }
 
        for(int i =2; i<temp; i++)
{
        max =atof(stockarray[i].c_str());
        min = atof(stockarray[i].c_str());
        if(min<nmin)
        {
        nmin=min;
        }
 
        if(max>nmax)
        {
        nmax=max;
        }
}
 
        candletop = nmax - atof(stockarray[temp-1].c_str());
        candlemiddle =abs(atof(stockarray[temp-1].c_str())-atoi(stockarray[2].c_str()));
        candlebottom = atof(stockarray[2].c_str())-nmin;
 
        cout<<"STOCK SYMBOL: "<<stockarray[1]<< endl;
        cout<<"OPEN: "<<stockarray[2]<<"      "<<"CLOSE: "<<stockarray[temp-1]<<"      "<<"HIGH: "<<nmax<<"      "<<"LOW: "<<nmin<<endl;
 
        cout<<"HIGH ("<<nmax<<")"<<endl;
        for(int hpic = 0; hpic<floor(candletop+ 0.5); hpic++)
        {
        cout<<"          "<<candlestick[0]<<endl;
        }
        cout<<"          "<<candlestick[1]<<endl;
        for(int mpic = 0; mpic<floor(candlemiddle+ 0.5); mpic++)
        {
        cout<<"          "<<candlestick[2]<<endl;
        }
        cout<<"          "<<candlestick[1]<<endl;
        for(int lpic = 0; lpic<floor(candlebottom+ 0.5); lpic++)
        {
        cout<<"          "<<candlestick[0]<<endl;
        }
        cout<<"LOW ("<<nmin<<")"<<endl;
 
 
    myfile.close();
  }
 
  else cout << "Unable to open file";
 
  return 0;
}

Execution

An example run of your code (be sure to show off all operations):

lab46:~$ g++ -ostock1 stock1.cc
lab46:~$ ./stock1 stock1.csv
STOCK SYMBOL: GLW
OPEN: 13.92      CLOSE: 12.99      HIGH: 13.92      LOW: 12.6
HIGH (13.92)
            *
          *****
          *****
            *
LOW (12.6)
lab46:~$ ./stock1 stock2.csv
STOCK SYMBOL: F
OPEN: 11.94      CLOSE: 12.52      HIGH: 12.71      LOW: 11.94
HIGH (12.71)
          *****
          *   *
          *   *
          *****
LOW (11.94)
lab46:~$ ./stock1 stock3.csv
STOCK SYMBOL: HDNG
OPEN: 9.45      CLOSE: 9.34      HIGH: 9.74      LOW: 9.19
HIGH (9.74)
          *****
          *****
LOW (9.19)
lab46:~$ ./stock1 stock4.csv
STOCK SYMBOL: IR
OPEN: 39.03      CLOSE: 40.93      HIGH: 41.72      LOW: 39.03
HIGH (41.72)
            *
          *****
          *   *
          *   *
          *****
LOW (39.03)
lab46:~$

Reflection

This project was very interesting. It was cool to see how the candle was visualized using an array. I had a little trouble at first ending my while loop, because i could not use NULL with numerical data. I also had a little trouble with my candle rounding, but i resolved both problems with a little research. The type of candle is a normal candle with a upper and lower shadow.

References

In performing this project, the following resources were referenced:

  • wiki
  • Matt
  • pocket reference
user/dmckinn2/portfolio/cprogproject4.txt · Last modified: 2012/05/01 13:42 by dmckinn2