forked from mylesgamez/Crypto-Arbitrage-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto-arb-bot.py
180 lines (138 loc) · 5.79 KB
/
crypto-arb-bot.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
import ccxt
import time
import logging
import os
# Initialize the logging system
logging.basicConfig(filename='arb_bot.log', level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
# 1. Configurations and Secrets
API_KEYS = {
"binance": {
"apiKey": os.environ["BINANCE_API_KEY"],
"secret": os.environ["BINANCE_API_SECRET"]
},
"coinbasepro": {
"apiKey": os.environ["COINBASE_PRO_API_KEY"],
"secret": os.environ["COINBASE_PRO_API_SECRET"]
}
}
# 2. Dynamic Exchange Initialization
exchanges = {}
for exchange_name, credentials in API_KEYS.items():
exchanges[exchange_name] = getattr(ccxt, exchange_name)(credentials)
# Define the trading pairs
symbols = ['BTC/USDT', 'ADA/USDT', 'ETH/USDT',
'DOGE/USDT', 'LTC/USDT', 'XRP/USDT']
# Define your trading parameters
trade_amount = 0.1
profit_threshold = 0.5
trailing_stop_percentage = 2.0
highest_prices = {symbol: None for symbol in symbols}
buy_orders = {}
sell_orders = {}
max_errors = 5
error_count = 0
# Placeholder for trading fees (in percentage)
TRADING_FEES = {
'binance': 0.1, # assuming 0.1% fee for Binance
'coinbasepro': 0.25 # assuming 0.25% fee for Coinbase Pro
}
def log_info(message):
logging.info(message)
print(message)
def log_error(message):
global error_count
error_count += 1
if error_count >= max_errors:
log_info("Circuit breaker triggered. Halting trading.")
time.sleep(3600)
logging.error(message)
print(f"ERROR: {message}")
def reset_circuit_breaker():
global error_count
error_count = 0
def get_prices(exchanges, symbols):
prices = {}
for exchange in exchanges.values():
for symbol in symbols:
try:
ticker = exchange.fetch_ticker(symbol)
prices[f"{exchange.id} - {symbol}"] = ticker['last']
except Exception as e:
log_error(f"Error fetching data from {exchange.id}: {str(e)}")
return prices
def update_trailing_stop(symbol, current_price):
global highest_prices
if symbol in highest_prices:
if highest_prices[symbol] is None or current_price > highest_prices[symbol]:
highest_prices[symbol] = current_price
else:
highest_prices[symbol] = current_price
def calculate_profit(base_price, comp_price, buy_exchange_name, sell_exchange_name):
"""Calculate profit after considering trading fees."""
buy_fee = TRADING_FEES[buy_exchange_name] / 100
sell_fee = TRADING_FEES[sell_exchange_name] / 100
effective_buy_price = comp_price * (1 + buy_fee)
effective_sell_price = base_price * (1 - sell_fee)
profit_percentage = (
(effective_sell_price - effective_buy_price) / effective_buy_price) * 100
return profit_percentage
def execute_trade(buy_exchange, sell_exchange, symbol, amount, trailing_stop_percentage):
try:
# Define buy and sell orders
buy_order = buy_exchange.create_limit_buy_order(
symbol, amount, buy_price)
sell_order = sell_exchange.create_limit_sell_order(
symbol, amount, sell_price)
# Store order IDs in the respective dictionaries
buy_orders[symbol] = buy_order['id']
sell_orders[symbol] = sell_order['id']
log_info(f"Buy order ID: {buy_order['id']}")
log_info(f"Sell order ID: {sell_order['id']}")
log_info(
f"Successfully executed arbitrage trade on {buy_exchange.id} and {sell_exchange.id}.")
# Implement trailing stop
current_sell_price = sell_order['price']
update_trailing_stop(symbol, current_sell_price)
trailing_stop_price = highest_prices[symbol] * \
(1 - trailing_stop_percentage / 100)
# Continuously check if the trailing stop is triggered
while current_sell_price > trailing_stop_price:
time.sleep(1) # Adjust the interval as needed
current_sell_price = sell_exchange.fetch_order(sell_orders[symbol])[
'price']
if current_sell_price > trailing_stop_price:
log_info("Trailing stop activated. Selling position.")
sell_order = sell_exchange.create_market_sell_order(
symbol, amount)
except Exception as e:
error_message = f"Error executing trade: {str(e)}"
log_error(error_message)
while True:
try:
reset_circuit_breaker()
prices = get_prices(exchanges, symbols)
log_info(prices)
for symbol in symbols:
for ref_exchange_name, ref_exchange in exchanges.items():
base_price = prices.get(f"{ref_exchange_name} - {symbol}")
if not base_price:
continue
for comp_exchange_name, comp_exchange in exchanges.items():
if comp_exchange_name == ref_exchange_name:
continue
comp_price = prices.get(f"{comp_exchange_name} - {symbol}")
if not comp_price:
continue
profit_percentage = calculate_profit(
base_price, comp_price, comp_exchange_name, ref_exchange_name)
if profit_percentage >= profit_threshold:
log_info(
f"Profit opportunity found on {comp_exchange_name} for {symbol} ({profit_percentage}%).")
buy_exchange, sell_exchange = (
comp_exchange, ref_exchange) if base_price > comp_price else (ref_exchange, comp_exchange)
execute_trade(buy_exchange, sell_exchange, symbol, trade_amount,
trailing_stop_percentage, comp_price, base_price)
except Exception as e:
log_error(f"Error: {str(e)}")
time.sleep(5)