-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.py
More file actions
153 lines (131 loc) · 4.26 KB
/
stack.py
File metadata and controls
153 lines (131 loc) · 4.26 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
import datetime
import krakenex
import json
projectPath = '/mnt/hdd/git/kraken-auto-stack/'
k = krakenex.API()
k.load_key(projectPath + 'kraken.key')
def getConfig():
with open(projectPath + 'config.json') as f:
return json.load(f)
c = getConfig()
def configLegit():
keys = ['minBalance', 'minSell', 'maxSell', 'quote', 'buy']
for alt in c['alts']:
# Check all the config keys are there
for key in keys:
if key not in c['alts'][alt]:
print("Configuration error!")
print("%s is missing key [%s]" % (alt, key))
return False
# Check minBuy is less than maxBuy (if maxBuy is set)
maxSell = c['alts'][alt]['maxSell']
minSell = c['alts'][alt]['minSell']
if maxSell and minSell > maxSell:
print("Configuration error!")
print("[maxBuy] must be less than [minBuy] for %s" % (alt))
return False
return True
def getBalance():
return k.query_private('Balance')['result']
def getTradableBalance(bal):
tb = {}
for alt in c['alts']:
tb[alt] = float(bal[alt])
return tb
def getMinOrder(alt):
a = getAltConfig(alt)
pair = alt+a['quote']
response = k.query_public('AssetPairs', {'pair': pair})
orderMin = response['result'][pair]['ordermin']
return orderMin
def getPrice(asset, quote):
pair = asset+quote
ticker = k.query_public('Ticker', {'pair': pair})
last_close_raw = ticker["result"][pair]["c"]
last_close = last_close_raw[0]
return float(last_close)
def getAltConfig(ticker):
for alt in c['alts']:
if alt == ticker:
return c['alts'][alt]
def getReserveUnits(tradable):
reserve = {}
for alt in tradable:
a = getAltConfig(alt)
reserve[alt] = a['minBalance'] / getPrice(alt, a['quote'])
return reserve
def getSellableUnits(tradable, reserves):
# units of each tradable asset can be sold
sellable = {}
for alt in tradable:
unitsOfMin = reserves[alt]
if tradable[alt] > unitsOfMin:
sellable[alt] = tradable[alt] - unitsOfMin
return sellable
def getPair(alt):
return c['alts'][alt]['pair']
def marketBuy(alt, amt, orderType):
pair = getPair(alt)
print(pair)
print(amt)
print(orderType)
response = k.query_private('AddOrder', {
'pair': pair,
'type': orderType,
'ordertype': 'market',
'volume': str(amt)
})
print(response)
print("%s txid %s" % (response['result']['descr']['order'], response['result']['txid'][0]))
def worthIt(alt, amt):
a = getAltConfig(alt)
return getPrice(alt, a['quote'])*amt > a['minSell']
def getVolume(alt, amt):
a = getAltConfig(alt)
if a['maxSell']:
stackSize = a['maxSell'] / getPrice(alt, a['quote'])
if amt > stackSize:
return round(stackSize, 4)
return round(amt, 4)
def processTrades():
bal = getBalance()
printTable(bal, "Account Balances:")
tradable = getTradableBalance(bal)
reserves = getReserveUnits(tradable)
printTable(reserves, "Minimum Required Balances:")
sellable = getSellableUnits(tradable, reserves)
if sellable:
aboutToSell={}
minOrders={}
for alt in sellable:
amt = sellable[alt]
if worthIt(alt, amt):
vol = getVolume(alt, amt)
minOrder = getMinOrder(alt)
aboutToSell[alt] = vol
minOrders[alt] = minOrder
printTable(aboutToSell, "Eligible to Trade:")
printTable(minOrders, "Kraken Min Orders:")
for alt in aboutToSell:
if aboutToSell[alt] > float(minOrders[alt]):
order = 'sell'
if getPair(alt).startswith('XBT'):
order = 'buy'
marketBuy(alt, aboutToSell[alt], order)
else:
print("%s volume does not meet Kraken's minimum requirements for trade." % (alt))
else:
print("No alts to trade. Good job!")
def printTable(vals, desc):
print(datetime.datetime.now())
print(desc)
for key in vals:
print("%s %s" % (key, vals[key]))
print()
def main():
if configLegit():
processTrades()
print("Done.")
if __name__ == "__main__":
main()