Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import datetime
- import math
- import gdax
- import progressbar
- from pymongo import MongoClient
- """
- To Do:
- -- Write tests
- -- Run module on tests
- """
- def main(pair, start, end, resolution, collection_name):
- """
- Main function. Sets up variables, instantiates MongoClient and GDAX client,
- sets up rate limiting, and then loops through collecting candles from the
- GDAX API.
- :param: pair: <str> The desired pair to collect data on. E.g.: ETH-USD.
- :param: start_date: <str> The start date of the period in ISO 8601 format.
- :param: end_date: <str> The end date of the period in ISO 8601 format.
- :param: resolution: <int> The resolution of the data in seconds (candle perid).
- :param: collection_name: <str> The desired name of the new Mongo collection.
- """
- # Max request rate for public client per second
- GDAX_PUBLIC_REQUESTS_RATE = 3
- # Max number of candles allowed per request
- GDAX_MAX_DATA_POINTS = 200
- bar = progressbar.ProgressBar()
- # Initialize Mongo Client
- mongo_client = MongoClient()
- # Access database
- db = mongo_client.gdax_historic_rates
- # Create collection
- collection = db[collection_name]
- # Initalize GDAX client
- public_client = gdax.PublicClient()
- # Start and end dates
- start_date = datetime.datetime.strptime(start, "%Y-%m-%dT%H:%M:%S")
- end_date = datetime.datetime.strptime(end, "%Y-%m-%dT%H:%M:%S")
- # Duration timedelta object
- duration = end_date - start_date
- # Duration in seconds
- duration_seconds = duration.total_seconds()
- # Length of time period covered by each request
- period = resolution * GDAX_MAX_DATA_POINTS
- # Period as timedelta object
- period_delta = datetime.timedelta(seconds=period)
- # Calculate number of loops and round up to nearest integer
- num_loops = math.ceil(duration_seconds/period)
- # Loop through at 1/GDAX_PUBLIC_REQUESTS_RATE ensuring we don't exceed rate limits
- for i in bar(range(0, num_loops)):
- start = start_date.isoformat()
- end = (start_date + period_delta).isoformat()
- candles = public_client.get_product_historic_rates(pair, start, end, resolution)
- write_data(candles, collection)
- start_date = start_date + period_delta
- time.sleep(1.0/GDAX_PUBLIC_REQUESTS_RATE)
- def write_data(candles, collection):
- """
- Loops through candles and writes to a Mongo collection.
- :param: candles: <list> Nested python list of candles from GDAX API.
- :param: collection: <class 'pymongo.collection.Collection'> Mongo collection class.
- """
- for candle in candles:
- entry = {"Timestamp": candle[0], "Low": candle[1], "High": candle[2],
- "Open": candle[3], "Close": candle[4], "Volume": candle[5]}
- collection.insert_one(entry)
- if __name__ == "__main__":
- pair = input("Enter pair: e.g. ETH-USD: ")
- start = input("Start date (ISO 8601) e.g. 2017-11-01T00:00:00: ")
- end = input("End date (ISO 8601): ")
- resolution = int(input("Enter desired length of candle in seconds. E.g. 60: "))
- collection_name = input("Enter collection name: ")
- main(pair, start, end, resolution, collection_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement