diff --git a/Agent.java b/Agent.java new file mode 100644 index 00000000..98a6df42 --- /dev/null +++ b/Agent.java @@ -0,0 +1,135 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.ArrayList; + +/* + Agent podejmuje decyzje w oparciu o wskazniki dlugoterminowe, przyjmując przekazane + kursy jako kursy zamknięcia danego dnia, dla maksymalizacji efektywności częsotliwość + podejmowania decyzji, powinna zostać dopasowana do danych z pliku CSV + (w tym przypadku najlepiej powinno działać podejmowanie decyzji z dnia na dzień) +*/ + +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 zamknięcia: %-12.2f| zmiana: %7.2f", date, closeValue, change); + } + } + + ArrayList dane; + Orderbook book; + boolean worth=false; + double lastAvgMaRatio=0.0; + double currentAvgMaRatio=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 calcMovingAverage(int modifyDate, int shortRange, int longRange, double...vals) + { + lastAvgMaRatio = currentAvgMaRatio; + + if(modifyDate+shortRange>dane.size() || modifyDate+longRange>dane.size()) + { + System.out.println("Podany zakres nie jest przechowywany"); + return -1; + } + else + { + 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 kroczących:" + currentAvgMaRatio); + + return currentAvgMaRatio; + } + + } + + /* + Funkcja zwraca podejmowaną decyzję (1: kupno, 0: oczekiwanie, -1: sprzedaz) + Zmienna modifyDate pozwala manipulować okresem analizowanych danych + np. jeżeli chcemy analizować dane (wstecz) począwszy od dzisiaj + modifyDate powinno przyjąć wartość 0, jeżeli chcemy zacząć analizę tydzień wstecz + modifyDate powinno przyjąć wartość 7 + */ + + 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 && currentAvgMaRatiolastAvgMaRatio) + { + System.out.println("DECYZJA SPRZEDAJ"); + return -1; + } + else + { + System.out.println("DECYZJA CZEKAJ"); + return 0; + } + } + +} diff --git a/BTCdata.csv b/BTCdata.csv new file mode 100644 index 00000000..0cec19fe --- /dev/null +++ b/BTCdata.csv @@ -0,0 +1,871 @@ +01.06.2020,9548.50,101.6, +31.05.2020,9446.90,-253.2, +30.05.2020,9700.10,271.5, +29.05.2020,9428.60,-135.7, +28.05.2020,9564.30,364.3, +27.05.2020,9200,344.6, +26.05.2020,8855.40,-54.2, +25.05.2020,8909.60,187.7, +24.05.2020,8721.90,-476.8, +23.05.2020,9198.70,20.3, +22.05.2020,9178.40,133.6, +21.05.2020,9044.80,-482.8, +20.05.2020,9527.60,-272.2, +19.05.2020,9799.80,57.6, +18.05.2020,9742.20,59.3, +17.05.2020,9682.90,298.1, +16.05.2020,9384.80,64.9, +15.05.2020,9319.90,-468.2, +14.05.2020,9788.10,460.7, +13.05.2020,9327.40,490, +12.05.2020,8837.40,268, +11.05.2020,8569.40,-178.8, +10.05.2020,8748.20,-796.3, +09.05.2020,9544.50,-278, +08.05.2020,9822.50,-174.1, +07.05.2020,9996.60,828, +06.05.2020,9168.60,133.3, +05.05.2020,9035.30,131.3, +04.05.2020,8904,-12.9, +03.05.2020,8916.90,-76.4, +02.05.2020,8993.30,155.2, +01.05.2020,8838.10,211.2, +30.04.2020,8626.90,-151.4, +29.04.2020,8778.30,1005.4, +28.04.2020,7772.90,-14, +27.04.2020,7786.90,86.9, +26.04.2020,7700,159, +25.04.2020,7541,14.6, +24.04.2020,7526.40,33.6, +23.04.2020,7492.80,352.5, +22.04.2020,7140.30,267.4, +21.04.2020,6872.90,24.2, +20.04.2020,6848.70,-287.8, +19.04.2020,7136.50,-121.6, +18.04.2020,7258.10,221.7, +17.04.2020,7036.40,-85.6, +16.04.2020,7122,474.6, +15.04.2020,6647.40,-240.8, +14.04.2020,6888.20,25.1, +13.04.2020,6863.10,-54.1, +12.04.2020,6917.20,17.2, +11.04.2020,6900,22.3, +10.04.2020,6877.70,-433.2, +09.04.2020,7310.90,-67.2, +08.04.2020,7378.10,163.4, +07.04.2020,7214.70,-141.7, +06.04.2020,7356.40,563.5, +05.04.2020,6792.90,-87.6, +04.04.2020,6880.50,137.5, +03.04.2020,6743,-70.5, +02.04.2020,6813.50,177.7, +01.04.2020,6635.80,194.3, +31.03.2020,6441.50,37.9, +30.03.2020,6403.60,507.8, +29.03.2020,5895.80,-375.8, +28.03.2020,6271.60,-111.6, +27.03.2020,6383.20,-388.2, +26.03.2020,6771.40,61.7, +25.03.2020,6709.70,-71.3, +24.03.2020,6781,297.9, +23.03.2020,6483.10,633.2, +22.03.2020,5849.90,-362.3, +21.03.2020,6212.20,-8.7, +20.03.2020,6220.90,0.5, +19.03.2020,6220.40,809.4, +18.03.2020,5411,29.6, +17.03.2020,5381.40,324, +16.03.2020,5057.40,-365.5, +15.03.2020,5422.90,172.9, +14.03.2020,5250,-436.9, +13.03.2020,5686.90,766.4, +12.03.2020,4920.50,-3021.6, +11.03.2020,7942.10,37.3, +10.03.2020,7904.80,-42.9, +09.03.2020,7947.70,-111.5, +08.03.2020,8059.20,-863.9, +07.03.2020,8923.10,-236.6, +06.03.2020,9159.70,73.7, +05.03.2020,9086,288.2, +04.03.2020,8797.80,1.7, +03.03.2020,8796.10,-130.4, +02.03.2020,8926.50,353.7, +01.03.2020,8572.80,13.8, +29.02.2020,8559,-139.3, +28.02.2020,8698.30,-151.5, +27.02.2020,8849.80,28.5, +26.02.2020,8821.30,-512.5, +25.02.2020,9333.80,-356.8, +24.02.2020,9690.60,-309.3, +23.02.2020,9999.90,299.2, +22.02.2020,9700.70,-20.3, +21.02.2020,9721,114.8, +20.02.2020,9606.20,-11.1, +19.02.2020,9617.30,-561.5, +18.02.2020,10178.80,433.7, +17.02.2020,9745.10,-205.8, +16.02.2020,9950.90,45.9, +15.02.2020,9905,-470, +14.02.2020,10375,134, +13.02.2020,10241,-140.6, +12.02.2020,10381.60,93.1, +11.02.2020,10288.50,409, +10.02.2020,9879.50,-300.3, +09.02.2020,10179.80,250.9, +08.02.2020,9928.90,94, +07.02.2020,9834.90,61.7, +06.02.2020,9773.20,157.2, +05.02.2020,9616,422.4, +04.02.2020,9193.60,-120.1, +03.02.2020,9313.70,-37.2, +02.02.2020,9350.90,-44.1, +01.02.2020,9395,38.3, +31.01.2020,9356.70,-184.1, +30.01.2020,9540.80,234.3, +29.01.2020,9306.50,-53.2, +28.01.2020,9359.70,447.6, +27.01.2020,8912.10,312.1, +26.01.2020,8600,232.3, +25.01.2020,8367.70,-89.2, +24.01.2020,8456.90,32.9, +23.01.2020,8424,-257.9, +22.01.2020,8681.90,-78.8, +21.01.2020,8760.70,97.1, +20.01.2020,8663.60,-54.4, +19.01.2020,8718,-221.9, +18.01.2020,8939.90,25.1, +17.01.2020,8914.80,167.7, +16.01.2020,8747.10,-71.1, +15.01.2020,8818.20,-29.4, +14.01.2020,8847.60,714.6, +13.01.2020,8133,-57, +12.01.2020,8190,136.2, +11.01.2020,8053.80,-146.2, +10.01.2020,8200,345, +09.01.2020,7855,-214.9, +08.01.2020,8069.90,-119.6, +07.01.2020,8189.50,394.2, +06.01.2020,7795.30,407.9, +05.01.2020,7387.40,7.6, +04.01.2020,7379.80,28.5, +03.01.2020,7351.30,366.5, +02.01.2020,6984.80,-253.4, +01.01.2020,7238.20,-20.5, +31.12.2019,7258.70,-18.6, +30.12.2019,7277.30,-135.4, +29.12.2019,7412.70,86.5, +28.12.2019,7326.20,49.2, +27.12.2019,7277,45.3, +26.12.2019,7231.70,-2.6, +25.12.2019,7234.30,-33.8, +24.12.2019,7268.10,-94.1, +23.12.2019,7362.20,-174.8, +22.12.2019,7537,318.8, +21.12.2019,7218.20,-18.6, +20.12.2019,7236.80,22.3, +19.12.2019,7214.50,-116.5, +18.12.2019,7331,623.8, +17.12.2019,6707.20,-249.3, +16.12.2019,6956.50,-210.3, +15.12.2019,7166.80,30.1, +14.12.2019,7136.70,-155.2, +13.12.2019,7291.90,27.9, +12.12.2019,7264,-16.2, +11.12.2019,7280.20,-29.1, +10.12.2019,7309.30,-98.7, +09.12.2019,7408,-176.9, +08.12.2019,7584.90,9.4, +07.12.2019,7575.50,-24.4, +06.12.2019,7599.90,144, +05.12.2019,7455.90,197.9, +04.12.2019,7258,-114.1, +03.12.2019,7372.10,-20.8, +02.12.2019,7392.90,-82.8, +01.12.2019,7475.70,-142.2, +30.11.2019,7617.90,-210, +29.11.2019,7827.90,310.7, +28.11.2019,7517.20,-42.8, +27.11.2019,7560,310.1, +26.11.2019,7249.90,93, +25.11.2019,7156.90,171.7, +24.11.2019,6985.20,-392.1, +23.11.2019,7377.30,37.4, +22.11.2019,7339.90,-343.5, +21.11.2019,7683.40,-464.4, +20.11.2019,8147.80,-10.2, +19.11.2019,8158,-82, +18.11.2019,8240,-309.4, +17.11.2019,8549.40,39.2, +16.11.2019,8510.20,-12.4, +15.11.2019,8522.60,-170.7, +14.11.2019,8693.30,-134.8, +13.11.2019,8828.10,-21.8, +12.11.2019,8849.90,66, +11.11.2019,8783.90,-284.4, +10.11.2019,9068.30,234.6, +09.11.2019,8833.70,-4.6, +08.11.2019,8838.30,-405.8, +07.11.2019,9244.10,-158.8, +06.11.2019,9402.90,53.5, +05.11.2019,9349.40,-59.4, +04.11.2019,9408.80,158, +03.11.2019,9250.80,-81.9, +02.11.2019,9332.70,70.2, +01.11.2019,9262.50,79.3, +31.10.2019,9183.20,-25.7, +30.10.2019,9208.90,-230.4, +29.10.2019,9439.30,202.5, +28.10.2019,9236.80,-302, +27.10.2019,9538.80,311.8, +26.10.2019,9227,574.7, +25.10.2019,8652.30,1202.3, +24.10.2019,7450,-37.5, +23.10.2019,7487.50,-585.4, +22.10.2019,8072.90,-176.6, +21.10.2019,8249.50,-0.5, +20.10.2019,8250,266, +19.10.2019,7984,-7.1, +18.10.2019,7991.10,-114.9, +17.10.2019,8106,82.5, +16.10.2019,8023.50,-165.7, +15.10.2019,8189.20,-165.2, +14.10.2019,8354.40,34.4, +13.10.2019,8320,-13.1, +12.10.2019,8333.10,40.5, +11.10.2019,8292.60,-309.3, +10.10.2019,8601.90,-11.2, +09.10.2019,8613.10,407, +08.10.2019,8206.10,-2.8, +07.10.2019,8208.90,284.6, +06.10.2019,7924.30,-230.6, +05.10.2019,8154.90,-24.8, +04.10.2019,8179.70,-87.7, +03.10.2019,8267.40,-119.5, +02.10.2019,8386.90,39.4, +01.10.2019,8347.50,26.2, +30.09.2019,8321.30,241.3, +29.09.2019,8080,-153.1, +28.09.2019,8233.10,10.6, +27.09.2019,8222.50,170.9, +26.09.2019,8051.60,-415.7, +25.09.2019,8467.30,-124.7, +24.09.2019,8592,-1120, +23.09.2019,9712,-349.4, +22.09.2019,10061.40,62.1, +21.09.2019,9999.30,-196.6, +20.09.2019,10195.90,-84.5, +19.09.2019,10280.40,98.3, +18.09.2019,10182.10,-9.9, +17.09.2019,10192,-100.4, +16.09.2019,10292.40,-7.5, +15.09.2019,10299.90,-68.3, +14.09.2019,10368.20,-5.2, +13.09.2019,10373.40,-33.5, +12.09.2019,10406.90,236.9, +11.09.2019,10170,32.3, +10.09.2019,10137.70,-173.7, +09.09.2019,10311.40,-90.5, +08.09.2019,10401.90,-66.5, +07.09.2019,10468.40,141.8, +06.09.2019,10326.60,-248.4, +05.09.2019,10575,-17.4, +04.09.2019,10592.40,-54.8, +03.09.2019,10647.20,257.2, +02.09.2019,10390,634.7, +01.09.2019,9755.30,135, +31.08.2019,9620.30,16.4, +30.08.2019,9603.90,97, +29.08.2019,9506.90,-234.1, +28.08.2019,9741,-450.4, +27.08.2019,10191.40,-165.3, +26.08.2019,10356.70,211.7, +25.08.2019,10145,2, +24.08.2019,10143,-262.5, +23.08.2019,10405.50,251.2, +22.08.2019,10154.30,-15, +21.08.2019,10169.30,-573.1, +20.08.2019,10742.40,-183.4, +19.08.2019,10925.80,586.9, +18.08.2019,10338.90,108.9, +17.08.2019,10230,-135.5, +16.08.2019,10365.50,41.1, +15.08.2019,10324.40,270.4, +14.08.2019,10054,-793.1, +13.08.2019,10847.10,-570.9, +12.08.2019,11418,-150.3, +11.08.2019,11568.30,251.8, +10.08.2019,11316.50,-550.3, +09.08.2019,11866.80,-128.3, +08.08.2019,11995.10,68.1, +07.08.2019,11927,455.8, +06.08.2019,11471.20,-324, +05.08.2019,11795.20,799.9, +04.08.2019,10995.30,191.8, +03.08.2019,10803.50,267.3, +02.08.2019,10536.20,135.4, +01.08.2019,10400.80,306.1, +31.07.2019,10094.70,501.4, +30.07.2019,9593.30,72.7, +29.07.2019,9520.60,-15.8, +28.07.2019,9536.40,51.4, +27.07.2019,9485,-384.8, +26.07.2019,9869.80,-35.5, +25.07.2019,9905.30,105.3, +24.07.2019,9800,-70.3, +23.07.2019,9870.30,-499.7, +22.07.2019,10370,-204.4, +21.07.2019,10574.40,-150.4, +20.07.2019,10724.80,177.5, +19.07.2019,10547.30,-112.3, +18.07.2019,10659.60,965.7, +17.07.2019,9693.90,243.1, +16.07.2019,9450.80,-1396, +15.07.2019,10846.80,607.4, +14.07.2019,10239.40,-1107.3, +13.07.2019,11346.70,-467, +12.07.2019,11813.70,435.3, +11.07.2019,11378.40,-757, +10.07.2019,12135.40,-442.7, +09.07.2019,12578.10,277.8, +08.07.2019,12300.30,850.2, +07.07.2019,11450.10,162.3, +06.07.2019,11287.80,271.8, +05.07.2019,11016,-266.8, +04.07.2019,11282.80,-705.8, +03.07.2019,11988.60,1198.9, +02.07.2019,10789.70,210.7, +01.07.2019,10579,-194.1, +30.06.2019,10773.10,-1118.1, +29.06.2019,11891.20,-538.9, +28.06.2019,12430.10,1194.9, +27.06.2019,11235.20,-1905.1, +26.06.2019,13140.30,1334.6, +25.06.2019,11805.70,837, +24.06.2019,10968.70,99.1, +23.06.2019,10869.60,140.8, +22.06.2019,10728.80,559.6, +21.06.2019,10169.20,664.8, +20.06.2019,9504.40,225.7, +19.06.2019,9278.70,183.5, +18.06.2019,9095.20,-195.2, +17.06.2019,9290.40,309.6, +16.06.2019,8980.80,160.3, +15.06.2019,8820.50,108.9, +14.06.2019,8711.60,413.4, +13.06.2019,8298.20,113.3, +12.06.2019,8184.90,222, +11.06.2019,7962.90,-102, +10.06.2019,8064.90,367.6, +09.06.2019,7697.30,-287.7, +08.06.2019,7985,-99.8, +07.06.2019,8084.80,225.8, +06.06.2019,7859,-7.9, +05.06.2019,7866.90,117, +04.06.2019,7749.90,-474.2, +03.06.2019,8224.10,-510.9, +02.06.2019,8735,101.1, +01.06.2019,8633.90,-13.9, +31.05.2019,8647.80,270.8, +30.05.2019,8377,-330.6, +29.05.2019,8707.60,5.4, +28.05.2019,8702.20,38.6, +27.05.2019,8663.60,49.8, +26.05.2019,8613.80,553.8, +25.05.2019,8060,39, +24.05.2019,8021,91.6, +23.05.2019,7929.40,242.6, +22.05.2019,7686.80,-262.8, +21.05.2019,7949.60,-0.4, +20.05.2019,7950,-221.5, +19.05.2019,8171.50,791.1, +18.05.2019,7380.40,-90.3, +17.05.2019,7470.70,-549.9, +16.05.2019,8020.60,-154.8, +15.05.2019,8175.40,170.1, +14.05.2019,8005.30,98.2, +13.05.2019,7907.10,962.2, +12.05.2019,6944.90,-112.3, +11.05.2019,7057.20,636.1, +10.05.2019,6421.10,171.3, +09.05.2019,6249.80,267.2, +08.05.2019,5982.60,185, +07.05.2019,5949.90,189.9, +06.05.2019,5760,-34.6, +05.05.2019,5794.60,-26.9, +04.05.2019,5821.50,127.8, +03.05.2019,5693.70,257.1, +02.05.2019,5436.60,82.4, +01.05.2019,5354.20,37.1, +30.04.2019,5317.10,61.3, +29.04.2019,5255.80,-16.7, +28.04.2019,5272.50,-8.5, +27.04.2019,5281,37.7, +26.04.2019,5243.30,26.4, +25.04.2019,5216.90,-280.8, +24.04.2019,5497.70,-76.5, +23.04.2019,5574.20,168.4, +22.04.2019,5405.80,57.3, +21.04.2019,5348.50,-16.9, +20.04.2019,5365.40,36.7, +19.04.2019,5328.70,-27, +18.04.2019,5355.70,48.3, +17.04.2019,5307.40,20.1, +16.04.2019,5287.30,145.2, +15.04.2019,5142.10,-105.7, +14.04.2019,5247.80,78.8, +13.04.2019,5169,2.3, +12.04.2019,5166.70,24.7, +11.04.2019,5142,-254.2, +10.04.2019,5396.20,114, +09.04.2019,5282.20,-62.8, +08.04.2019,5345,135.3, +07.04.2019,5209.70,92, +06.04.2019,5117.70,37.8, +05.04.2019,5079.90,129.5, +04.04.2019,4950.40,-29.6, +03.04.2019,4980,17.9, +02.04.2019,4962.10,750.1, +01.04.2019,4212,62, +19.03.2019,4038.70,2.3, +18.03.2019,4036.40,3.3, +17.03.2019,4033.10,-5.2, +16.03.2019,4038.30,57.6, +15.03.2019,3980.70,49.7, +14.03.2019,3931,-1, +13.03.2019,3932,-1.7, +12.03.2019,3933.70,11.9, +11.03.2019,3921.80,-32.1, +10.03.2019,3953.90,-19.3, +09.03.2019,3973.20,80.2, +08.03.2019,3893,-5.9, +07.03.2019,3898.90,-4.9, +06.03.2019,3903.80,-11.4, +05.03.2019,3915.20,129.7, +04.03.2019,3785.50,-79.9, +03.03.2019,3865.40,-10.6, +02.03.2019,3876,15.8, +01.03.2019,3860.20,12.3, +28.02.2019,3847.90,-2.9, +27.02.2019,3850.80,-0.1, +26.02.2019,3850.90,-18.9, +25.02.2019,3869.80,104.3, +24.02.2019,3765.50,-345.5, +23.02.2019,4111,153.1, +22.02.2019,3943.50,17.4, +21.02.2019,3928.60,-66.2, +20.02.2019,3994.80,49.2, +19.02.2019,3945.60,41.5, +18.02.2019,3904.10,236.1, +17.02.2019,3668,-29.2, +16.02.2019,3697.20,14.9, +15.02.2019,3682.30,5.1, +14.02.2019,3677.20,-7.7, +13.02.2019,3684.90,-11.1, +12.02.2019,3696,-2.9, +11.02.2019,3698.90,-53, +10.02.2019,3751.90,42.4, +09.02.2019,3709.50,-12.8, +08.02.2019,3722.30,240.5, +07.02.2019,3481.80,-0.5, +06.02.2019,3482.30,-42.7, +05.02.2019,3525,19.1, +04.02.2019,3505.90,10.8, +03.02.2019,3495.10,-49.5, +02.02.2019,3544.60,43, +01.02.2019,3501.60,8, +31.01.2019,3493.60,-11.9, +30.01.2019,3505.50,46.1, +29.01.2019,3459.40,-33.6, +28.01.2019,3493,-105.8, +27.01.2019,3598.80,-14.7, +26.01.2019,3613.50,1.5, +25.01.2019,3612,-1, +24.01.2019,3613,23, +23.01.2019,3590,-33.6, +22.01.2019,3623.60,25.7, +21.01.2019,3597.90,-4.1, +20.01.2019,3602,-136.5, +19.01.2019,3738.50,56.1, +18.01.2019,3682.40,-27.4, +17.01.2019,3709.80,37.2, +16.01.2019,3672.60,1.9, +15.01.2019,3670.70,-51.1, +14.01.2019,3721.80,131.3, +13.01.2019,3590.50,-129.5, +12.01.2019,3720,-31.9, +11.01.2019,3751.90,36.9, +10.01.2019,3715,-333.8, +09.01.2019,4048.80,4.4, +08.01.2019,4044.40,-0.9, +07.01.2019,4045.30,-40.8, +06.01.2019,4086.10,221, +05.01.2019,3865.10,-46.6, +04.01.2019,3911.70,28.6, +03.01.2019,3883.10,-88.4, +02.01.2019,3971.50,28.8, +01.01.2019,3942.70,112.2, +31.12.2018,3830.50,-110.9, +30.12.2018,3941.40,125.8, +29.12.2018,3815.60,-166, +28.12.2018,3981.60,259, +27.12.2018,3722.60,-207.2, +26.12.2018,3929.80,33.6, +25.12.2018,3896.20,-194.3, +24.12.2018,4090.50,96.7, +23.12.2018,3993.80,-53.6, +22.12.2018,4047.40,153.2, +21.12.2018,3894.20,-212.6, +20.12.2018,4106.80,375.6, +19.12.2018,3731.20,44.4, +18.12.2018,3686.80,142.2, +17.12.2018,3544.60,264.6, +16.12.2018,3280,24.8, +15.12.2018,3255.20,11.5, +14.12.2018,3243.70,-86.8, +13.12.2018,3330.50,-147, +12.12.2018,3477.50,52.5, +11.12.2018,3421.20,-45.3, +10.12.2018,3466.50,-113.3, +09.12.2018,3579.80,115.7, +08.12.2018,3464.10,12.9, +07.12.2018,3451.20,-77.5, +06.12.2018,3528.70,-247.6, +05.12.2018,3776.30,-166, +04.12.2018,3942.30,56.6, +03.12.2018,3885.70,-277.5, +02.12.2018,4163.20,-15.2, +01.12.2018,4178.40,154.8, +30.11.2018,4018.80,-259.3, +29.11.2018,4278.10,27.9, +28.11.2018,4250.20,403.1, +27.11.2018,3847.10,-9.2, +26.11.2018,3856.30,-203.2, +25.11.2018,4059.50,175.3, +24.11.2018,3884.20,-493.5, +23.11.2018,4377.70,19.5, +22.11.2018,4358.20,-303.3, +21.11.2018,4661.50,167.3, +20.11.2018,4494.20,-313.4, +19.11.2018,4807.60,-805.2, +18.11.2018,5612.80,32.2, +17.11.2018,5580.60,-60, +16.11.2018,5640.60,-87.1, +15.11.2018,5727.70,-82.2, +14.11.2018,5809.90,-520, +13.11.2018,6329.90,-26, +12.11.2018,6355.90,-17.6, +11.11.2018,6373.50,7.6, +10.11.2018,6365.90,5.1, +09.11.2018,6360.80,-47.9, +08.11.2018,6408.70,-80.3, +07.11.2018,6489,37.4, +06.11.2018,6451.60,22.1, +05.11.2018,6429.50,-22.1, +04.11.2018,6451.60,59.7, +03.11.2018,6391.90,-42.8, +02.11.2018,6434.70,28.9, +01.11.2018,6405.80,-1.9, +31.10.2018,6407.70,20.7, +30.10.2018,6387,4.8, +29.10.2018,6382.20,-122.7, +28.10.2018,6504.90,-3, +27.10.2018,6507.90,26.4, +26.10.2018,6481.50,-36.4, +25.10.2018,6517.90,-32.2, +24.10.2018,6550.10,21.7, +23.10.2018,6528.40,-37.7, +22.10.2018,6566.10,8.2, +21.10.2018,6557.90,20.2, +20.10.2018,6537.70,5, +19.10.2018,6532.70,-41.9, +18.10.2018,6574.60,-66.6, +17.10.2018,6641.20,-24.7, +16.10.2018,6621.90,-19.2, +15.10.2018,6641.10,303.5, +14.10.2018,6337.60,12.9, +13.10.2018,6324.70,6.9, +12.10.2018,6317.80,40.4, +11.10.2018,6277.40,-343.9, +10.10.2018,6621.30,-19, +09.10.2018,6640.30,-17.2, +08.10.2018,6657.50,83.5, +07.10.2018,6574,1.4, +06.10.2018,6572.60,-36.5, +05.10.2018,6609.10,51.9, +04.10.2018,6557.20,75.2, +03.10.2018,6482,-55, +02.10.2018,6537,-51.6, +01.10.2018,6588.60,-19.2, +30.09.2018,6607.80,-9.3, +29.09.2018,6617.10,-38.2, +28.09.2018,6655.30,-23.1, +27.09.2018,6678.40,205.3, +26.09.2018,6473.10,30.1, +25.09.2018,6443,-136, +24.09.2018,6579,-93.4, +23.09.2018,6672.40,-23.2, +22.09.2018,6695.60,-50.3, +21.09.2018,6745.90,291.7, +20.09.2018,6454.20,86.9, +19.09.2018,6367.30,22.4, +18.09.2018,6344.90,73.4, +17.09.2018,6271.50,-246.5, +16.09.2018,6518,-4.6, +15.09.2018,6522.60,44.2, +14.09.2018,6478.40,-0.1, +13.09.2018,6478.50,146.2, +12.09.2018,6332.30,41.6, +11.09.2018,6290.70,-42.1, +10.09.2018,6332.80,30.2, +09.09.2018,6302.60,71.5, +08.09.2018,6231.10,-183.9, +07.09.2018,6415,-101.4, +06.09.2018,6516.40,-137.9, +05.09.2018,6654.30,-634.8, +04.09.2018,7289.10,113.8, +03.09.2018,7175.30,-6.4, +02.09.2018,7181.70,59.6, +01.09.2018,7122.10,175.6, +31.08.2018,6946.50,-3.3, +30.08.2018,6949.80,-50.2, +29.08.2018,7000,-22, +28.08.2018,7022,184.5, +27.08.2018,6837.50,122.5, +26.08.2018,6715,1.6, +25.08.2018,6713.40,58.6, +24.08.2018,6654.80,132.7, +23.08.2018,6522.10,149.8, +22.08.2018,6372.30,-110.6, +21.08.2018,6482.90,187.6, +20.08.2018,6295.30,-180.1, +19.08.2018,6475.40,67.3, +18.08.2018,6408.10,-161.7, +17.08.2018,6569.80,221.1, +16.08.2018,6348.70,38.5, +15.08.2018,6310.20,90.9, +14.08.2018,6219.30,-87.2, +13.08.2018,6306.50,-30.8, +12.08.2018,6156,0, +11.08.2018,6156,-13.4, +10.08.2018,6169.40,-382.4, +09.08.2018,6551.80,197.9, +08.08.2018,6309.40,-426.2, +07.08.2018,6735.60,-214, +06.08.2018,6949.60,-75, +05.08.2018,7024.60,0.7, +04.08.2018,7023.90,-377.1, +03.08.2018,7401,-158.9, +02.08.2018,7559.90,-70.1, +01.08.2018,7630,-186.4, +31.07.2018,7816.40,-314.8, +30.07.2018,8131.20,-26, +29.07.2018,8157.20,21.2, +28.07.2018,8136,54.1, +27.07.2018,8081.90,209.9, +26.07.2018,7872,-221, +25.07.2018,8093,-194.8, +24.07.2018,8287.80,629.4, +23.07.2018,7658.40,285.4, +22.07.2018,7373,-10.2, +21.07.2018,7383.20,63.3, +20.07.2018,7319.90,-106.3, +19.07.2018,7426.20,85.3, +18.07.2018,7340.90,89.8, +17.07.2018,7251.10,562, +16.07.2018,6689.10,336.5, +15.07.2018,6352.60,91.2, +14.07.2018,6261.40,43.6, +13.07.2018,6217.80,-37.5, +12.07.2018,6255.30,-126.8, +11.07.2018,6382.10,52.1, +10.07.2018,6330,-311.2, +09.07.2018,6641.20,-51.7, +08.07.2018,6692.90,-19.3, +07.07.2018,6712.20,118.7, +06.07.2018,6593.50,29.6, +05.07.2018,6563.90,1.9, +04.07.2018,6562,55.9, +03.07.2018,6506.10,-145.4, +02.07.2018,6651.50,285, +01.07.2018,6366.50,-40.8, +30.06.2018,6407.30,165.3, +29.06.2018,6242,295.6, +28.06.2018,5946.40,-253.4, +27.06.2018,6199.80,73.7, +26.06.2018,6126.10,-191.8, +25.06.2018,6317.90,93.5, +24.06.2018,6224.40,12.7, +23.06.2018,6211.70,49.6, +22.06.2018,6162.10,-575.5, +21.06.2018,6737.60,-12.3, +20.06.2018,6749.90,-17.9, +19.06.2018,6767.80,17.4, +18.06.2018,6750.40,191.5, +17.06.2018,6558.90,-25.2, +16.06.2018,6584.10,114, +15.06.2018,6470.10,-249.8, +14.06.2018,6719.90,305.7, +13.06.2018,6414.20,-248.8, +12.06.2018,6663,-300.9, +11.06.2018,6963.90,56.8, +10.06.2018,6907.10,-636.4, +09.06.2018,7543.50,-105.6, +08.06.2018,7649.10,-39.3, +07.06.2018,7688.40,33.7, +06.06.2018,7654.70,18.1, +05.06.2018,7636.60,58.1, +04.06.2018,7578.50,-138.5, +03.06.2018,7717,34.4, +02.06.2018,7682.60,96.2, +01.06.2018,7586.40,51.7, +31.05.2018,7534.70,98.3, +30.05.2018,7436.40,-108.5, +29.05.2018,7544.90,335.2, +28.05.2018,7209.70,-263.1, +27.05.2018,7472.80,18.6, +26.05.2018,7454.20,-103.5, +25.05.2018,7557.70,-88.9, +24.05.2018,7646.60,1.9, +23.05.2018,7644.70,-449.6, +22.05.2018,8094.30,-392.7, +21.05.2018,8487,-132.6, +20.05.2018,8619.60,294.9, +19.05.2018,8324.70,0.8, +18.05.2018,8323.90,123.3, +17.05.2018,8200.60,-242.1, +16.05.2018,8442.70,-148.7, +15.05.2018,8591.40,-113.1, +14.05.2018,8704.50,-130.5, +13.05.2018,8835,219.2, +12.05.2018,8615.80,86.2, +11.05.2018,8529.60,-566.2, +10.05.2018,9095.80,-236.2, +09.05.2018,9332,95.3, +08.05.2018,9236.70,-148, +07.05.2018,9384.70,-255.1, +06.05.2018,9639.80,-144, +05.05.2018,9783.80,118.9, +04.05.2018,9664.90,-15.1, +03.05.2018,9680,495.5, +02.05.2018,9184.50,125.6, +01.05.2018,9058.90,-215.7, +30.04.2018,9274.60,-123.4, +29.04.2018,9398,49, +28.04.2018,9349,355.5, +27.04.2018,8993.50,-284.9, +26.04.2018,9278.40,365.7, +25.04.2018,8912.70,-716.3, +24.04.2018,9629,683.5, +23.04.2018,8945.50,121.3, +22.04.2018,8824.20,-66.1, +21.04.2018,8890.30,19, +20.04.2018,8871.30,594.6, +19.04.2018,8276.70,121.1, +18.04.2018,8155.60,228.7, +17.04.2018,7926.90,-113.1, +16.04.2018,8040,-268.2, +15.04.2018,8308.20,319.7, +14.04.2018,7988.50,126.1, +13.04.2018,7862.40,-61.7, +12.04.2018,7924.10,979.8, +11.04.2018,6944.30,103, +10.04.2018,6841.30,68.5, +09.04.2018,6772.80,-215.3, +08.04.2018,6988.10,103.9, +07.04.2018,6884.20,240.1, +06.04.2018,6644.10,-185.8, +05.04.2018,6829.90,-64.4, +04.04.2018,6894.30,-594.5, +03.04.2018,7488.80,379.4, +02.04.2018,7109.40,198.9, +01.04.2018,6910.50,-86.9, +31.03.2018,6997.40,23.4, +30.03.2018,6974,-248, +29.03.2018,7222,-889.5, +28.03.2018,8111.50,132.6, +27.03.2018,7978.90,-328.8, +26.03.2018,8307.70,-237.3, +25.03.2018,8545,-133.9, +24.03.2018,8678.90,-298.1, +23.03.2018,8977,161.9, +22.03.2018,8815.10,-257.8, +21.03.2018,9072.90,66.6, +20.03.2018,9006.30,305.8, +19.03.2018,8700.50,344.4, +18.03.2018,8356.10,303.3, +17.03.2018,8052.80,-347.2, +16.03.2018,8400,-55.2, +15.03.2018,8455.20,68, +14.03.2018,8387.20,-946.8, +13.03.2018,9334,4.2, +12.03.2018,9329.80,-460.2, +11.03.2018,9790,720.9, +10.03.2018,9069.10,-477.7, +09.03.2018,9546.80,-80.5, +08.03.2018,9627.30,-637.7, +07.03.2018,10265,-658.9, +06.03.2018,10923.90,-581.1, +05.03.2018,11505,-125.4, +04.03.2018,11630.40,20.7, +03.03.2018,11609.70,373, +02.03.2018,11236.70,184, +01.03.2018,11052.70,511.7, +28.02.2018,10541,-159.4, +27.02.2018,10700.40,244.9, +26.02.2018,10455.50,686.2, +25.02.2018,9769.30,-118.5, +24.02.2018,9887.80,-412.4, +23.02.2018,10300.20,245.2, +22.02.2018,10055,-619.5, +21.02.2018,10674.50,-835.4, +20.02.2018,11509.90,133.9, +19.02.2018,11376,684.8, +18.02.2018,10691.20,-594.9, +17.02.2018,11286.10,934.2, +16.02.2018,10351.90,117.9, +15.02.2018,10234,554.9, +14.02.2018,9679.10,900.9, +13.02.2018,8778.20,-221.8, +12.02.2018,9000,748, +11.02.2018,8252,-440.8, +10.02.2018,8692.80,-96.3, +09.02.2018,8789.10,294.8, +08.02.2018,8494.30,621.1, +07.02.2018,7873.20,-93.9, +06.02.2018,7967.10,901.2, +05.02.2018,7065.90,-1549.3, +04.02.2018,8615.20,-913.1, +03.02.2018,9528.30,366.1, +02.02.2018,9162.20,-155.8, +01.02.2018,9318,-1397, +31.01.2018,10715,-0.9, +30.01.2018,10715.90,-1058.4, +29.01.2018,11774.30,-330.9, +28.01.2018,12105.20,247.3, +27.01.2018,11857.90,136.8, +26.01.2018,11721.10,11.2, +25.01.2018,11709.90,-400.1, +24.01.2018,12110,528.1, +23.01.2018,11581.90,-27, +22.01.2018,11608.90,-706.28, +21.01.2018,12315.18,-1276.53, +20.01.2018,13591.71,1339.83, +19.01.2018,12251.88,286.25, +18.01.2018,11965.63,130.4, +17.01.2018,11835.23,-139.81, +16.01.2018,11975.04,-2759.66, +15.01.2018,14734.70,-121.2, +14.01.2018,14855.90,-504.1, +13.01.2018,15360,216.91, +12.01.2018,15143.09,588.57, +11.01.2018,14554.52,-1714.57, +10.01.2018,16269.09,269.68, +09.01.2018,15999.41,-664.24, +08.01.2018,16663.65,-1028.43, +07.01.2018,17692.08,-632.77, +06.01.2018,18324.85,136.95, +05.01.2018,18187.90,1937.9, +04.01.2018,16250,209, +03.01.2018,16041,31, +02.01.2018,16010,787.98, +01.01.2018,15222.02,-251.65 \ No newline at end of file diff --git a/BitBayOrderbook.java b/BitBayOrderbook.java new file mode 100644 index 00000000..81776a1e --- /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("BitBay: "+ (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; + ArrayList bought; + + public Budget() + { + resources = new ArrayList(); + bought = 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)); + } + + 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) + { + if(res.currency.equals(currency) && res.ammount >= ammount) + { + res.ammount-=ammount; + System.out.println("\nSprzedano walute: "+currency+"\tw ilosci: "+ammount); + return true; + } + } + + System.out.println("Brak wystarczajacej ilości waluty "+currency); + 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() + { + String output="[BUDŻET]\n"; + for(Resource res : resources) + { + output+=res+"\n"; + } + + return output; + } + + +} + diff --git a/CexIoOrderbook.java b/CexIoOrderbook.java new file mode 100644 index 00000000..1c754415 --- /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("CexIo: "+ (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..3cfc7855 --- /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("Ftx: "+ (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/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/Main.java b/Main.java new file mode 100644 index 00000000..2c7f87d2 --- /dev/null +++ b/Main.java @@ -0,0 +1,154 @@ +import java.io.IOException; + +public class Main +{ + final static String [] api_names = {"BitBay","Cex.IO","WhiteBit","FTX"}; + + final static String [] pairs = {"BTC", "USD", + "ETH", "USD", + "LTC", "USD", + "ETH", "BTC"}; + + public static void update(Orderbook [][] data, int xsize, int ysize) throws IOException + { + for(int i=0; i0) + { + System.out.println("Na giełdzie "+ api_names[exchange] +" można kupić "+ Math.min(buy.ammount, sell.ammount) + + " " +pairs[2*market] +" po kursie "+ buy.price + pairs[2*market+1]+ + " i sprzedać na giełdzie "+ api_names[i] +" po kursie "+ sell.price + " " + pairs[2*market+1] + + " zyskując " + String.format("%.8f", profit)+ " "+pairs[2*market+1]); + + if(profit>bestProfit) + { + best=sell; + bestProfit=profit; + } + } + } + + if(best!=null) + { + budget.add(pairs[2*market+1], bestProfit); + } + } + + public static void zad1_3() 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<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(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); + + 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 ilości: "+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 ilości: "+cex.buy.get(0).ammount+ "\tza: "+cena); + } + else + { + System.out.println("Nie sprzedano BTC - brak waluty lub transakcja nieopłacalna"); + } + } + Thread.sleep(5000); + } + System.out.println(budget); + } + + public static void main(String[] args) throws Exception + { + //zad1_3(); + zad4(0.0005); + } +} \ No newline at end of file diff --git a/Offer.java b/Offer.java new file mode 100644 index 00000000..3c64f6f9 --- /dev/null +++ b/Offer.java @@ -0,0 +1,33 @@ +public class Offer +{ + boolean type; // 0-BUY 1-SELL + double price; + double ammount; + + public Offer() + { + type = true; + price = 0; + ammount = 0; + } + + public Offer(boolean type, double price, double ammount) + { + this.type = type; + this.price = price; + this.ammount = ammount; + } + + @Override + public String toString() + { + String s = ""; + + if(!type) s+="SELL "; + else s+="BUY "; + + s+= String.format("|price: %-15.2f|ammount: %-12.8f|", price, ammount); + return s; + } + +} diff --git a/Orderbook.java b/Orderbook.java new file mode 100644 index 00000000..c22bc7d2 --- /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|\nRóżnica:%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("---------------------------------------------------"); + + 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/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/WhiteBitOrderbook.java b/WhiteBitOrderbook.java new file mode 100644 index 00000000..67d96262 --- /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("WhiteBit: "+ (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