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 #153

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

L6 #153

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
143 changes: 143 additions & 0 deletions barracuda/src/main/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package main;

import market_observer.*;

import java.time.*;
import java.util.ArrayList;

public class Main
{
private static ArrayList<market_observer> market_observers = new ArrayList<market_observer>();
private static int refresh_interval;
private static float wallet;

private static void refresh()
{
System.out.println("\n----- Status for " + Instant.now() + " -----");
for ( market_observer observer: market_observers )
{
observer.update_data();
observer.print_status();
}
}

private static void make_transactions()
{
while (true) make_best_transaction();
}

private static void make_best_transaction()
{
float best_amount = 0;
float best_profit = 0;
int best_seller_index = 0;
int best_buyer_index = 0;

for(int i = 0; i < market_observers.size(); i++)
{
market_observer comparing = market_observers.get(i);
int highest_bid_index = i;
int lowest_ask_index = i;
float highest_bid = comparing.bid_price();
float lowest_ask = comparing.ask_price();

for(int j = 0; j < market_observers.size(); j++)
{
market_observer comparing_2 = market_observers.get(j);
if(comparing.currency() == comparing_2.currency())
{
if(comparing_2.bid_price() > highest_bid)
{
highest_bid = comparing_2.bid_price();
highest_bid_index = j;
}
if(comparing_2.ask_price() < lowest_ask)
{
lowest_ask = comparing_2.ask_price();
lowest_ask_index = j;
}
}
}

market_observer bidder = market_observers.get(highest_bid_index);
market_observer asker = market_observers.get(lowest_ask_index);

float current_amount = Math.min(wallet / asker.ask_price(), Math.min(bidder.bid_amount(), asker.ask_amount()));
float current_profit = current_amount * (1 - asker.commission_fee()) * bidder.bid_price() * (1 - bidder.commission_fee())
- current_amount * asker.ask_price();

if(current_profit > best_profit)
{
best_amount = current_amount;
best_profit = current_profit;
best_buyer_index = highest_bid_index;
best_seller_index = lowest_ask_index;
}

}

market_observer best_seller = market_observers.get(best_seller_index);
market_observer best_buyer = market_observers.get(best_buyer_index);

if(best_profit >= 0.00005)
{
wallet += best_profit;
best_seller.apply_buying_transaction(best_amount); // buy from them
best_buyer.apply_selling_transaction(best_amount); // sell them

System.out.println(
"Buying " + best_amount + " of " + best_seller.currency() +
" for " + best_amount * best_seller.ask_price() + " USD on " +
best_seller.name() + " and selling with a profit of " + best_profit +
" on " + best_buyer.name() + "; current wallet is $" + wallet
);
}
}

private static void refresh_periodically()
{
while (true)
{
refresh();
try
{
Thread.sleep(refresh_interval);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
}

public static void main(String[] args)
{
wallet = 100000;
refresh_interval = 4000;

market_observers.add(new bitbay_observer("BTC"));
market_observers.add(new bybit_observer("BTC"));
market_observers.add(new bitfinex_observer("BTC"));
// market_observers.add(new whitebit_observer("BTC"));


market_observers.add(new bitbay_observer("ETH"));
market_observers.add(new bybit_observer("ETH"));
market_observers.add(new bitfinex_observer("ETH"));

// market_observers.add(new bitbay_observer("LSK"));
// market_observers.add(new bitbay_observer("GAME"));
// market_observers.add(new bitbay_observer("REP"));
// market_observers.add(new bitbay_observer("PAY"));

Thread thread = new Thread(Main::refresh_periodically);
thread.setDaemon(true);
thread.start();

make_transactions();

try { thread.join(); }
catch (InterruptedException e) { Thread.currentThread().interrupt(); }

}
}
76 changes: 76 additions & 0 deletions barracuda/src/market_observer/bitbay_observer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package market_observer;

import java.io.*;
import java.net.*;

public class bitbay_observer extends market_observer
{
private URL currency_url;

public bitbay_observer(String currency)
{
super(currency, "bitbay", 0.003f);
try
{
currency_url = new URL("https://bitbay.net/API/Public/"+ currency + "/orderbook.json");
update_data();
print_status();
}
catch (MalformedURLException e)
{
System.out.println("Invalid URL for " + currency);
e.printStackTrace();
}
}

@Override
public void update_data()
{
try
{
URLConnection conn = currency_url.openConnection();
try
{
BufferedReader buffered_reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
try
{
String line;
if ((line = buffered_reader.readLine()) != null) get_data_from_json_string(line);
}
catch (EOFException ignored) { }

buffered_reader.close();
}
catch (IOException e) { e.printStackTrace(); }
}
catch (IOException e) { e.printStackTrace(); }
}

private void get_data_from_json_string(String json_string)
{
// System.out.println(json_string);
// return;
if(json_string == null) return;

try
{
int bid_price_start_position = json_string.indexOf("\"bids\":[[") + 9;
int bid_price_end_position = json_string.indexOf(',', bid_price_start_position );
int bid_amount_end_position = json_string.indexOf(']', bid_price_end_position );

int ask_price_start_position = json_string.indexOf("\"asks\":[[") + 9;
int ask_price_end_position = json_string.indexOf(',', ask_price_start_position );
int ask_amount_end_position = json_string.indexOf(']', ask_price_end_position );

bid_price = Float.parseFloat( json_string.substring(bid_price_start_position, bid_price_end_position ) );
bid_amount = Float.parseFloat( json_string.substring(bid_price_end_position + 1, bid_amount_end_position ) );

ask_price = Float.parseFloat( json_string.substring(ask_price_start_position, ask_price_end_position ) );
ask_amount = Float.parseFloat( json_string.substring(ask_price_end_position + 1, ask_amount_end_position ) );

// System.out.println("bid: " + bid_amount + " ✖️ " + bid_price + "; ask: " + ask_amount + " ✖️ " + ask_price);
}
catch (java.lang.NumberFormatException ignored){ /* System.out.println(ignored); */ }

}
}
79 changes: 79 additions & 0 deletions barracuda/src/market_observer/bitfinex_observer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package market_observer;

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class bitfinex_observer extends market_observer
{
private URL currency_url;

public bitfinex_observer(String currency)
{
super(currency, "bitfinex", 0.002f);
try
{
currency_url = new URL("https://api-pub.bitfinex.com/v2/ticker/t"+ currency + "USD");
update_data();
print_status();
}
catch (MalformedURLException e)
{
System.out.println("Invalid URL for " + currency);
e.printStackTrace();
}
}

@Override
public void update_data()
{
try
{
URLConnection conn = currency_url.openConnection();
try
{
BufferedReader buffered_reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
try
{
String line;
if ((line = buffered_reader.readLine()) != null) get_data_from_json_string(line);
}
catch (EOFException ignored) { }

buffered_reader.close();
}
catch (IOException e) { e.printStackTrace(); }
}
catch (IOException e) { e.printStackTrace(); }
}

private void get_data_from_json_string(String json_string)
{
// System.out.println(json_string);
// return;
if(json_string == null) return;

try
{
int bid_price_start_position = json_string.indexOf('[') + 1;
int bid_amount_start_position = json_string.indexOf(',', bid_price_start_position ) + 1;
int ask_price_start_position = json_string.indexOf(',', bid_amount_start_position) + 1;
int ask_amount_start_position = json_string.indexOf(',', ask_price_start_position ) + 1;
int ask_amount_end_position = json_string.indexOf(',', ask_amount_start_position ) - 1;
//
bid_price = Float.parseFloat( json_string.substring(bid_price_start_position, bid_amount_start_position - 1 ) );
bid_amount = Float.parseFloat( json_string.substring(bid_amount_start_position, ask_price_start_position - 2 ) );

ask_price = Float.parseFloat( json_string.substring(ask_price_start_position, ask_amount_start_position - 1 ) );
ask_amount = Float.parseFloat( json_string.substring(ask_amount_start_position, ask_amount_end_position ) );
//
//// System.out.println("bid: " + bid_amount + " ✖️ " + bid_price + "; ask: " + ask_amount + " ✖️ " + ask_price);
}
catch (NumberFormatException ignored){ /* System.out.println(ignored); */ }

}
}
83 changes: 83 additions & 0 deletions barracuda/src/market_observer/bybit_observer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package market_observer;

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class bybit_observer extends market_observer
{
private URL currency_url;

public bybit_observer(String currency)
{
super(currency, "bybit", 0.0007f);
try
{
currency_url = new URL("https://api.bybit.com/v2/public/orderBook/L2?symbol="+ currency + "USD");
update_data();
print_status();
}
catch (MalformedURLException e)
{
System.out.println("Invalid URL for " + currency);
e.printStackTrace();
}
}

@Override
public void update_data()
{
try
{
URLConnection conn = currency_url.openConnection();
try
{
BufferedReader buffered_reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
try
{
String line;
if ((line = buffered_reader.readLine()) != null) get_data_from_json_string(line);
}
catch (EOFException ignored) { }

buffered_reader.close();
}
catch (IOException e) { e.printStackTrace(); }
}
catch (IOException e) { e.printStackTrace(); }
}

private void get_data_from_json_string(String json_string)
{
// System.out.println(json_string);
// return;
if(json_string == null) return;

try
{
int bid_price_start_position = json_string.indexOf("\"price\":\"") + 9;
int bid_price_end_position = json_string.indexOf("\",\"size\":", bid_price_start_position );
int bid_amount_end_position = json_string.indexOf(',', bid_price_end_position + 9);

int first_sell_position = json_string.indexOf("Sell") - 40;

int ask_price_start_position = json_string.indexOf("\"price\":\"", first_sell_position ) + 9;
int ask_price_end_position = json_string.indexOf("\",\"size\":", ask_price_start_position );
int ask_amount_end_position = json_string.indexOf(',', ask_price_end_position + 9);

bid_price = Float.parseFloat( json_string.substring(bid_price_start_position, bid_price_end_position ) );
bid_amount = Float.parseFloat( json_string.substring(bid_price_end_position + 9, bid_amount_end_position ) ) / bid_price;

ask_price = Float.parseFloat( json_string.substring(ask_price_start_position, ask_price_end_position ) );
ask_amount = Float.parseFloat( json_string.substring(ask_price_end_position + 9, ask_amount_end_position ) ) / ask_price;

// System.out.println("bid: " + bid_amount + " ✖️ " + bid_price + "; ask: " + ask_amount + " ✖️ " + ask_price);
}
catch (NumberFormatException ignored){ /* System.out.println(ignored); */ }

}
}
Loading