forked from aspectolog/yobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balance.py
131 lines (106 loc) · 4.73 KB
/
balance.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
######################################################################################################
# Переводит все монеты портфеля с ненулевым балансом в рубли по текущим ценам покупки и продажи.
# Подсчитывает ПРАВДИВУЮ полную текущую стоимость портфеля в рублях.
######################################################################################################
import os
import urllib, http.client
import hmac, hashlib
import requests
import json
from decimal import Decimal
from cfg import *
SELLSPREAD = Decimal(SELLSPREAD)
BIDSPREAD = Decimal(BIDSPREAD)
if not os.path.exists(nonce_file):
with open(nonce_file, "w") as out:
out.write('1')
# Получаем цены = list[str, str]
def GetInfo(pair):
otvet = requests.get('https://yobit.net/api/3/depth/'+pair)
data = json.loads(otvet.text)
return data
class YobitException(Exception):
pass
def call_api(**kwargs):
# При каждом обращении к торговому API увеличиваем счетчик nonce на единицу
with open(nonce_file, 'r+') as inp:
nonce = int(inp.read())
inp.seek(0)
inp.write(str(nonce + 1))
inp.truncate()
print("Счетчик: ", nonce)
payload = {'nonce': nonce}
if kwargs:
payload.update(kwargs)
payload = urllib.parse.urlencode(payload)
H = hmac.new(key=SECRET, digestmod=hashlib.sha512)
H.update(payload.encode('utf-8'))
sign = H.hexdigest()
headers = {"Content-type": "application/x-www-form-urlencoded",
"Key": TRADE_KEY,
"Sign": sign}
conn = http.client.HTTPSConnection("yobit.net", timeout=60)
conn.request("POST", "/tapi/", payload, headers)
response = conn.getresponse().read()
conn.close()
try:
obj = json.loads(response.decode('utf-8'))
if 'error' in obj and obj['error']:
raise YobitException(obj['error'])
return obj
except json.decoder.JSONDecodeError:
raise YobitException('Ошибка анализа возвращаемых данных, получена строка', response)
#Получаем i-ую от края цену
def Ask_Price(data, i):
try:
result = Decimal(data["asks"][i][0]).quantize(Decimal("1.00000000"))
except:
result = Decimal("0").quantize(Decimal("1.00000000"))
return result
def Bid_Price(data, i):
try:
result = Decimal(data["bids"][i][0]).quantize(Decimal("1.00000000"))
except:
result = Decimal("0").quantize(Decimal("1.00000000"))
return result
###############################################################################################################
asksumm = 0
bidsumm = 0
count = 0
pair = ''
amount = 0
paircount = 0
five_pair = {}
balances = call_api(method="getInfo")['return']['funds']
for h in balances:
if Decimal(balances[h]) != 0 and h != 'yovi' and h != CURRENCY:
paircount += 1
five_pair[h + '_' + CURRENCY] = Decimal(balances[h]).quantize(Decimal("1.00000000"))
if count == 0:
pair = h + '_' + CURRENCY
count += 1
elif count < 10:
pair = pair + '-' + h + '_' + CURRENCY
count += 1
else:
PriceData = GetInfo(pair)
for this_pair in PriceData:
bidamount = (five_pair[this_pair] * Bid_Price(PriceData[this_pair], 0)).quantize(Decimal("1.00000000"))
askamount = (five_pair[this_pair] * Ask_Price(PriceData[this_pair], 0)).quantize(Decimal("1.00000000"))
print(this_pair, ' = ', askamount.quantize(Decimal("1.0000")), ' ', bidamount.quantize(Decimal("1.0000")))
bidsumm = bidsumm + bidamount
asksumm = asksumm + askamount
count = 0
five_pair.clear()
if count > 0:
PriceData = GetInfo(pair)
for this_pair in PriceData:
bidamount = (five_pair[this_pair] * Bid_Price(PriceData[this_pair], 0)).quantize(Decimal("1.00000000"))
askamount = (five_pair[this_pair] * Ask_Price(PriceData[this_pair], 0)).quantize(Decimal("1.00000000"))
print(this_pair, ' = ', askamount.quantize(Decimal("1.0000")), ' ', bidamount.quantize(Decimal("1.0000")))
bidsumm = bidsumm + bidamount
asksumm = asksumm + askamount
print('__________________________________________________________________________')
print('ИТОГО ВЕСЬ ПОРТФЕЛЬ ПО ЦЕНЕ ПРЕДЛОЖЕНИЯ:', asksumm.quantize(Decimal("1.0000")))
print('ИТОГО ВЕСЬ ПОРТФЕЛЬ ПО ЦЕНЕ СПРОСА:', bidsumm.quantize(Decimal("1.0000")))
print('ВСЕГО ПАР:', paircount)