-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbybit_sma_bot.py
420 lines (320 loc) · 12.7 KB
/
bybit_sma_bot.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# Ryuryu's Bybit USDT Perpetual Futures Bot
# Simpe Moving Average Edition (Production Mode #6973)
# ----------------------------------------------------
# (c) 2023 Ryan Hayabusa
# GitGub: https://github.com/ryu878
# Web: https://aadresearch.xyz
# Discord: ryuryu#4087
# ----------------------------------------------------
import os
import time
import sqlite3
import pandas as pd
from config import *
import datetime as dt
from datetime import datetime
from inspect import currentframe
from pybit import usdt_perpetual
terminal_title = 'Bybit USDT Perpetual Futures Bot'
# print(f'\33]0;{terminal_title}[]\a', end='', flush=True)
ver = '1.0'
client = usdt_perpetual.HTTP(endpoint=endpoint, api_key=api_key, api_secret=api_secret)
session_unauth = usdt_perpetual.HTTP(endpoint=endpoint)
def get_linenumber():
cf = currentframe()
global line_number
line_number = cf.f_back.f_lineno
######################
# Asking for Variables
######################
print('')
print('',terminal_title,ver)
print(' ─────────────────────────────────────')
symbol = input(' What asset to Trade? ')
symbol = (symbol+'USDT').upper()
print(' Asset:',symbol)
print(' Data refresh interval: 1 3 5 15 30 60 120 240 360 720 "D" "M" "W"')
interval = input(' Choose your Timeframe: ')
if interval.isdigit():
interval = int(interval)
ma_length = input(' MA Lenght: ')
ma_length = int(ma_length)
waittime = input(' Timeout (minutes): ')
waittime = int(waittime)
# waittime = 1
csize = input(' Lot size: ')
csize = int(csize)
# csize = 1
####################
# Removing Settings
####################
try:
os.remove('settings_futures.db')
os.remove('settings_futures.db-journal')
except Exception as e:
# get_linenumber()
# print(line_number, 'exeception: {}'.format(e))
pass
#########################
# Saving Veriables to SQL
#########################
conn = sqlite3.connect('settings_futures.db')
cursor = conn.cursor()
conn.execute("""
CREATE TABLE IF NOT EXISTS settings (
interval text UNIQUE PRIMARY KEY NOT NULL,
ma_length int,
waittime int,
csize real
)""")
sqlite_update = """
INSERT OR REPLACE INTO settings (
interval,
ma_length,
waittime,
csize)
VALUES (?, ?, ?, ?)
"""
data_tuple = (interval,ma_length,waittime,csize)
cursor.execute(sqlite_update, data_tuple)
conn.commit()
conn.close()
######################
# Get Settings from DB
######################
def get_settings_from_database():
conn = sqlite3.connect('settings_futures.db')
cursor = conn.cursor()
data = cursor.execute(f'SELECT * FROM settings').fetchall()
for settings in data:
global interval_db, ma_length_db, waittime_db, csize_db
interval_db = settings[0]
ma_length_db = settings[1]
waittime_db = settings[2]
csize_db = settings[3]
conn.commit()
conn.close()
get_settings_from_database()
def datefromtimestamp(timestamp):
date = datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:00')
return date
def year_from_timestamp(timestamp):
year = datetime.utcfromtimestamp(timestamp).strftime('%Y')
return int(year)
def month_from_timestamp(timestamp):
month = datetime.utcfromtimestamp(timestamp).strftime('%m')
return int(month)
def day_from_timestamp(timestamp):
day = datetime.utcfromtimestamp(timestamp).strftime('%d')
return int(day)
def hour_from_timestamp(timestamp):
hour = datetime.utcfromtimestamp(timestamp).strftime('%H')
return int(hour)
def min_from_timestamp(timestamp):
minute = datetime.utcfromtimestamp(timestamp).strftime('%M')
return int(minute)
def sec_from_timestamp(timestamp):
sec = datetime.utcfromtimestamp(timestamp).strftime('%S')
return int(sec)
def get_orderbook_rest():
orderbook = session_unauth.orderbook(symbol=symbol)
df = pd.DataFrame(orderbook['result'])
# print(df)
global ask, bid
ask = float(df['price'][25])
bid = float(df['price'][0])
# print(ask,bid)
try:
get_orderbook_rest()
except Exception as e:
get_linenumber()
print(line_number, 'exeception: {}'.format(e))
pass
print(' Ask:',ask)
print(' Bid:',bid)
def get_position_rest():
positions = client.my_position(symbol=symbol)
global sell_position_size
global sell_position_prce
global buy_position_size
global buy_position_prce
# print(positions)
for position in positions['result']:
if position['side'] == 'Sell':
sell_position_size = position['size']
sell_position_prce = position['entry_price']
if position['side'] == 'Buy':
buy_position_size = position['size']
buy_position_prce = position['entry_price']
get_position_rest()
while True:
# time_now = int(time.time()) + 10800 # +3 Hours since UTC
# time_now = int(time.time()) + 7200 # +2 Hours since UTC
# time_now = int(time.time()) + 3600 # +1 Hour since UTC
time_now = int(time.time()) # UTC time
print(' Time now:',time_now)
print(' Date from ts:',datefromtimestamp(time_now))
if interval == int(interval):
# print('Integer')
from_time = time_now - (interval * ma_length * 60)
start_year = year_from_timestamp(from_time)
start_month = month_from_timestamp(from_time)
start_day = day_from_timestamp(from_time)
start_hour = hour_from_timestamp(from_time)
start_min = min_from_timestamp(from_time)
start_sec = sec_from_timestamp(from_time)
if interval == 'D':
# print('Day')
from_time = time_now - (1440 * ma_length * 60)
start_year = year_from_timestamp(from_time)
start_month = month_from_timestamp(from_time)
start_day = day_from_timestamp(from_time)
start_hour = hour_from_timestamp(from_time)
start_min = min_from_timestamp(from_time)
start_sec = sec_from_timestamp(from_time)
if interval == 'W':
# print('Week')
from_time = time_now - (10080 * ma_length * 60)
start_year = year_from_timestamp(from_time)
start_month = month_from_timestamp(from_time)
start_day = day_from_timestamp(from_time)
start_hour = hour_from_timestamp(from_time)
start_min = min_from_timestamp(from_time)
start_sec = sec_from_timestamp(from_time)
if interval == 'M':
# print('Week')
from_time = time_now - (43800 * ma_length * 60)
start_year = year_from_timestamp(from_time)
start_month = month_from_timestamp(from_time)
start_day = day_from_timestamp(from_time)
start_hour = hour_from_timestamp(from_time)
start_min = min_from_timestamp(from_time)
start_sec = sec_from_timestamp(from_time)
print(' ─────────────────────────────────────')
print(' Asset:',symbol)
print(' Timeframe:',interval_db)
print(' MA length:',ma_length_db)
print(' Timeout:',waittime_db)
print(' Lot size:',csize_db)
print(' From Time:',from_time)
# print('Human Start Time:',startTime)
print(' Time:',start_year,start_month,start_day,start_hour,start_min)
print('')
print(' Time now:',time_now,datefromtimestamp(time_now))
print(' From time:',from_time,datefromtimestamp(from_time))
print(' Diff:',time_now - from_time,(time_now - from_time) / 60)
def get_bybit_candles(symbol, interval, limit, startTime):
startTime = str(int(startTime.timestamp()))
data = session_unauth.query_kline(symbol=symbol,interval=interval,limit=limit,from_time=startTime)
df = pd.DataFrame(data['result'])
if df.empty == False:
df.index = [dt.datetime.fromtimestamp(x) for x in df.open_time]
df.index.name = 'DataTime'
return df
else:
return None
df_list = []
# Input data
startTime = dt.datetime(start_year, start_month, start_day, start_hour, start_min)
while True:
new_df = get_bybit_candles(symbol=symbol, interval=interval, limit=200, startTime=startTime)
time.sleep(0.1)
if new_df is None:
print('end')
break
df_list.append(new_df)
startTime = max(new_df.index)
df = pd.concat(df_list)
df = df.drop(columns = ['turnover','volume'])
print(df)
get_settings_from_database()
df['open_time'] = pd.to_datetime(df['open_time'], unit='s', origin='unix')
print(df)
df['sma'] = df['close']
df['sma'] = pd.to_numeric(df['sma'], downcast='float')
df['sma'] = df['sma'].mean()
global sma
sma = df['sma'].iloc[-1]
print(sma)
try:
get_orderbook_rest()
except Exception as e:
get_linenumber()
print(line_number, 'exeception: {}'.format(e))
pass
print(' ─────────────────────────────────────')
print(' Ask:',ask,'Bid:',bid)
print(' SMA:',sma)
print(' ─────────────────────────────────────')
try:
get_position_rest()
except Exception as e:
get_linenumber()
print(line_number, 'exeception: {}'.format(e))
pass
if buy_position_size == 0 and sell_position_size == 0:
if ask > sma:
print(' Ask > SMA')
print(' To Buy',csize,'lots')
try:
market_buy = client.place_active_order(side='Buy',symbol=symbol,order_type='Market',qty=csize,time_in_force='GoodTillCancel',reduce_only=False,close_on_trigger=False)
print(' Market Buy')
except Exception as e:
get_linenumber()
print(line_number, 'exeception: {}'.format(e))
pass
time.sleep(0.01)
print(' Waiting...',waittime,'mins')
if bid < sma:
print(' Bid < SMA')
print(' To Sell',csize,'lots')
try:
market_sell = client.place_active_order(side='Sell',symbol=symbol,order_type='Market',qty=csize,time_in_force='GoodTillCancel',reduce_only=False,close_on_trigger=False)
print(' Market Sell')
except Exception as e:
get_linenumber()
print(line_number, 'exeception: {}'.format(e))
pass
time.sleep(0.01)
print(' Waiting...',waittime,'mins')
else:
print(' Positions found:')
print(' Buy:',buy_position_size,'@',buy_position_prce)
print(' Sell:',sell_position_size,'@',sell_position_prce)
if buy_position_size > 0 and ask > sma:
print(' Buy position found. Ask > SMA. Waiting...')
if sell_position_size > 0 and bid < sma:
print(' Sell position found. Bid < SMA. Waiting...')
if buy_position_size > 0 and bid < sma:
print(' Buy position found, but Bid < SMA. Closing it and opening Sell.')
try:
market_sell = client.place_active_order(side='Sell',symbol=symbol,order_type='Market',qty=buy_position_size,time_in_force='GoodTillCancel',reduce_only=True,close_on_trigger=True)
except Exception as e:
get_linenumber()
print(line_number, 'exeception: {}'.format(e))
pass
time.sleep(0.01)
try:
market_sell = client.place_active_order(side='Sell',symbol=symbol,order_type='Market',qty=csize,time_in_force='GoodTillCancel',reduce_only=False,close_on_trigger=False)
except Exception as e:
get_linenumber()
print(line_number, 'exeception: {}'.format(e))
pass
time.sleep(0.01)
if sell_position_size > 0 and ask > sma:
print(' Sell position found, but Bid < SMA. Closing it and opening Buy.')
try:
market_buy = client.place_active_order(side='Buy',symbol=symbol,order_type='Market',qty=sell_position_size,time_in_force='GoodTillCancel',reduce_only=True,close_on_trigger=True)
except Exception as e:
get_linenumber()
print(line_number, 'exeception: {}'.format(e))
pass
time.sleep(0.01)
try:
market_buy = client.place_active_order(side='Buy',symbol=symbol,order_type='Market',qty=csize,time_in_force='GoodTillCancel',reduce_only=False,close_on_trigger=False)
except Exception as e:
get_linenumber()
print(line_number, 'exeception: {}'.format(e))
pass
time.sleep(0.01)
time.sleep(waittime*60)