-
Notifications
You must be signed in to change notification settings - Fork 4
/
Python email trade automater OLDER
200 lines (183 loc) · 7.34 KB
/
Python email trade automater OLDER
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
from flask import Flask
import json
import bybit
import time
import re
from imapclient import IMAPClient
import hashlib
import hmac
import requests
import urllib3
app = Flask(__name__)
@app.route('/')
def execute_trade():
imap_server = IMAPClient('outlook.office365.com',993, ssl=True)
imap_server.login('@outlook.com', 'PASSWORD')
imap_server.select_folder('INBOX')
# messages = imap_server.fetch(emails, ['BODY[]'])
while True:
# search_criteria = {'UNSEEN'}
# Get the first email
unseen_mails = imap_server.search("UNSEEN")
if unseen_mails:
mail_id = list(unseen_mails)[0]
else:
print("No messages found")
for email_id, email_data in imap_server.fetch(unseen_mails, 'RFC822').items():
email_body = email_data.get_body().decode()
passphrase = re.search(r'Passphrase: (.*)', email_body).group(1)
strategy_position_size = re.search(
r'Position size: (.*)', email_body).group(1)
strategy_order_action = re.search(
r'Order action: (.*)', email_body).group(1)
strategy_order_contracts = re.search(
r'Order contracts: (.*)', email_body).group(1)
strategy_order_price = re.search(
r'Order price: (.*)', email_body).group(1)
strategy_order_id = re.search(
r'Order ID: (.*)', email_body).group(1)
strategy_market_position = re.search(
r'Market position: (.*)', email_body).group(1)
strategy_market_position_size = re.search(
r'Market position size: (.*)', email_body).group(1)
strategy_prev_market_position = re.search(
r'Previous market position: (.*)', email_body).group(1)
strategy_prev_market_position_size = re.search(
r'Previous market position size: (.*)', email_body).group(1)
#message_ids = imap_server.get_messages_ids(unseen_mails)
imap_server.set_flags([bytes(str(mail_id), 'utf-8') for email_id in unseen_mails], [b'\\Seen'])
time.sleep(5)
imap_server.logout()
# Log out of the email server
# # imap_server.logout()
# Define json data
json_data = {
"timestamp": "{{timestamp}}",
"exchange": "{{exchange}}",
"symbol": "{{ticker}}",
"candle": {
"open": "{{open}}",
"high": "{{high}}",
"low": "{{low}}",
"close": "{{close}}",
"volume": "{{volume}}",
},
"order": {
"side": "side",
"symbol": "symbol",
"type": "Limit",
"qty": "qty",
"price": "price",
"time_in_force": "gtc"
},
"passphrase": "{{passphrase}}"
}
data = '{"passphrase": "a321!", "time": "2023-01-01T12:00:00", "exchange": "abc", "ticker": "abc_123", "bar": {"time": "2023-01-01T12:00:00", "open": 100.0, "high": 101.0, "low": 99.0, "close": 100.5, "volume": 1000}, "strategy": {"position_size": 100, "order_action": "buy", "order_contracts": 5, "order_price": 100.0, "order_id": 123456, "market_position": "long", "market_position_size": 5}, "account": {"side": "buy", "symbol": "abc_123", "order_type": "limit", "qty": 5, "price": 100.0, "time_in_force": "gtc"}}'
# Load the data
json_data = json.loads(data)
# Access the values in the dictionary
passphrase = json_data["passphrase"]
time1 = json_data["time"]
exchange = json_data["exchange"]
ticker = json_data["ticker"]
bar = json_data["bar"]
strategy = json_data.get("strategy", {})
account = json_data.get("account", {})
# Access the values within the bar dictionary
bar_time = bar["time"]
bar_open = bar["open"]
bar_high = bar["high"]
bar_low = bar["low"]
bar_close = bar["close"]
bar_volume = bar["volume"]
# Access the values within the strategy dictionary
position_size = strategy.get("position_size", "")
order_action = strategy.get("order_action", "")
order_contracts = strategy.get("order_contracts", "")
order_price = strategy.get("order_price", "")
order_id = strategy.get("order_id", "")
market_position = strategy.get("market_position", "")
market_position_size = strategy.get("market_position_size", "")
prev_market_position = strategy.get("prev_market_position", "")
# Access the values within the account dictionary
side = account.get("side", "")
symbol = account.get("symbol", "")
order_type = account.get("order_type", "")
qty = account.get("qty", "")
price = account.get("price", "")
time_in_force = account.get("time_in_force", "")
# Print the values
print("Passphrase:", passphrase)
print("Time:", time)
print("Exchange:", exchange)
print("Ticker:", ticker)
print("Bar Time:", bar_time)
print("Bar Open:", bar_open)
print("Bar High:", bar_high)
print("Bar Low:", bar_low)
print("Bar Close:", bar_close)
print("Bar Volume:", bar_volume)
print("Position Size:", position_size)
print("Order Action:", order_action)
print("Order Contracts:", order_contracts)
print("Order Price:", order_price)
print("Order ID:", order_id)
print("Market Position:", market_position)
print("Market Position Size:", market_position_size)
print("Prev Market Position:", prev_market_position)
print("Side:", side)
print("Symbol:", symbol)
print("Order Type:", order_type)
print("Qty:", qty)
print("Price:", price)
print("Time In Force:", time_in_force)
# Check if the passphrase in the email matches the expected passphrase
expected_passphrase = "a321!"
if "passphrase" in json_data and json_data["passphrase"] != expected_passphrase:
print("Invalid passphrase. Trade not executed.")
exit()
# Extract the other information from the email
#api_key = json_data["api_key"]
#secret_key = json_data["secret_key"]
with open("side.json") as f:
symbol_json_data = json.load(f)
symbol = symbol_json_data["symbol"]
with open("side.json") as f:
order_side_json_data = json.load(f)
order_side = order_side_json_data["side"]
with open("order_type.json") as f:
order_type_json_data = json.load(f)
order_type = order_type_json_data["order_type"]
with open("qty.json") as f:
qty_json_data = json.load(f)
qty = qty_json_data["qty"]
with open("price.json") as f:
price_json_data = json.load(f)
price = price_json_data["price"]
with open("time_in_force.json") as f:
time_in_force_json_data = json.load(f)
time_in_force = time_in_force_json_data["time_in_force"]
strategy = json_data["strategy"]
## Initialize the Bybit client
client = bybit.bybit(test=False, api_key= "76QjbsflcrBdSZiSrU", api_secret="SGAAHPUfQnTm1d1EX12ffVaizjbFA4iT32ps")
# Place the order
print(client.Order.Order_new(side="side", symbol="symbol", order_type="order_type", qty=100, price=100, time_in_force="time_in_force").result())
# Get the order ID from the response
print(client.Order.Order_getOrders(symbol="symbol").result())
#order_id = response[0]
# Cancel the order
# print(client.Order.Order_cancel(symbol="symbol", order_id=order_id).result())
# Cancel All Active Orders
# print(client.Order.Order_cancelAll(symbol="BTCUSD").result())
# Replace Active Order
# print(client.Order.Order_replace(symbol="BTCUSD", order_id="e838ebcd-77be-43e7-ae4a-9bc380bad6ec", p_r_qty="3").result())
#Query Active Orders(real-time)
#print(client.Order.Order_query(symbol="BTCUSD", order_id="e838ebcd-77be-43e7-ae4a-9bc380bad6ec").result())
#status = order_details["result"]["order_status"]
# My Position
print(client.Positions.Positions_myPosition(symbol="symbol").result())
# If the order is filled, print a message
#if myPosition == True:
# print("Order Active")
#else:
# print("Order not filled.")