Skip to content

Instantly share code, notes, and snippets.

@springzh
Forked from justindavies/import_candles.py
Created December 24, 2018 07:29
Show Gist options
  • Save springzh/1a6668753697c5fef010d65a0c48f7ca to your computer and use it in GitHub Desktop.
Save springzh/1a6668753697c5fef010d65a0c48f7ca to your computer and use it in GitHub Desktop.
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