User Tools

Site Tools


user:smalik2:portfolio:cprogproject4

Project: STOCK CHART CANDLES

A project for C/C++ Programming by Saad Malik during Spring 2012.

This project was begun on April 27, 2012 and is anticipated to take the weekend to complete. Project was completed on April 28, 2012.

Objectives

The purpose of this project is to make a program that will use stock information to depict useful things about said information.

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

The purpose of this project is to take a file with stock information, and using the data from that file output the high, low, open, and close of the stock. It will also visually output a Candlestick chart.

The candle stick works as such.

At the top, you have the high, where the wick meets the body is the close (if it's a white candle, if it's black it's the open), where the bottom of the body meets the bottom wick it is the open (again, switched if it's a black candle), and the bottom of the bottom wick is the low.

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 <stdio.h>
#include <stdlib.h>
 
 
int main(int argc, char  **argv)
{
    if (argc != 2)
    {   
        printf("Program needs a stock chart file as a command line argument to run");
        return 0;
    }   
 
    FILE *stockfile = fopen(*(argv+1), "r");
 
    if (stockfile == 0)
    {   
        printf("Could not open file, double check spelling and whatnot");
        return 0;
    }   
 
    float *values;
    values = (float*)malloc(sizeof(float)*255);
    char  *stocksymbol;
    stocksymbol = (char*)malloc(sizeof(char)*10);
    int count = 0;
    fscanf(stockfile, "%s", stocksymbol);
 
    fscanf(stockfile, "%f", (values+count));
    while (*(values+count) !=-1.0)
    {   
        ++count;
        fscanf(stockfile, "%f", (values+count));
    }   
    count = count-1;                                                                                                                                         
 
    float open=*(values);
    float close=*(values+count);
    int count2;
    float high = *(values);
    for(count2=0; count2<count; ++count2)
    {   
         if(high < *(values+count2))
        {   
            high = *(values+count2);
        }   
    }   
    float low = *(values);
    for(count2=0; count2<count; ++count2)
    {   
        if(low > *(values+count2))
        {   
            low = *(values+count2);
        }   
    }   
    printf("\nStockSymbol: %s\n", stocksymbol);
    printf("OPEN: %.0f    CLOSE: %.0f    HIGH: %.0f    LOW: %.0f\n\n", open, close, high, low);
    printf("High (%.2f):\t*", high);
 
    for(count2=0; count2<=(high-close); ++count2)
    {   
        printf("\n%17s", "*");
    }   
    printf("\n%19s", "*****");  
 
    for(count2=0; count2<=(close-open); ++count2)
    {   
        printf("\n%19s", "*   *");
    }   
    printf("\n%19s", "*****");
 
    for(count2=0; count2<=(open-low); ++count2)
    {   
        printf("\n%17s", "*");
    }   
    printf("\nLow (%.2f):\t*\n\n", low);
 
    fclose(stockfile);
    return 0;
}

Execution

lab46:~/src/cprog/projects/project4$ ./Project4
Program needs a stock chart file as a command line argument to runlab46:~/src/cprog/projects/project4$ ./Project4 BLAHBLAH
Could not open file, double check spelling and whatnotlab46:~/src/cprog/projects/project4$ ./Project4 stock4.csv 

StockSymbol: IR
OPEN: 39    CLOSE: 47    HIGH: 52    LOW: 26

High (51.83):	*
                *
                *
                *
                *
                *
                *
              *****
              *   *
              *   *
              *   *
              *   *
              *   *
              *   *
              *   *
              *   *
              *****
                *
                *
                *
                *
                *
                *
                *
                *
                *
                *
                *
                *
                *
                *
Low (25.98):	*

lab46:~/src/cprog/projects/project4$ 

Reflection

This project was a piece of cake. It only took me a few hours to get some of the file input stuff figured out, and after that it was smooth sailing.

It served as a good brushing up on handling information from files.

References

user/smalik2/portfolio/cprogproject4.txt · Last modified: 2012/04/28 21:27 by smalik2