Skip to content
Open

L4 #164

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

2 changes: 0 additions & 2 deletions README.md

This file was deleted.

6 changes: 0 additions & 6 deletions l1

This file was deleted.

2 changes: 0 additions & 2 deletions l2

This file was deleted.

68 changes: 68 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.util.ArrayList;

public class Main {
private static float wallet;
public static void main(String[] args) {
try {
Thread thread = new Thread(Main::refresh);
thread.setDaemon(true);
thread.start();
thread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
public static void makeTransaction(ArrayList<Observer> list) {
int lowest_i = 0;
int highest_i = 0;
float highest_bid_temp = list.get(0).getBid_prize();
float lowest_ask_temp = list.get(0).getAsk_prize();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getCurrency() == list.get(highest_i).getCurrency()) {
if (highest_bid_temp < list.get(i).getBid_prize()) {
highest_bid_temp = list.get(i).getBid_prize();
highest_i = i;
}
if (lowest_ask_temp > list.get(i).getAsk_prize()) {
lowest_ask_temp = list.get(i).getAsk_prize();
lowest_i = i;
}
}
}
Observer high = list.get(highest_i);
Observer low = list.get(lowest_i);
float amount = Math.min(list.get(highest_i).getBid_quant(), list.get(lowest_i).getAsk_quant());
float profit = amount * ((1 - list.get(highest_i).getFee()) * list.get(highest_i).getBid_prize() - (1 - list.get(lowest_i).getFee()) * list.get(lowest_i).getAsk_prize());
wallet += profit;
System.out.println("On stock "+low.getName()+ " we can buy " + amount + " " +low.getCurrency()+ " for " +
low.getAsk_prize() + " USD and sell on stock " + high.getName() + " for " +high.getBid_prize() +
" and make "+ profit + " $ profit.");
}
private static void refresh() {
while (true) {
try {
try {
ArrayList<Observer> list = new ArrayList<>();
list.add(Observer.getDataBitbay("BTC", "USD", "BTC"));
list.add(Observer.getDataBittrex("USD", "BTC","BTC"));
list.add(Observer.getDataBitstamp("btc", "usd","BTC"));
list.add(Observer.getDataBitfinex("BTC", "USD","BTC"));
list.add(Observer.getDataBitbay("LTC", "USD","LTC"));
list.add(Observer.getDataBittrex("USD", "LTC","LTC"));
list.add(Observer.getDataBitstamp("ltc", "usd","LTC"));
list.add(Observer.getDataBitfinex("LTC", "USD","LTC"));
Main.makeTransaction(list);
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
131 changes: 131 additions & 0 deletions src/Observer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class Observer {
public float getBid_prize() {
return bid_prize;
}
public float getAsk_prize() {
return ask_prize;
}
public float getBid_quant() {
return bid_quant;
}
public float getAsk_quant() {
return ask_quant;
}
float bid_prize, ask_prize, bid_quant, ask_quant;
public String getName() {
return name;
}
public float getFee() {
return fee;
}
float fee;
String name;

public String getCurrency() {
return currency;
}

String currency;
public Observer(float bidp, float askp, float bidq, float askq, String sname, String currencyname, float takerFee){
bid_prize =bidp;
ask_prize = askp;
bid_quant = bidq;
ask_quant = askq;
name = sname;
currency = currencyname;
fee = takerFee;
}

static Observer getDataBitbay(String currency1, String currency2, String currencyName) throws IOException, ParseException {
URL url = new URL("https://bitbay.net/API/Public/"+ currency1 + currency2 +"/orderbook.json");
URLConnection connection = url.openConnection();
BufferedReader b_reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Object obj = new JSONParser().parse(new BufferedReader(new InputStreamReader(connection.getInputStream())));
JSONObject data = (JSONObject) obj;
JSONArray bids = (JSONArray) data.get("bids");
JSONArray asks = (JSONArray) data.get("asks");
ArrayList bid = (ArrayList) bids.get(0);
ArrayList ask = (ArrayList) asks.get(0);
float bid_price = Float.valueOf(bid.get(0).toString());
float bid_quant = Float.valueOf(bid.get(1).toString());
float ask_price = Float.valueOf(ask.get(0).toString());
float ask_quant = Float.valueOf(ask.get(1).toString());
System.out.println("Bid= " + bid_price + "\t Quantity= " + bid_quant + "\t Ask = " + ask_price + "\t Quantitiy = " + ask_quant);
Observer ob1 = new Observer(bid_price, ask_price, bid_quant, ask_quant, "Bitbay", currencyName, (float) 0.0001);
b_reader.close();
return ob1;
}
static Observer getDataBittrex(String currency1, String currency2, String currencyName) throws IOException, ParseException {
URL url = new URL("https://api.bittrex.com/api/v1.1/public/getorderbook?market="+ currency1 +"-"+ currency2 +"&type=both");
URLConnection connection = url.openConnection();
BufferedReader b_reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Object obj = new JSONParser().parse(new BufferedReader(new InputStreamReader(connection.getInputStream())));
JSONObject data = (JSONObject) obj;

JSONObject results = (JSONObject) data.get("result");
JSONArray bids = (JSONArray) results.get("buy");
JSONArray asks = (JSONArray) results.get("sell");
JSONObject bid = (JSONObject) bids.get(0);
JSONObject ask = (JSONObject) bids.get(0);
float bid_price = Float.valueOf(bid.get("Rate").toString());
float bid_quant = Float.valueOf(bid.get("Quantity").toString());
float ask_price = Float.valueOf(ask.get("Rate").toString());
float ask_quant = Float.valueOf(ask.get("Quantity").toString());
System.out.println("Bid= " + bid_price + "\t Quantity= " + bid_quant + "\t Ask = " + ask_price + "\t Quantitiy = " + ask_quant);
Observer ob1 = new Observer(bid_price, ask_price, bid_quant, ask_quant, "Bittrex", currencyName, (float)0.0002);
b_reader.close();
return ob1;
}
static Observer getDataBitstamp(String currency1, String currency2, String currencyName) throws IOException, ParseException {
URL url = new URL("https://www.bitstamp.net/api/v2/order_book/"+ currency1 + currency2 +"/");
URLConnection connection = url.openConnection();
BufferedReader b_reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Object obj = new JSONParser().parse(new BufferedReader(new InputStreamReader(connection.getInputStream())));
JSONObject data = (JSONObject) obj;
JSONArray bids = (JSONArray) data.get("bids");
JSONArray asks = (JSONArray) data.get("asks");
ArrayList bid = (ArrayList) bids.get(0);
ArrayList ask = (ArrayList) asks.get(0);
float bid_price = Float.valueOf(bid.get(0).toString());
float bid_quant = Float.valueOf(bid.get(1).toString());
float ask_price = Float.valueOf(ask.get(0).toString());
float ask_quant = Float.valueOf(ask.get(1).toString());
System.out.println("Bid= " + bid_price + "\t Quantity= " + bid_quant + "\t Ask = " + ask_price + "\t Quantitiy = " + ask_quant);
Observer ob1 = new Observer(bid_price, ask_price, bid_quant, ask_quant, "Bittstamp", currencyName, (float)0.00025);
b_reader.close();
return ob1;
}

static Observer getDataBitfinex(String currency1, String currency2, String currencyName) throws IOException, ParseException {
URL url = new URL("https://api.bitfinex.com/v1/book/" + currency1 + currency2);
URLConnection connection = url.openConnection();
BufferedReader b_reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Object obj = new JSONParser().parse(new BufferedReader(new InputStreamReader(connection.getInputStream())));
JSONObject data = (JSONObject) obj;

JSONArray bids = (JSONArray) data.get("bids");
JSONArray asks = (JSONArray) data.get("asks");
JSONObject bid = (JSONObject) bids.get(0);
JSONObject ask = (JSONObject) bids.get(0);
float bid_price = Float.valueOf(bid.get("price").toString());
float bid_quant = Float.valueOf(bid.get("amount").toString());
float ask_price = Float.valueOf(ask.get("price").toString());
float ask_quant = Float.valueOf(ask.get("amount").toString());
System.out.println("Bid= " + bid_price + "\t Quantity= " + bid_quant + "\t Ask = " + ask_price + "\t Quantitiy = " + ask_quant);

Observer ob1 = new Observer(bid_price, ask_price, bid_quant, ask_quant, "Bitfinex", currencyName,(float)0.0002);
b_reader.close();
return ob1;
}

}