Created
June 5, 2017 12:17
-
-
Save justindavies/b3f01cd04cc4d2e2c48ba50013498f05 to your computer and use it in GitHub Desktop.
Import NASDAQ stocks from Quandl EOD data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymongo | |
from pymongo import MongoClient | |
import csv | |
import urllib2 | |
import sys | |
import os.path | |
from datetime import datetime, timedelta | |
QUANDL_API_KEY = "YOUR_API_KEY" | |
# Are we using development or Production ? | |
# uri = "mongodb://USERNAME:PASSWORD@INSTANCE.documents.azure.com:10255/?ssl=true&replicaSet=globaldb" | |
uri = 'mongodb://localhost:27017' | |
# Make sure unique index has been created | |
# db.candles.createIndex({ date: 1, ticker: 1 }, { unique: true }) | |
client = MongoClient(uri) | |
db = client.fluid | |
candles = db.candles | |
# We don't need all candles from IPO to now - I'll start with the past few months | |
sixty_days_ago = datetime.now() - timedelta(days=60) | |
# Get the list of tickers from the companies collection | |
companies = db.companies.find() | |
for company in companies: | |
print("Getting " + company['Name']) | |
url = 'https://www.quandl.com/api/v3/datatables/WIKI/PRICES.csv?date.gte=' + str(sixty_days_ago.date()) + '&api_key=' + QUANDL_API_KEY + "&ticker=" + company['Symbol'] | |
csvfile = urllib2.urlopen(url) | |
reader = csv.DictReader(csvfile, delimiter=',') | |
for line in reader: | |
sys.stdout.write('.') | |
candles.insert_one(line) | |
sys.stdout.write('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment