Skip navigation.
Home

Quantitative Analysis: Determine If A Price Is Rising Or Falling

Often we are presented with a set of prices for something we are considering selling or purchasing.  We can scan the data with our eyes and determine this, but how can we do so with the computer?  We simply draw a "line" between the starting price and the ending price for a given price period.

Once we have this line, we need to determine its slope.  If the slope is less than zero, the price is falling.  If the slope is zero, the price is staying steady.  If the slope is greater than zero, then the price is rising.

Algebra Refresher!  Here is a quick derivation of how we get to our formula for calculating the slope:

So lets say we have a bunch of pricing data chronologically ordered and equally spaced chronologically that read as:

10.11
12.30
15.60
12.80
23.60
16.20
11.23

We want to know between given time periods whether the stock is rising or falling.  For our purposes, the x axis will be a number representing the entry.  If one were to have two peices of data, date and price, one would then use the date (suggest using a timestamp) as the x axis.  This of course leaves the price on the y-axis which is normally how humans interprete such data visually on a cartesian coordinate chart.

Reviewing the code below, you will see that we keep our price data in a STL vector.  This allows some automatic variability when loading and unloading data sets.  The index is our time period and the vector stores the price at that time period.  This is also the purpose of NumberOfEntries and how it might reset the EndPeriod.

An additional bonus to using a vector as that additional statistical processing can be done on the data set (more on that in future blogs.)

ReadData() is pretty straight forward, we simply pull the data from the data file into the vector.  As the commenting notes, in real life you are probably going to want to pull this sort of information out of a database, but it works for our illustration of the concept.

Our LinearSlope() function simply implements the equation we came to above.

Once we have our slope, back in the main() function, we can offer some analysis based on it's value relative to zero.

As you might expect, a future blog entry will be finding the upper and lower boundary of a set of data.  The purpose for this is if a price stays within a boundary it normally oscillates within, you can be comfortable - but if it steps out of the boundary - decisions can be made!

Source Code:

//
//  Quantitative Analysis Blog Entry
//  Determine if a price is rising or falling over a time period
//  Scott Auge sauge@amduus.com
//

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

//
// Prototypes
//

double LinearSlope (vector<double> *, int, int) ;
int ReadData (char *, vector<double> *);

//
// Entry point
//

int main (int argc, char * const argv[]) {

  vector<double> ChartData;
  int NumberOfEntries;
  double OurSlope;
 
  int StartPeriod = 3;
  int EndPeriod = 9;
    
  // For speed, we'll reserve here and now instead of while reading in the file
  // Normally we want a database to call from with a count (*) to determine our reserve.
    
  ChartData.reserve(10);
 
  // Insert the goods into our vector
  // Assume entries are chronologically equally spaced apart.
 
  NumberOfEntries = ReadData ("/Users/scott_auge/Documents/Code/cpp/Blog/Slopes/SampleChartData.d", &ChartData);
  if (EndPeriod > NumberOfEntries - 1) EndPeriod = NumberOfEntries - 1;  

  // Now we have some data, lets play!
    
    OurSlope = LinearSlope (&ChartData, StartPeriod, EndPeriod);
    
    cout << "Our Slope for Period: " << OurSlope << endl;
    
    // Once we have our slope, we can give some advice!

    if (OurSlope > 0)
      cout << "Analysis: Price is increasing" << endl;
    else if (OurSlope == 0)
      cout << "Analysis: Hold" << endl;
    else
      cout << "Analysis: Price is falling!" << endl;
     
    return 0;

}

//
// We let the computer review the price over time to determine if it is increasing
// or decreasing.  We do this by the simple algebraic formula for determining the
// slope of a line.  The line is what we draw through the starting period price
// through the ending period price.
//

double LinearSlope ( vector<double> *ChartData, int StartPeriod, int EndPeriod) {

  double Slope = 0;

  // Calc slope with algebraic equation for slope of a line
 
  Slope = (ChartData->at(EndPeriod) - ChartData->at(StartPeriod)) / (EndPeriod - StartPeriod);
 
  return Slope;
 
}

//
// Load data (no error checking)
// Normally we are pulling this from a database, not an OS file.
//

int ReadData (char *FileName, vector<double> *V) {

  ifstream din;
  double Price;
  int DataCount = 0;

  din.open(FileName);
 
  while (! din.eof()) {
 
    DataCount++;
    
    din >> Price;
    
    V->push_back(Price);
    
  }
 
  din.close();
 
  return DataCount;

}
AttachmentSize
CalculateSlope.jpg43.16 KB