diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 1b31956e..00000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2020 Mateusz Magda - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/README.md b/README.md deleted file mode 100644 index 6661a2ed..00000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# MSiD -System Analysis and Decision Support Methods diff --git a/l1 b/l1 deleted file mode 100644 index 76140c26..00000000 --- a/l1 +++ /dev/null @@ -1,6 +0,0 @@ -1) Sforkować repozytorium na Wasze konto -2) Przejść na Waszego forka -3) Ściągnąć wasze repo na dysk i powiązać z repozytorium zdalnym (remote) -4) Napisać sortowanie liczb w dwoma znanymi Wam metodami(język jak ustalaliśmy na laboratorium). Starajcie się napisać funkcje w sposób zwarty, bez żadnych komentarzy czy śmieci w kodzie, w sposób optymalny, z użyciem najlepszych, znanych Wam struktur danych i metod przeglądania ich. Tworząc rozwiązania pracujcie z gitem- dodawajcie na stage, twórzcie commity. Pracujcie na branchu, stwórzcie na potrzeby zadania brancha i nazwijcie go np SortingMethods -5) wypchnijcie zmiany lokalne na repo zdalne -6) utwórzcie merge requesta z brancha SortingMethods do brancha master mojego repo (oryginalnego repo, z którego powstał fork) i nazwijcie go L1. diff --git a/l2 b/l2 deleted file mode 100644 index 89f8155e..00000000 --- a/l2 +++ /dev/null @@ -1,2 +0,0 @@ -Rozwinięcie zadania z listy 1. Do wybranych algorytmów sortowania z listy 1 dobrać 2 inne i zaimplementować je. Napisać funkcję służącą do porównywania algorytmów sortowania, która przyjmuje funkcję i mierzy czas / czasy sortowania. Za jej pomocą porównać algorytmy sortowania. -Pracujcie z gitem, proponuję zrobić osobnego brancha, nazwać go SortingComparison i w nim pracować. Na koniec - jak poprzednio - wysłać na repozytorium remote i wystawić pull request do mojego repozytorium, z którego pierwotnie forkowaliście. Nie akceptuję plików dodanych do gita przez „upload”. Wysyłanie plików na wasze remote repo odbywa się poprzez git push. diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 00000000..3ea40e55 --- /dev/null +++ b/src/Main.java @@ -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 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 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(); + } + } + } +} \ No newline at end of file diff --git a/src/Observer.java b/src/Observer.java new file mode 100644 index 00000000..32c2eb1b --- /dev/null +++ b/src/Observer.java @@ -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; + } + +}