Rate my current method of storing and accessing historical data

Currently I have a function that takes the arguments: (ticker, start_date, end_date), and looks to see if I have a .csv file of that ticker's data on my pc. Right now I'm only dealing with OHLC but I do plan on incorporating other types of data like fundamental or other.

Function:

If I don't have a .csv file of that ticker in the folder where my data is stored:

Pull that ticker's data in the desired date range from the api and save a .csv

If I do have a .csv:

Read in the file and check to see if the desired date range exists

If the date range doesn't exist:

Pull that ticker's data in the desired date range from the api and save a .csv

I don't know anything about database platforms or languages. What are the specific benefits of using something like SQL as opposed to what I'm doing? I'm trying to get an idea of at what point it would be worth it for me to learn something like this.

Posted Jan 22, 2020, 12:30 AM UTC · edited

613
Beliavsky•

To speed things up you could save your data as an unformatted binary file with suffix .bin (for example) and read from that during future runs. I do this.

5
BrononymousEngineerOP•

This is a new concept to me. Is it as simple as using a function from whatever language you're using to just read in the binary file? What does the data look like when you read it in?

1
Beliavsky•

The specifics are programming language dependent, but I think every language has the feature. Two terms used are "pickling" and "serialization".

1
Freed4ever•

As you expand your data into more than just prices (e.g. Fundamentals, sentiments, events, etc.) a relational database will serve you well. If you only have price data then a relational database is not necessary. Just my 2 cents.

3
BrononymousEngineerOP•

This is what I suspected.

1
robo_jojo_77•

Relational databases are useful once the data gets more complicated. The only problem with your method now is that it's going to be overwriting .csv's a lot if you're looking for different time periods of the same ticker. Ideally you'd have a way to append date ranges to a .csv

I'm new to this but based on my experience with pandas in general, you could probably use CSVs even with more stock fundamentals data. Depending on how many extra columns you'd want to add.

Events would be better served by a database though.

1
optionseller•

SQL gives you the flexibility to pick and transform your data at ease. For example, to access the SPY last 10 minutes candles before market closes on Jan 17, 2020:

mysql> select symbol, open, high, low, close, volume, datetime
    -> from minute where symbol='SPY' 
    -> and datetime <=  '2020-01-17 16:00:00' 
    -> order by datetime desc limit 10;
+--------+---------+--------+----------+---------+----------+---------------------+
| symbol | open    | high   | low      | close   | volume   | datetime            |
+--------+---------+--------+----------+---------+----------+---------------------+
| SPY    |  332.02 | 332.02 | 330.8539 |  331.91 | 20628549 | 2020-01-17 16:00:00 |
| SPY    | 331.895 | 332.09 |  331.875 |  332.01 |  2465125 | 2020-01-17 15:59:00 |
| SPY    |  332.07 | 332.08 |   331.86 |   331.9 |   625788 | 2020-01-17 15:58:00 |
| SPY    |  332.14 | 332.18 |   332.07 |  332.07 |   326443 | 2020-01-17 15:57:00 |
| SPY    | 332.055 | 332.16 |  332.055 |  332.13 |   734572 | 2020-01-17 15:56:00 |
| SPY    |  332.04 | 332.09 |      332 |  332.05 |   615337 | 2020-01-17 15:55:00 |
| SPY    | 331.905 | 332.06 |  331.905 | 332.045 |   577719 | 2020-01-17 15:54:00 |
| SPY    |   331.9 | 331.91 |   331.88 |   331.9 |   385084 | 2020-01-17 15:53:00 |
| SPY    |  331.84 |  331.9 |   331.84 |   331.9 |   488369 | 2020-01-17 15:52:00 |
| SPY    |  331.83 | 331.86 |    331.8 |  331.85 |   351272 | 2020-01-17 15:51:00 |
+--------+---------+--------+----------+---------+----------+---------------------+

You can do lots of preprocessing before pulling out the data. So your driver program can be simplified. So you can screen stocks in the database, based on any criteria you want (volume, volatility, net change). For example, you can calculate return like this:

mysql> select symbol, close, 
    -> datetime,
    -> lag(close, 1) over (order by datetime desc) - close as `return`
    -> from minute where symbol='SPY' 
    -> and datetime <=  '2020-01-17 16:00:00' 
    -> order by datetime desc limit 20;
+--------+----------+---------------------+-----------------------+
| symbol | close    | datetime            | return                |
+--------+----------+---------------------+-----------------------+
| SPY    |   331.91 | 2020-01-17 16:00:00 |                  NULL |
| SPY    |   332.01 | 2020-01-17 15:59:00 |   -0.0999999999999659 |
| SPY    |    331.9 | 2020-01-17 15:58:00 |   0.11000000000001364 |
| SPY    |   332.07 | 2020-01-17 15:57:00 |  -0.17000000000001592 |
| SPY    |   332.13 | 2020-01-17 15:56:00 | -0.060000000000002274 |
| SPY    |   332.05 | 2020-01-17 15:55:00 |   0.07999999999998408 |
| SPY    |  332.045 | 2020-01-17 15:54:00 | 0.0049999999999954525 |
| SPY    |    331.9 | 2020-01-17 15:53:00 |   0.14500000000003865 |
| SPY    |    331.9 | 2020-01-17 15:52:00 |                     0 |
| SPY    |   331.85 | 2020-01-17 15:51:00 |  0.049999999999954525 |
| SPY    |   331.84 | 2020-01-17 15:50:00 |  0.010000000000047748 |
| SPY    |  331.785 | 2020-01-17 15:49:00 |   0.05499999999994998 |
| SPY    |    331.8 | 2020-01-17 15:48:00 | -0.014999999999986358 |
| SPY    |   331.71 | 2020-01-17 15:47:00 |   0.09000000000003183 |
| SPY    |   331.71 | 2020-01-17 15:46:00 |                     0 |
| SPY    | 331.6854 | 2020-01-17 15:45:00 |  0.024599999999963984 |
| SPY    |   331.66 | 2020-01-17 15:44:00 |   0.02539999999999054 |
| SPY    |   331.72 | 2020-01-17 15:43:00 | -0.060000000000002274 |
| SPY    |   331.72 | 2020-01-17 15:42:00 |                     0 |
| SPY    |   331.72 | 2020-01-17 15:41:00 |                     0 |
+--------+----------+---------------------+-----------------------+

If your goal is research, it wouldn't make too much of a difference between csv and sql, but if your goal is live trading, even SQL is too slow.

2
BrononymousEngineerOP•

This is exactly the sort of response I was looking for. I am just using this function for research and model building purposes, so efficiency of analysis is important, and this seems very nice. I imagine it could help in simplifying and compartmentalizing the code, that way I can focus more on analysis rather than manipulating the raw data. Thanks!

1
[deleted]•

[removed]

1
[deleted]•

[removed]

1
RobinReborn•

Relational databases are useful once the data gets more complicated. The only problem with your method now is that it's going to be overwriting .csv's a lot if you're looking for different time periods of the same ticker. Ideally you'd have a way to append date ranges to a .csv

1
BrononymousEngineerOP•

Ideally you'd have a way to append date ranges to a .csv

I agree, I've just been procrastinating coming in up with a robust solution

1
Peekman•

I use SQL. Only import what you don't have and it's easy to link with Excel and manipulate.

Also, all tickets on one table.

1