R - Download daily stock data, store in MySQL - Matlab, R project and Python | futures io social day trading
futures io futures trading


R - Download daily stock data, store in MySQL
Updated: Views / Replies:8,297 / 30
Created: by Big Mike Attachments:20

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 20  
 
Thread Tools Search this Thread
 

R - Download daily stock data, store in MySQL

  #1 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,220 received

R - Download daily stock data, store in MySQL

Hey guys,

I've been writing code in R for only one day, but here is my first script.

Goal:

- Download list of all tickers from Yahoo for AMEX, NASDAQ, NYSE exchanges
- Sort that list by market cap, filter to only the top 100 tickers
- Retrieve historical data from Yahoo
- Store historical data into MySQL

Everything seems to work. I am posting the code below as well as attaching it.

The next step will be to make this smarter --- it doesn't need to download the entire history of the ticker every single day. Instead, it should look at the last entry in the db, then download from that point (-1 day).

It should also auto-retry on failed data downloads, right now it just reports them (this is due to Yahoo rate limiting I suspect).

I'll try to make those improvements in the next few days.

Please don't laugh at my code too much. Like I said, I have literally been working with R for about 1 day.

init.R

 
Code
# Big Mike Trading
# www.bigmiketrading.com
# November 2013
# v1.0.0

# Init

rm(list=ls())

source("~/R/Daily/downloadData.R")

source("~/R/Daily/mysqlData.R")
downloadData.R

 
Code
# Big Mike Trading
# www.bigmiketrading.com
# November 2013
# v1.0.0

library(quantstrat)
library(TTR)

# Download all stocks for AMEX, NASDAQ, NYSE

cat("Fetching all tickers\n")

all.symbols <- stockSymbols(c("AMEX","NASDAQ","NYSE"),sort.by=c("MarketCap","Symbol"),quiet=TRUE)

# Get 100 biggest by market cap

biggest.symbols <- tail(all.symbols,n=100)

# Cleanup

clean.symbols <- subset(biggest.symbols, select = c("Symbol","Name","LastSale","MarketCap","Sector"))
row.names(clean.symbols) <- NULL

# Download data

cat("Starting download for ",nrow(biggest.symbols)," tickers, with 10 second delay\n")

for (i in 1:nrow(clean.symbols)){
  symbol <- clean.symbols[i,]$Symbol
  cat("Downloading",symbol)
  
  try (getSymbols(symbol, verbose=FALSE, warnings=FALSE, from='1900-01-01'))
  
  if(!exists(symbol)){
    cat(", error - did not download (likely due to rate limiting\n")
  }
  else {
    cat("\n")
  }
  Sys.sleep(10)
}
mysqlData.R

 
Code
# Big Mike Trading
# www.bigmiketrading.com
# November 2013
# v1.0.0

# Save data to MySQL

library(RMySQL)

con <- dbConnect(MySQL(), dbname="daily")

# Loop through each symbol

for (i in 1:nrow(clean.symbols)){
  symbol <- clean.symbols[i,]$Symbol
  df = data.frame(Date=index(get(symbol)), coredata(get(symbol)))
  setnames(df,c("Date","Open","High","Low","Close","Volume","Adjusted"))

  cat("Storing",symbol,"\n")
  
  if(dbExistsTable(con, symbol)){
    dbRemoveTable(con, symbol)
  }
  
  dbWriteTable(con, name=symbol, value=df, row.names=FALSE)
  
}
Please share any thoughts.

Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Attached Files
Register to download File Type: r init.R (161 Bytes, 23 views)
Register to download File Type: r downloadData.R (901 Bytes, 22 views)
Register to download File Type: r mysqlData.R (559 Bytes, 18 views)
Reply With Quote
The following 8 users say Thank You to Big Mike for this post:
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,220 received


Example MySQL

 
Code
MariaDB [daily]> show tables;
+-----------------+
| Tables_in_daily |
+-----------------+
| AIG             |
| AZN             |
| EBAY            |
| F               |
| HON             |
| NKE             |
| UBS             |
| UNH             |
| UNP             |
| USB             |
+-----------------+
10 rows in set (0.00 sec)

MariaDB [daily]> select * from EBAY limit 5;
+------------+-------+-------+-------+-------+-----------+----------+
| Date       | Open  | High  | Low   | Close | Volume    | Adjusted |
+------------+-------+-------+-------+-------+-----------+----------+
| 1998-09-24 |  53.5 | 54.25 | 44.25 | 47.38 | 218289600 |     1.97 |
| 1998-09-25 |  45.5 | 46.13 | 41.13 | 44.88 |  51033600 |     1.87 |
| 1998-09-28 |  47.5 | 50.88 |    46 | 48.25 |  37783200 |     2.01 |
| 1998-09-29 | 49.38 | 50.25 | 47.75 | 48.88 |  16536000 |     2.04 |
| 1998-09-30 | 47.19 |    48 | 43.25 | 45.06 |  18566400 |     1.88 |
+------------+-------+-------+-------+-------+-----------+----------+
5 rows in set (0.41 sec)
Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
 
  #4 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,220 received

New version that auto-retries on download error.

Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Attached Files
Register to download File Type: r downloadData.R (1.4 KB, 14 views)
Reply With Quote
The following user says Thank You to Big Mike for this post:
 
  #5 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,220 received

OK, several other changes... LOL.

 
Code
Fetching all tickers

Starting download for  1000  tickers, with  10 second delay

Downloading SIVB ( 1 / 1000 )
Downloading CBOE ( 2 / 1000 )
Downloading PBI ( 3 / 1000 )
Downloading PINC ( 4 / 1000 )

And now I let that run for a while...

Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Attached Files
Register to download File Type: r init.R (280 Bytes, 19 views)
Register to download File Type: r downloadData.R (1.2 KB, 13 views)
Register to download File Type: r mysqlData.R (603 Bytes, 14 views)
Reply With Quote
The following user says Thank You to Big Mike for this post:
 
  #6 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,220 received

When I have some time to get back to this, I will rework the code so it saves the MySQL data after each successful download, instead of in batches at the end.

Will learn how to do functions in R... but not right now, off to trade, then sleep!

Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
 
  #7 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,220 received

Progress still...

 
Code
2% complete: downloading CEA, storing... done
2% complete: downloading PDCO, storing... done
2% complete: downloading RDC, storing... done
2% complete: downloading JHX, storing... done
2% complete: downloading LSI, storing... done
2% complete: downloading NSM, storing... done
2% complete: downloading UTHR, storing... done
2% complete: downloading HCC, storing... done
2% complete: downloading GEL, storing... done
2% complete: downloading AVY, storing... done
2% complete: downloading WPC, storing... done
3% complete: downloading SNH, storing... done
3% complete: downloading ALGN, storing... done
3% complete: downloading LAMR, storing... done
3% complete: downloading PBCT, storing... done
3% complete: downloading G, storing... done
3% complete: downloading TWTC, storing... done
3% complete: downloading IACI, storing... done
3% complete: downloading SLXP, storing... done
Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
 
  #8 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,220 received

To-do:

- Sleep
- Add meta table to db, store things like company name, market cap, sector, average daily volume
- Look into adding earnings data, upcoming
- Query last date in db, resume download -- vs downloading all data every time

I've already made many other changes and will upload again later today.

Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
 
  #9 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,220 received

Latest version

- Dynamically adjust wait time based on success/failure on downloading (rate limiting)
- Auto retry failed download
- Only download missing data

To-do

- Add meta table to db, store things like company name, market cap, sector, average daily volume
- Look into adding earnings data, upcoming

Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Attached Files
Register to download File Type: r init.R (349 Bytes, 16 views)
Register to download File Type: r downloadData.R (1.7 KB, 15 views)
Register to download File Type: r mysqlData.R (856 Bytes, 15 views)
Reply With Quote
 
  #10 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,220 received


It seems the RMySQL package does not escape the table name automatically?

 
Code
21% complete: downloading SSL( 1900-01-01 ), storing
Error in mysqlExecStatement(conn, statement, ...) : 
  RS-DBI driver: (could not run statement: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SSL
(
	Date text,
	Open double,
	High double,
	Low double,
	Close double,
	Volum' at line 1)
... done
Code is dbWriteTable(con, name=symbol, value=df, row.names=FALSE, append=TRUE)

I will have to try and research this and see how to overcome it. The issue is the ticker is SSL which is also a keyword in MySQL.

Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote

Reply



futures io > > > > R - Download daily stock data, store in MySQL

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using MySQL for storing tick data Big Mike The Elite Circle 131 October 3rd, 2016 04:09 PM
R and MySQL for tick data Big Mike Matlab, R project and Python 3 November 10th, 2013 12:08 PM
Looking for Software to store Price data in MySQL caliph Matlab, R project and Python 4 January 7th, 2013 03:11 PM
Store real time data from NT in mysql bascher NinjaTrader 23 November 4th, 2011 06:53 AM
PLT_NTDataCollect store bid ask tick data to its local database? rcabri Trading Reviews and Vendors 3 December 27th, 2010 11:00 AM


All times are GMT -4. The time now is 02:28 PM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts
Page generated 2017-12-12 in 0.12 seconds with 20 queries on phoenix via your IP 54.163.209.109