-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrading_bot.py
More file actions
155 lines (121 loc) · 6.29 KB
/
trading_bot.py
File metadata and controls
155 lines (121 loc) · 6.29 KB
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
from datetime import datetime, timedelta
import asyncio
import sys
from ccxt import bitmex
sys.path.append("indicators")
import ichimoku
class Bot:
def __init__(self, config: dict, logger=None, client=None):
self._config = config
self._logger = logger
self._symbol = config["symbol"]
self._ichimoku_tenkan_sen_period = config["ichimoku_tenkan_sen_period"]
self._ichimoku_kijun_sen_period = config["ichimoku_kijun_sen_period"]
self._ichimoku_senkou_span_b_period = config["ichimoku_senkou_span_b_period"]
self._ichimoku_displacement_period = config["ichimoku_displacement_period"]
self._data_period = config["data_period"]
self._leverage = config["leverage"]
self._n_multiplier = config["n_multiplier"]
self._seconds_per_cycle = config["seconds_per_cycle"]
if client:
self._client = client
else:
self._client = bitmex({
"test": config["test"],
"apiKey": config["apiKey"],
"secret": config["secret"]
})
def _get_current_price(self) -> float:
return [self._client.fetch_ticker(self._symbol)["high"], self._client.fetch_ticker(self._symbol)["close"]]
def _get_free_balance(self) -> float:
return self._client.fetch_balance()[self._symbol[:self._symbol.index("/")]]["free"]
def _ichimoku_calculate_purchase_size(self, hits):
n = self._n_multiplier
return self._leverage * ((n**hits) / (1 + n + n**2 + n**3))
async def start(self):
previous_ichimokus = []
current_price = self._get_current_price()
free_balance = self._get_free_balance()
usd_free_balance_low = free_balance * current_price[0]
usd_free_balance_high = free_balance * current_price[1]
self._logger.info("----------------------------------------")
self._logger.info("The initial balance is {} BTC.".format(round((((usd_free_balance_low + usd_free_balance_high) / 2) / ((current_price[0] + current_price[1]) / 2)), 6)))
bulls = 0
bears = 0
buy_orders = 0
sell_orders = 0
buys = 0
sells = 0
buy_order_prices = []
sell_order_prices = []
while True:
current_price = self._get_current_price()
usd_free_balance_low = self._get_free_balance() * current_price[0]
usd_free_balance_high = self._get_free_balance() * current_price[1]
previous_ichimokus.append(ichimoku.get(self._client, self._symbol, self._data_period, self._ichimoku_tenkan_sen_period, self._ichimoku_kijun_sen_period,
self._ichimoku_senkou_span_b_period, self._ichimoku_displacement_period))
self._logger.info(previous_ichimokus[len(previous_ichimokus) - 1])
if len(previous_ichimokus) >= 2:
ichimoku_signals = ichimoku.signal(previous_ichimokus)
current_bulls = 0
current_bears = 0
for ichimoku_signal in ichimoku_signals:
signal = ichimoku_signal[1]
if signal == "Bullish":
current_bulls += 1
bulls += 1
elif signal == "Bearish":
current_bears += 1
bears += 1
signal_names = ""
for i in range(0, len(ichimoku_signals) - 1):
signal_names += ichimoku_signals[i][0] + "-" + ichimoku_signals[i][1] + ", "
if len(ichimoku_signals) > 0:
signal_names += ichimoku_signals[len(ichimoku_signals) - 1][0] + "-" + ichimoku_signals[len(ichimoku_signals) - 1][1]
if signal_names != "":
self._logger.info("The following trends were indentified: {}.".format(signal_names))
else:
self._logger.info("No trends were identified.")
current_buy_position = current_bulls - current_bears
if current_buy_position > 0 and (len(sell_order_prices) == 0 or current_price[0] < (sum(sell_order_prices) / len(sell_order_prices))):
current_order_size = int(usd_free_balance_low * self._ichimoku_calculate_purchase_size(abs(current_buy_position)))
self._logger.info("Buying {} contracts at ${}.".format(current_order_size, current_price[0]))
try:
#self._client.create_order(self._symbol, 'limit', amount=(current_order_size + sells), price=current_price)
#self._client.create_order(symbol=self._symbol, type="limit", amount=(current_order_size + buys), price=current_price)
#self._client.create_market_buy_order(symbol=self._symbol, amount=(current_order_size + sells))
self._client.create_limit_buy_order(self._symbol, (current_order_size + sells), current_price[0])
buy_orders += 1
self._logger.info("Bought {} contracts at ${}.".format(current_order_size, current_price[0]))
sell_order_prices = [order_price for order_price in sell_order_prices if order_price > current_price[0]]
buy_order_prices.append(current_price[0])
sells = 0
buys += current_order_size
except Exception:
self._logger.info("Order Failed.")
elif current_buy_position < 0 and (len(buy_order_prices) == 0 or current_price[1] > (sum(buy_order_prices) / len(buy_order_prices))):
current_order_size = int(usd_free_balance_high * self._ichimoku_calculate_purchase_size(abs(current_buy_position)))
self._logger.info("Selling {} contracts at ${}.".format(current_order_size, current_price[1]))
try:
#self._client.create_order(self._symbol, 'limit', amount=-(current_order_size + sells), price=current_price)
#self._client.create_order(symbol=self._symbol, type="limit", amount=-(current_order_size + buys), price=current_price)
#self._client.create_market_sell_order(symbol=self._symbol, amount=(current_order_size + buys))
self._client.create_limit_sell_order(self._symbol, (current_order_size + sells), current_price[1])
sell_orders += 1
self._logger.info("Sold {} contracts at ${}.".format(current_order_size, current_price[1]))
buy_order_prices = [order_price for order_price in buy_order_prices if order_price < current_price[1]]
sell_order_prices.append(current_price[1])
buys = 0
sells += current_order_size
except Exception:
self._logger.info("Order Failed.")
else:
self._logger.info("No viable trade present.")
bulls_to_bears = round((bulls / bears), 2) if bears > 0 else bulls
self._logger.info("Bulls to Bears ratio is {}.".format(bulls_to_bears))
buys_to_sells = round((buy_orders / sell_orders), 2) if sell_orders > 0 else buy_orders
self._logger.info("Actual Buys to Sells ratio is {}.".format(buys_to_sells))
else:
self._logger.info("Waiting until enough data is present to make predictions.")
self._logger.info("----------------------------------------")
await asyncio.sleep(self._seconds_per_cycle - 1)