forked from kdmukai/gemini_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemini_bot.py
233 lines (186 loc) · 7.88 KB
/
gemini_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
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
#!/usr/bin/env python
import argparse
import boto3
import configparser
import datetime
import decimal
import json
import math
import requests
import os
import time
from decimal import Decimal
from gemini_api import GeminiApiConnection, GeminiRequestException
"""
Gemini API docs: https://docs.gemini.com/rest-api/
"""
def get_timestamp():
ts = time.time()
return datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
parser = argparse.ArgumentParser(
description="""
Basic Gemini DCA buying/selling bot.
ex:
BTCUSD BUY 14 USD (buy $14 worth of BTC)
BTCUSD BUY 0.00125 BTC (buy 0.00125 BTC)
ETHBTC SELL 0.00125 BTC (sell 0.00125 BTC worth of ETH)
ETHBTC SELL 0.1 ETH (sell 0.1 ETH)
""",
formatter_class=argparse.RawTextHelpFormatter
)
# Required positional arguments
parser.add_argument('market_name', help="(e.g. BTCUSD, ETHBTC, etc)")
parser.add_argument('order_side',
type=str,
choices=["BUY", "SELL"])
parser.add_argument('amount',
type=Decimal,
help="The quantity to buy or sell in the amount_currency")
parser.add_argument('amount_currency',
help="The currency the amount is denominated in")
# Additional options
parser.add_argument('-sandbox',
action="store_true",
default=False,
dest="sandbox_mode",
help="Run against sandbox, skips user confirmation prompt")
parser.add_argument('-warn_after',
default=300,
action="store",
type=int,
dest="warn_after",
help="secs to wait before sending an alert that an order isn't done")
parser.add_argument('-j', '--job',
action="store_true",
default=False,
dest="job_mode",
help="Suppresses user confirmation prompt")
parser.add_argument('-c', '--config',
default="settings.conf",
dest="config_file",
help="Override default config file location")
if __name__ == "__main__":
args = parser.parse_args()
market_name = args.market_name
order_side = args.order_side.lower()
amount = args.amount
amount_currency = args.amount_currency
sandbox_mode = args.sandbox_mode
job_mode = args.job_mode
warn_after = args.warn_after
if not sandbox_mode and not job_mode:
response = input("Production purchase! Confirm [Y]: ")
if response != 'Y':
print("Exiting without submitting purchase.")
exit()
# Read settings
config = configparser.ConfigParser()
config.read(args.config_file)
config_section = 'production'
if sandbox_mode:
config_section = 'sandbox'
client_key = config.get(config_section, 'CLIENT_KEY')
secret_key = config.get(config_section, 'CLIENT_SECRET')
sns_topic = config.get(config_section, 'SNS_TOPIC')
aws_access_key_id = config.get(config_section, 'AWS_ACCESS_KEY_ID')
aws_secret_access_key = config.get(config_section, 'AWS_SECRET_ACCESS_KEY')
aws_region = config.get(config_section, 'AWS_REGION')
gemini_api_conn = GeminiApiConnection(client_key=client_key, client_secret=secret_key)
# Configure the market details
symbol_details = gemini_api_conn.symbol_details(market_name)
base_currency = symbol_details.get("base_currency")
quote_currency = symbol_details.get("quote_currency")
base_min_size = Decimal(str(symbol_details.get("min_order_size"))).normalize()
base_increment = Decimal(str(symbol_details.get("tick_size"))).normalize()
quote_increment = Decimal(str(symbol_details.get("quote_increment"))).normalize()
if amount_currency == symbol_details.get("quote_currency"):
amount_currency_is_quote_currency = True
elif amount_currency == symbol_details.get("base_currency"):
amount_currency_is_quote_currency = False
else:
raise Exception(f"amount_currency {amount_currency} not in market {market_name}")
print(f"base_min_size: {base_min_size}")
print(f"base_increment: {base_increment}")
print(f"quote_increment: {quote_increment}")
# Prep boto SNS client for email notifications
sns = boto3.client(
"sns",
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=aws_region
)
def calculate_midmarket_price():
order_book = gemini_api_conn.current_order_book(market_name)
bid = Decimal(order_book.get('bids')[0].get('price')).quantize(quote_increment)
ask = Decimal(order_book.get('asks')[0].get('price')).quantize(quote_increment)
# Avg the bid/ask but round to nearest quote_increment
if order_side == "buy":
midmarket_price = (math.floor((ask + bid) / Decimal('2.0') / quote_increment) * quote_increment).quantize(quote_increment, decimal.ROUND_DOWN)
else:
midmarket_price = (math.floor((ask + bid) / Decimal('2.0') / quote_increment) * quote_increment).quantize(quote_increment, decimal.ROUND_UP)
print(f"ask: ${ask}")
print(f"bid: ${bid}")
print(f"midmarket_price: ${midmarket_price}")
return midmarket_price
def place_order(price):
try:
if amount_currency_is_quote_currency:
result = gemini_api_conn.new_order(
market=market_name,
side=order_side,
amount=float((amount / price).quantize(base_increment)),
price=price
)
else:
result = gemini_api_conn.new_order(
market=market_name,
side=order_side,
amount=float(amount.quantize(base_increment)),
price=price
)
except GeminiRequestException as e:
sns.publish(
TopicArn=sns_topic,
Subject=f"ERROR placing {base_currency} {order_side} order: {e.response_json.get('reason')}",
Message=json.dumps(e.response_json, indent=4)
)
print(json.dumps(e.response_json, indent=4))
exit()
return result
midmarket_price = calculate_midmarket_price()
order = place_order(midmarket_price)
print(json.dumps(order, indent=2))
order_id = order.get("order_id")
# Set up monitoring loop for the next hour
wait_time = 60
total_wait_time = 0
retries = 0
while Decimal(order.get('remaining_amount')) > Decimal('0'):
if total_wait_time > warn_after:
sns.publish(
TopicArn=sns_topic,
Subject=f"{market_name} {order_side} order of {amount} {amount_currency} OPEN/UNFILLED",
Message=json.dumps(order, indent=4)
)
exit()
if order.get('is_cancelled'):
# Most likely the order was manually cancelled in the UI
sns.publish(
TopicArn=sns_topic,
Subject=f"{market_name} {order_side} order of {amount} {amount_currency} CANCELLED",
Message=json.dumps(order, sort_keys=True, indent=4)
)
exit()
print(f"{get_timestamp()}: Order {order_id} still pending. Sleeping for {wait_time} (total {total_wait_time})")
time.sleep(wait_time)
total_wait_time += wait_time
order = gemini_api_conn.order_status(order_id=order_id)
# Order status is no longer pending!
print(json.dumps(order, indent=2))
subject = f"{market_name} {order_side} order of {amount} {amount_currency} complete @ {midmarket_price} {quote_currency}"
print(subject)
sns.publish(
TopicArn=sns_topic,
Subject=subject,
Message=json.dumps(order, sort_keys=True, indent=4)
)