Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ Post trades from Gemini to MongoDB """
- from gemini.client import Client
- from pymongo import MongoClient
- import os
- import datetime
- import time
- def calc_sma(prices, periods):
- # TODO: implement later
- if len(prices) < periods:
- return 0
- else:
- return int(sum(prices[-periods::])/len(prices[-periods::]))
- def calc_ema(prices, periods, ema):
- # TODO: implement later
- multiplier = (2 / (periods + 1))
- if len(ema) == 0:
- return 0
- else:
- current_price = prices[-1]
- prev_ema = ema[-1]
- return ((current_price - prev_ema)*multiplier)+prev_ema
- if __name__ == '__main__':
- # connect to Mongo
- try:
- client = MongoClient('mongodb://localhost:27017')
- except:
- print("Cannot connect to Mongo")
- # epoch timestamps with ms
- current_time = int(time.time()*1000)
- # client object for the exchange
- c = Client(os.environ['GEMINI_API_KEY'], os.environ['GEMINI_API_SECRET'])
- # select database
- db = client['trades']
- gemini = db['gemini']
- # figure out where we are starting
- pipeline = [
- {
- "$group": {
- "_id": "Latest Record",
- "latest": {
- "$max": "$timestampms"
- }
- }
- }
- ]
- latest = list(db.gemini.aggregate(pipeline))
- try:
- start_time = latest[0]['latest']+1
- except IndexError:
- # new collection - start over with 1/1/2015
- start_time = 1420070400000
- print("Blank collection...setting new time")
- while start_time < current_time:
- s = start_time / 1000.0
- print(datetime.datetime.fromtimestamp(s).strftime('%Y-%m-%d %H:%M:%S.%f'))
- # list to hold documents for bulk inserts
- doc = list()
- data = c.get_trade_history(symbol='btcusd', since=start_time, limit_trades=500)
- if data:
- data.reverse()
- for d in data:
- if d['timestampms'] > start_time:
- start_time = d['timestampms']+1
- doc.append({
- "price": str(d['price']),
- "timestampms": d['timestampms'],
- "tid": d['tid'],
- "type": str(d['type']),
- "timestamp": d['timestamp'],
- "amount": d['amount'],
- "exchange": str(d['exchange'])
- })
- # bulk insert this batch
- doc_ids = gemini.insert_many(doc)
- else:
- "Incrementing start time"
- start_time += 1
- # cleanup: remove any duplicate transaction id's
- pipeline = [{
- "$group": {
- "_id": {
- "tid": "$tid"
- },
- "count": {
- "$sum": 1
- }
- }
- },{
- "$match": {
- "count": {
- "$gt": 1
- }
- }
- }]
- dups = list(db.gemini.aggregate(pipeline))
- for d in dups:
- deleteOne = gemini.delete_one(d['_id'])
- print("Cleaning up duplicate tid's...")
- print("Done\n")
- # print some stats/sanity checks
- stats = db.command("collstats", "gemini")
- dups = list(db.gemini.aggregate(pipeline))
- print("Size on disk:\t%s" % stats['storageSize'])
- print("Number of documents:\t%s" % gemini.count())
- print("Duplicate tid's:\t%s" % len(dups))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement