I heartily welcome patches to update this data or any gifts if you appreciate it, e.g. a postcard -- vijay@yoyo.org
This is a raw set of imported daily S&P500 CSV data from Yahoo Finance converted to MySQL and sqlite formats. The data is daily open, close, high, low and volume since 3, January 1950.
I wanted to fetch the S&P500 historical data for my reference.
- Get the latest CSV file of results from Yahoo!
- Put the file in the folder 'csv', overwriting
sp500.csv - In phpMyAdmin use the import feature to import the CSV file to the database
- Convert the date column to a proper date and the encoding to ASCII and table format to 'ARCHIVE'
- Export the data file
Go to rebasedata.com/convert-mysql-to-sqlite-online for instructions on how to easily convert the data between various DBMSs. This is how it was done originally:
curl -F files[]=@sp500.sql 'https://www.rebasedata.com/api/v1/convert?outputFormat=sqlite&errorResponse=zip' -o sp500.sqlite.zip
unzip sp500.sqlite.zip && mv data.sqlite sp500.sqlite && rm sp500.sqlite.zip
For convenience, this is in the shell script sql/dbconvert.sh
Refer to sql/queries/ and feel free to send any other queries with a github pull request.
e.g. Get all columns (date, open, high, low, close, adj close, volume) for days from 1 November 2018:
select * from sp500 where date >= '2018-11-01' order by date;
SELECT
ROUND(MIN(Low)) Lowest,
ROUND(MAX(High)) Highest,
ROUND(MIN(`Open`)) `Open Min`,
ROUND(MIN(`Close`)) `Close Min`,
ROUND(MAX(`Open`)) `Open Max`,
ROUND(MAX(`Close`)) `Close Max`,
ROUND(AVG(`Open`)) `Open Avg`,
ROUND(AVG(`Close`)) `Close Avg`
FROM sp500
WHERE
DATE BETWEEN '2018-01-01' AND '2018-12-01';
SELECT
LEFT(`Date`,10) `Date`,
DAYNAME(`Date`) AS 'Day',
ROUND(`Open`,2) `Open`,
ROUND(`Close`,2) `Close`,
CONCAT(
ROUND(`High` - `Open`),
'/',
ROUND(`Open` - `Low`),
'/',
ROUND(`Open` - `Close`)
) `Up/Down/Close`,
ROUND(`High`,2) `High`,
ROUND(`Low`,2) `Low`,
ROUND(`High` - `Low`) `Range`
FROM sp500
WHERE `DATE` BETWEEN '2018-01-01' AND '2018-12-31'
ORDER BY `DATE` DESC;
SELECT LEFT(`Date`,11) AS `Date`,
round(Volume / 1000000) Volume,
round(Low) Low,
round(High) High,
round(`Open`) `Open`,
round(`Close`) `Close`,
round(High - Low) `Range`,
round(High - `Open`) Max_Up,
round(`Open` - Low) Max_Down,
round(`Close` - `Open`) Rise_Fall
FROM sp500
WHERE `Date` >= '2010-01-01'
ORDER BY `Date` DESC
LIMIT 10;
SELECT LEFT(`Date`,11) AS `Date`,
round(Low) Low,
round(High) High,
round(`Open`) `Open`,
round(`Close`) `Close`,
round(High - Low) `Range`,
round(High - `Open`) Max_Up,
round(`Open` - Low) Max_Down,
round(`Close` - `Open`) Rise_Fall
FROM sp500
WHERE `Open` BETWEEN 2605 AND 2645
ORDER BY `Open` DESC;
SELECT ROUND(`Close` - `Open`) AS Gain, COUNT(*) AS `Days`
FROM sp500
WHERE
`DATE` > '2007-01-01 00:00:00' AND '2018-12-31 00:00:00'
GROUP BY `Gain`
;
SELECT ROUND(`High` - `Low`) AS `Range`, COUNT(*) AS `Days`
FROM sp500
WHERE
`DATE` > '2007-01-01 00:00:00' AND '2018-12-31 00:00:00'
GROUP BY `Range`
ORDER BY `Range`
;
SELECT
YEAR(`date`) AS `Year`,
LEFT(`Volume`,1) `Volume (blns)`,
COUNT(*) AS Sessions
FROM sp500
WHERE Volume > 7000000000
GROUP BY YEAR(`date`), LEFT(Volume, 1)
ORDER BY YEAR(`date`) DESC, `Sessions` DESC;
SELECT
YEAR(`date`) AS `Year`,
MONTHNAME(`date`) AS `Month`,
ROUND(MAX(Volume / 1000000000),2) `Volume (Max)`,
ROUND(MIN(Volume / 1000000000),2) `Volume (Min)`,
ROUND(AVG(Volume / 1000000000),2) `Volume (Avg)`,
COUNT(*) AS Sessions
FROM sp500
WHERE Volume > 7000000000
GROUP BY YEAR(`Date`), MONTH(`Date`)
ORDER BY YEAR(`DATE`) DESC, `Sessions` DESC;
SELECT
YEAR(`date`) AS `Year`,
MONTHNAME(`date`) AS `Month`,
ROUND(MAX(Volume / 1000000000),2) `Volume (Max)`,
ROUND(MIN(Volume / 1000000000),2) `Volume (Min)`,
ROUND(AVG(Volume / 1000000000),2) `Volume (Avg)`,
COUNT(*) AS Sessions
FROM sp500
WHERE `Date` BETWEEN '2018-01-01' AND LAST_DAY(NOW())
GROUP BY YEAR(`DATE`), MONTH(`DATE`)
ORDER BY YEAR(`DATE`) DESC, MONTH(`DATE`) DESC, `Sessions` DESC;
SELECT LEFT(`Date`,11) AS `Date`,
round(Low) Low,
round(High) High,
round(`Open`) `Open`,
round(`Close`) `Close`,
round(`Open` - Low) `Most Points Down`,
round(`CLOSE` - `OPEN`) `Rise or Fall`
FROM sp500
WHERE `DATE` BETWEEN '2018-12-01' AND NOW()
ORDER BY round(`Open` - Low) DESC;
--