This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swaps.py
99 lines (79 loc) · 2.84 KB
/
swaps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import sys
from decimal import *
from schema import Balance, Base, Swap
from db import Session
from bitapi import Api, ApiException
from config import api_key, api_secret
import json
cutoff_rate = Decimal('0.95')
def profitable(last, current, cutoff):
earnings_delta = current.earnings - last.earnings
rate_delta = current.rate - (cutoff_rate * cutoff)
print "Earnings delta: {0}".format(earnings_delta)
print "Swap rate: {0}, cutoff: {1}".format(current.rate, cutoff)
print "Rate delta: {0}".format(rate_delta)
return earnings_delta > 0 and rate_delta > Decimal('-0.1')
class Client():
def __init__(self):
self.api = Api(api_key, api_secret)
def create_swap(self):
session = Session()
cutoff = self.current_cutoff()
print "Current cutoff: {0}".format(cutoff)
rate = (cutoff_rate * cutoff).quantize(Decimal('.01'), rounding=ROUND_HALF_UP)
print "Rate for new swaps: {0}".format(rate)
balance = self.get_balance()
print "Available balance: {0}".format(balance)
if balance > 0:
print "Creating new swap..."
res = self.api.open_swap(amount = balance, rate = rate)
swap = Swap(amount = balance, rate = rate, timestamp = res['time'], swap_id = res['data']['id'], op = 'open')
print "Created swap: {0}".format(swap)
session.add(swap)
session.commit()
def update_swaps(self):
res = self.api.list_btc_swaps()
timestamp = res['time']
data = res['data']
for s in data:
s['timestamp'] = timestamp
swap = Swap.fromjson(s)
self.update_swap(swap)
def update_swap(self, swap):
swap_id = swap.swap_id
session = Session()
print "Updating swap: {0}".format(swap)
last_swap_state = session.query(Swap).filter_by(swap_id = swap_id).order_by(Swap.id.desc()).first()
if last_swap_state == None or last_swap_state.op == 'open':
print "No existing swap earnings record found"
swap.op = 'update'
session.add(swap)
elif last_swap_state.op == "close":
print "Swap already closed in the db"
else:
print "Existing swap record found: {0}".format(last_swap_state)
cutoff = self.current_cutoff()
if profitable(last_swap_state, swap, cutoff):
print "Swap still profitable"
swap.op = 'update'
session.add(swap)
else:
print "Swap not profitable, closing..."
self.api.close_swap(swap_id)
swap.op = 'close'
session.add(swap)
session.commit()
def current_cutoff(self):
res = self.api.btc_swaps()
return Decimal(str(res['cutoff']))
def get_balance(self):
return Decimal(str(self.api.info()['data']['balances']['available']['BTC']))
def main():
try:
client = Client()
client.update_swaps()
client.create_swap()
except ApiException as err:
print("API error: {0}".format(err))
raise
if __name__ == "__main__": main()