-
Notifications
You must be signed in to change notification settings - Fork 4
/
default.py
78 lines (62 loc) · 2.49 KB
/
default.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
import requests
import base64
import config
from pybit import usdt_perpetual
import ast
def tg_send_message(text):
return requests.get('https://api.telegram.org/bot' + config.BOT_TOKEN +
'/sendMessage?chat_id=' + config.CHAT_ID +
'&text=' + text + '').json()
def run(event, context):
try:
message = base64.b64decode(event['body']).decode('utf-8')
dictionary = ast.literal_eval(message.replace("'", "\""))
ticker = dictionary['ticker'].replace(".P", "")
action = dictionary['action']
contracts = dictionary['contracts']
tg_send_message(f'{ticker} {action} {contracts}')
session_auth = usdt_perpetual.HTTP(endpoint="https://api.bybit.com",
api_key=config.API_KEY, api_secret=config.SECRET_KEY)
response = ""
if action == 'long':
response = session_auth.place_active_order(
symbol=ticker,
side='Buy',
order_type='Market',
qty=contracts,
time_in_force="GoodTillCancel",
reduce_only=False,
close_on_trigger=False)
if action == 'Close entry(s) order long':
response = session_auth.place_active_order(
symbol=ticker,
side='Sell',
order_type='Market',
qty=contracts,
time_in_force="GoodTillCancel",
reduce_only=True,
close_on_trigger=False)
if action == 'short':
response = session_auth.place_active_order(
symbol=ticker,
side='Sell',
order_type='Market',
qty=contracts,
time_in_force="GoodTillCancel",
reduce_only=False,
close_on_trigger=False)
if action == 'Close entry(s) order short':
response = session_auth.place_active_order(
symbol=ticker,
side='Buy',
order_type='Market',
qty=contracts,
time_in_force="GoodTillCancel",
reduce_only=True,
close_on_trigger=False)
tg_send_message(str(response))
response = {'statusCode': 200, 'body': 'Message was successfully sent!'}
except Exception as e:
response = {'statusCode': 404, 'body': 'Something went wrong! ' + str(e)}
tg_send_message(str(response))
return response