Skip to content

Instantly share code, notes, and snippets.

@troyscott
Created April 5, 2019 06:25
Show Gist options
  • Save troyscott/6f5459116d41cda39f9771f9c75f76da to your computer and use it in GitHub Desktop.
Save troyscott/6f5459116d41cda39f9771f9c75f76da to your computer and use it in GitHub Desktop.
from decimal import Decimal
from bson.decimal128 import Decimal128
import datetime
import time
import config
import json
import requests
from pymongo import MongoClient
from cryptolib import utils
def crypto_test():
print('running test ...')
def crypto_prices():
current_timestamp = datetime.datetime.utcnow()
print(current_timestamp)
current_datetime = utils.getdatetime(current_timestamp)
current_date = utils.getdate(current_timestamp)
current_time = utils.gettime(current_timestamp)
batch_id = utils.getbatchid(current_timestamp)
# Call the local API which queries Binance server for data
url = base_url + '/crypto/prices'
print("Get data ...")
r = requests.get(url)
data = r.json()
print("Processing batch ...")
for d in data:
trade_pair = d['symbol'][-3:]
if trade_pair == 'BTC':
ticker = utils.getticker(d['symbol'])
trade_pair_symbol = d['symbol']
btcusd_price = Decimal(utils.getusd(data))
btc_price = Decimal(d['price'])
usd_price = Decimal(btc_price) * Decimal(btcusd_price)
# Create document/record
doc = {
"batch_id": batch_id,
"price_utc_timestamp": current_timestamp,
"price_utc_datetime": current_datetime,
"price_utc_date": current_date,
"price_utc_time": current_time,
"ticker": ticker,
"trade_pair_symbol": trade_pair_symbol,
"trade_pair": trade_pair,
"btc_price": Decimal128(Decimal(btc_price)),
"usd_price": Decimal128(Decimal(usd_price)),
"btcusd_price": Decimal128(Decimal(btcusd_price))
}
prices_id = prices.insert_one(doc).inserted_id
print("Inserted: " + str(prices_id))
print("Batch complete ...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment