This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
exit_script.py
47 lines (36 loc) · 1.62 KB
/
exit_script.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
#!/bin/python3
import ccxt, os
from datetime import datetime
from pybit.unified_trading import HTTP
live_trade = True
pair = "BTC" + "USDT"
trade_qty = 0.001
exchange = ccxt.bybit()
client = HTTP(
testnet=False,
api_key=os.environ.get('BYBIT_KEY'),
api_secret=os.environ.get('BYBIT_SECRET'))
def position_information(pair):
response = client.get_positions(category='linear', symbol=pair)
position = response['result']['list'][0] if response['result']['list'] else None
return position
def market_open_long(pair, trade_qty):
if live_trade: client.place_order(category="linear", symbol=pair, side='Buy', qty=trade_qty, order_type='Market')
print("🚀 GO_LONG 🚀")
def market_open_short(pair, trade_qty):
if live_trade: client.place_order(category="linear", symbol=pair, side='Sell', qty=trade_qty, order_type='Market')
print("💥 GO_SHORT 💥")
def market_close_long(pair):
if live_trade: client.place_order(symbol=pair, side='Sell', order_type='Market', qty=0, reduce_only=True, category='linear', position_idx=0)
print("💰 CLOSED_LONG 💰")
def market_close_short(pair):
if live_trade: client.place_order(symbol=pair, side='Buy', order_type='Market', qty=0, reduce_only=True, category='linear', position_idx=0)
print("💰 CLOSED_SHORT 💰")
def close_position(pair):
response = position_information(pair)
# print(response)
if response['size'] > '0': market_close_long(pair)
elif response['size'] < '0': market_close_short(pair)
else: print("No position opened")
print("Last action executed @ " + datetime.now().strftime("%H:%M:%S") + "\n")
close_position(pair)