-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
224 lines (192 loc) · 7.49 KB
/
Copy pathbot.py
File metadata and controls
224 lines (192 loc) · 7.49 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import json
import time
from pprint import pprint
from log import Log
from binance_f import RequestClient
from binance_f.model.constant import *
import utils
class Bot:
def __init__(self):
# read configuration from json file
with open('config.json', 'r') as file:
config = json.load(file)
self.api_key = config['api_key']
self.api_secret = config['api_secret']
self.api_url = config['api_url']
self.sandbox = config['is_sandbox'] == "True"
self.percentage = float(config['percentage'])
self.sl_percentage = float(config['sl_percentage'])
self.tp_percentage = float(config['tp_percentage'])
self.time = int(config['time'])
self.leverage = int(config['leverage'])
self.size = int(config['size'])
self.client = RequestClient(api_key=self.api_key, secret_key=self.api_secret, url=self.api_url)
self.prices = {
"before": {},
"now": {}
}
def run(self):
self.store_prices()
while 1:
time.sleep(self.time)
self.store_prices()
self.detect_mooning()
def detect_mooning(self):
if "before" not in self.prices or "now" not in self.prices:
return
for symbol, price in self.prices["now"].items():
if symbol == "BTCUSDT" or symbol == "ENSUSDT" or symbol == "NKNUSDT":
continue
old_price = self.prices["before"][symbol]
percentage = ((price - old_price) / old_price) * 100.0
if abs(percentage) > self.percentage:
print("mooning ! : " + symbol + " " + str(percentage))
print("current price : " + str(price))
print("old price : " + str(old_price))
print("qty : ")
quantity = self.calculate_position(symbol)
pprint(quantity)
self.add_order(
symbol=symbol,
price=price,
percentage=percentage,
quantity=quantity
)
self.add_sl(
symbol=symbol,
price=price,
old_price=old_price,
percentage=percentage,
quantity=quantity
)
self.add_tp(
symbol=symbol,
price=price,
old_price=old_price,
percentage=percentage,
quantity=quantity
)
def get_all_prices(self):
return self.client.get_symbol_price_ticker()
def store_prices(self):
self.prices["before"] = self.prices["now"]
self.prices["now"] = {}
prices = self.get_all_prices()
for price in prices:
if price.symbol[-4:] == "USDT":
self.prices["now"][price.symbol] = price.price
return self.prices
def add_order(self, symbol, price, percentage, quantity):
if utils.is_buying(percentage):
side_order = OrderSide.BUY
position_order = PositionSide.LONG
else:
side_order = OrderSide.SELL
position_order = PositionSide.SHORT
Log.log_trade(
symbol=symbol,
position=position_order,
quantity=quantity,
price=price
)
if not self.sandbox:
self.client.change_initial_leverage(
symbol=symbol,
leverage=self.leverage
)
# self.client.change_margin_type(
# symbol=symbol,
# marginType=FuturesMarginType.ISOLATED
# )
return self.client.post_order(
symbol=symbol,
side=side_order,
# positionSide=position_order,
ordertype=OrderType.MARKET,
quantity=quantity
)
def add_sl(self, symbol, price, old_price, percentage, quantity):
sl_price = abs(float((price - old_price) * self.sl_percentage))
if utils.is_buying(percentage):
sl_price = price - sl_price
side_order = OrderSide.SELL
else:
sl_price = price + sl_price
side_order = OrderSide.BUY
sl_price = utils.truncate(sl_price, 4)
# precision = self.get_market_precision(_market=symbol)
# sl_price = self.round_to_precision(_qty=sl_price, _precision=precision)
# print("precision sl")
# pprint(precision)
# print("sl price")
# pprint(sl_price)
Log.log_sl(
symbol=symbol,
quantity=quantity,
price=sl_price
)
if not self.sandbox:
return self.client.post_order(
symbol=symbol,
side=side_order,
ordertype=OrderType.STOP_MARKET,
stopPrice=sl_price,
closePosition=True,
workingType=WorkingType.MARK_PRICE
)
def add_tp(self, symbol, price, old_price, percentage, quantity):
tp_price = abs(float((price - old_price) * self.tp_percentage))
if utils.is_buying(percentage):
tp_price = price + tp_price
side_order = OrderSide.SELL
else:
tp_price = price - tp_price
side_order = OrderSide.BUY
tp_price = utils.truncate(tp_price, 4)
# precision = self.get_market_precision(_market=symbol)
# tp_price = self.round_to_precision(_qty=tp_price, _precision=precision)
# print("precision tp")
# pprint(precision)
# print("price tp")
# pprint(tp_price)
Log.log_tp(
symbol=symbol,
quantity=quantity,
price=tp_price
)
if not self.sandbox:
return self.client.post_order(
symbol=symbol,
side=side_order,
ordertype=OrderType.TAKE_PROFIT_MARKET,
stopPrice=tp_price,
closePosition=True,
workingType=WorkingType.MARK_PRICE
)
# Calcul des prix qty
# calculate how big a position we can open with the margin we have and the leverage we are using
def calculate_position_size(self, usdt_balance=1.0, _market="BTCUSDT", _leverage=1):
price = self.client.get_symbol_price_ticker(_market)
price = price[0].price
qty = (float(usdt_balance) / price) * _leverage
qty = round(qty * 0.99, 8)
return qty
# get the precision of the market, this is needed to avoid errors when creating orders
def get_market_precision(self, _market="BTCUSDT"):
market_data = self.client.get_exchange_information()
precision = 3
for market in market_data.symbols:
if market.symbol == _market:
precision = market.quantityPrecision
break
return precision
# round the position size we can open to the precision of the market
def round_to_precision(self, _qty, _precision):
new_qty = "{:0.0{}f}".format(_qty, _precision)
return float(new_qty)
# calculate a rounded position size for the bot, based on current USDT holding, leverage and market
def calculate_position(self, _market="BTCUSDT"):
qty = self.calculate_position_size(usdt_balance=self.size, _market=_market, _leverage=self.leverage)
precision = self.get_market_precision(_market=_market)
qty = self.round_to_precision(qty, precision)
return qty