-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
188 lines (158 loc) · 6.97 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
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
import json
import time
from telepot.api import download
import config
from flask import Flask, request, render_template
from binance.client import Client
from binance.enums import *
import math
import cryptocompare
import telepot
# Function that truncates a [number] to a certain amount of [digits]
def truncate(number, digits) -> float:
stepper = 10.0 ** digits
return math.trunc(stepper * number) / stepper
# Executes an order, by default a market one
def order(side, quantity, symbol, order_type=ORDER_TYPE_MARKET):
try:
order = client.create_order(
symbol=symbol, side=side, type=order_type, quantity=quantity)
except Exception as e:
err = "{}".format(e)
bot.sendMessage(
config.USER_ID, f"⛔ *{side}* order on *{symbol}* failed\n\n\
*error*: {err}", parse_mode='Markdown')
return False
return order
# Flask setup
app = Flask(__name__)
# Binance API setup
client = Client(config.API_KEY, config.API_SECRET, tld='com')
# Telepot telegram API setup
bot = telepot.Bot(config.TOKEN)
# A placeholder homepage
@app.route('/')
def welcome():
return render_template('index.html')
# The actual webhook that TradingView uses
@ app.route('/webhook', methods=['POST'])
def webhook():
data = json.loads(request.data)
if data['passphrase'] != config.WEBHOOK_PASSPHRASE:
bot.sendMessage(
config.USER_ID, "⚠ *Unauthorized request warning* ⚠\n\n\
Someone has just tried to execute an order via your bot,\
but he doesn't know the passphrase. Consider changing it.",\
parse_mode='Markdown')
return {
"code": "error",
"message": "Nice try, invalid passphrase"
}
activeSymbol = data["strategy"]["symbol"].upper()
pair = activeSymbol.split(" ")
activeSymbol = pair[0]+pair[1]
doubleOrder = bool(data["strategy"]["doubleorder"])
balances = [float(client.get_asset_balance(asset=pair[0])['free']),
float(client.get_asset_balance(asset=pair[1])['free'])]
price = cryptocompare.get_price(pair[0], pair[1])[pair[0]][pair[1]]
minNotional = float(client.get_symbol_info(
activeSymbol)['filters'][3]['minNotional'])
side = data['strategy']['order_action'].upper()
if doubleOrder:
priceUp = float(client.get_avg_price(symbol=pair[0]+"UPUSDT")["price"])
priceDown = float(client.get_avg_price(symbol=pair[0]+"DOWNUSDT")["price"])
balanceDown = float(client.get_asset_balance(asset=pair[0]+"DOWN")['free'])
usdtBalance = float(client.get_asset_balance(asset="USDT")['free'])
balanceUp = float(client.get_asset_balance(asset=pair[0]+"UP")['free'])
if side == "SELL":
quantity = str(truncate(balanceUp/1.05, 2))
order_response = order(side, quantity, pair[0]+"UPUSDT")
time.sleep(1)
side = "BUY"
usdtBalance = float(client.get_asset_balance(asset="USDT")['free'])
quantity = str(truncate(usdtBalance/priceDown/1.05, 1))
order_response2 = order(side, quantity, pair[0]+"DOWNUSDT")
elif side == "BUY":
side = "SELL"
quantity = str(truncate(balanceDown/1.05, 1))
order_response = order(side, quantity, pair[0]+"DOWNUSDT")
time.sleep(1)
side = "BUY"
usdtBalance = float(client.get_asset_balance(asset="USDT")['free'])
quantity = str(truncate(usdtBalance/priceUp/1.05, 2))
order_response2 = order(side, quantity, pair[0]+"UPUSDT")
if side == "SELLHALF":
quantity = str(truncate(balanceUp/2/1.05, 2))
order_response = order(side, quantity, pair[0]+"UPUSDT")
time.sleep(1)
side = "BUY"
usdtBalance = float(client.get_asset_balance(asset="USDT")['free'])
quantity = str(truncate(usdtBalance/priceDown/1.05, 1))
order_response2 = order(side, quantity, pair[0]+"DOWNUSDT")
elif side == "BUYHALF":
side = "SELL"
quantity = str(truncate(balanceDown/2/1.05, 1))
order_response = order(side, quantity, pair[0]+"DOWNUSDT")
time.sleep(1)
side = "BUY"
usdtBalance = float(client.get_asset_balance(asset="USDT")['free'])
quantity = str(truncate(usdtBalance/priceUp/1.05, 2))
order_response2 = order(side, quantity, pair[0]+"UPUSDT")
if side == "SELLTHIRD":
quantity = str(truncate(balanceUp/3/1.05, 2))
order_response = order(side, quantity, pair[0]+"UPUSDT")
time.sleep(1)
side = "BUY"
usdtBalance = float(client.get_asset_balance(asset="USDT")['free'])
quantity = str(truncate(usdtBalance/priceDown/1.05, 1))
order_response2 = order(side, quantity, pair[0]+"DOWNUSDT")
elif side == "BUYTHIRD":
side = "SELL"
quantity = str(truncate(balanceDown/3/1.05, 1))
order_response = order(side, quantity, pair[0]+"DOWNUSDT")
time.sleep(1)
side = "BUY"
usdtBalance = float(client.get_asset_balance(asset="USDT")['free'])
quantity = str(truncate(usdtBalance/priceUp/1.05, 2))
order_response2 = order(side, quantity, pair[0]+"UPUSDT")
elif side == "SELL":
quantity = str(truncate(balances[0], 3))
elif side == "BUY":
quantity = str(truncate(balances[1]/price, 3))
elif side == "SELLHALF":
side = "SELL"
quantity = str(truncate(balances[0]/2, 3))
elif side == "BUYHALF":
side = "BUY"
quantity = str(truncate(balances[1]/price/2, 3))
elif side == "SELLTHIRD":
side = "SELL"
quantity = str(truncate(balances[0]/3, 3))
elif side == "BUYTHIRD":
side = "BUY"
quantity = str(truncate(balances[1]/price/3, 3))
if float(quantity) <= minNotional:
quantity = minNotional + \
truncate(minNotional/5, len(str(minNotional).split('.')[1]))
if not doubleOrder:
order_response = order(side, quantity, activeSymbol)
if order_response:
if order_response2 & doubleOrder:
bot.sendMessage(config.USER_ID,\
f"💹 *{side}* double order on *{pair[0]}* of {quantity}\n\
from the _{data['time']}m trigger_", parse_mode='Markdown')
else:
bot.sendMessage(config.USER_ID,\
f"✅ *{side}* order on *{activeSymbol}* of {quantity} {pair[1]}\n\
from the _{data['time']}m trigger_", parse_mode='Markdown')
return {
"code": "success",
"message": "order executed"
}
else:
return {
"code": "error",
"message": "order failed"
}
if __name__ == '__main__':
app.run()