-
Notifications
You must be signed in to change notification settings - Fork 229
/
CryptoStocks.py
151 lines (134 loc) · 5.25 KB
/
CryptoStocks.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
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
# implements API access to CryptoStocks.com
# not a conventional bitcoin exchange but has basically
# the same API functions
from Exchange import Exchange
from order import Order
class CryptoStocks(Exchange):
def __init__(self, api_key, secret):
self.api = Cryptsy_api(api_key, secret)
super(Cryptsy, self).__init__()
self.name = 'CRYPTSY'
self.trading_fee = 0.003# 0.2% to buy and 0.3% to sell. Those bastards!
self.marketids = {}
def get_tradeable_pairs(self):
tradeable_pairs = []
self.market_ids = {}
data = self.api.getMarketDataV2()
if data is None:
# try again
print('retrying Cryptsy request...')
return self.get_tradeable_pairs()
else:
for market in data['return']['markets'].values():
base = market['primarycode']
alt = market['secondarycode']
tradeable_pairs.append((base, alt))
# while we're doing this, also
# initialize the maps to marketids
slug = base + "_" + alt
self.market_ids[slug] = market['marketid']
return tradeable_pairs
def get_min_vol(self, pair, depth):
'''
Mar 21 2014 - Cryptsy changes minimum order volume to 0.0000001
'''
base, alt = pair
slug = base + "_" + alt
test = self.get_validated_pair(pair)
if test is not None:
true_pair, swapped = test
if not swapped:
return 0.0000001
else:
# we need to use the depth information to calculate
# how much alt we need to trade to fulfill min base vol
return self.get_clipped_alt_volume(depth, 0.0000001)
def get_highest_bid(self, base, alt):
# RETURNS PRICE in units of ALT!
pair0 = (base, alt)
pair, swapped = self.get_validated_pair(pair0)
newbase, newalt = pair
marketid = self.get_market_id(newbase, newalt)
data = self.api.getOrderbookData(marketid)['return'][newbase]
if swapped:
# we are interested in sell orders instead of buy orders
bidprice = float(data['sellorders'][0]['price'])
bidprice = 1.0/bidprice
else:
# first one is the best offer
bidprice = float(data['buyorders'][0]['price'])
return bidprice
def get_lowest_ask(self, base, alt):
pair0 = (base, alt)
pair, swapped = self.get_validated_pair(pair0)
newbase, newalt = pair
marketid = self.get_market_id(newbase, newalt)
data = self.api.getOrderbookData(marketid)['return'][newbase]
if swapped:
askprice = float(data['buyorders'][0]['price'])
askprice = 1.0/askprice
else:
askprice = float(data['sellorders'][0]['price'])
return askprice
def get_depth(self, base, alt):
pair0 = (base, alt)
pair, swapped = self.get_validated_pair(pair0)
newbase, newalt = pair
marketid = self.get_market_id(newbase, newalt)
data = self.api.depth(marketid)['return']
book = { "bids" : [], "asks" : [] }
if swapped:
for bid in data['buy']:
# find out
o = Order(float(bid[0]),float(bid[1]))
ask = get_swapped_order(o)
book['asks'].append(ask)
for ask in data['sell']:
o = Order(float(ask[0]), float(ask[1]))
bid = get_swapped_order(o)
book['bids'].append(bid)
else:
for bid in data['buy']:
o = Order(float(bid[0]),float(bid[1]))
book['bids'].append(o)
for ask in data['sell']:
o = Order(float(ask[0]), float(ask[1]))
book['asks'].append(o)
return book
def get_balance(self, currency):
balances = self.api.getInfo()['return']['balances_available']
return float(balances.get(currency, 0.0))
def get_all_balances(self):
data = self.api.getInfo()
balances = {k:float(v) for k,v in data['return']['balances_available'].items()}
return balances
def submit_order(self, gc, gv, rc, rv):
pair0 = (gc, rc)
pair, swapped = self.get_validated_pair(pair0)
marketid = self.get_market_id(pair)
if swapped:
ordertype = "Buy"
quantity = rv
price = gv/rv
else:
ordertype = "Sell"
quantity = gv
price = rv/gv
self.api.createOrder(marketid, ordertype, quantity, price)
def confirm_order(self, orderID):
data = self.api.allMyOrders()
#data is all my open orders so check to see if orderID is no longer in there
return NotImplemented
# CRYPTSY-specific
def get_market_id(self, base, alt):
if (base, alt) in self.tradeable_pairs:
slug = base + "_" + alt
return self.market_ids[slug]
elif (alt, base) in self.tradeable_pairs:
slug = alt + "_" + base
return self.market_ids[slug]
else:
return 'ERROR!'
# def validate_data(self, data):
# if data['success'] != 1:
# DONT WORK ON THIS NPW