Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Import NASDAQ stocks from Quandl EOD data
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