Check Your Stock Gains – Solution

The challenge for this month’s Exercise is to determine the greatest price gain for a stock during the trading day. The gain is calculated moving forward in time, from a low to a high. It’s easy to see with human eyeballs looking at a chart, but not so easy when you must code a solution.

A program with this level of complexity involves several steps. I puzzled over it for a long time. The approach I take is this:

  1. Moving forward through the prices, determine the maximum gain for each 30 min time period.
  2. Given the period with the greatest gain, obtain the starting time and value.
  3. Use the gain itself to determine which time period has the ending value.
  4. Output the timestamp for the low and high values, along with their values.

Even expressed as a list, these steps seem clunky. So why not dive right in?

In my solution, Step 1 is accomplished by a getrange() function. It plows through the stock price array and returns the maximum difference between the current element and the remaining elements. This function is similar to a common max() function, though it determines the maximum difference for each subsequent element of the array:

int getrange(int t,int s[])
{
    int i,start,largest,spread;

    largest = 0;
    start = s[t];
    for( i=t+1; i<SIZE; i++ )
    {
        spread = s[i] - start;
        if( spread > largest )
            largest = spread;
    }

    return(largest);
}

In the main() function, the calculated maximum range values are saved in the range[] array:

/* evaluate increases */
for(t=0; t<SIZE; t++)
{
    range[t] = getrange(t,stock_price);
}

For this Exercise, array range[] contains these elements:

5 4 6 7 5 3 1 2 3 0 0 6 3 2 0

The greatest range returned from the getrange() function is 7 at element number 3 (the 4th element). Of course, you and I see this item with our eyeballs. The computer must evaluate the array to find the largest value. The element number for this value is stored in variable low, which is Step 2:

/* determine the maximum spread */
max_spread = low = high = 0;
    /* find the low value */
for( t=0; t<SIZE; t++ )
{
    if( range[t] > max_spread )
    {
        max_spread = range[t];
        low = t;
    }
}

Variable max_spread is put to work finding the ending value’s array element, which is Step 3. This value (the element number) is stored in variable high:

    /* find the high value */
for( t=low+1; t<SIZE; t++ )
{
    if( stock_price[t]==stock_price[low]+max_spread )
    {
        high = t;
        break;
    }
}

Each array element is compared with the high value, calculated as the stock_price[low] value plus max_spread. When this element is found, its element number (not the value) is saved in variable high. (You need not save the value, because the element number links to the value, as in stock_price[t].)

Variables high and low now contain the element numbers representing the greatest price increase throughout the day as stored in the stock_price[] array. The code outputs these values, which looks like this:

The best time to buy a share is at 10:30, $6/share
The best time to sell is at 1:30, $13/share

Click here to view my full solution.

One major difference between the solution to this month’s Exercise and last month’s code, is that I created a separate timestamp() function. This function takes the array element number and returns an hour-minute timestamp string. It was necessary to create this function to cleanup the code.

I hope your solution met with success. I hope there’s a more elegant way to solve the puzzle. I feel mine is rather clunky, but it works.

Leave a Reply