forked from miguelvazsilva/binance-portfolio-rebalancing-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tr-bot-mvs-balance.py
196 lines (152 loc) · 5.97 KB
/
tr-bot-mvs-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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from binance.client import Client
from binance.enums import *
from binance.exceptions import BinanceAPIException
from time import sleep
from sys import exit
import time
import datetime
import smtplib
def find_quantity(total, price):
quantity = float(total)/ float(price)
return quantity
def balances_coins():
global balanceBTC
global balance
global bitcoins
global btc_coins
global balancecoin1
global balancecoin2
global balancecoin3
global balancecoin4
global balancecoin5
global coin1_coins
global coin2_coins
global coin3_coins
global coin4_coins
global coin5_coins
#Balances
balanceBTC = client.get_asset_balance(asset='BTC')
balance = client.get_asset_balance('BTC')
bitcoins = float(balance['free'])
btc = format(bitcoins/1, '.8f')
balancecoin1 = client.get_asset_balance(asset=coin1)
balance = client.get_asset_balance(coin1)
coin1_coins = float(balance['free'])
coin1_coins = format(coin1_coins/1, '.8f')
coin1_coins = find_quantity(coin1_coins, 1)
balancecoin2 = client.get_asset_balance(asset=coin2)
balance = client.get_asset_balance(coin2)
coin2_coins = float(balance['free'])
coin2_coins = format(coin2_coins/1, '.8f')
coin2_coins = find_quantity(coin2_coins, 1)
balancecoin3 = client.get_asset_balance(asset=coin3)
balance = client.get_asset_balance(coin3)
coin3_coins = float(balance['free'])
coin3_coins = format(coin3_coins, '.8f')
coin3_coins = find_quantity(coin3_coins, 1)
balancecoin4 = client.get_asset_balance(asset=coin4)
balance = client.get_asset_balance(coin4)
coin4_coins = float(balance['free'])
coin4_coins = format(coin4_coins, '.8f')
coin4_coins = find_quantity(coin4_coins, 1)
balancecoin5 = client.get_asset_balance(asset=coin5)
balance = client.get_asset_balance(coin5)
coin5_coins = float(balance['free'])
coin5_coins = format(coin5_coins, '.8f')
coin5_coins = find_quantity(coin5_coins, 1)
coin1 = 'ADA'
coin2 = 'XLM'
coin3 = 'ZEC'
coin4 = 'NEO'
coin5 = 'BTS'
#Binance API credentials
api_key = 'yourapikey'
api_pw = 'yourapipassword'
client = Client(api_key, api_pw)
balances_coins()
total_b_btc=0
btc_coin1 =0
btc_coin2 =0
btc_coin3 =0
btc_coin4 =0
btc_coin5 =0
btc_coins =0
#LastPrices
#****************** COIN1 ******************
sym1 = coin1 + "BTC"
klines = client.get_historical_klines(sym1, Client.KLINE_INTERVAL_1MINUTE, "1 min ago UTC")
while not klines:
klines = client.get_historical_klines(sym1, Client.KLINE_INTERVAL_1MINUTE, "1 min ago UTC")
most_recent = klines.pop()
last_closing_coin1 = most_recent[4]
last_closing_coin1 = float(last_closing_coin1)
last_closing_coin1 = format(last_closing_coin1/1, '.8f')
last_closing_coin1 = find_quantity(last_closing_coin1, 1)
#****************** COIN2 ******************
sym2 = coin2 + "BTC"
klines = client.get_historical_klines(sym2, Client.KLINE_INTERVAL_1MINUTE, "1 min ago UTC")
while not klines:
klines = client.get_historical_klines(sym2, Client.KLINE_INTERVAL_1MINUTE, "1 min ago UTC")
most_recent = klines.pop()
last_closing_coin2 = most_recent[4]
last_closing_coin2 = float(last_closing_coin2)
last_closing_coin2 = format(last_closing_coin2/1, '.8f')
last_closing_coin2 = find_quantity(last_closing_coin2, 1)
#****************** COIN3 ******************
sym3 = coin3 + "BTC"
klines = client.get_historical_klines(sym3, Client.KLINE_INTERVAL_1MINUTE, "1 min ago UTC")
while not klines:
klines = client.get_historical_klines(sym3, Client.KLINE_INTERVAL_1MINUTE, "1 min ago UTC")
most_recent = klines.pop()
last_closing_coin3 = most_recent[4]
last_closing_coin3 = float(last_closing_coin3)
last_closing_coin3 = format(last_closing_coin3/1, '.8f')
last_closing_coin3 = find_quantity(last_closing_coin3, 1)
#****************** COIN4 ******************
sym4 = coin4 + "BTC"
klines = client.get_historical_klines(sym4, Client.KLINE_INTERVAL_1MINUTE, "1 min ago UTC")
while not klines:
klines = client.get_historical_klines(sym4, Client.KLINE_INTERVAL_1MINUTE, "1 min ago UTC")
most_recent = klines.pop()
last_closing_coin4 = most_recent[4]
last_closing_coin4 = float(last_closing_coin4)
last_closing_coin4 = format(last_closing_coin4/1, '.8f')
last_closing_coin4 = find_quantity(last_closing_coin4, 1)
#****************** COIN5 ******************
sym5 = coin5 + "BTC"
klines = client.get_historical_klines(sym5, Client.KLINE_INTERVAL_1MINUTE, "1 min ago UTC")
while not klines:
klines = client.get_historical_klines(sym5, Client.KLINE_INTERVAL_1MINUTE, "1 min ago UTC")
most_recent = klines.pop()
last_closing_coin5 = most_recent[4]
last_closing_coin5 = float(last_closing_coin5)
last_closing_coin5 = format(last_closing_coin5/1, '.8f')
last_closing_coin5 = find_quantity(last_closing_coin5, 1)
#BTC Value Calculation
btc_coin1 = coin1_coins * last_closing_coin1
btc_coin1 = '{0:.8f}'.format(btc_coin1)
btc_coin1 = float(btc_coin1)
btc_coin2 = coin2_coins * last_closing_coin2
btc_coin2 = '{0:.8f}'.format(btc_coin2)
btc_coin2 = float(btc_coin2)
btc_coin3 = coin3_coins * last_closing_coin3
btc_coin3 = '{0:.8f}'.format(btc_coin3)
btc_coin3 = float(btc_coin3)
btc_coin4 = coin4_coins * last_closing_coin4
btc_coin4 = '{0:.8f}'.format(btc_coin4)
btc_coin4 = float(btc_coin4)
btc_coin5 = coin5_coins * last_closing_coin5
btc_coin5 = '{0:.8f}'.format(btc_coin5)
btc_coin5 = float(btc_coin5)
total_b_btc = btc_coin1+btc_coin2+btc_coin3+btc_coin4+btc_coin5+bitcoins
total_b_btc = '{0:.8f}'.format(total_b_btc)
total_b_btc = float(total_b_btc)
msg1 = "Original Balance - Balance BTC %s | Balance %s %s | Balance %s %s | Balance %s %s | Balance %s %s | Balance %s %s" % (bitcoins,coin1,coin1_coins,coin2,coin2_coins,coin3,coin3_coins,coin4,coin4_coins,coin5,coin5_coins)
msg2 = "Balance in BTC - Balance BTC %s | Balance %s %s | Balance %s %s | Balance %s %s | Balance %s %s | Balance %s %s" % (bitcoins,coin1,btc_coin1,coin2,btc_coin2,coin3,btc_coin3,coin4,btc_coin4,coin5,btc_coin5)
bal = "Total BTC: %s" % (total_b_btc)
print(bal)
print("\n")
print(msg1)
print("\n")
print(msg2)
sym = input('> ')