-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
162 lines (137 loc) · 5.88 KB
/
app.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
from flask import Flask, session, render_template, flash
from flask_session import Session
from binance.client import Client
import config
import math
import itertools
import requests
app = Flask(__name__)
app.secret_key = b'kdrtfjd53453543jyrtjstjsrjstjhtsrhs'
client = Client(config.API_KEY, config.API_SECRET)
starting_crypto = "USDT"
@app.route('/')
def index():
#############################################
white_list = ["BNB", "EUR", "ETH"]
fee = 0.075
#############################################
# Create stepSize.txt
exchangeInfo = client.get_exchange_info()
totalSymbols = exchangeInfo["symbols"]
txt_file = open("stepSize.txt", "r+")
txt_file.truncate(0)
for symbol in totalSymbols:
s0 = symbol
st = symbol["symbol"]
sz = symbol["filters"][2]["stepSize"]
tt = st + ": " + sz
txt_file.write(tt + "\n")
txt_file.close()
# Detect owned balances
def owned():
#############################################
# Value in USDT to overcome for black listing
greater = 5
#############################################
assets = client.get_exchange_info()
symbol_assets = assets["symbols"]
prices = client.get_all_tickers()
account = client.get_account()
balances = account["balances"]
for balance in balances:
if float(balance["free"]) > 0:
global starting_crypto
strAsset = str(balance["asset"]) + starting_crypto
if strAsset in str(symbol_assets):
for price in prices:
if price["symbol"] == strAsset:
converted_price = float(price["price"]) * float(balance["free"])
if converted_price > greater:
yield balance["asset"]
else:
strAsset = starting_crypto + str(balance["asset"])
if strAsset in str(symbol_assets):
for price in prices:
if price["symbol"] == strAsset:
converted_price = (1 / float(price["price"])) * float(balance["free"])
if converted_price > greater:
yield balance["asset"]
# Calculation quantities owned crypto
def raw_prices():
assets = client.get_exchange_info()
symbol_assets = assets["symbols"]
prices = client.get_all_tickers()
for blacked in black_list:
global starting_crypto
strAsset = str(blacked) + str(starting_crypto)
if strAsset in str(symbol_assets):
if blacked in str(client.get_symbol_info(strAsset)["baseAsset"]):
quantity = float(client.get_asset_balance(asset=blacked)["free"])
yield quantity
else:
strAsset = str(starting_crypto) + str(blacked)
if strAsset in str(symbol_assets):
if blacked in str(client.get_symbol_info(strAsset)["quoteAsset"]):
for price in prices:
if price["symbol"] == strAsset:
quantity = (1 / float(price["price"])) * float(client.get_asset_balance(asset=blacked)["free"])
yield quantity
# Function to calculate stepSize in number
def stepSizer(sy):
with open("stepSize.txt") as f:
for num, line in enumerate(f, 1):
if sy in line:
lineDect = line
lineDetected = lineDect.replace("\n", "")
stepSize_raw = lineDetected.partition(": ")[2]
stepSize_raw_position = stepSize_raw.find("1")
stepSize_pre_raw = stepSize_raw.partition(".")[2]
stepSize_pre_raw_raw = stepSize_pre_raw.partition("1")[0]
if stepSize_raw_position == 0:
noDec = True
return 0
else:
noDec = False
return stepSize_pre_raw_raw.count("0") + 1
# Truncate decimals without rounded them
def truncate(f, n):
return math.floor(f * 10 ** n) / 10 ** n
# Detect black listed assets
def symbols():
assets = client.get_exchange_info()
symbol_assets = assets["symbols"]
prices = client.get_all_tickers()
for blacked in black_list:
global starting_crypto
strAsset = str(blacked) + str(starting_crypto)
if strAsset in str(symbol_assets):
yield strAsset
else:
strAsset = str(starting_crypto) + str(blacked)
if strAsset in str(symbol_assets):
yield strAsset
###########################################
owned = list(owned())
black_list = [x for x in owned if x not in white_list]
symbols = list(symbols())
raw_prices = list(raw_prices())
# Send orders
def orders():
for symbol, raw_price in zip(symbols, raw_prices):
quantity = truncate(raw_price - ((raw_price * fee) / 100), stepSizer(symbol))
if symbol.startswith(starting_crypto):
side = "BUY"
else:
side = "SELL"
try:
client.create_order(symbol=symbol, side=side,type="MARKET",quantity=quantity)
# print("\n" + str(symbol) + " ORDER DONE")
yield symbol
except Exception as e:
flash(e.message, "error")
# print("\n" + str(symbol) + " " + e.message)
yield symbol
orders = list(orders())
return render_template("index.html", orders = orders)
if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port=80)