Skip to content

Instantly share code, notes, and snippets.

@iwasaki-kenta
Last active January 14, 2018 16:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwasaki-kenta/4d17258db22ef09a8a68 to your computer and use it in GitHub Desktop.
Save iwasaki-kenta/4d17258db22ef09a8a68 to your computer and use it in GitHub Desktop.
Triangular Arbitrage - Java Implementation
package codeit.suisse;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.json.JSONObject;
public class TriangularArbitrage extends Thread {
private static final int TRADE_AMOUNT = 500;
private double calculateSlope(Currency rateInfo, JSONObject newRateInfo) {
return calculateRateChange(rateInfo, newRateInfo)
/ calculateTimeChange(rateInfo);
}
private double calculateRateChange(Currency rateInfo, JSONObject newRateInfo) {
return rateInfo.getRate() - newRateInfo.getDouble("fxRate");
}
private long calculateTimeChange(Currency rateInfo) {
return (System.currentTimeMillis() - rateInfo.getLastRateUpdate()) / 1000;
}
@Override
public void run() {
while (true) {
try {
List<String> currencyNames = new ArrayList<String>();
currencyNames.addAll(Currency.getCurrencies().keySet());
for (String currencyName : currencyNames) {
Currency currency = Currency.getCurrency(currencyName);
JSONObject rateInfo = new JSONObject(Request
.Get(Main.API_URL + "fx/" + currencyName).execute()
.returnContent().asString())
.getJSONObject("fxValue");
if (currency.getRate() != rateInfo.getDouble("fxRate")) {
System.out
.format("FX Rate for currency %s updated %s second(s) ago. New FX Rate: %f | Change in Rate: %f | Slope: %f |\n",
rateInfo.getString("currencyPair"),
calculateTimeChange(currency),
rateInfo.getDouble("fxRate"),
calculateRateChange(currency, rateInfo),
calculateSlope(currency, rateInfo));
currency.setSlope(calculateSlope(currency, rateInfo));
currency.setRate(rateInfo.getDouble("fxRate"));
currency.setLastRateUpdate(System.currentTimeMillis());
}
}
for (int x = 0; x < currencyNames.size(); x++) {
String firstPair = currencyNames.get(x);
for (int y = 0; y < currencyNames.size(); y++) {
String secondPair = currencyNames.get(y);
if (!firstPair.equals(currencyNames.get(y))
&& !secondPair.equals(firstPair.substring(3)
+ firstPair.substring(0, 3))
&& currencyNames.get(y).substring(0, 3)
.equals(firstPair.substring(3))) {
String thirdPair = secondPair.substring(3)
+ firstPair.substring(0, 3);
double finalExchangeValue = TRADE_AMOUNT
* Currency.getCurrency(firstPair).getRate()
* Currency.getCurrency(secondPair)
.getRate()
* Currency.getCurrency(thirdPair).getRate();
if (finalExchangeValue - TRADE_AMOUNT > 0) {
processArbitrage(new String[] { firstPair,
secondPair, thirdPair });
}
}
}
}
TimeUnit.MILLISECONDS.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void processArbitrage(String[] currencyPairs) {
try {
for (String currencyPair : currencyPairs) {
JSONObject quoteParams = new JSONObject();
quoteParams.put("teamId", Main.TEAM_ID);
quoteParams.put("currencyPair", currencyPair);
quoteParams.put("quantity", TRADE_AMOUNT);
String quoteId = new JSONObject(Request
.Post(Main.API_URL + "fx/quote")
.bodyString(quoteParams.toString(),
ContentType.APPLICATION_JSON).execute()
.returnContent().asString()).getJSONObject(
"quoteResponse").getString("quoteId");
JSONObject executeParams = new JSONObject();
executeParams.put("teamId", Main.TEAM_ID);
executeParams.put("quoteId", quoteId);
Request.Post(Main.API_URL + "fx/quote/execute")
.bodyString(executeParams.toString(),
ContentType.APPLICATION_JSON).execute();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment