Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import time
  2. import datetime
  3. import math
  4. import gdax
  5. import progressbar
  6. from pymongo import MongoClient
  7.  
  8. """
  9. To Do:
  10. -- Write tests
  11. -- Run module on tests
  12. """
  13.  
  14. def main(pair, start, end, resolution, collection_name):
  15. """
  16. Main function. Sets up variables, instantiates MongoClient and GDAX client,
  17. sets up rate limiting, and then loops through collecting candles from the
  18. GDAX API.
  19.  
  20. :param: pair: <str> The desired pair to collect data on. E.g.: ETH-USD.
  21. :param: start_date: <str> The start date of the period in ISO 8601 format.
  22. :param: end_date: <str> The end date of the period in ISO 8601 format.
  23. :param: resolution: <int> The resolution of the data in seconds (candle perid).
  24. :param: collection_name: <str> The desired name of the new Mongo collection.
  25. """
  26. # Max request rate for public client per second
  27. GDAX_PUBLIC_REQUESTS_RATE = 3
  28. # Max number of candles allowed per request
  29. GDAX_MAX_DATA_POINTS = 200
  30.  
  31. bar = progressbar.ProgressBar()
  32.  
  33. # Initialize Mongo Client
  34. mongo_client = MongoClient()
  35.  
  36. # Access database
  37. db = mongo_client.gdax_historic_rates
  38.  
  39. # Create collection
  40. collection = db[collection_name]
  41.  
  42. # Initalize GDAX client
  43. public_client = gdax.PublicClient()
  44.  
  45. # Start and end dates
  46. start_date = datetime.datetime.strptime(start, "%Y-%m-%dT%H:%M:%S")
  47. end_date = datetime.datetime.strptime(end, "%Y-%m-%dT%H:%M:%S")
  48.  
  49. # Duration timedelta object
  50. duration = end_date - start_date
  51. # Duration in seconds
  52. duration_seconds = duration.total_seconds()
  53.  
  54. # Length of time period covered by each request
  55. period = resolution * GDAX_MAX_DATA_POINTS
  56. # Period as timedelta object
  57. period_delta = datetime.timedelta(seconds=period)
  58.  
  59. # Calculate number of loops and round up to nearest integer
  60. num_loops = math.ceil(duration_seconds/period)
  61.  
  62. # Loop through at 1/GDAX_PUBLIC_REQUESTS_RATE ensuring we don't exceed rate limits
  63. for i in bar(range(0, num_loops)):
  64. start = start_date.isoformat()
  65. end = (start_date + period_delta).isoformat()
  66.  
  67. candles = public_client.get_product_historic_rates(pair, start, end, resolution)
  68.  
  69. write_data(candles, collection)
  70.  
  71. start_date = start_date + period_delta
  72.  
  73. time.sleep(1.0/GDAX_PUBLIC_REQUESTS_RATE)
  74.  
  75. def write_data(candles, collection):
  76. """
  77. Loops through candles and writes to a Mongo collection.
  78.  
  79. :param: candles: <list> Nested python list of candles from GDAX API.
  80. :param: collection: <class 'pymongo.collection.Collection'> Mongo collection class.
  81. """
  82. for candle in candles:
  83. entry = {"Timestamp": candle[0], "Low": candle[1], "High": candle[2],
  84. "Open": candle[3], "Close": candle[4], "Volume": candle[5]}
  85. collection.insert_one(entry)
  86.  
  87. if __name__ == "__main__":
  88. pair = input("Enter pair: e.g. ETH-USD: ")
  89. start = input("Start date (ISO 8601) e.g. 2017-11-01T00:00:00: ")
  90. end = input("End date (ISO 8601): ")
  91. resolution = int(input("Enter desired length of candle in seconds. E.g. 60: "))
  92. collection_name = input("Enter collection name: ")
  93. main(pair, start, end, resolution, collection_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement