You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
totrader/trade.py

87 lines
2.2 KiB

#!/usr/bin/env python
from os import getenv
from dotenv import load_dotenv
from requests import get as requests_get
from requests import post as requests_post
from requests.auth import HTTPBasicAuth
from decimal import Decimal
from pprint import pprint
from time import sleep
from db import Ticker, Balance
class TradeOgre(object):
def __init__(self):
load_dotenv('.env')
self.base = 'https://tradeogre.com/api/v1'
self.auth = HTTPBasicAuth(
getenv('TO_USER'),
getenv('TO_PASS')
)
def req(self, route, method='get', data={}):
url = self.base + route
if method == 'get':
r = requests_get(url, auth=self.auth)
else:
r = requests_post(url, auth=self.auth, data=data)
return r.json()
def get_trade_pair(self, pair):
route = f'/ticker/{pair}'
return self.req(route)
def get_balance(self, currency):
route = '/account/balance'
return self.req(route, 'post', {'currency': currency})
def get_balances(self):
route = '/account/balances'
return self.req(route)
def run():
# define vars
to = TradeOgre()
base = 'BTC'
currency = 'WOW'
trade_pair = f'{base}-{currency}'
# ticker market data
tp_res = to.get_trade_pair(trade_pair)
print(tp_res)
spreat_btc = Decimal(tp_res['ask']) - Decimal(tp_res['bid'])
spread_sats = float(spreat_btc / Decimal(.00000001))
spread_perc = (spreat_btc / Decimal(tp_res['ask'])) * 100
t = Ticker(
trade_pair=trade_pair,
initial_price=tp_res['initialprice'],
current_price=tp_res['price'],
high_price=tp_res['high'],
low_price=tp_res['low'],
volume=tp_res['volume'],
bid=tp_res['bid'],
ask=tp_res['ask'],
spread_btc=spreat_btc,
spread_sats=spread_sats,
spread_perc=spread_perc
)
t.save()
for c in base, currency:
gb_res = to.get_balance(c)
print(gb_res)
b = Balance(
currency=c,
total=gb_res['balance'],
available=gb_res['available']
)
b.save()
if __name__ == '__main__':
while True:
run()
sleep(30)