-
Notifications
You must be signed in to change notification settings - Fork 0
/
trading_main.py
280 lines (214 loc) · 11.1 KB
/
trading_main.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
import json
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import websocket
import threading
from datetime import datetime
from dotenv import load_dotenv
import os
import logging
import time
from binance.client import AsyncClient
import pickle
import asyncio
from pykalman import KalmanFilter
from trading_strategy_utils import *
from binance_api_functions import *
logging.basicConfig(filename='trading.log', filemode='w',
format='%(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Add console handler to logger
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler)
async def fetchPriceAndTrade():
global data_btc
global data_eth
while True:
# Fetch price of BTC and assign to data_btc
data_btc = await binance_api_with_retry(get_symbol_ticker, binance_api, symbol="BTCUSDT")
data_eth = await binance_api_with_retry(get_symbol_ticker, binance_api, symbol="ETHUSDT")
# print(price_data)
# data_btc = price_data[0]
# data_eth = price_data[1]
logger.info("Data BTC: {}".format(json.dumps(data_btc)))
logger.info("Data ETH: {}".format(json.dumps(data_eth)))
await checkTradingLogic()
await asyncio.sleep(60) # Wait for 1 minute
async def shortMarginTrading(collateral_level, trade_dollars_amount, symbol, main_asset):
# Check the isolated margin account balance
isolated_margin_account = await binance_api_with_retry(get_isolated_margin_account, binance_api, symbols=symbol)
current_collateral = float(
isolated_margin_account['assets'][0]['quoteAsset']['totalAsset'])
if current_collateral < collateral_level:
# Add collateral to the isolated margin account
transfer_margin_to_isolated = await binance_api_with_retry(transfer_spot_to_isolated_margin, binance_api, transfer_amount=collateral_level-current_collateral, symbol=symbol)
logger.info("Transfer to margin amount: {}".format(
json.dumps(transfer_margin_to_isolated)))
# Send an alert here
# Borrow ETH for shorting in isolated margin mode
borrow_amount = '{:.4f}'.format(trade_dollars_amount /
float(isolated_margin_account['assets'][0]["indexPrice"]))
loan_response = await binance_api_with_retry(create_margin_loan, binance_api, asset=main_asset, borrow_amount=borrow_amount, symbol=symbol)
logger.info("Loan response: {}".format(json.dumps(loan_response)))
loan_conversion_to_usd_response = await binance_api_with_retry(create_margin_order, binance_api, borrow_amount=borrow_amount, symbol=symbol, side="SELL", type="MARKET")
logger.info("Loan conversion to USDT response: {}".format(
json.dumps(loan_conversion_to_usd_response)))
return float(borrow_amount)
async def long(amount, symbol):
long_response = await binance_api_with_retry(order_market_buy, binance_api, amount=float('{:.4f}'.format(amount)), symbol=symbol)
logger.info("Long order response: {}".format(json.dumps(long_response)))
async def openPosition(current_state):
global trading_states
# Define the collateral level and the amount of ETH and BTC to trade
collateral_level = 5 # This can be adjusted based on your preference
trade_dollars_amount = 10
# Get spot account balance for USDT
spot_account_balance = await binance_api_with_retry(get_asset_balance, binance_api, asset="USDT")
logger.info(
"Spot account balance for USDT before opening position: {}".format(json.dumps(spot_account_balance)))
if current_state == 2:
# Above line Open. short ETH, long BTC
trading_states["position"] = 1
borrow_amount = await shortMarginTrading(
collateral_level, trade_dollars_amount, "ETHUSDT", "ETH")
# Buy BTC using spot trading
long_amount = trading_states["reg_slope"]*borrow_amount
await long(long_amount, "BTCUSDT")
elif current_state == -2:
# Below line Open. short BTC, long ETH
trading_states["position"] = -1
borrow_amount = await shortMarginTrading(
collateral_level, trade_dollars_amount, "BTCUSDT", "BTC")
# Buy BTC using spot trading
long_amount = borrow_amount/trading_states["reg_slope"]
await long(long_amount, "ETHUSDT")
trading_states["is_open"] = True
trading_states["open_slope"] = trading_states["reg_slope"]
trading_states["borrow_amount"] = borrow_amount
trading_states["long_amount"] = long_amount
# Now open the positions. Also log price of entry. And transaction cost.
async def repayMarginTrading(borrow_amount, symbol):
# Repay the loan in isolated margin mode
repay_loan_response = await binance_api_with_retry(create_margin_order, binance_api, borrow_amount=borrow_amount, symbol=symbol, side="BUY", type="MARKET", sideEffectType="AUTO_REPAY")
logger.info("Repay loan response: {}".format(
json.dumps(repay_loan_response)))
async def closeLong(amount, symbol):
close_long_response = await binance_api_with_retry(order_market_sell, binance_api, amount=float('{:.4f}'.format(amount)), symbol=symbol)
logger.info("Close long position response: {}".format(
json.dumps(close_long_response)))
async def closePosition():
global trading_states
# After repaying, ideally everything should be converted to USDT
if trading_states["position"] == 1:
isolated_margin_account = await binance_api_with_retry(get_isolated_margin_account, binance_api, symbols="ETHUSDT")
logger.info("Isolated account before closing ETHUSDT: {}".format(
isolated_margin_account))
await repayMarginTrading(trading_states["borrow_amount"], "ETHUSDT")
await closeLong(trading_states["long_amount"], "BTCUSDT")
isolated_margin_account = await binance_api_with_retry(get_isolated_margin_account, binance_api, symbols="ETHUSDT")
transfer_margin_to_spot = await binance_api_with_retry(transfer_isolated_margin_to_spot, binance_api, transfer_amount=isolated_margin_account['assets'][0]['quoteAsset']['free'], symbol='ETHUSDT')
logger.info("Transferred balance from margin to spot ETHUSDT: {}".format(
json.dumps(transfer_margin_to_spot)))
elif trading_states["position"] == -1:
await repayMarginTrading(trading_states["borrow_amount"], "BTCUSDT")
await closeLong(trading_states["long_amount"], "ETHUSDT")
isolated_margin_account = await binance_api_with_retry(get_isolated_margin_account, binance_api, symbols="BTCUSDT")
transfer_margin_to_spot = await binance_api_with_retry(transfer_isolated_margin_to_spot, binance_api, transfer_amount=isolated_margin_account['assets'][0]['quoteAsset']['totalAsset'], symbol='BTCUSDT')
logger.info("Transferred balance from margin to spot BTCUSDT: {}".format(
json.dumps(transfer_margin_to_spot)))
spot_account_balance = await binance_api_with_retry(get_asset_balance, binance_api, asset="USDT")
logger.info("Spot account balance for USDT after closing position: {}".format(
json.dumps(spot_account_balance)))
trading_states["close_durations"].append(trading_states["open_time"])
trading_states["position"] = 0
trading_states["is_open"] = False
trading_states["open_time"] = 0
trading_states["cum_slope_diff"] = 0
async def checkTradingLogic():
global data_btc
global data_eth
global trading_states
if data_btc and data_eth:
print("Current position", trading_states["position"])
price_btc = float(data_btc['price'])
price_eth = float(data_eth['price'])
trading_states["kalman_theta"], trading_states["kalman_P"] = kalmanUpdate(
np.array(price_btc).reshape(1, 1), np.array(price_eth).reshape(1, 1), trading_states["kalman_theta"], trading_states["kalman_P"], kalman_params["W"], kalman_params["sigma_e"], kalman_params["window"])
print("New slope", trading_states["kalman_theta"][0][0])
trading_states["prev_slope"] = trading_states["reg_slope"]
trading_states["reg_slope"] = trading_states["kalman_theta"][0][0]
print("Open slope", trading_states["open_slope"])
if trading_states["is_open"]:
trading_states["cum_slope_diff"] += (
trading_states["reg_slope"] - trading_states["prev_slope"])
trading_states["open_time"] += 1
if np.abs(trading_states["cum_slope_diff"]) > trading_params["slope_diff_threshold"]:
await closePosition()
# Send an alert here and stop trading. Exit code.
current_state = currentPos(
price_btc, price_eth, trading_states["open_slope"], trading_params["lower_bound"], trading_params["upper_bound"])
else:
current_state = currentPos(
price_btc, price_eth, trading_states["reg_slope"], trading_params["lower_bound"], trading_params["upper_bound"])
print("Current state", current_state)
if (current_state == 2 or current_state == -2) and trading_states["position"] == 0:
await openPosition(current_state)
# Send an alert message here as well.
elif (current_state == -1 or current_state == -2) and trading_states["position"] == 1:
await closePosition()
# Send an alert message here as well.
elif (current_state == 1 or current_state == 2) and trading_states["position"] == -1:
await closePosition()
# Send an alert message here as well.
data_btc = None
data_eth = None
# Save trading_states here. Also modify code to pick-up latest states when code is restarted.
with open('trading_states.pickle', 'wb') as file:
pickle.dump(trading_states, file)
async def main():
global binance_api
binance_api = await AsyncClient.create(api_key, api_secret)
await fetchPriceAndTrade()
if __name__ == "__main__":
data_btc = None
data_eth = None
kalman_params = {
"W": np.array([1e-6]).reshape(1, 1),
"sigma_e": 3,
"window": 1
}
trading_params = {
"slope_diff_threshold": 0.02,
"lower_bound": -0.05,
"upper_bound": 0.05,
}
if os.path.exists('trading_states.pickle'):
with open('trading_states.pickle', 'rb') as file:
trading_states = pickle.load(file)
else:
trading_states = {
"kalman_theta": np.array([0.02]).reshape(1, 1),
"kalman_P": np.array([1e-6]).reshape(1, 1),
"position": 0,
"returns": [],
"percentage_returns": [],
"transaction_costs": [],
"is_open": False,
"borrow_amount": 0,
"long_amount": 0,
"open_time": 0,
"close_durations": [],
"cum_slope_diff": 0,
"open_slope": 0,
"prev_slope": 0,
"reg_slope": 0
}
# Define the Binance API
load_dotenv()
api_key = os.getenv("API_KEY")
api_secret = os.getenv("API_SECRET")
loop = asyncio.get_event_loop()
loop.run_until_complete(main())