aboutsummaryrefslogtreecommitdiff
path: root/src/history.c
blob: 3f4b0393f18a7ffbc61b164c1f236ec707398a98 (plain)
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
#include "globals.h"

void add_hist(struct stock_t *stock, enum history_action action, ullong count)
{
    /* add a history item for this transaction */
    struct history_item *newhist = malloc(sizeof(struct history_item));
    newhist->action = action;
    newhist->count = count;
    newhist->price.cents = stock->current_price.cents;
    newhist->next = NULL;

    time_t timer = time(0);
    struct tm *tm = localtime(&timer);

    newhist->action_time.year = tm->tm_year - (EPOCH_YEAR - 1900);
    newhist->action_time.month = tm->tm_mon;
    newhist->action_time.day = tm->tm_mday;
    newhist->action_time.hour = tm->tm_hour;
    newhist->action_time.minute = tm->tm_min;
    newhist->action_time.second = tm->tm_sec;

    if(!stock->history)
    {
        stock->history = newhist;
    }
    else
    {
        struct history_item *last = stock->history;
        while(last)
        {
            if(last->next)
                last = last->next;
            else
                break;
        }
        last->next = newhist;
    }

    ++stock->history_len;
}

void print_history(struct stock_t *stock)
{
    assert(stock);

    struct history_item *hist = stock->history;

    while(hist)
    {
        ullong total = hist->count * hist->price.cents;

        output("[%d-%d-%d %d:%02d:%02d] ", hist->action_time.year + EPOCH_YEAR, hist->action_time.month + 1, hist->action_time.day + 1,
               hist->action_time.hour, hist->action_time.minute, hist->action_time.second);

        switch(hist->action)
        {
        case BUY:
            output("[BUY]  %llu shares for $%llu.%02llu each (", hist->count, hist->price.cents / 100, hist->price.cents % 100);
            use_color(COL_GREEN);
            output("+$%llu.%02llu", total / 100, total % 100);
            stop_color(COL_GREEN);
            output(").\n");
            break;
        case SELL:
            output("[SELL] %llu shares for $%llu.%02llu each (", hist->count, hist->price.cents / 100, hist->price.cents % 100);
            use_color(COL_RED);
            output("-$%llu.%02llu", total / 100, total % 100);
            stop_color(COL_RED);
            output(").\n");
            break;
        default:
            output("unknown history enum (%d).\n", hist->action);
            break;
        }
        hist = hist->next;
    }
}