81 lines
3.3 KiB
Python
81 lines
3.3 KiB
Python
# Copyright 2022-present Coinbase Global, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from urllib.parse import urlparse
|
|
import json, hmac, hashlib, time, os, base64, requests, time, datetime
|
|
import psycopg2
|
|
from pytz import timezone
|
|
|
|
SLEEP=5
|
|
|
|
#API_KEY = os.environ.get('ACCESS_KEY')
|
|
#SECRET_KEY = os.environ.get('SIGNING_KEY')
|
|
#PASSPHRASE = os.environ.get('PASSPHRASE')
|
|
#PORTFOLIO_ID = os.environ.get('PORTFOLIO_ID')
|
|
PASSPHRASE = '5Rb5KFBCGLQ=aaJvs7jo8bBSLPFS5rWT0w=='
|
|
API_KEY = 'ZdlFsitVWo4=fEcyG3K5zeUZb7931FVSiWTO+rOgizJeuzP9uCuy9M0='
|
|
SECRET_KEY = '/NqFL5Fk4qM=0sm9mnMqB0cz4+MSKgEQ1Th2lPqMBpYjY5dZTGSlix5jBBodmxfi0jTwDfEcjhoUM0jqrTtgqQCLlSpRetEqOg=='
|
|
PORTFOLIO_ID = 'a2005845-db3c-4300-be3f-f5edba80d377'
|
|
|
|
def get_db_connection():
|
|
return psycopg2.connect(host='127.0.0.1', database='crypto_acctval', user='joseph')
|
|
|
|
def get_it():
|
|
uri = f'https://api.prime.coinbase.com/v1/portfolios/{PORTFOLIO_ID}/balances'
|
|
url_path = urlparse(uri).path
|
|
timestamp = str(int(time.time()))
|
|
message = timestamp + 'GET' + url_path
|
|
signature_b64 = base64.b64encode(hmac.digest(SECRET_KEY.encode(), message.encode(), hashlib.sha256))
|
|
|
|
headers = {
|
|
'X-CB-ACCESS-SIGNATURE': signature_b64,
|
|
'X-CB-ACCESS-TIMESTAMP': timestamp,
|
|
'X-CB-ACCESS-KEY': API_KEY,
|
|
'X-CB-ACCESS-PASSPHRASE': PASSPHRASE,
|
|
'Accept': 'application/json'
|
|
}
|
|
|
|
response = requests.get(uri, headers=headers)
|
|
parsed_response = json.loads(response.text)
|
|
#balances = [ x for x in parsed_response['balances'] if float(x['amount']) > 0 ]
|
|
if 'balances' in parsed_response:
|
|
balances = { x['symbol']: x for x in parsed_response['balances'] if float(x['amount']) > 0 }
|
|
#nw = sum([ float(x['fiat_amount']) for x in balances ])
|
|
nw = sum([ float(x['fiat_amount']) for x in balances.values() ]) + float(balances['usd']['holds'])
|
|
return(nw)
|
|
|
|
if __name__ == '__main__':
|
|
TZ_CUT = timezone('US/Eastern')
|
|
TZ_UTC = timezone('UTC')
|
|
dbconn = get_db_connection()
|
|
while True:
|
|
nw = get_it()
|
|
print(str(nw))
|
|
if nw is not None:
|
|
cur = dbconn.cursor()
|
|
cur.execute("INSERT INTO acctval (created_at, val) VALUES (%s, %s)", ('NOW()', nw))
|
|
dbconn.commit()
|
|
|
|
now_utc = datetime.datetime.now(TZ_CUT).replace(hour=0, minute=0, second=0, microsecond=0).astimezone(TZ_UTC)
|
|
cur.execute('SELECT val FROM acctval WHERE created_at > %s ORDER BY created_at LIMIT 1', (now_utc,))
|
|
resrow = cur.fetchone()
|
|
if resrow is not None:
|
|
pnl = nw - float(resrow[0])
|
|
print('pnl %s' % pnl)
|
|
cur.execute("INSERT INTO pnl_intra (created_at, pnl) VALUES (%s, %s)", ('NOW()', pnl))
|
|
dbconn.commit()
|
|
|
|
cur.close()
|
|
time.sleep(SLEEP)
|