Trouble Merging two working programs

Hi,
I am having issues with the two programs that I have included below. The first program asks the user for a stock ticker, start date, and end date from which it strings together the url for yahoo finance and pulls up the website. Ideally this would automatically download the information in the format the next function needs it in, but as it is now the user has to open the csv file in excel, delete some columns, and add a -1 to the end to end and save it as a text file. This is fine, but if anyone has some ideas on making this a more efficient process I would love to hear.

The next program takes each of the prices in the new file, calculates the return ((new price - old price)/new price), and then takes each of these returns and subtracts it from the average return and squares the difference. Finding the variance of the returns. Each of these programs work fine by themselves, but when I combine them for some reason it give me weird output.

I have included the first program, which works fine by itself and when combined (pulling up the website), and the second program ( which calculates the correct average and standard deviation of .001451 and .039252. I didn't include the third program, but it is simply the two programs in the same main function exactly as the are below, and the website comes up fine, but the average and variance print out as -1.#QNANO. I have tried writing the end several different ways, and can get the average to print out correctly sometimes but never the standard deviation.

I have also included the input file I am using so you can see the format being used, I have tried this with several different companies and dates of returns and get the same error. I have only been coding for about 7 weeks now, so I know there are probably many things I should have done differently. If anyone could help me to get this working it would be greatly appreciated.

Code to pull up website:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* Carter Young
October 17, 2013
Trying to get to yahoo finance and
dowload the historical prices for
amount of time*/

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <windows.h>
#include <math.h>


int main()
{

    // To initialize the variables
    char my_stock[10], new_stock[10], company_name[100], freq[10];
    int start_month, start_day, start_year;
    int end_month, end_day, end_year;
    char s_month[10], s_day[10], s_year[10];
    char e_month[10], e_day[10], e_year[10];

    // To read in the stock from the user
    printf("What is the stock ticker that you would like to use?\n");
    scanf("%s", &my_stock);

    // To read in the start date from the user
    printf("Please enter the date you would like to start with\n");
    scanf("%d", &start_month);
    scanf("%d", &start_day);
    scanf("%d", &start_year);

    // To read in the end date from the user
    printf("Please enter the date you would like to end with?\n");
    scanf("%d", &end_month);
    scanf("%d", &end_day);
    scanf("%d", &end_year);

    // To print out dates and verify accuracy
    printf("The data will start on %d/%d/%d and end on %d/%d/%d\n",
    start_month, start_day, start_year, end_month, end_day, end_year);

    // To determine frequency of his price
    printf("At what frequency would you like to pull the prices: Daily (d),\n Monthly (m), or yearly (y)?\n");
    scanf("%s", &freq);

    // To decrease start and end months by one to conform to url string
    start_month--;
    end_month--;

    // To convert integer variables into strings to converge into url string
    itoa(start_month, s_month, 10);
    itoa(start_day, s_day, 10);
    itoa(start_year, s_year, 10);
    itoa(end_month, e_month, 10);
    itoa(end_day, e_day, 10);
    itoa(end_year, e_year, 10);

    // To build the yahoo finance url and print it out
    char s1[100] = "http://finance.yahoo.com/q/hp?s=";
    strcat(s1, my_stock);
    char s2[10] = "&a=";
    strcat(s2, s_month);
    char s3[10] = "&b=";
    strcat(s3, s_day);
    char s4[10] = "&c=";
    strcat(s4, s_year);
    char s5[10] = "&d=";
    strcat(s5, e_month);
    char s6[10] = "&e=";
    strcat(s6, e_day);
    char s7[10] = "&f=";
    strcat(s7, e_year);
    char s8[10] = "&g=";
    char s9[15] = "&ignore=.csv";
    strcat(s8, freq);
    strcat(s1, s2);
    strcat(s1, s3);
    strcat(s1, s4);
    strcat(s1, s5);
    strcat(s1, s6);
    strcat(s1, s7);
    strcat(s1, s8);
    strcat(s1, s9);
    printf("%s\n\n", s1);

    // To open of the url
    ShellExecute(NULL, "open", s1, NULL, NULL, SW_SHOWNORMAL);
    return 0;
}


Code to calculate variance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main()
{

    // To initialize variables
    FILE *ifp = fopen("hist_prices.txt","r");
    float current_price;
    float prices[500];
    float returns[500];
    float ssd[500];
    float sum_ssd = 0;
    float average_return, variance, standard_deviation;
    int j = 0, i = 0, k = 0;

    // To read prices into array
    while (current_price != -1)
    {
          fscanf(ifp, "%f", &current_price);
          if (current_price == -1)
             break;
          prices[j] = current_price;
          j++;
    }

    j--;

    // To read returns into array
    for (i = 0; i < j; i++)
    {
        k = i + 1;
        returns[i] = (prices[i] - prices[k])/prices[k];
        average_return += returns[i];
    }

    // To calculate the average return
    average_return = average_return / j;
    i = 0;


    // to read squared deviations into the array
    for (i = 0; i < j; i++)
    {
        ssd[i] = pow((returns[i] - average_return), 2);
        sum_ssd += ssd[i];
    }

    // To calculate variance and print out the results
    j--;
    variance = sum_ssd/j;
    printf("The variance of the historical returns is %f\n", variance);
    standard_deviation = sqrt(variance);
    printf("The standard deviation of the historical returns is %f\n", standard_deviation);
    return 0;
}


Once again any help would be greatly appreciated,
Carter
Last edited on
I guess you can't attach files, the input file is simply as follows:


13.06
12.89
12.98
12.92
12.87
13.02
13.12
13.09
13.09
13.13
13.21
13.13
13.04
13.34
13.46
13.83
13.63
13.27
13.09
14.15
14
13.33
13.65
13.85
13.85
14.02
13.88
13.84
14.46
14.14
13.73
13.37
13.35
13.21
13.35
13.12
12.92
13.39
13.22
13.39
13.68
13.42
13.92
14.09
14.17
14.19
14.63
14.42
14.65
14.39
14.48
14.89
15.61
15.3
15.82
16.44
16.48
16.62
16.68
16.09
17.83
15.14
15.39
15.7
14.78
15.17
15.48
15.28
14.71
14.85
14.11
14.01
13.97
13.77
13.55
13.22
12.84
13.1
13.22
12.93
12.98
12.79
12.68
12.8
12.71
12.81
12.91
12.77
12.76
13.34
13.02
13.18
12.54
12.87
12.06
11.93
11.87
11.54
11.47
11.39
11.55
11.38
11.33
11.58
11.51
11.7
11.88
11.29
8.25
8.66
8.2
8.42
8.61
8.61
8.63
8.36
8.28
8.19
8.61
8.52
8.31
8.42
8.45
8.63
8.36
7.95
7.5
7.45
7.35
7.61
7.75
7.81
7.86
7.77
7.58
7.86
7.62
7.75
7.95
7.89
8.08
8.19
8.16
8.1
7.99
8.13
8.41
8.32
8.62
8.57
8.46
8.45
8.44
8.42
8.4
8.37
8.58
8.48
8.84
9.25
9.28
9.86
8.62
8.78
8.74
9.07
9.27
9.37
9.59
9.21
9.3
10.43
10.46
10.43
10.57
10.7
10.5
10.88
10.89
11.04
11.33
11.26
10.91
11.2
11.63
11.75
12.1
11.97
11.85
12.14
12.3
12.26
12.85
12.62
12.81
12.89
13.04
13.14
13.27
13.06
12.85
13.13
12.92
13.37
13.1
12.94
12.68
13.09
13.88
14.29
13.54
14.48
15.55
15.78
15.38
13.51
13.55
13.59
13.6
13.18
13.13
13.09
12.89
12.68
12.6
12.55
12.63
12.41
12.12
11.99
11.62
11.55
11.49
11.54
11.59
11.25
11.49
11.46
11.74
11.41
11.03
10.97
11.25
11.08
10.91
10.78
10.54
10.53
11.55
12.34
-1
Topic archived. No new replies allowed.