-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrade.py
More file actions
197 lines (163 loc) · 7.63 KB
/
trade.py
File metadata and controls
197 lines (163 loc) · 7.63 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
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
import api_access_data
import constants
from datetime import datetime
import gdax
from influxdb import InfluxDBClient
import MySQLdb
BUY_CONFIDENCE_THRESHOLD = 0.02
SELL_CONFIDENCE_THRESHOLD = -0.01
BASE_SIZE = 0.01
LIMIT_ORDER_BID_BUFFER = 0.01
LIMIT_ORDER_ASK_BUFFER = 0.01
gdax_auth_client = gdax.AuthenticatedClient(api_access_data.GDAX_API_KEY,
api_access_data.GDAX_API_SECRET,
api_access_data.GDAX_PASSPHRASE)
influxdb_client = InfluxDBClient(
constants.INFLUXDB_HOST, constants.INFLUXDB_PORT,
api_access_data.INFLUXDB_USER, api_access_data.INFLUXDB_PASS,
constants.INFLUXDB_DB_NAME)
def write_transaction_to_mysql(cursor, order, fiat_currency, crypto_currency):
insert_query = """insert into %s values ('%s', %s, '%s', '%s', '%s', \
'%s', '%s', '%s', %s, %s, %s)""" % (constants.TRANSACTIONS_TABLE,
order[constants.GDAX_ID],
order[constants.GDAX_SIZE],
order[constants.GDAX_PRODUCT_ID],
order[constants.GDAX_SIDE],
order[constants.GDAX_TYPE],
order[constants.GDAX_CREATED_AT],
order[constants.GDAX_DONE_AT],
order[constants.GDAX_DONE_REASON],
order[constants.GDAX_FILL_FEES],
order[constants.GDAX_FILLED_SIZE],
order[constants.GDAX_EXECUTED_VALUE])
cursor.execute(insert_query)
# Update bankroll in MySQL
transaction_cost = float(order[constants.GDAX_FILL_FEES]) + float(
order[constants.GDAX_EXECUTED_VALUE])
amount_filled = float(order[constants.GDAX_FILLED_SIZE])
if (order[constants.GDAX_SIDE] == constants.ORDER_BUY):
transaction_cost = -transaction_cost
amount_filled = -amount_filled
update_fiat_query = """update %s set %s = %s + %f where %s='%s'""" % (
constants.BANKROLL_TABLE, constants.BANKROLL_AMOUNT,
constants.BANKROLL_AMOUNT, transaction_cost,
constants.BANKROLL_CURRENCY, fiat_currency)
cursor.execute(update_fiat_query)
update_crypto_query = """update %s set %s = %s - %f where %s='%s'""" % (
constants.BANKROLL_TABLE, constants.BANKROLL_AMOUNT,
constants.BANKROLL_AMOUNT, amount_filled, constants.BANKROLL_CURRENCY,
crypto_currency)
cursor.execute(update_crypto_query)
def write_bankroll_to_influxdb(cursor):
fetch_bankroll_query = """select %s, %s from %s""" % (
constants.BANKROLL_CURRENCY, constants.BANKROLL_AMOUNT,
constants.BANKROLL_TABLE)
cursor.execute(fetch_bankroll_query)
bankroll = cursor.fetchall()
data = []
for balance in bankroll:
datapoint = {}
datapoint[
constants.
INFLUXDB_MEASUREMENT] = constants.INFLUXDB_MEASUREMENT_BANKROLL
datapoint[constants.INFLUXDB_TAGS] = {
constants.INFLUXDB_TAGS_CURRENCY: balance[0]
}
datapoint[constants.INFLUXDB_FIELDS] = {
constants.INFLUXDB_FIELDS_AMOUNT: balance[1]
}
data.append(datapoint)
influxdb_client.write_points(data)
def handle_outstanding_orders(cursor, fiat_currency, crypto_currency):
# Check on previous orders
fetch_order_query = """select %s from %s""" % (
constants.PENDING_ORDERS_ORDER_ID, constants.PENDING_ORDERS_TABLE)
cursor.execute(fetch_order_query)
pending_orders = cursor.fetchall()
for order in pending_orders:
# Cancel outstanding orders
order_id = order[0]
cancelled_order = gdax_auth_client.cancel_order(order_id)
print(cancelled_order)
order_status = gdax_auth_client.get_order(order_id)
if (constants.GDAX_MESSAGE in order_status):
print("Error: %s" % (order_status[constants.GDAX_MESSAGE]))
else:
write_transaction_to_mysql(cursor, order_status, fiat_currency,
crypto_currency)
# Clear pending orders
clear_orders_query = """delete from %s""" % (
constants.PENDING_ORDERS_TABLE)
cursor.execute(clear_orders_query)
def get_balance(currency, cursor):
bal_query = """select %s from %s where %s='%s'""" % (
constants.BANKROLL_AMOUNT, constants.BANKROLL_TABLE,
constants.BANKROLL_CURRENCY, currency)
cursor.execute(bal_query)
return cursor.fetchone()[0]
def trade(pred, product, cursor):
print('Trading with trend prediction of %f for %s at %s' %
(pred, product, datetime.now()))
crypto_currency, fiat_currency = product.split('-')
handle_outstanding_orders(cursor, fiat_currency, crypto_currency)
crypto_bal = get_balance(crypto_currency, cursor)
fiat_bal = get_balance(fiat_currency, cursor)
ticker_data = gdax_auth_client.get_product_ticker(product_id=product)
order_id = ''
# Check thresholds and place orders
if (pred > BUY_CONFIDENCE_THRESHOLD):
bid_price = ticker_data[constants.GDAX_BID]
bid_price = float(bid_price) - LIMIT_ORDER_BID_BUFFER
# TODO: Make this a function of the magnitude of the change and
# available balance
num_units = 1
order_size = BASE_SIZE * num_units
# Check to see if we have enough money
if (fiat_bal >= order_size * bid_price):
result = gdax_auth_client.buy(
price=bid_price,
size=order_size,
type=constants.ORDER_LIMIT,
side=constants.ORDER_BUY,
product_id=product)
print('Place limit buy order at %f for %f unit(s) of %s on %s' %
(bid_price, order_size, crypto_currency, product))
print(result)
if (constants.GDAX_ID in result):
order_id = result[constants.GDAX_ID]
else:
print('%s balance too low to purchase unit(s) on %s' %
(fiat_currency, product))
elif (pred < SELL_CONFIDENCE_THRESHOLD):
ask_price = ticker_data[constants.GDAX_ASK]
ask_price = float(ask_price) + LIMIT_ORDER_ASK_BUFFER
# TODO: Make this a function of the magnitude of the change and
# available balance
num_units = 1
order_size = BASE_SIZE * num_units
# Check to see if we have enough money
if (crypto_bal >= order_size):
result = gdax_auth_client.sell(
price=ask_price,
size=order_size,
type=constants.ORDER_LIMIT,
side=constants.ORDER_SELL,
product_id=product)
print('Place limit sell order at %f for %f unit(s) of %s on %s' %
(ask_price, order_size, crypto_currency, product))
print(result)
if (constants.GDAX_ID in result):
order_id = result[constants.GDAX_ID]
else:
print('No units of %s available to sell on %s' % (crypto_currency,
product))
else:
print('Predicted trend of %f for %s is below the buy confidence \
threshold and above the sell confidence threshold.' % (pred,
product))
# Store new order in pending orders table
if (order_id):
store_order_query = """insert into %s values('%s')""" % (
constants.PENDING_ORDERS_TABLE, order_id)
cursor.execute(store_order_query)
write_bankroll_to_influxdb(cursor)