generated from OPCODE-Open-Spring-Fest/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo.py
211 lines (176 loc) · 6.66 KB
/
algo.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
import logging
import queue
import threading
import time
from enum import StrEnum
import ccxt
import numpy as np
class Order(StrEnum):
BUY = 'BUY'
SELL = 'SELL'
HOLD = 'HOLD'
# Initialize logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Trading parameters
MAX_POSITION_SIZE = 100
RISK_PER_TRADE = 0.01
TAKE_PROFIT_RATIO = 0.02
STOP_LOSS_RATIO = 0.01
DYNAMIC_RISK_ADJUSTMENT = True
POSITION_SIZE_BASED_ON_VOLATILITY = True
# Order execution parameters
LATENCY = 0.001
SLIPPAGE = 0.001
# Initialize the exchange
exchange = ccxt.binance({
'apiKey': 'YOUR_API_KEY',
'secret': 'YOUR_SECRET',
})
# Fetch market data from the exchange
symbol = 'BTC/USDT'
data = exchange.fetch_ohlcv(symbol, timeframe='1m', limit=1000)
closing_prices = np.array([candle[4] for candle in data]) # Closing prices
# Market data processing
def process_market_data(prices, data_queue, stop_flag):
for price in prices:
if stop_flag.is_set():
break
time.sleep(0.001) # Simulate market data processing latency
data_queue.put(price)
stop_flag.set()
# Order execution
class OrderManagement:
def __init__(self, exchange):
self.exchange = exchange
self.orders = {}
self.hedge_positions = {}
def generate_bollinger_band_signal(self, closing_prices, window_size, num_std_dev):
try:
sma = np.mean(closing_prices[-window_size:])
std_dev = np.std(closing_prices[-window_size:])
upper_band = sma + num_std_dev * std_dev
lower_band = sma - num_std_dev * std_dev
current_price = closing_prices[-1]
if current_price > upper_band:
return 'SELL'
elif current_price < lower_band:
return 'BUY'
else:
return 'HOLD'
except Exception as e:
print(f"Error generating Bollinger Bands signal: {e}")
return 'HOLD'
def generate_ema_signal(self, closing_prices, period):
try:
ema = np.mean(closing_prices[-period:])
current_price = closing_prices[-1]
if current_price > ema:
return 'BUY'
elif current_price < ema:
return 'SELL'
else:
return 'HOLD'
except Exception as e:
print(f"Error generating EMA signal: {e}")
return 'HOLD'
def create_hedge_position(self, symbol, quantity, expiry, signal):
try:
hedge_order = self.exchange.create_market_buy_order(symbol, quantity, {'expiry': expiry, 'signal': signal})
self.hedge_positions[(symbol, expiry)] = hedge_order
return hedge_order
except Exception as e:
print(f"Error creating hedge position: {e}")
return None
def close_hedge_position(self, symbol, expiry):
try:
if (symbol, expiry) in self.hedge_positions:
close_order = self.exchange.create_market_sell_order(symbol, self.hedge_positions[(symbol, expiry)]['amount'])
del self.hedge_positions[(symbol, expiry)]
return close_order
else:
print("Hedge position not found")
return None
except Exception as e:
print(f"Error closing hedge position: {e}")
return None
def hedge(self, symbol, quantity, weekly_expiry, monthly_expiry, closing_prices, bollinger_window_size, bollinger_num_std_dev, ema_period):
try:
bollinger_signal = self.generate_bollinger_band_signal(closing_prices, bollinger_window_size, bollinger_num_std_dev)
ema_signal = self.generate_ema_signal(closing_prices, ema_period)
weekly_signal = self.create_hedge_position(symbol, quantity, weekly_expiry, bollinger_signal)
monthly_signal = self.create_hedge_position(symbol, quantity, monthly_expiry, ema_signal)
return weekly_signal, monthly_signal
except Exception as e:
print(f"Error hedging: {e}")
return None, None
def unhedge(self, symbol, expiry):
try:
if (symbol, expiry) in self.hedge_positions:
return self.close_hedge_position(symbol, expiry)
else:
print("No hedge position to close")
return None
except Exception as e:
print(f"Error unhedging: {e}")
return None
# Trading strategy
def ema_strategy(prices, short_ema_period, long_ema_period):
short_ema = np.mean(prices[-short_ema_period:])
long_ema = np.mean(prices[-long_ema_period:])
if short_ema > long_ema:
return Order.BUY
elif short_ema < long_ema:
return Order.SELL
else:
return Order.HOLD
def bollinger_band_strategy(prices, window_size, num_std_dev):
sma = np.mean(prices[-window_size:])
std_dev = np.std(prices[-window_size:])
upper_band = sma + num_std_dev * std_dev
lower_band = sma - num_std_dev * std_dev
current_price = prices[-1]
if current_price > upper_band:
return Order.SELL
elif current_price < lower_band:
return Order.BUY
else:
return Order.HOLD
# Main trading loop
data_queue = queue.Queue()
order_queue = queue.Queue()
stop_flag = threading.Event()
# Start threads for market data processing and order execution
market_data_thread = threading.Thread(target=process_market_data, args=(closing_prices, data_queue, stop_flag))
market_data_thread.start()
order_execution_thread = threading.Thread(target=execute_order, args=(order_queue, stop_flag))
order_execution_thread.start()
# Strategy parameters
short_ema_period = 10
long_ema_period = 50
bollinger_window_size = 20
bollinger_num_std_dev = 2
position = 0
while not stop_flag.is_set():
try:
current_price = data_queue.get(timeout=1)
# EMA strategy
ema_signal = ema_strategy(closing_prices, short_ema_period, long_ema_period)
# Bollinger Bands strategy
bb_signal = bollinger_band_strategy(closing_prices, bollinger_window_size, bollinger_num_std_dev)
# Buy or sell based on the signals
if ema_signal == Order.BUY and bb_signal == Order.BUY and position < MAX_POSITION_SIZE:
execute_buy_order(current_price)
position += 1
elif ema_signal == Order.SELL and bb_signal == Order.SELL and position > -MAX_POSITION_SIZE:
execute_sell_order(current_price)
position -= 1
data_queue.task_done()
except queue.Empty:
break
except Exception as e:
logger.error(f"An error occurred: {e}")
# Wait for threads to finish
stop_flag.set()
market_data_thread.join()
order_execution_thread.join()