Designing Class for Holding Historic Values

Hi,

Please help in designing a class for holding the historic closing value for a stock along with the date.
The class should be able to store the Historic value for the stocks and when needed should be able to retrieve all the historic values for a stock along with closing value and date.

With Regards
All the duty homework programming staff are at the corporation headquarters party at the moment.

Best wishes :)
I took a break from the party. What do you need help with, litusahoo?
what resolution? The prices update every few seconds these days, or you can get day start and day end for each day, etc?

I think you are going about it wrong (don't store the history in one class, but rather make a container of snapshots of historical data). But I take a keep it simple approach. I would just make a snapshot class here: stock name, timestamp, value seem like the pieces you need. Then just make a vector of those sorted on timestamp (probably, a vector of vectors, one level for all stocks, the inner layer for that stock's full data). Closing time is the same most days but once in a blue moon they shut it off early -- you can add a boolean for is closed value or you can just capture the value at regular close (it won't change from actual close on the early days) if that is good enough. From there you can take it up a notch depending on what you wanted to do, for example you can use an integer that is an offset into your vector as a skip-list data structure type idea to traverse {days, months, years} at a single leap, for faster searching of specific days (faster than binary search if you do this right). The biggest issue you have to answer is how many data points you keep per day and how many years history you want for ancient stocks, and how to handle that if it becomes huge. Stock name could be factored out to save space if you decide to store a LOT of data. That would become necessary if you want 'extra' info like the full company name, profile, and other one-copy-needed info for each.

Some of the above is probably overkill if this is classroom work... you have to tell us what you need better if this isnt useful..
Last edited on
Sheesh! OP asked for the time, not how to build a freakin’ watch.
Hi

I want to design a class like this where in a key of a map the class object can be there, and in key it will be a combination of date and the closing price of the stock.there will be multiple data for a stock along with date and price.

if the designing of my class is not correct, you can help me in correcting it.

The problem i see here is there will be a stock "APPL" which will be an object of Stock Class, how to store that object along with the multiple days data with date and value.

class Stock
{
private:
std::string Name;
std::string Date;
std::string Value;
std::map<Stock,std::map<std::string,std::string>> mStockData;
public:
bool GetStockVal();

bool operator<(const Stock& instObj1) const {
if(instObj1.Name<this->Name)
return true;
}
};

Regards
Where you are making a (common) mistake is combining the Stock_Item and Stock_List as a single Object.

Make them 2 separate classes and you can make it sing.

The Stock_Item is obvious, while the Stock_Pricing_History_List whatever if you like encapsulates the <map> and relevant methods to suit your application. The key links the two just like a relational database.

You don't have to change much else so it's not a matter of throwing your existing code away.
Last edited on
For an individual stock, you can simply do:
map<date, double>; // map closing date to closing price.

And if you have many stocks do deal with, map the ticker to the above:
map<string, map<date, double> >; // maps ticker symbol to closing date/price data

In a real life application there would be more info on a stock and more prices than just the closing price, but this might suffice for your purposes.

Now comes the hard part, what is a "date??" How will you represent it? You could do something like Julian date and lookup code on the web to convert month/day/year to Julian date.
an easy way to deal with dates is the simple sortable integer format, eg 20200115.
you can do most of the simple date things on them without the clutter or performance hits of the heavy date objects. The date tools are there if you need something complicated, though.
Last edited on
Hi dhayden

I understand this is the easiest way to map<string, map<date, double> >;Ric as key in primary map and date as key in secondary map associated with value.

But just wanted to learn some class concepts with these maps and all, so was thinking to do in a class oriented way.

Hi againtry

i understand what you are trying to say , but getting no idea how to design the two classes to for m a primary key relationship between them
Ordinary stock prices are determined by the 'course of sales' which record the event when buyer and seller meet and conduct the transaction.

The last sale of the day (or earlier if there is no movement) determines it's closing price

Each course of sale Transaction is a combination of Date/Time, StockID, BuyerID, SellerID, Quantity and SalePrice. For any active stock there might be many thousands of transactions in a day.

Searching every Stock and its Transactions if the two were wrapped into a single Stock object would be 'interesting' to say the least.
Hi againtry

My requirement is not that complicated. Dont need BuyerId and SellerId. need a class design to hold closing values for differerent dates along with stockname.
Hi litusahoo,

I understand what you are driving at and hesitated to comment in the first place, believe it or not :)

However, once you see the power of it you will give yourself greater flexibility if you ever decide to extend it, perhaps even keeping track of a personal portfolio.

A Transaction can be just a StockID, Price & Date as a separate <map> i.e 2 simple/same <maps> or <vectors> linked by StockID.

Obviously I also understand it's your call.
You almost had the "simple table":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>

struct Stock
{
  std::string Name;
  std::string Date;
  int Value;
}

int main() {
  std::vector<Stock> history;
  // Fill history

  // Show data for Google
  std::cout << "Google\n";
  for ( auto point : history ) {
    if ( point.Name == "Google" ) {
      std::cout << point.Date << ' ' << point.Value << '\n';
    }
  }
}

There the company name repeats a lot.

1
2
3
4
5
6
7
8
struct Stock
{
  std::string Date;
  int Value;
}

int main() {
  std::map<std::string, std::vector<Stock>> history;

This time each company has its own table of {Date,Value} and keys of the history are names of companies.
but getting no idea how to design the two classes to form a primary key relationship between them
Sorry, I missed that and made a few assumptions.

Take "Apple Incorporated Common Stock" for instance. Its Nasdaq symbol is "AAPL" which is a unique identifier for that company. That symbol is the primary key that links the two tables, it is unique. The Nasdaq would fail catastrophically otherwise.

See it at first hand:
https://www.nasdaq.com/market-activity/quotes/historical
Hence Stock -> Symbol & Stock type and company namel

Click on "AAPL" and it takes you to the second table:
https://www.nasdaq.com/market-activity/stocks/aapl/historical
(You obviously aren't inerested in modelling all the columns on this table, but you can download some free test data.)
Hence History -> Symbol & Date & Price

The tables <maps> are linked by selecting the Symbol, manually or automatically.


The unique Price identifier (ref_no) is equivalent to the auto-generated key value in OO database systems and is essential to a simple mapping system in the Price table while the link between tables is the nasdaq symbol.

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
92
#include <iostream>
#include <map>

struct Price
{
    int ref_no;
    
    std::string symbol;
    std::string date;
    std::string closing_price;
    
    Price(int aRef, std::string aSymbol, std::string aDate, std::string aClosing_price)
    {
        ref_no = aRef;
        symbol = aSymbol;
        date = aDate;
        closing_price = aClosing_price;
    }
};

std::ostream& operator<<(std::ostream& out, Price aPrice)
{
    out
    << aPrice.ref_no << ' '
    << aPrice.symbol  << ' '
    << aPrice.date  << ' '
    << aPrice.closing_price;
    
    return out;
}

struct Stock
{
    std::string symbol;
    std::string name;
    
    Stock(std::string aSymbol, std::string aName)
    {
        symbol = aSymbol;
        name = aName;
    }
};

std::ostream& operator<<(std::ostream& out, Stock aStock)
{
    out
    << aStock.symbol  << ' '
    << aStock.name;
    
    return out;
}


int main()
{
    Stock apple( "APPL", "Apple Inc. Common Stock");
    Stock starb( "SBUX", "Starbucks Corporation");
    
    Price ap1(1, "APPL", "12/31/2019", "$293.65");
    Price ap2(2, "APPL", "12/30/2019", "$291.52");
    Price ap3(3, "APPL", "12/27/2019", "$289.80");
    
    Price ap5(4, "SBUX", "12/31/2019", "$87.92");
    Price ap7(5, "SBUX", "12/30/2019", "$83.45");
    
    std::map<std::string, Stock> Stocks;
    Stocks.emplace(apple.symbol, apple);
    Stocks.emplace(starb.symbol, starb);
    
    
    std::map<int, Price> Prices;
    Prices.emplace(ap1.ref_no, ap1);
    Prices.emplace(ap2.ref_no, ap2);
    Prices.emplace(ap3.ref_no, ap3);
    Prices.emplace(ap5.ref_no, ap5);
    Prices.emplace(ap7.ref_no, ap7);
    
    for(auto i:Stocks)
    {
        std::cout << i.second << '\n';
        
        for(auto j: Prices)
        {
            if(i.first  == j.second.symbol) // THE 'LINK'
                std::cout << '*' << j.second << '\n';
        }
        std::cout << '\n';
    }
    
    
    return 0;
}



APPL Apple Inc. Common Stock
*1 APPL 12/31/2019 $293.65
*2 APPL 12/30/2019 $291.52
*3 APPL 12/27/2019 $289.80

SBUX Starbucks Corporation
*4 SBUX 12/31/2019 $87.92
*5 SBUX 12/30/2019 $83.45

Program ended with exit code: 0
Registered users can post here. Sign in or register to post.