From 03438b872765c5e807ceeadfc31b493e4417c45f Mon Sep 17 00:00:00 2001 From: Vesker7 Date: Mon, 23 Mar 2020 17:01:10 +0100 Subject: [PATCH 01/20] v.1 --- Sorting.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Sorting.java diff --git a/Sorting.java b/Sorting.java new file mode 100644 index 00000000..65e38161 --- /dev/null +++ b/Sorting.java @@ -0,0 +1,4 @@ +public interface Sorting +{ + public void sort(int [] tab); +} \ No newline at end of file From 7859df16a24714fef508d055a1e7092c93c8cb83 Mon Sep 17 00:00:00 2001 From: Vesker7 Date: Mon, 23 Mar 2020 17:34:17 +0100 Subject: [PATCH 02/20] Bubble --- BubbleSort.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 BubbleSort.java diff --git a/BubbleSort.java b/BubbleSort.java new file mode 100644 index 00000000..6b3dc568 --- /dev/null +++ b/BubbleSort.java @@ -0,0 +1,27 @@ +public class BubbleSort implements Sorting +{ + @Override + public void sort(int[] tab) + { + int [] liczby = tab.clone(); + boolean swaped; + int temp; + + for(int i=0; iliczby[j+1]) + { + swaped = true; + temp = liczby[j]; + liczby[j] = liczby[j+1]; + liczby[j+1] = swap; + } + } + if (swaped==false) break; + } + } + +} \ No newline at end of file From 3fd2bc34d8cf3c5fa9922ad8deabc2c1f3ecc954 Mon Sep 17 00:00:00 2001 From: Vesker7 Date: Mon, 23 Mar 2020 17:44:21 +0100 Subject: [PATCH 03/20] Select --- SelectSort.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 SelectSort.java diff --git a/SelectSort.java b/SelectSort.java new file mode 100644 index 00000000..8547074f --- /dev/null +++ b/SelectSort.java @@ -0,0 +1,28 @@ +public class SelectSort implements Sorting +{ + @Override + public void sort(int[] tab) + { + int [] liczby = tab.clone(); + long start = System.currentTimeMillis(); + int swap, min_index; + + for(int i=0; i Date: Mon, 23 Mar 2020 17:51:14 +0100 Subject: [PATCH 04/20] Test --- Main.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Main.java diff --git a/Main.java b/Main.java new file mode 100644 index 00000000..be793e74 --- /dev/null +++ b/Main.java @@ -0,0 +1,28 @@ +import java.util.Random; + +public class Main +{ + private static int [] random_array(int n) + { + int [] liczby = new int[n]; + Random r = new Random(); + for(int i=0; i Date: Mon, 6 Apr 2020 20:26:39 +0200 Subject: [PATCH 05/20] Previous task, added compare func --- lab2/BubbleSort.java | 29 +++++++++++++++++++++++++++++ lab2/Main.java | 34 ++++++++++++++++++++++++++++++++++ lab2/SelectSort.java | 28 ++++++++++++++++++++++++++++ lab2/Sorting.java | 4 ++++ 4 files changed, 95 insertions(+) create mode 100644 lab2/BubbleSort.java create mode 100644 lab2/Main.java create mode 100644 lab2/SelectSort.java create mode 100644 lab2/Sorting.java diff --git a/lab2/BubbleSort.java b/lab2/BubbleSort.java new file mode 100644 index 00000000..1c2aca00 --- /dev/null +++ b/lab2/BubbleSort.java @@ -0,0 +1,29 @@ +public class BubbleSort implements Sorting +{ + @Override + public long sort(int[] tab) + { + int [] liczby = tab.clone(); + long start = System.currentTimeMillis(); + boolean swaped; + int temp; + + for(int i=0; iliczby[j+1]) + { + swaped = true; + temp = liczby[j]; + liczby[j] = liczby[j+1]; + liczby[j+1] = temp; + } + } + if (swaped==false) break; + } + return ( System.currentTimeMillis() - start); + } + +} \ No newline at end of file diff --git a/lab2/Main.java b/lab2/Main.java new file mode 100644 index 00000000..ede9bd79 --- /dev/null +++ b/lab2/Main.java @@ -0,0 +1,34 @@ +import java.util.Random; + +public class Main +{ + private static int [] random_array(int n) + { + int [] liczby = new int[n]; + Random r = new Random(); + for(int i=0; i Date: Mon, 6 Apr 2020 20:36:23 +0200 Subject: [PATCH 06/20] Previous task, added comp func --- BubbleSort.java | 8 +++++--- Main.java | 18 ++++++++++++------ SelectSort.java | 4 ++-- Sorting.java | 2 +- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/BubbleSort.java b/BubbleSort.java index 6b3dc568..1c2aca00 100644 --- a/BubbleSort.java +++ b/BubbleSort.java @@ -1,9 +1,10 @@ public class BubbleSort implements Sorting { @Override - public void sort(int[] tab) + public long sort(int[] tab) { - int [] liczby = tab.clone(); + int [] liczby = tab.clone(); + long start = System.currentTimeMillis(); boolean swaped; int temp; @@ -17,11 +18,12 @@ public void sort(int[] tab) swaped = true; temp = liczby[j]; liczby[j] = liczby[j+1]; - liczby[j+1] = swap; + liczby[j+1] = temp; } } if (swaped==false) break; } + return ( System.currentTimeMillis() - start); } } \ No newline at end of file diff --git a/Main.java b/Main.java index be793e74..ede9bd79 100644 --- a/Main.java +++ b/Main.java @@ -14,15 +14,21 @@ public class Main return liczby; } - public static void main(String[] args) + public static long comparesort(Sorting sorting_method) { - Sorting bubble = new BubbleSort(), select = new SelectSort(); - final int size = 40000; int [] los_liczby = random_array(size); - System.out.println("Bubble Sort: " + bubble.sort(los_liczby)); - System.out.println("Select Sort: " + select.sort(los_liczby)); + return sorting_method.sort(los_liczby); + } + + public static void main(String[] args) + { + Sorting bubble = new BubbleSort(), select = new SelectSort(); + + System.out.println("Bubble Sort: " + comparesort(bubble)); + System.out.println("Select Sort: " + comparesort(select)); + System.out.println("____________________________________"); } -} +} \ No newline at end of file diff --git a/SelectSort.java b/SelectSort.java index 8547074f..57138f1d 100644 --- a/SelectSort.java +++ b/SelectSort.java @@ -1,7 +1,7 @@ public class SelectSort implements Sorting { @Override - public void sort(int[] tab) + public long sort(int[] tab) { int [] liczby = tab.clone(); long start = System.currentTimeMillis(); @@ -22,7 +22,7 @@ public void sort(int[] tab) liczby[i]=liczby[min_index]; liczby[min_index] = swap; } - + return ( System.currentTimeMillis() - start); } } \ No newline at end of file diff --git a/Sorting.java b/Sorting.java index 65e38161..b031afba 100644 --- a/Sorting.java +++ b/Sorting.java @@ -1,4 +1,4 @@ public interface Sorting { - public void sort(int [] tab); + public long sort(int [] tab); } \ No newline at end of file From 0d16ba708b73be74d7d79ca83d2015e590bca323 Mon Sep 17 00:00:00 2001 From: Vesker7 Date: Mon, 6 Apr 2020 20:45:00 +0200 Subject: [PATCH 07/20] Previous task, added comp func --- LICENSE | 21 --------------------- README.md | 2 -- l1 | 6 ------ lab2/BubbleSort.java | 29 ----------------------------- lab2/Main.java | 34 ---------------------------------- lab2/SelectSort.java | 28 ---------------------------- lab2/Sorting.java | 4 ---- 7 files changed, 124 deletions(-) delete mode 100644 LICENSE delete mode 100644 README.md delete mode 100644 l1 delete mode 100644 lab2/BubbleSort.java delete mode 100644 lab2/Main.java delete mode 100644 lab2/SelectSort.java delete mode 100644 lab2/Sorting.java 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/lab2/BubbleSort.java b/lab2/BubbleSort.java deleted file mode 100644 index 1c2aca00..00000000 --- a/lab2/BubbleSort.java +++ /dev/null @@ -1,29 +0,0 @@ -public class BubbleSort implements Sorting -{ - @Override - public long sort(int[] tab) - { - int [] liczby = tab.clone(); - long start = System.currentTimeMillis(); - boolean swaped; - int temp; - - for(int i=0; iliczby[j+1]) - { - swaped = true; - temp = liczby[j]; - liczby[j] = liczby[j+1]; - liczby[j+1] = temp; - } - } - if (swaped==false) break; - } - return ( System.currentTimeMillis() - start); - } - -} \ No newline at end of file diff --git a/lab2/Main.java b/lab2/Main.java deleted file mode 100644 index ede9bd79..00000000 --- a/lab2/Main.java +++ /dev/null @@ -1,34 +0,0 @@ -import java.util.Random; - -public class Main -{ - private static int [] random_array(int n) - { - int [] liczby = new int[n]; - Random r = new Random(); - for(int i=0; i Date: Mon, 6 Apr 2020 20:58:11 +0200 Subject: [PATCH 08/20] Added HeapSort and QuickSort --- HeapSort.java | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ Main.java | 11 ++++++---- QuickSort.java | 40 ++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 HeapSort.java create mode 100644 QuickSort.java diff --git a/HeapSort.java b/HeapSort.java new file mode 100644 index 00000000..a1334076 --- /dev/null +++ b/HeapSort.java @@ -0,0 +1,55 @@ +public class HeapSort implements Sorting +{ + + @Override + public long sort(int[] tab) + { + int [] liczby = tab.clone(); + long start = System.currentTimeMillis(); + + heapsort(liczby,0,liczby.length-1); + + return ( System.currentTimeMillis() - start); + } + + private void heapsort(int[] tab, int begin, int end) + { + int n = tab.length; + + for(int i = (n/2-1); i>=0; i--) + { + heapify(tab, n, i); + } + + for(int i = n-1; i>=0; i--) + { + int swap = tab[0]; + tab[0] = tab[i]; + tab[i] = swap; + + heapify(tab, i, 0); + } + } + + private void heapify(int [] tab, int n, int i) + { + int largest = i; + int left = 2*i+1; + int right = 2*i+2; + + if(left < n && tab[left] > tab[largest]) largest = left; + + if(right < n && tab[right] > tab[largest]) largest = right; + + if(largest != i) + { + int swap = tab[i]; + tab[i] = tab[largest]; + tab[largest] = swap; + + heapify(tab, n, largest); + } + + } + +} \ No newline at end of file diff --git a/Main.java b/Main.java index ede9bd79..9d5397a4 100644 --- a/Main.java +++ b/Main.java @@ -2,6 +2,9 @@ public class Main { + final int size = 40000; + int [] los_liczby = random_array(size); + private static int [] random_array(int n) { int [] liczby = new int[n]; @@ -16,18 +19,18 @@ public class Main public static long comparesort(Sorting sorting_method) { - final int size = 40000; - int [] los_liczby = random_array(size); - return sorting_method.sort(los_liczby); } public static void main(String[] args) { - Sorting bubble = new BubbleSort(), select = new SelectSort(); + Sorting bubble = new BubbleSort(), select = new SelectSort(), + heap = new HeapSort(), quick = new QuickSort(); System.out.println("Bubble Sort: " + comparesort(bubble)); System.out.println("Select Sort: " + comparesort(select)); + System.out.println("Heap Sort: " + comparesort(heap)); + System.out.println("Quick Sort: " + comparesort(quick)); System.out.println("____________________________________"); } diff --git a/QuickSort.java b/QuickSort.java new file mode 100644 index 00000000..ed9385cd --- /dev/null +++ b/QuickSort.java @@ -0,0 +1,40 @@ +public class QuickSort implements Sorting +{ + @Override + public long sort(int[] tab) + { + int [] liczby = tab.clone(); + long start = System.currentTimeMillis(); + + qsort(liczby,0,liczby.length-1); + + return ( System.currentTimeMillis() - start); + } + + private void qsort(int[] tab, int begin, int end) + { + int i = begin, j = end, pivot, temp; + + pivot = tab[ (begin+end)/2 ]; + + while (i<=j) + { + while (tab[i] Date: Sat, 18 Apr 2020 17:01:23 +0200 Subject: [PATCH 09/20] Delete BubbleSort.java --- BubbleSort.java | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 BubbleSort.java diff --git a/BubbleSort.java b/BubbleSort.java deleted file mode 100644 index 1c2aca00..00000000 --- a/BubbleSort.java +++ /dev/null @@ -1,29 +0,0 @@ -public class BubbleSort implements Sorting -{ - @Override - public long sort(int[] tab) - { - int [] liczby = tab.clone(); - long start = System.currentTimeMillis(); - boolean swaped; - int temp; - - for(int i=0; iliczby[j+1]) - { - swaped = true; - temp = liczby[j]; - liczby[j] = liczby[j+1]; - liczby[j+1] = temp; - } - } - if (swaped==false) break; - } - return ( System.currentTimeMillis() - start); - } - -} \ No newline at end of file From 9c07a648cfc42a857aa8b038a7d350c37d3d9622 Mon Sep 17 00:00:00 2001 From: Vesker7 <61987610+Vesker7@users.noreply.github.com> Date: Sat, 18 Apr 2020 17:01:31 +0200 Subject: [PATCH 10/20] Delete HeapSort.java --- HeapSort.java | 55 --------------------------------------------------- 1 file changed, 55 deletions(-) delete mode 100644 HeapSort.java diff --git a/HeapSort.java b/HeapSort.java deleted file mode 100644 index a1334076..00000000 --- a/HeapSort.java +++ /dev/null @@ -1,55 +0,0 @@ -public class HeapSort implements Sorting -{ - - @Override - public long sort(int[] tab) - { - int [] liczby = tab.clone(); - long start = System.currentTimeMillis(); - - heapsort(liczby,0,liczby.length-1); - - return ( System.currentTimeMillis() - start); - } - - private void heapsort(int[] tab, int begin, int end) - { - int n = tab.length; - - for(int i = (n/2-1); i>=0; i--) - { - heapify(tab, n, i); - } - - for(int i = n-1; i>=0; i--) - { - int swap = tab[0]; - tab[0] = tab[i]; - tab[i] = swap; - - heapify(tab, i, 0); - } - } - - private void heapify(int [] tab, int n, int i) - { - int largest = i; - int left = 2*i+1; - int right = 2*i+2; - - if(left < n && tab[left] > tab[largest]) largest = left; - - if(right < n && tab[right] > tab[largest]) largest = right; - - if(largest != i) - { - int swap = tab[i]; - tab[i] = tab[largest]; - tab[largest] = swap; - - heapify(tab, n, largest); - } - - } - -} \ No newline at end of file From 6976cf314130b80a244e4c887fc3c0b2ec5895d8 Mon Sep 17 00:00:00 2001 From: Vesker7 <61987610+Vesker7@users.noreply.github.com> Date: Sat, 18 Apr 2020 17:01:47 +0200 Subject: [PATCH 11/20] Delete Main.java --- Main.java | 37 ------------------------------------- 1 file changed, 37 deletions(-) delete mode 100644 Main.java diff --git a/Main.java b/Main.java deleted file mode 100644 index 9d5397a4..00000000 --- a/Main.java +++ /dev/null @@ -1,37 +0,0 @@ -import java.util.Random; - -public class Main -{ - final int size = 40000; - int [] los_liczby = random_array(size); - - private static int [] random_array(int n) - { - int [] liczby = new int[n]; - Random r = new Random(); - for(int i=0; i Date: Sat, 18 Apr 2020 17:01:53 +0200 Subject: [PATCH 12/20] Delete QuickSort.java --- QuickSort.java | 40 ---------------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 QuickSort.java diff --git a/QuickSort.java b/QuickSort.java deleted file mode 100644 index ed9385cd..00000000 --- a/QuickSort.java +++ /dev/null @@ -1,40 +0,0 @@ -public class QuickSort implements Sorting -{ - @Override - public long sort(int[] tab) - { - int [] liczby = tab.clone(); - long start = System.currentTimeMillis(); - - qsort(liczby,0,liczby.length-1); - - return ( System.currentTimeMillis() - start); - } - - private void qsort(int[] tab, int begin, int end) - { - int i = begin, j = end, pivot, temp; - - pivot = tab[ (begin+end)/2 ]; - - while (i<=j) - { - while (tab[i] Date: Sat, 18 Apr 2020 17:02:00 +0200 Subject: [PATCH 13/20] Delete SelectSort.java --- SelectSort.java | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 SelectSort.java diff --git a/SelectSort.java b/SelectSort.java deleted file mode 100644 index 57138f1d..00000000 --- a/SelectSort.java +++ /dev/null @@ -1,28 +0,0 @@ -public class SelectSort implements Sorting -{ - @Override - public long sort(int[] tab) - { - int [] liczby = tab.clone(); - long start = System.currentTimeMillis(); - int swap, min_index; - - for(int i=0; i Date: Sat, 18 Apr 2020 17:02:06 +0200 Subject: [PATCH 14/20] Delete Sorting.java --- Sorting.java | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 Sorting.java diff --git a/Sorting.java b/Sorting.java deleted file mode 100644 index b031afba..00000000 --- a/Sorting.java +++ /dev/null @@ -1,4 +0,0 @@ -public interface Sorting -{ - public long sort(int [] tab); -} \ No newline at end of file From 542ee38b1ad34df705fa086f18c4992218a5da95 Mon Sep 17 00:00:00 2001 From: Vesker7 <61987610+Vesker7@users.noreply.github.com> Date: Sat, 18 Apr 2020 17:02:14 +0200 Subject: [PATCH 15/20] Delete l2 --- l2 | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 l2 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. From e94c1b6401f58c4988a8581c557dd971eca0b50b Mon Sep 17 00:00:00 2001 From: Vesker7 Date: Sat, 18 Apr 2020 17:10:00 +0200 Subject: [PATCH 16/20] Updating and diff calc - lab3 done --- Main.java | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Offer.java | 33 +++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 Main.java create mode 100644 Offer.java diff --git a/Main.java b/Main.java new file mode 100644 index 00000000..6bff0203 --- /dev/null +++ b/Main.java @@ -0,0 +1,86 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; + +@SuppressWarnings("unused") +public class Main +{ + static ArrayList buy; + static ArrayList sell; + + private static void bitbayUpdate(URL source) throws IOException + { + String tab[]; + int i=0; buy = new ArrayList(); sell = new ArrayList(); + + URLConnection connection = source.openConnection(); + connection.connect(); + + BufferedReader buffer = new BufferedReader(new InputStreamReader(connection.getInputStream())); + + tab = buffer.readLine().replaceAll("[:\\[\\]\\{\\}]", "").split(","); + + tab[0]=tab[0].replace("\"bids\"", ""); + + for(; !tab[i].contains("asks"); i+=2) + { + Offer temp = new Offer(true,Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])); + buy.add(temp); + } + + tab[i]=tab[i].replace("\"asks\"", ""); + + + for(; i Date: Mon, 1 Jun 2020 19:21:25 +0200 Subject: [PATCH 17/20] Lab4 without ex4 - agent --- BitBayOrderbook.java | 65 ++++++++++++++++ Budget.java | 59 ++++++++++++++ CexIoOrderbook.java | 64 ++++++++++++++++ FtxOrderbook.java | 65 ++++++++++++++++ Main.java | 170 +++++++++++++++++++++++------------------ Orderbook.java | 68 +++++++++++++++++ WhiteBitOrderbook.java | 63 +++++++++++++++ 7 files changed, 480 insertions(+), 74 deletions(-) create mode 100644 BitBayOrderbook.java create mode 100644 Budget.java create mode 100644 CexIoOrderbook.java create mode 100644 FtxOrderbook.java create mode 100644 Orderbook.java create mode 100644 WhiteBitOrderbook.java diff --git a/BitBayOrderbook.java b/BitBayOrderbook.java new file mode 100644 index 00000000..6fd3545d --- /dev/null +++ b/BitBayOrderbook.java @@ -0,0 +1,65 @@ +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 BitBayOrderbook extends Orderbook +{ + public BitBayOrderbook(String currency1, String currency2) throws IOException + { + super(new URL("https://bitbay.net/API/Public/" + currency1 + currency2 + "/orderbook.json")); + } + + @Override + void update(int limit) throws IOException + { + String tab[]; + buy = new ArrayList(); sell = new ArrayList(); + int i=0, counter=0; + + //long start = System.currentTimeMillis(); + + URLConnection connection = source.openConnection(); + connection.addRequestProperty("User-Agent", "Mozilla"); + connection.connect(); + + BufferedReader buffer = new BufferedReader(new InputStreamReader(connection.getInputStream())); + + //System.out.println("Czas: "+ (System.currentTimeMillis()-start)); + + tab = buffer.readLine().replaceAll("[:\\[\\]\\{\\}\"]", "").split(","); + + tab[0]=tab[0].replace("bids", ""); + + for(; !tab[i].contains("asks"); i+=2) + { + Offer temp = new Offer(true,Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])); + if(counter < limit) + { + buy.add(temp); + counter++; + } + + } + + counter = 0; + tab[i]=tab[i].replace("asks", ""); + + for(; i resources; + + public Budget() + { + resources = new ArrayList(); + } + + public void add(String currency, double ammount) + { + boolean added = false; + for(Resource res : resources) + { + if(res.currency.equals(currency)) + { + res.ammount+=ammount; + added = true; + break; + } + + } + if(!added) resources.add(new Resource(currency, ammount)); + } + + @Override + public String toString() + { + String output="[BUDET]\n"; + for(Resource res : resources) + { + output+=res+"\n"; + } + + return output; + } + + +} + diff --git a/CexIoOrderbook.java b/CexIoOrderbook.java new file mode 100644 index 00000000..7125a80a --- /dev/null +++ b/CexIoOrderbook.java @@ -0,0 +1,64 @@ +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 CexIoOrderbook extends Orderbook +{ + public CexIoOrderbook(String currency1, String currency2) throws IOException + { + super(new URL("https://cex.io/api/order_book/"+currency1+"/"+currency2+"/")); + } + + @Override + void update(int limit) throws IOException + { + String tab[]; + buy = new ArrayList(); sell = new ArrayList(); + int i=1, counter=0; + + //long start = System.currentTimeMillis(); + + URLConnection connection = source.openConnection(); + connection.addRequestProperty("User-Agent", "Mozilla"); + connection.connect(); + + BufferedReader buffer = new BufferedReader(new InputStreamReader(connection.getInputStream())); + + //System.out.println("Czas: "+ (System.currentTimeMillis()-start)); + + + tab = buffer.readLine().replaceAll("[:\\[\\]\\{\\}]", "").split(","); + + tab[1]=tab[1].replace("\"bids\"", ""); + + for(; !tab[i].contains("asks"); i+=2) + { + if(counter < limit) + { + Offer temp = new Offer(true,Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])); + buy.add(temp); + counter++; + } + } + + counter = 0; + tab[i]=tab[i].replace("\"asks\"", ""); + + for(; !tab[i].contains("pair"); i+=2) + { + if(counter < limit) + { + Offer temp = new Offer(false,Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])); + sell.add(temp); + counter++; + } + } + + buy.trimToSize(); + sell.trimToSize(); + } + +} \ No newline at end of file diff --git a/FtxOrderbook.java b/FtxOrderbook.java new file mode 100644 index 00000000..5b252c4a --- /dev/null +++ b/FtxOrderbook.java @@ -0,0 +1,65 @@ +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 FtxOrderbook extends Orderbook +{ + public FtxOrderbook(String currency1, String currency2) throws IOException + { + super(new URL("https://ftx.com/api//markets/"+currency1+"_"+currency2+"/orderbook")); + } + + @Override + void update(int limit) throws IOException + { + String tab[]; + buy = new ArrayList(); sell = new ArrayList(); + int i=0, counter=0; + + //long start = System.currentTimeMillis(); + + URLConnection connection = source.openConnection(); + connection.addRequestProperty("User-Agent", "Mozilla"); + connection.connect(); + + BufferedReader buffer = new BufferedReader(new InputStreamReader(connection.getInputStream())); + + //System.out.println("Czas: "+ (System.currentTimeMillis()-start)); + + + tab = buffer.readLine().replaceAll("[:\\[\\]\\{\\}\"]", "").split(","); + + tab[0]=tab[0].replace("resultasks", ""); + + + for(; !tab[i].contains("bids"); i+=2) + { + if(counter < limit) + { + Offer temp = new Offer(false,Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])); + sell.add(temp); + counter++; + } + } + + counter = 0; + tab[i]=tab[i].replace("bids", ""); + + for(; !tab[i].contains("suc"); i+=2) + { + if(counter < limit) + { + Offer temp = new Offer(true,Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])); + buy.add(temp); + counter++; + } + } + + buy.trimToSize(); + sell.trimToSize(); + } + +} \ No newline at end of file diff --git a/Main.java b/Main.java index 6bff0203..27e0d52e 100644 --- a/Main.java +++ b/Main.java @@ -1,86 +1,108 @@ -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -@SuppressWarnings("unused") public class Main { - static ArrayList buy; - static ArrayList sell; + final static String [] api_names = {"BitBay","Cex.IO","WhiteBit","FTX"}; - private static void bitbayUpdate(URL source) throws IOException - { - String tab[]; - int i=0; buy = new ArrayList(); sell = new ArrayList(); - - URLConnection connection = source.openConnection(); - connection.connect(); - - BufferedReader buffer = new BufferedReader(new InputStreamReader(connection.getInputStream())); - - tab = buffer.readLine().replaceAll("[:\\[\\]\\{\\}]", "").split(","); - - tab[0]=tab[0].replace("\"bids\"", ""); - - for(; !tab[i].contains("asks"); i+=2) - { - Offer temp = new Offer(true,Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])); - buy.add(temp); - } - - tab[i]=tab[i].replace("\"asks\"", ""); - - - for(; i0) + { + System.out.println("Na giedzie "+ api_names[exchange] +" mona kupi "+ Math.min(buy.ammount, sell.ammount) + + " " +pairs[2*market] +" po kursie "+ buy.price + pairs[2*market+1]+ + " i sprzeda na giedzie "+ api_names[i] +" po kursie "+ sell.price + " " + pairs[2*market+1] + + " zyskujc " + String.format("%.8f", profit)+ " "+pairs[2*market+1]); + + if(profit>bestProfit) + { + best=sell; + bestProfit=profit; + } + } + } - inspectDiff(bitBayUrl); + if(best!=null) + { + budget.add(pairs[2*market+1], bestProfit); + } + } + + + public static void main(String[] args) throws Exception + { + Budget budget = new Budget(); + Orderbook[][] dane = new Orderbook[4][4]; + + for(int i=0; i<4; i++) + { + dane[0][i] = new BitBayOrderbook(pairs[2*i], pairs[2*i+1]); + } + for(int i=0; i<4; i++) + { + dane[1][i] = new CexIoOrderbook(pairs[2*i], pairs[2*i+1]); + } + for(int i=0; i<4; i++) + { + dane[2][i] = new WhiteBitOrderbook(pairs[2*i], pairs[2*i+1]); + } + for(int i=0; i<4; i++) + { + dane[3][i] = new FtxOrderbook(pairs[2*i], pairs[2*i+1]); + } + + for(int rep=0; rep<10; rep++) + { + System.out.println("Pobieranie danych."); + update(dane, 4, 4); + System.out.println("Pobrano dane."); + for(int i=0; i<4; i++) + { + for(int j=0; j<4; j++) + { + arbitrage(budget, dane, i, j, 4, 0.0005); // np. 0.001 oznacza prowizj na poziomie 0,1% + } + System.out.println(); + } + if(!budget.resources.isEmpty()) System.out.println(budget); + } + + + - } } \ No newline at end of file diff --git a/Orderbook.java b/Orderbook.java new file mode 100644 index 00000000..862aa3b7 --- /dev/null +++ b/Orderbook.java @@ -0,0 +1,68 @@ +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; + +abstract public class Orderbook +{ + URL source; + ArrayList buy; + ArrayList sell; + + public Orderbook(URL source) throws IOException + { + buy = new ArrayList(); + sell = new ArrayList(); + this.source = source; + + } + + public Orderbook(ArrayList buy, ArrayList sell, URL source) throws IOException + { + this.buy = buy; + this.sell = sell; + this.source = source; + } + + public double buySellDiff() + { + System.out.println(String.format("|BUY: %-15.2f|SELL: %-15.2f|", buy.get(0).price, sell.get(0).price)); + return ( (sell.get(0).price - buy.get(0).price)/buy.get(0).price )*100; + } + + public double calcDiff() throws IOException + { + double diff = ((sell.get(0).price - buy.get(0).price)/buy.get(0).price )*100; + System.out.println(String.format("|BUY: %-15.2f|SELL: %-15.2f|\nRnica:%10.6f%%", buy.get(0).price, sell.get(0).price, diff)); + return diff; + } + + public void printOrderbook() + { + for(Offer o : buy) + { + System.out.println(o); + } + + System.out.println("\n___________________________________________________\n"); + + for(Offer o : sell) + { + System.out.println(o); + } + System.out.println(); + } + + abstract void update(int limit) throws IOException; + + public void inspect(long interval) throws IOException, InterruptedException + { + while(true) + { + update(1); + calcDiff(); + Thread.sleep(interval); + } + } + + +} diff --git a/WhiteBitOrderbook.java b/WhiteBitOrderbook.java new file mode 100644 index 00000000..90085e8f --- /dev/null +++ b/WhiteBitOrderbook.java @@ -0,0 +1,63 @@ +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 WhiteBitOrderbook extends Orderbook +{ + public WhiteBitOrderbook(String currency1, String currency2) throws IOException + { + super(new URL("https://whitebit.com/api/v1/public/depth/result?market="+currency1+"_"+currency2+"&limit=10")); + } + + @Override + void update(int limit) throws IOException + { + String tab[]; + buy = new ArrayList(); sell = new ArrayList(); + int i=0, counter=0; + + //long start = System.currentTimeMillis(); + + URLConnection connection = source.openConnection(); + connection.addRequestProperty("User-Agent", "Mozilla"); + connection.connect(); + + BufferedReader buffer = new BufferedReader(new InputStreamReader(connection.getInputStream())); + + //System.out.println("Czas: "+ (System.currentTimeMillis()-start)); + + tab = buffer.readLine().replaceAll("[:\\[\\]\\{\\}\"]", "").split(","); + + tab[0]=tab[0].replace("asks", ""); + + for(; !tab[i].contains("bids"); i+=2) + { + if(counter < limit) + { + Offer temp = new Offer(false,Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])); + sell.add(temp); + counter++; + } + } + + counter = 0; + tab[i]=tab[i].replace("bids", ""); + + for(; i Date: Tue, 2 Jun 2020 12:45:29 +0200 Subject: [PATCH 18/20] Decision Agent work in progress --- Agent.java | 93 +++++ BTCdata.csv | 871 +++++++++++++++++++++++++++++++++++++++++ BitBayOrderbook.java | 2 +- CexIoOrderbook.java | 2 +- FtxOrderbook.java | 2 +- Main.java | 89 +++-- Orderbook.java | 2 +- WhiteBitOrderbook.java | 2 +- 8 files changed, 1021 insertions(+), 42 deletions(-) create mode 100644 Agent.java create mode 100644 BTCdata.csv diff --git a/Agent.java b/Agent.java new file mode 100644 index 00000000..33367f2d --- /dev/null +++ b/Agent.java @@ -0,0 +1,93 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.ArrayList; + +public class Agent +{ + public class Record + { + String date; + double closeValue; + double change; + + public Record(String date, double closeValue, double change) + { + this.date = date; + this.closeValue = closeValue; + this.change = change; + } + + @Override + public String toString() + { + return String.format("%-15s| kurs zamknicia: %-12.2f| zmiana: %7.2f", date, closeValue, change); + } + } + + ArrayList dane; + Orderbook book; + double longTermMA=0.0; + double shortTermMA=0.0; + + public Agent(String dataSource, Orderbook book) throws Exception + { + this.book = book; + + String tab [], line; + dane = new ArrayList(); + + BufferedReader buffer = new BufferedReader(new FileReader(dataSource)); + + while( (line=buffer.readLine()) != null) + { + line = line.replaceAll("[^0-9.,-]", ""); + tab = line.split(","); + dane.add(new Record(tab[0], Double.parseDouble(tab[1]), Double.parseDouble(tab[2]))); + } + buffer.close(); + } + + public double calcMA(boolean longTerm, int movingRange, double...vals) + { + if(movingRange>dane.size()) + { + System.out.println("Podany zakres jest za duy!"); + return -1; + } + else + { + double movingAverage=0.0; + for(int i=0; i Date: Tue, 2 Jun 2020 15:11:17 +0200 Subject: [PATCH 19/20] Decisions and Budget administration added --- Agent.java | 85 +++++++++++++++++++++++--------- Budget.java | 22 ++++++++- CexIoOrderbook.java | 4 +- Main.java | 116 ++++++++++++++++++++++++-------------------- 4 files changed, 148 insertions(+), 79 deletions(-) diff --git a/Agent.java b/Agent.java index 33367f2d..8e4badd6 100644 --- a/Agent.java +++ b/Agent.java @@ -2,6 +2,13 @@ import java.io.FileReader; import java.util.ArrayList; +/* + Agent podejmuje decyzje w oparciu o wskazniki dlugoterminowe, przyjmujc przekazane + kursy jako kursy zamknicia danego dnia, dla maksymalizacji efektywnoci czsotliwo + podejmowania decyzji, powinna zosta dopasowana do danych z pliku CSV + (w tym przypadku najlepiej powinno dziaa podejmowanie decyzji z dnia na dzie) +*/ + public class Agent { public class Record @@ -26,8 +33,9 @@ public String toString() ArrayList dane; Orderbook book; - double longTermMA=0.0; - double shortTermMA=0.0; + boolean worth=false; + double lastAvgMaRatio=0.0; + double currentAvgMaRatio=0.0; public Agent(String dataSource, Orderbook book) throws Exception { @@ -47,47 +55,80 @@ public Agent(String dataSource, Orderbook book) throws Exception buffer.close(); } - public double calcMA(boolean longTerm, int movingRange, double...vals) + public double calcMovingAverage(int modifyDate, int shortRange, int longRange, double...vals) { - if(movingRange>dane.size()) + lastAvgMaRatio = currentAvgMaRatio; + + if(modifyDate+shortRange>dane.size() || modifyDate+longRange>dane.size()) { - System.out.println("Podany zakres jest za duy!"); + System.out.println("Podany zakres nie jest przechowywany"); return -1; } else { - double movingAverage=0.0; + double shortMA=0.0, longMA=0.0; + for(int i=0; ilongMA) worth=true; + System.out.println("------------------------------------"); + System.out.println("Moving Avg"+shortRange+":\t"+ shortMA + "\nMoving Avg"+longRange+":\t" +longMA); + System.out.println("------------------------------------"); + + currentAvgMaRatio = (shortMA+longMA)/2; + System.out.println("Stosunek rednich kroczcych:" + currentAvgMaRatio); + + return currentAvgMaRatio; } } - public int makeDecision(Orderbook book, int shortTerm, int longTerm) //Funkcja zwraca podejmowan decyzj (1: kupno, 0: oczekiwanie, -1: sprzedaz) + /* + Funkcja zwraca podejmowan decyzj (1: kupno, 0: oczekiwanie, -1: sprzedaz) + Zmienna modifyDate pozwala manipulowa okresem analizowanych danych + np. jeeli chcemy analizowa dane (wstecz) poczwszy od dzisiaj + modifyDate powinno przyj warto 0, jeeli chcemy zacz analiz tydzie wstecz + modifyDate powinno przyj warto 7 + */ + + public int makeDecision(Orderbook book, int shortRange, int longRange, int modifyDate) { - return 1; + calcMovingAverage(modifyDate, shortRange, longRange, book.buy.get(0).price); + + if(lastAvgMaRatio!=0 && currentAvgMaRatiolastAvgMaRatio) + { + System.out.println("DECYZJA SPRZEDAJ"); + return -1; + } + else + { + System.out.println("DECYZJA CZEKAJ"); + return 0; + } } } diff --git a/Budget.java b/Budget.java index 83f93e25..5472a44b 100644 --- a/Budget.java +++ b/Budget.java @@ -14,8 +14,10 @@ public Resource(String currency, double ammount) } @Override - public String toString() { - return currency + " Ilo: "+ String.format("%.8f", ammount); + public String toString() + { + if(currency.equals("USD")) return currency + " Ilo: "+ String.format("%.2f", ammount); + else return currency + " Ilo: "+ String.format("%.8f", ammount); } } @@ -41,6 +43,22 @@ public void add(String currency, double ammount) } if(!added) resources.add(new Resource(currency, ammount)); } + + public boolean sell(String currency, double ammount) + { + for(Resource res: resources) + { + if(res.currency.equals(currency) && res.ammount >= ammount) + { + res.ammount-=ammount; + System.out.println("Sprzedano walute: "+currency+"\tw ilosci: "+ammount); + return true; + } + } + + System.out.println("Brak wystarczajacej iloci waluty "+currency); + return false; + } @Override public String toString() diff --git a/CexIoOrderbook.java b/CexIoOrderbook.java index 544c25fb..1c754415 100644 --- a/CexIoOrderbook.java +++ b/CexIoOrderbook.java @@ -19,7 +19,7 @@ void update(int limit) throws IOException buy = new ArrayList(); sell = new ArrayList(); int i=1, counter=0; - //long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); URLConnection connection = source.openConnection(); connection.addRequestProperty("User-Agent", "Mozilla"); @@ -27,7 +27,7 @@ void update(int limit) throws IOException BufferedReader buffer = new BufferedReader(new InputStreamReader(connection.getInputStream())); - //System.out.println("CexIo: "+ (System.currentTimeMillis()-start)); + System.out.println("CexIo: "+ (System.currentTimeMillis()-start)); tab = buffer.readLine().replaceAll("[:\\[\\]\\{\\}]", "").split(","); diff --git a/Main.java b/Main.java index 0d146336..b2a87cd3 100644 --- a/Main.java +++ b/Main.java @@ -62,62 +62,72 @@ public static void arbitrage(Budget budget, Orderbook [][] data, int exchange, i } } - public static void main(String[] args) throws Exception + public static void zad1_3() throws Exception { - //ZADANIA 1-3 - -// Budget budget = new Budget(); -// Orderbook[][] dane = new Orderbook[4][4]; -// -// for(int i=0; i<4; i++) -// { -// dane[0][i] = new BitBayOrderbook(pairs[2*i], pairs[2*i+1]); -// } -// for(int i=0; i<4; i++) -// { -// dane[1][i] = new CexIoOrderbook(pairs[2*i], pairs[2*i+1]); -// } -// for(int i=0; i<4; i++) -// { -// dane[2][i] = new WhiteBitOrderbook(pairs[2*i], pairs[2*i+1]); -// } -// for(int i=0; i<4; i++) -// { -// dane[3][i] = new FtxOrderbook(pairs[2*i], pairs[2*i+1]); -// } -// -// for(int rep=0; rep<1; rep++) -// { -// System.out.println("Pobieranie danych."); -// update(dane, 4, 4); -// System.out.println("Pobrano dane."); -// for(int i=0; i<4; i++) -// { -// for(int j=0; j<4; j++) -// { -// arbitrage(budget, dane, i, j, 4, 0.0005); // np. 0.001 oznacza prowizj na poziomie 0,1% -// } -// System.out.println(); -// } -// if(!budget.resources.isEmpty()) System.out.println(budget); -// } - - //ZADANIE 4 - double a,b,c,d; - + Budget budget = new Budget(); + Orderbook[][] dane = new Orderbook[4][4]; + + for(int i=0; i<4; i++) + { + dane[0][i] = new BitBayOrderbook(pairs[2*i], pairs[2*i+1]); + } + for(int i=0; i<4; i++) + { + dane[1][i] = new CexIoOrderbook(pairs[2*i], pairs[2*i+1]); + } + for(int i=0; i<4; i++) + { + dane[2][i] = new WhiteBitOrderbook(pairs[2*i], pairs[2*i+1]); + } + for(int i=0; i<4; i++) + { + dane[3][i] = new FtxOrderbook(pairs[2*i], pairs[2*i+1]); + } + + for(int rep=0; rep<1; rep++) + { + System.out.println("Pobieranie danych."); + update(dane, 4, 4); + System.out.println("Pobrano dane."); + for(int i=0; i<4; i++) + { + for(int j=0; j<4; j++) + { + arbitrage(budget, dane, i, j, 4, 0.0005); // np. 0.001 oznacza prowizj na poziomie 0,1% + } + System.out.println(); + } + if(!budget.resources.isEmpty()) System.out.println(budget); + } + } + + public static void zad4() throws Exception + { + Budget budget = new Budget(); Orderbook cex = new CexIoOrderbook("BTC","USD"); - cex.update(1); - //cex.printOrderbook(); Agent agent = new Agent("BTCdata.csv", cex); - System.out.println("OFERTY KUPNA"); - a=agent.calcMA(false, 50, cex.buy.get(0).price); - b=agent.calcMA(true, 100, cex.buy.get(0).price); - System.out.println("Stosunek: "+a/b); - System.out.println(); - System.out.println("OFERTY SPRZEDAY"); - c=agent.calcMA(false, 50, cex.sell.get(0).price); - d=agent.calcMA(true, 100, cex.sell.get(0).price); - System.out.println("Stosunek: "+c/d); + budget.add("USD", 17000); + budget.sell("USD", 3000); + budget.sell("USD", 4000); + budget.sell("USD", 11000); + budget.add("BTC", 0.007362); + budget.sell("BTC", 0.007); + budget.sell("BTC", 0.007); + System.out.println(budget); + +// for(int i=0; i<10; i++) +// { +// System.out.println(); +// cex.update(1); +// agent.makeDecision(cex, 50, 100, 0); +// Thread.sleep(10000); +// } + } + + public static void main(String[] args) throws Exception + { + //zad1_3(); + zad4(); } } \ No newline at end of file From d7cf9d0b61c292f7f97de6340027bdf055dfd987 Mon Sep 17 00:00:00 2001 From: Vesker7 Date: Tue, 2 Jun 2020 21:09:59 +0200 Subject: [PATCH 20/20] Done --- Agent.java | 3 ++- Budget.java | 22 ++++++++++++++++++++- Main.java | 55 ++++++++++++++++++++++++++++++++++++----------------- 3 files changed, 61 insertions(+), 19 deletions(-) diff --git a/Agent.java b/Agent.java index 8e4badd6..98a6df42 100644 --- a/Agent.java +++ b/Agent.java @@ -110,8 +110,9 @@ public double calcMovingAverage(int modifyDate, int shortRange, int longRange, d modifyDate powinno przyj warto 7 */ - public int makeDecision(Orderbook book, int shortRange, int longRange, int modifyDate) + public int makeDecision(Orderbook book, int shortRange, int longRange, int modifyDate) throws Exception { + book.update(1); calcMovingAverage(modifyDate, shortRange, longRange, book.buy.get(0).price); if(lastAvgMaRatio!=0 && currentAvgMaRatio resources; + ArrayList bought; public Budget() { resources = new ArrayList(); + bought = new ArrayList(); } public void add(String currency, double ammount) @@ -44,6 +46,12 @@ public void add(String currency, double ammount) if(!added) resources.add(new Resource(currency, ammount)); } + public void add(String currency, double ammount, double boughtPrice) + { + add(currency, ammount); + bought.add(new Offer(false,boughtPrice,ammount)); + } + public boolean sell(String currency, double ammount) { for(Resource res: resources) @@ -51,7 +59,7 @@ public boolean sell(String currency, double ammount) if(res.currency.equals(currency) && res.ammount >= ammount) { res.ammount-=ammount; - System.out.println("Sprzedano walute: "+currency+"\tw ilosci: "+ammount); + System.out.println("\nSprzedano walute: "+currency+"\tw ilosci: "+ammount); return true; } } @@ -60,6 +68,18 @@ public boolean sell(String currency, double ammount) return false; } + public boolean profitAvailable(double price, double ammount) + { + double sum = 0; + + for(Offer o : bought) + { + if(o.price<=price) sum+=o.ammount; + } + + return sum>=ammount; + } + @Override public String toString() { diff --git a/Main.java b/Main.java index b2a87cd3..2c7f87d2 100644 --- a/Main.java +++ b/Main.java @@ -101,33 +101,54 @@ public static void zad1_3() throws Exception } } - public static void zad4() throws Exception + public static void zad4(double tax) throws Exception { Budget budget = new Budget(); + budget.add("USD", 10000); + System.out.println(budget); + Orderbook cex = new CexIoOrderbook("BTC","USD"); Agent agent = new Agent("BTCdata.csv", cex); - budget.add("USD", 17000); - budget.sell("USD", 3000); - budget.sell("USD", 4000); - budget.sell("USD", 11000); - budget.add("BTC", 0.007362); - budget.sell("BTC", 0.007); - budget.sell("BTC", 0.007); + for(int reps=0; reps<100; reps++) + { + System.out.println(); + int decyzja = agent.makeDecision(cex, 50, 100, 0); + if(decyzja==1) + { + double cena = (cex.sell.get(0).price * cex.sell.get(0).ammount)*(1+tax); + cena*=100; cena=Math.round(cena); cena/=100; + + if(budget.sell("USD", cena)) + { + budget.add("BTC", cex.sell.get(0).ammount, cena); + System.out.println("Zakupiono BTC w iloci: "+cex.sell.get(0).ammount+ "\tza: "+cena); + } + } + else if(decyzja==-1) + { + double cena = (cex.buy.get(0).price * cex.buy.get(0).ammount)*(1+tax); + cena*=100; cena=Math.round(cena); cena/=100; + + if(budget.profitAvailable(cena, cex.buy.get(0).ammount)) + { + budget.sell("BTC", cex.buy.get(0).ammount); + budget.add("USD", cena); + System.out.println("Sprzedano BTC w iloci: "+cex.buy.get(0).ammount+ "\tza: "+cena); + } + else + { + System.out.println("Nie sprzedano BTC - brak waluty lub transakcja nieopacalna"); + } + } + Thread.sleep(5000); + } System.out.println(budget); - -// for(int i=0; i<10; i++) -// { -// System.out.println(); -// cex.update(1); -// agent.makeDecision(cex, 50, 100, 0); -// Thread.sleep(10000); -// } } public static void main(String[] args) throws Exception { //zad1_3(); - zad4(); + zad4(0.0005); } } \ No newline at end of file