Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

L6 #156

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open

L6 #156

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea/
MSiD.iml
/out/
93 changes: 93 additions & 0 deletions src/Application.py
Original file line number Diff line number Diff line change
@@ -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()))
13 changes: 13 additions & 0 deletions src/ArrayDefaultSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ArrayDefaultSort<T extends Comparable<? super T>> extends Sorting<T>{


public void sort(ArrayList<T> array){
Collections.sort(array);
// System.out.println(array.toString());

}
}
53 changes: 53 additions & 0 deletions src/BogoSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.*;

public class BogoSort <T extends Comparable<? super T>> extends Sorting<T>
{
public static void main(String[] args)
{
BogoSort<Integer> bongos = new BogoSort<>();
Random r = new Random();
ArrayList<Integer> testArray1 = new ArrayList<>();
ArrayList<String> 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<T> 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 <T extends Comparable<? super T>> boolean isSorted(Iterable<T> iterable) {
Iterator<T> 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;
}
}
Binary file added src/Figure_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Figure_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions src/HeapSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.util.ArrayList;
import java.util.Random;


public class HeapSort<T extends Comparable<? super T>> extends Sorting<T> //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<Integer> Sorter = new HeapSort<>();
HeapSort<String> Sorter2 = new HeapSort<>();
Random r = new Random();
ArrayList<Integer> testArray1 = new ArrayList<>();
ArrayList<String> 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<T> 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<T> 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);
}
}
}
139 changes: 139 additions & 0 deletions src/Lab6Api.py
Original file line number Diff line number Diff line change
@@ -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()
Binary file added src/Lab6wyniki.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/Sorting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import java.util.ArrayList;

public abstract class Sorting<T extends Comparable<? super T>> {

public void sort(ArrayList<T> array){

}
}
Loading