-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheckForSingleCurrencyOpps.py
226 lines (193 loc) · 8.74 KB
/
checkForSingleCurrencyOpps.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
##!/usr/bin/python
# Name: checkForSingleCurrencyOpps.py
# Author: Patrick Mullaney
# Date Created: 1-20-2018
# Last Edited: 3-10-2018
# Description: This script checks for single currency arbitrage opportunities.
import readExchangeRatesGDAX, readExchangeRatesGemini
import currency, exchange
import numpy
# Opportunity object stores the potential info about an exchange.
class Opportunity():
currency = None
buyExchange = None
sellExchange = None
buyPrice = None
sellPrice = None
amount = None
profitLoss = 0.00
################################################################################
# Calculates revenue with deposit and withdraw fee costs.
def calcRev1(amount, lowPrice, highPrice, exchangeCostLow, exchangeCostHigh, depositCost, withdrawCost):
revenue = ((amount * lowPrice) * depositCost)/lowPrice * exchangeCostLow * highPrice * exchangeCostHigh * withdrawCost
return revenue
# Calculates revenue without deposit and withdraw fee costs.
def calcRev2(amount, lowPrice, highPrice, exchangeFeeLow, exchangeFeeHigh):
revenue = (amount * highPrice * exchangeFeeHigh) - (amount * lowPrice * exchangeFeeLow)
return revenue
################################################################################
# Same results as calcRev1, just easier to read and prints out for debugging.
def calcRev3(amount, lowPrice, highPrice, exchangeFeeLow, exchangeFeeHigh, depositCost, withdrawCost):
'''
print "Amount: ", amount
print "Low Price: ", lowPrice
print "High Price: ", highPrice
print "exchangeLow: ", exchangeFeeLow
print "exchangeHigh: ", exchangeFeeHigh
print "dep cost: ", depositCost
print "withdrawCost: ", withdrawCost
'''
revenue = (amount * lowPrice) * depositCost
revenue = revenue/lowPrice * exchangeFeeLow
revenue = revenue * highPrice * exchangeFeeHigh
revenue = revenue * withdrawCost
return revenue
################################################################################
# Takes the amount of coins, information about the high-price exchange, low-price exchange, and returns info about arbitrage opportunity.
def calculateProfitLoss(amount, high, low):
# Fiat deposit fee in %.
depositCost = float(100.00 - low.depositFee)/100.00
# Exchange fee of lower currency in %.
exchangeCostLow = float(100.00 - low.exchangeFee)/100.00
# Exchange fee of higher currency in %.
exchangeCostHigh = float(100.00 - high.exchangeFee)/100.00
# Fiat withdrawal fee in %.
withdrawCost = float(100.00 - high.withdrawFee)/100.00
# Calculate revenue.
# Original-> revenue = ((((amount * low.price) * depositCost)/low.price * exchangeCostLow) * high.price * exchangeCostHigh) * withdrawCost
revenue = calcRev1(amount, low.price, high.price, exchangeCostLow, exchangeCostHigh, depositCost, withdrawCost)
# Profit/loss = revenue - investment.
profit = revenue - (low.price * amount)
# Round down to two decimals.
profit = float('%.2f'%(profit))
# Create opportunity object
arbitrage = Opportunity()
arbitrage.currency = high.currency
arbitrage.buyExchange = low.name
arbitrage.sellExchange = high.name
arbitrage.profitLoss = profit
arbitrage.sellPrice = '${:,.2f}'.format(high.price)
arbitrage.buyPrice = '${:,.2f}'.format(low.price)
arbitrage.amount = amount
# Optimize by include exchange prices/fees?
return arbitrage
################################################################################
# Checks for an arbitrage opportunity for a given amount between exchanges.
def checkOpportunity(amount, gdax, gemini):
# Set max opportunity amount to arbitrary negative number
maxOpp = Opportunity()
maxOpp.profitLoss = -999999999.99;
# If GDAX price is higher.
if gdax.price > gemini.price:
# Calculate opportunities from 1 to amount.
for i in range(1, amount):
# Calculate profit/loss opportunity.
opportunity = calculateProfitLoss(i, gdax, gemini)
# If profit greater than the current max, update.
if opportunity.profitLoss > maxOpp.profitLoss:
maxOpp = opportunity
return maxOpp
# Else if Gemini price is higher.
elif gdax.price < gemini.price:
# Calculate opportunities from 1 to amount.
for i in range(1, amount):
# Calculate profit/loss opportunity.
opportunity = calculateProfitLoss(i, gemini, gdax)
# If profit greater than the current max, update.
if opportunity.profitLoss > maxOpp.profitLoss:
maxOpp = opportunity
return maxOpp
# Else prices equal, no arbitrage opportunity.
elif gdax.price == gemini.price:
return None
################################################################################
# Calculates arbitrage opportunities for all currencies at exchanges.
def checkAllCurrencies(amount):
# amount = 100 - for testing.
# GDAX ethereum (ETH) exchange info.
gdaxEth = exchange.getExchange1('gdax', 'ETH')
# Gemini ethereum (ETH) exchange info.
geminiEth = exchange.getExchange1('gemini', 'ETH')
# Check opportunities for ethereum.
oppEth = checkOpportunity(amount, gdaxEth, geminiEth)
# GDAX Bitcoin Core (BTC) exchange info.
gdaxBtc = exchange.getExchange1('gdax', 'BTC')
# Gemini Bitcoin Core (BTC) exchange info.
geminiBtc = exchange.getExchange1('gemini', 'BTC')
# Check opportunities for litecoin.
oppBtc = checkOpportunity(amount, gdaxBtc, geminiBtc)
# Return array of arbitrage opportunities.
arbOpps = [oppEth, oppBtc]
return arbOpps
################################################################################
# Added new code below:
# Returns amount of coins to check based on dollar amount provided.
def getAmtCoins(amount, xchng1, xchng2):
amtCoins = 0
# Amount of coins to check = amt/lowest price.
if xchng1.price > xchng2:
price = xchng2.price
else:
price = xchng1.price
amtCoins = amount/price
return amtCoins
################################################################################
# Calculates arbitrage opportunities for all currencies at exchanges, taking into consideration minimum profit.
def checkAllbyProfit(maxCost, minProfit):
# GDAX ethereum (ETH) exchange info.
gdaxEth = exchange.getExchange('gdax', 'ETH')
# Gemini ethereum (ETH) exchange info.
geminiEth = exchange.getExchange('gemini', 'ETH')
amtCoins = getAmtCoins(maxCost, gdaxEth, geminiEth)
# Check opportunities for ethereum.
oppEth = checkOppProfit(amtCoins, gdaxEth, geminiEth)
# GDAX Bitcoin Core (BTC) exchange info.
gdaxBtc = exchange.getExchange('gdax', 'BTC')
# Gemini Bitcoin Core (BTC) exchange info.
geminiBtc = exchange.getExchange('gemini', 'BTC')
# Check opportunities for litecoin.
amtCoins = getAmtCoins(maxCost, gdaxBtc, geminiBtc)
oppBtc = checkOppProfit(amtCoins, gdaxBtc, geminiBtc)
# Return array of arbitrage opportunities.
arbOpps = [oppEth, oppBtc]
#print arbOpps
# Determine profitable opps.
profitableOpps = []
for i in arbOpps:
if i.profitLoss > minProfit:
#print "Profit!"
#print (i)
profitableOpps.append(i)
return profitableOpps
################################################################################
# Helper function for checkAllbyProfit. Checks for an arbitrage opportunity
#for a given amount of coins (float) between exchanges.
def checkOppProfit(amount, gdax, gemini):
# Set max opportunity amount to arbitrary negative number
maxOpp = Opportunity()
maxOpp.profitLoss = -999999999.99;
# If GDAX price is higher.
if gdax.price > gemini.price:
# Calculate opportunities from 0.001 to amount.
oppList = numpy.arange(0.001, amount, 0.001)
for i in oppList:
# Calculate profit/loss opportunity.
opportunity = calculateProfitLoss(i, gdax, gemini)
# If profit greater than the current max, update.
if opportunity.profitLoss > maxOpp.profitLoss:
maxOpp = opportunity
return maxOpp
# Else if Gemini price is higher.
elif gdax.price < gemini.price:
# Calculate opportunities from 0.001 to amount.
oppList = numpy.arange(0.001, amount, 0.001)
for i in oppList:
# Calculate profit/loss opportunity.
opportunity = calculateProfitLoss(i, gemini, gdax)
# If profit greater than the current max, update.
if opportunity.profitLoss > maxOpp.profitLoss:
maxOpp = opportunity
return maxOpp
# Else prices equal, no arbitrage opportunity.
elif gdax.price == gemini.price:
return None