diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..44c0ff5c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.idea/ +MSiD.iml +/out/ \ No newline at end of file diff --git a/src/Application.py b/src/Application.py new file mode 100644 index 00000000..c9f8bc2b --- /dev/null +++ b/src/Application.py @@ -0,0 +1,93 @@ +import requests + +CURRENCIES = ['ETH','LTC','TRX','XRP'] +FEE = {} +FEE['tokok'] = 0.002 +FEE['bitbay'] = 0.001 +FEE['latoken'] = 0.001 +FEE['biki'] = 0.0015 +FEE['hitbtc'] = 0.002 + +def getBTCtoPLN(): + r = requests.get("https://bitbay.net/API/Public/BTCPLN/orderbook.json") + return float(r.json()['asks'][0][0]) + + + +wallet =1000 +outcome = wallet / getBTCtoPLN() + +# api data for diffrent markets - since many sites require sign in I've found that in someone else's repo +# tokok = "https://www.tokok.com/api/v1/depth?symbol={}_BTC" +# bitbay = "https://bitbay.net/API/Public/{}BTC/orderbook.json" +# biki = "https://openapi.biki.com/open/api/market_dept?symbol={}btc&type=step0" +# latoken = "https://api.latoken.com/v2/marketOverview/orderbook/{}_BTC" +# hitbtc = ""https://api.hitbtc.com/api/2/public/orderbook/{}BTC.json"" +# not working urls +# wave = "https://marketdata.wavesplatform.com/api/v1/" +# bittrex = "https://api.bittrex.com/api/v1.1/public/getorderbook?market=BTC&type=both" + + + +def getMarket(market, URL): + print("\nRetreiving data from: " + market) + currentMarket = {} + for c in CURRENCIES: + info = {} + print(c, end =" ") + respons = requests.get(URL.format(c)) + info['asks'] = respons.json()['asks'] + info['bids'] = respons.json()['bids'] + currentMarket[c] = info + return currentMarket + +def getMarketWithTick(market, URL): + #Biki market needs "tick" to respond so it have to has own function + print("\nRetreiving data from: " + market) + currentMarket = {} + for c in CURRENCIES: + info = {} + print(c, end =" ") + respons = requests.get(URL.format(c.lower())) + info['asks'] = respons.json()['data']['tick']['asks'] + info['bids'] = respons.json()['data']['tick']['bids'] + currentMarket[c] = info + return currentMarket + +def calculateOffer(toMe, toMarket, asksOffers, bidsOffers, crypto): + if (float(asksOffers[0][1]) < float(bidsOffers[0][1])): + amount = float(asksOffers[0][1]) + else: + amount = float(bidsOffers[0][1]) + print("Buy {} {} in {} for {}BTC, sell in {} for {}BTC".format( + amount, crypto, toMe, asksOffers[0][0], toMarket, bidsOffers[0][0])) + global outcome + outcome += amount * (float(bidsOffers[0][0]) * (1 + FEE[toMarket]) - float(asksOffers[0][0]) * (1 - FEE[toMe])) + print("MONEEEEEY = {}BTC".format(outcome)) + + +def bestOfferSearch(market, orderbook): + for crypto, orders in orderbook.items(): + print("Comparing {} in {} ".format(crypto, market)) + if (orders['asks']): + bestAskOrder = orders['asks'][0] + for otherMarketName, otherOrderbook in marketsOrderbooks.items(): + if (otherOrderbook[crypto]['bids']): + if (float(bestAskOrder[0]) * (1 + FEE[market]) < float( + otherOrderbook[crypto]['bids'][0][0]) * (1 - FEE[otherMarketName])): + calculateOffer(market, orders['asks'], otherMarketName, + otherOrderbook[crypto]['bids'], crypto) + + + +while (True): + marketsOrderbooks = {} + marketsOrderbooks['tokok'] = getMarket("Toktok", "https://www.tokok.com/api/v1/depth?symbol={}_BTC") + marketsOrderbooks['bitbay'] = getMarket("BitBay", "https://bitbay.net/API/Public/{}BTC/orderbook.json") + marketsOrderbooks['latoken'] = getMarket("Latoken", "https://api.latoken.com/v2/marketOverview/orderbook/{}_BTC") + marketsOrderbooks['biki'] = getMarketWithTick("Biki", "https://openapi.biki.com/open/api/market_dept?symbol={}btc&type=step0") + + for market, orderbook in marketsOrderbooks.items(): + print("\n I will now look for best offers in: " + market) + bestOfferSearch(market, orderbook) + print("Your overall outcome is astonishing {} PLN".format(outcome * getBTCtoPLN())) diff --git a/src/ArrayDefaultSort.java b/src/ArrayDefaultSort.java new file mode 100644 index 00000000..9c8efbb9 --- /dev/null +++ b/src/ArrayDefaultSort.java @@ -0,0 +1,13 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public class ArrayDefaultSort> extends Sorting{ + + + public void sort(ArrayList array){ + Collections.sort(array); + // System.out.println(array.toString()); + + } +} diff --git a/src/BogoSort.java b/src/BogoSort.java new file mode 100644 index 00000000..b2ff45f5 --- /dev/null +++ b/src/BogoSort.java @@ -0,0 +1,53 @@ +import java.util.*; + +public class BogoSort > extends Sorting +{ + public static void main(String[] args) + { + BogoSort bongos = new BogoSort<>(); + Random r = new Random(); + ArrayList testArray1 = new ArrayList<>(); + ArrayList testArray2 = new ArrayList<>(); + for(int i=0;i<10; i++){ + testArray1.add(r.nextInt(201)-100); + testArray2.add(Integer.toString(r.nextInt(201)-100)); + } + bongos.sort(testArray1); + } + + public void sort(ArrayList array) + { + + long i=0; + long start = System.currentTimeMillis(); + long time = System.currentTimeMillis(); + //System.out.println("Finally!"); + while(!isSorted(array) && (time-start)<10000){ + Collections.shuffle(array); + i++; + time = System.currentTimeMillis(); + } + if(isSorted(array)) + System.out.println("Finally!"); + else + System.out.println("Sorry i couldn't make it :C"); + //System.out.println(array.toString()); + } + + + public > boolean isSorted(Iterable iterable) { + Iterator iter = iterable.iterator(); + if (!iter.hasNext()) { + return true; + } + T t = iter.next(); + while (iter.hasNext()) { + T t2 = iter.next(); + if (t.compareTo(t2) > 0) { + return false; + } + t = t2; + } + return true; + } +} diff --git a/src/Figure_1.png b/src/Figure_1.png new file mode 100644 index 00000000..f52759f8 Binary files /dev/null and b/src/Figure_1.png differ diff --git a/src/Figure_2.png b/src/Figure_2.png new file mode 100644 index 00000000..e2710c84 Binary files /dev/null and b/src/Figure_2.png differ diff --git a/src/HeapSort.java b/src/HeapSort.java new file mode 100644 index 00000000..a680d39e --- /dev/null +++ b/src/HeapSort.java @@ -0,0 +1,63 @@ +import java.util.ArrayList; +import java.util.Random; + + +public class HeapSort> extends Sorting //not quite sure why, have to read more about Comparable +{ + public static void main(String[] args) + { + Integer[] testArray = {12,5,124,214,1,2,564,6,3,2,1,78,4,2,543,33,0}; + + HeapSort Sorter = new HeapSort<>(); + HeapSort Sorter2 = new HeapSort<>(); + Random r = new Random(); + ArrayList testArray1 = new ArrayList<>(); + ArrayList testArray2 = new ArrayList<>(); + for(int i=0;i<50; i++){ + testArray1.add(r.nextInt(201)-100); + testArray2.add(Integer.toString(r.nextInt(201)-100)); + } + + + Sorter.sort(testArray1); + Sorter2.sort(testArray2); + } + + public void sort(ArrayList array) + { + int size = array.size(); + for (int i = size / 2 - 1; i > -1; i--) + heapify(array, size, i); + + for (int i = size - 1; i > -1; i--) + { + T temp = array.get(0); + array.set(0, array.get(i)); + array.set(i, temp); + + heapify(array, i, 0); + } + //System.out.println(array.toString()); + } + + void heapify(ArrayList array, int size, int i) + { + int max = i; + int left = 2 * i + 1; + int right = 2 * i + 2; + + if (left < size && array.get(left).compareTo(array.get(max)) > 0) + max = left; + + if (right < size && array.get(right).compareTo(array.get(max)) > 0) + max = right; + + if (max != i) + { + T temp = array.get(i); + array.set(i, array.get(max)); + array.set(max, temp); + heapify(array, size, max); + } + } +} \ No newline at end of file diff --git a/src/Lab6Api.py b/src/Lab6Api.py new file mode 100644 index 00000000..b87a8a50 --- /dev/null +++ b/src/Lab6Api.py @@ -0,0 +1,139 @@ +import requests +import matplotlib.pyplot +from datetime import datetime +import time +import random + +#apiCurrencies = ['BTCUSDT', 'ETHBTC', 'LTCBTC'] + +INPUT_LENGTH = 5 +SSS = 200 #Single Simulation Steps +SA = 5 #Simulation amount(how many simulations will be done) +START_DATE = '2019-11-01' +END_DATE = '2020-02-01' +RESULTS_AMOUNT = 4 +RANDOM_MODIFIER = 0.0000001 + +def getData(startDate, endDate, symbol, delta=3600000): + sTimeStamp = time.mktime(datetime.strptime(startDate,"%Y-%m-%d").timetuple())*1000 + eTimeStamp = time.mktime(datetime.strptime(endDate, "%Y-%m-%d").timetuple())*1000 + deltaTimeStamp = delta*999 + data = [] + previousTimeStamp = sTimeStamp + for i in range(int(eTimeStamp-sTimeStamp)//deltaTimeStamp): + currentTimeStamp = sTimeStamp+(i+1)*deltaTimeStamp + request = requests.get('https://api.binance.com/api/v3/klines', + params={'symbol':symbol,'interval':'1h','startTime':str(int(previousTimeStamp)),'endTime':str(int(currentTimeStamp)),'limit':'1000'}) + previousTimeStamp = currentTimeStamp + data= data + request.json() + + request = requests.get('https://api.binance.com/api/v3/klines', + params={'symbol': symbol, 'interval': '1h', 'startTime': str(int(previousTimeStamp)),'endTime': str(int(currentTimeStamp)), 'limit': '1000'}) + data = data + request.json() + return data + +def shiftData(data): + calculated = [] + for d in data: + changedData = [] + changedData.append(int(d[0])) + changedData.append((float(d[4]) - float(d[1])) / float(d[4])) + changedData.append(float(d[4])) + changedData.append(float(d[5])) + calculated.append(changedData) +# i[0] - open time, i[1] - % daily change, i[2] - close value, i[3] - volume + return calculated + +# Here I came across an idea, that in order to check "correctness" of simulation, +# I can print both simulation and real data for simulating period +# (Data used for learning do not include stock values for simulatig period + +def deviationCalculation(givenData,learnData): + exchange = 0.000000000001 + volume = 0.000000000001 + for i in range(len(givenData)): + exchange += pow(learnData[i][1] - givenData[i][0],2) + volume += pow(learnData[i][3] - givenData[i][1],2) + overallDeviation = exchange*volume + return[exchange, volume, overallDeviation] + +def estimate(givenInterval, learningData): + deviation= [] + + for calculatingIntervalNum in range(len(learningData) - len(givenInterval) - 1): + deviation.append([calculatingIntervalNum + len(givenInterval), + deviationCalculation(givenInterval, + learningData[calculatingIntervalNum:calculatingIntervalNum + len(givenInterval)])]) +#looking for best results + for i in range(RESULTS_AMOUNT): + min_idx = i + for j in range(i+1, len(deviation)): + if deviation[min_idx][1][2] > deviation[j][1][2]: + min_idx = j + deviation[i], deviation[min_idx] = deviation[min_idx], deviation[i] + + return calculateEstimation(deviation[:RESULTS_AMOUNT], learningData) + +def calculateEstimation(bestMatchedResults, systemData): + excSum = posEstimate = corSum = volSum = 0 + for resultIndex, deviations in bestMatchedResults: + correctness = (1-random.uniform(-RANDOM_MODIFIER, RANDOM_MODIFIER))/deviations[0] + corSum += correctness + excSum += correctness * systemData[resultIndex][1] + volSum += correctness * systemData[resultIndex][2] + if systemData[resultIndex][1] > 0: + posEstimate += correctness + return [excSum/corSum, posEstimate/corSum, volSum/corSum, corSum] + +def calculateAverageResults(): + averageResults = [] + for stepIndex in range(SSS): + averageOfStep = 0 + for simulationIndex in range(SA): + #print(stepIndex, simulationIndex) + averageOfStep += simulationResults[simulationIndex][stepIndex] + averageOfStep = averageOfStep/SA + averageResults.append(averageOfStep) + return averageResults + + +print("bip") +#print(getData(START_DATE,END_DATE,'BTCUSDT') ) +data = getData(START_DATE,END_DATE,'ETHBTC') + +shiftedData = shiftData(data) +learnData = shiftedData[:len(data)-SSS] + +inputs = [[learnData[i][1],learnData[i][3]]for i in range(len(learnData))] + +calcData = [learnData[i][2] for i in range(len(learnData))] +realData = [shiftedData[i][2] for i in range(len(shiftedData))] + +#simulation +simulationResults = [] +volumes = [] +for simulationIndex in range(SA): + print("Simulation number {}".format(simulationIndex+1)) + inputs = [[learnData[i][1],learnData[i][3]]for i in range(len(learnData))] + calcData = [learnData[i][2] for i in range(len(learnData))] + + for i in range(SSS): + givenRequest = inputs[-INPUT_LENGTH :] + estimatedChange, positiveChangeChance, estimatedVolume, qualityOfEstimate = estimate(givenRequest, learnData) + volumes.append(estimatedVolume) + inputs.append([estimatedChange, estimatedVolume]) + calcData.append(estimatedChange*calcData[-1]+calcData[-1]) + simulationResults.append(calcData[-SSS:]) + +simulation = calculateAverageResults() + +#Plot +domain1 = [x for x in range(len(realData)-SSS,len(realData))] +domain2 = [x for x in range(len(realData))] +domain3 = [x for x in range(len(realData)-SSS,len(realData))] +matplotlib.pyplot.plot(domain1, simulation, label = "Average simulation") +matplotlib.pyplot.plot(domain2, realData,label ="Real data") +matplotlib.pyplot.plot(domain3, simulationResults[1], label = "single simulation") +print("boop - gotowe") +matplotlib.pyplot.legend() +matplotlib.pyplot.show() \ No newline at end of file diff --git a/src/Lab6wyniki.PNG b/src/Lab6wyniki.PNG new file mode 100644 index 00000000..357eeae6 Binary files /dev/null and b/src/Lab6wyniki.PNG differ diff --git a/src/Sorting.java b/src/Sorting.java new file mode 100644 index 00000000..f51c6902 --- /dev/null +++ b/src/Sorting.java @@ -0,0 +1,8 @@ +import java.util.ArrayList; + +public abstract class Sorting> { + + public void sort(ArrayList array){ + + } +} diff --git a/src/StalinSort.java b/src/StalinSort.java new file mode 100644 index 00000000..cf54796b --- /dev/null +++ b/src/StalinSort.java @@ -0,0 +1,34 @@ +import java.util.ArrayList; +import java.util.Random; + +public class StalinSort > extends Sorting{ + public static void main(String[] args) { + StalinSort stalin = new StalinSort<>(); + Random r = new Random(); + ArrayList testArray1 = new ArrayList<>(); + ArrayList testArray2 = new ArrayList<>(); + for (int i = 0; i < 50; i++) { + testArray1.add(r.nextInt(201) - 100); + testArray2.add(Integer.toString(r.nextInt(201) - 100)); + } + stalin.sort(testArray1); + + } + public void sort (ArrayList array) { + int length = array.size(); + for (int i = 1; i < length; i++) { + if (array.get(i).compareTo(array.get(i - 1)) < 0) { + array.remove(i); + length--; + i--; + } + + } + // System.out.println(array.toString()); + } + + + + +} + diff --git a/src/TimeTest.java b/src/TimeTest.java new file mode 100644 index 00000000..b961e7c1 --- /dev/null +++ b/src/TimeTest.java @@ -0,0 +1,33 @@ +import java.util.ArrayList; +import java.util.Random; + +public class TimeTest { + public static void main(String[] args){ + HeapSort sorter1 = new HeapSort<>(); + BogoSort sorter2 = new BogoSort<>(); + StalinSort sorter3 = new StalinSort<>(); + ArrayDefaultSort sorter4 = new ArrayDefaultSort<>(); + + testSortingMethods(sorter1,100,10); + // testSortingMethods(sorter2,100,10); + testSortingMethods(sorter3,100,10); + testSortingMethods(sorter4,100,10); + } + public static void testSortingMethods(Sorting test, int arraySize,int attemps){ + ArrayList array = new ArrayList(); + Random r = new Random(); + long time = 0, begin = 0; + for(int j = 0; j