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

L4 #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

L4 #146

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
134 changes: 134 additions & 0 deletions DecisionMaker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.time.LocalDate;

public class DecisionMaker
{
private ArrayList<Quotation> historical;
private double expAverage;

public class Quotation {

private String date;
private double close, volume;

public Quotation(String date, double close, double volume) {
this.date = date;
this.close = close;
this.volume = volume;
}

public String getDate() {
return date;
}

public double getClose() {
return close;
}

public double getVolume() {
return volume;
}

@Override
public String toString() {
return String.format("%s kurs: %.2f wolumen: %.2f", date, close, volume);
}
}

//tworzenie agenta decyzyjnego w oparciu o dane historyczne i analizowany przedzial czasowy (data w formacie rrrr-mm-dd)
public DecisionMaker(String csvFile, String dateToAnalyzeFrom) throws Exception {
historical = new ArrayList<>();
LocalDate today=LocalDate.now(), since = LocalDate.parse(dateToAnalyzeFrom);

if(today.isBefore(since)) throw new IllegalArgumentException("Podana data musi by� wcze�niejsza od dzisiejszej!");
else if (since.isBefore(LocalDate.of(2019, 1, 1))) throw new IllegalArgumentException("Podana data musi by� p�niejsza ni� 1 stycznia 2020r.");
else {

try {
BufferedReader buffer = new BufferedReader(new FileReader(csvFile));
String [] dane; String line="";

while( (line=buffer.readLine()) != null) {

dane = line.replaceAll("[^0-9.;-]", "").split(";");
historical.add(new Quotation(dane[0], Double.parseDouble(dane[1]), Double.parseDouble(dane[2])));
if(line.contains(dateToAnalyzeFrom)) break;
}

buffer.close();
}
catch (Exception e) {
System.out.println("Nie mo�na odnale�� pliku!");
}

expAverage=exponentialAverage();
}

}

public ArrayList<Quotation> getHistorical() {
return historical;
}

public double getExpAverage() {
return expAverage;
}

public int makeDecision(double currentPrice) {

double currentAverage = exponentialAverage(currentPrice);

// jesli wykres sredniej exponencjalnej przecina sie z wykresem kursu waluty od dolu wysylany jest sygnal kupna

if(expAverage<currentPrice && currentPrice <= currentAverage) {
System.out.println("KUP");
return 1;
}

// jesli wykres sredniej exponencjalnej przecina sie z wykresem kursu waluty od gory wysylany jest sygnal sprzedazy
else if(expAverage>=currentPrice && currentPrice > currentAverage) {
System.out.println("SPRZEDAJ");
return -1;
}

else {
System.out.println("CZEKAJ");
return 0;
}
}

// wyliczanie sredniej wazonej eksponencjalnej na podstawie danych historycznych
private double exponentialAverage() {

int N = historical.size();
double sum = 0, weights = 0;

for(int i=0; i<N-1; i++) {

double factorial = Math.pow(1-(2/(N+1)), i);
sum += historical.get(i).close*factorial;
weights += factorial;
}

return sum/weights;
}

// wyliczanie sredniej wazonej eksponencjalnej na podstawie danych historycznych oraz obecnego kursu
public double exponentialAverage(double currentPrice) {

int N = historical.size();
double sum = currentPrice, weights = 1.0;

for(int i=1; i<N+1; i++) {

double factorial = Math.pow(1-(2/(N+1)), i);
sum += historical.get(i-1).close*factorial;
weights += factorial;
}

return sum/weights;
}

}
221 changes: 221 additions & 0 deletions Exchange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
import java.net.URL;
import java.util.ArrayList;

public class Exchange
{
private JSONParser parser;
private String name, currency1, currency2;
private ArrayList <Offer> bids, asks;

public Exchange(String name, String currency1, String currency2, int quantity) {
this.name = name;
this.currency1 = currency1;
this.currency2 = currency2;
this.bids = new ArrayList<Offer>();
this.asks = new ArrayList<Offer>();
createParser();
fillData(quantity);
}

public ArrayList<Offer> getBids() {
return bids;
}

public ArrayList<Offer> getAsks() {
return asks;
}

public void printOrderbook() {

System.out.println("\n------------------------------------------------\n"+ name + " Exchange\t("+currency1+" - "+currency2+")");
System.out.println("\nBIDS\n------------------------------------------------");

for(Offer bid : bids) {
System.out.println("BID: " + bid);
}
System.out.println("------------------------------------------------\n\nASKS\n------------------------------------------------");


for(Offer ask : asks) {
System.out.println("ASK: " + ask);
}
System.out.println("------------------------------------------------");
}

public void update(int quantity) {
parser.updateData();
fillData(quantity);
}

private void fillData(int quantity) {
switch(name) {
case "BitBay":
bitbayData(quantity);
break;
case "CEX.IO":
cexioData(quantity);
break;
case "WhiteBit":
whitebitData(quantity);
break;
case "Ftx":
ftxData(quantity);
break;
default:
System.out.println("Nie obs�ugiwane �r�d�o!");
}
}

private void bitbayData(int quantity) {

String [] tab = parser.getData();
int i=0, counter=0;

tab[0]=tab[0].replace("bids", "");

for(; !tab[i].contains("asks"); i+=2) {

if(counter < quantity) {
if(bids.size()==counter) bids.add(new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
else bids.set(counter, new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
counter++;
}
}

counter = 0;
tab[i]=tab[i].replace("asks", "");

for(; i<tab.length-1; i+=2) {

if(counter < quantity) {
if(asks.size()==counter) asks.add(new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
else asks.set(counter, new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
counter++;
}
}

bids.trimToSize(); asks.trimToSize();
}

private void cexioData(int quantity) {

String [] tab = parser.getData();
int i=1, counter=0;

tab[1]=tab[1].replace("bids", "");

for(; !tab[i].contains("asks"); i+=2) {

if(counter < quantity) {
if(bids.size()==counter) bids.add(new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
else bids.set(counter, new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
counter++;
}
}

counter = 0;
tab[i]=tab[i].replace("asks", "");

for(; !tab[i].contains("pair"); i+=2) {

if(counter < quantity) {
if(asks.size()==counter) asks.add(new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
else asks.set(counter, new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
counter++;
}
}

bids.trimToSize(); asks.trimToSize();
}

private void whitebitData(int quantity) {

String [] tab = parser.getData();
int i=0, counter=0;

tab[0]=tab[0].replace("asks", "");

for(; !tab[i].contains("bids"); i+=2) {

if(counter < quantity) {
if(asks.size()==counter) asks.add(new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
else asks.set(counter, new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
counter++;
}
}

counter = 0;
tab[i]=tab[i].replace("bids", "");

for(; i<tab.length-1; i+=2) {

if(counter < quantity) {
if(bids.size()==counter) bids.add(new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
else bids.set(counter, new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
counter++;
}
}

bids.trimToSize(); asks.trimToSize();
}

private void ftxData(int quantity) {

String [] tab = parser.getData();
int i=0, counter=0;

tab[0]=tab[0].replace("resultasks", "");


for(; !tab[i].contains("bids"); i+=2) {

if(counter < quantity) {
if(asks.size()==counter) asks.add(new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
else asks.set(counter, new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
counter++;
}
}

counter = 0;
tab[i]=tab[i].replace("bids", "");

for(; !tab[i].contains("suc"); i+=2) {

if(counter < quantity) {
if(bids.size()==counter) bids.add(new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
else bids.set(counter, new Offer(Double.parseDouble(tab[i]),Double.parseDouble(tab[i+1])));
counter++;
}
}

bids.trimToSize(); asks.trimToSize();
}

private void createParser()
{
String address="";
switch(name) {
case "BitBay":
address=String.format("https://bitbay.net/API/Public/%s%s/orderbook.json", currency1, currency2);
break;
case "CEX.IO":
address=String.format("https://cex.io/api/order_book/%s/%s/", currency1, currency2);
break;
case "WhiteBit":
address=String.format("https://whitebit.com/api/v1/public/depth/result?market=%s_%s&limit=10", currency1, currency2);
break;
case "Ftx":
address=String.format("https://ftx.com/api//markets/%s_%s/orderbook", currency1, currency2);
break;
default:
System.out.println("Nie obs�ugiwane �r�d�o!");
}

try {
if(address!="") this.parser = new JSONParser(new URL(address));
}
catch (Exception e){
e.printStackTrace();
}
}
}
39 changes: 39 additions & 0 deletions JSONParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class JSONParser
{
private URL source;
private String [] data;

public JSONParser(URL source) {
this.source = source;
try {
updateData();
}
catch (Exception e) {
e.printStackTrace();
}
}

public String [] getData() {
return data;
}

public void updateData() {
try {
URLConnection connection = source.openConnection();
connection.addRequestProperty("User-Agent", "Mozilla");
connection.connect();

BufferedReader buffer = new BufferedReader(new InputStreamReader(connection.getInputStream()));
data = buffer.readLine().replaceAll("[:\\[\\]\\{\\}\"]", "").split(",");
}
catch (Exception e) {
e.printStackTrace();
}
}

}
Loading