From 0d729e3d86087453a1098d30ab9e13c2b8dec4ca Mon Sep 17 00:00:00 2001 From: Kuf <250875@student.pwr.edu.pl> Date: Tue, 9 Jun 2020 20:42:18 -0500 Subject: [PATCH 1/2] Garbage --- .DS_Store | Bin 0 -> 6148 bytes LICENSE | 21 ------- README.md | 2 - l1 | 6 -- l2 | 2 - l3 | 10 ---- l4 | 21 ------- searching_methods/src/algorithms/bubble.java | 28 --------- searching_methods/src/algorithms/insert.java | 37 ------------ searching_methods/src/algorithms/quick.java | 44 -------------- searching_methods/src/algorithms/select.java | 38 ------------ .../src/algorithms/sorting_algorithm.java | 38 ------------ searching_methods/src/testing/main.java | 56 ------------------ 13 files changed, 303 deletions(-) create mode 100644 .DS_Store delete mode 100644 LICENSE delete mode 100644 README.md delete mode 100644 l1 delete mode 100644 l2 delete mode 100644 l3 delete mode 100644 l4 delete mode 100644 searching_methods/src/algorithms/bubble.java delete mode 100644 searching_methods/src/algorithms/insert.java delete mode 100644 searching_methods/src/algorithms/quick.java delete mode 100644 searching_methods/src/algorithms/select.java delete mode 100644 searching_methods/src/algorithms/sorting_algorithm.java delete mode 100644 searching_methods/src/testing/main.java diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e7d981ccf7902a62c68030ca75e0ef90a023ef60 GIT binary patch literal 6148 zcmeHK!A`z2Q|>>JcBVW@YWfIfed@#o5j!K2#En=fEZW;2F%eW)|a3bDuEav27Z?T zJRdAjMB8AZQ5_vHs1^V)18yZ?t6zUW3mt&A!9*i?K)6l?)T!LG7~FIYT&KglZQ>J+ zI-PO#W?09(nVSxUo5q7%Dx7g!BaOrWG4PoI?EOIUc>f>&-2bZ}8i)a6U^yA!nTFG- zLv7}6U8oZ8S_`xdih^;8#(4@D>MDj source) - { - super(source); - } - - @Override - public ArrayList sort_out() - { - int size = to_be_sorted.size(); - for(int i = 0; i < size; i++) - for(int j = 0; j < size - 1; j++) - if(to_be_sorted.get(j) > to_be_sorted.get(j + 1)) - swap(j, j + 1); - - return to_be_sorted; - } -} diff --git a/searching_methods/src/algorithms/insert.java b/searching_methods/src/algorithms/insert.java deleted file mode 100644 index 4e81792c..00000000 --- a/searching_methods/src/algorithms/insert.java +++ /dev/null @@ -1,37 +0,0 @@ -package algorithms; - -import java.util.ArrayList; - -public class insert extends sorting_algorithm -{ - public insert() - { - super(); - } - - public insert(ArrayList to_be_sorted) - { - super(to_be_sorted); - } - - @Override - public ArrayList sort_out() - { - int size = to_be_sorted.size(); - for (int i = 1; i < size; i++) - { - int key = to_be_sorted.get(i); - int j = i - 1; - - while (j >= 0 && to_be_sorted.get(j) > key) - { - to_be_sorted.set(j + 1, to_be_sorted.get(j)); - j = j - 1; - } - - to_be_sorted.set(j + 1, key); - } - - return to_be_sorted; - } -} diff --git a/searching_methods/src/algorithms/quick.java b/searching_methods/src/algorithms/quick.java deleted file mode 100644 index b1b7ea75..00000000 --- a/searching_methods/src/algorithms/quick.java +++ /dev/null @@ -1,44 +0,0 @@ -package algorithms; - -import java.util.ArrayList; - -public class quick extends sorting_algorithm -{ - public quick() - { - super(); - } - - public quick(ArrayList source) - { - super(source); - } - - @Override - public ArrayList sort_out() - { - sorting_procedure(0, to_be_sorted.size() - 1); - - return to_be_sorted; - } - - void sorting_procedure(int low, int high) - { - int pivot = to_be_sorted.get((low + high) / 2); - int i = low, j = high; - - do{ - while (to_be_sorted.get(i) < pivot) i++; - while (to_be_sorted.get(j) > pivot) j--; - if (i <= j) - { - swap(i, j); - i++; - j--; - } - } while (i <= j); - - if (j > low) sorting_procedure(low, j); - if (i < high) sorting_procedure(i, high); - } -} diff --git a/searching_methods/src/algorithms/select.java b/searching_methods/src/algorithms/select.java deleted file mode 100644 index 2b5ac8f9..00000000 --- a/searching_methods/src/algorithms/select.java +++ /dev/null @@ -1,38 +0,0 @@ -package algorithms; - -import java.util.ArrayList; - -public class select extends sorting_algorithm -{ - public select() - { - super(); - } - - public select(ArrayList source) - { - super(source); - } - - @Override - public ArrayList sort_out() - { - for (int i = to_be_sorted.size(); i >= 2; i--) - { - int max = max_element_index(i); - if (max != i - 1) - swap(i - 1, max); - } - - return to_be_sorted; - } - - int max_element_index(int limit) - { - int max = 0; - for (int i = 1; i < limit; i++) - if (to_be_sorted.get(i) > to_be_sorted.get(max)) - max = i; - return max; - } -} diff --git a/searching_methods/src/algorithms/sorting_algorithm.java b/searching_methods/src/algorithms/sorting_algorithm.java deleted file mode 100644 index 34f95675..00000000 --- a/searching_methods/src/algorithms/sorting_algorithm.java +++ /dev/null @@ -1,38 +0,0 @@ -package algorithms; - -import java.util.ArrayList; - -public abstract class sorting_algorithm -{ - ArrayList to_be_sorted; - - sorting_algorithm() - { - this.to_be_sorted = new ArrayList<>(); - } - - sorting_algorithm(ArrayList to_be_sorted) - { - this.to_be_sorted = new ArrayList<>(to_be_sorted.size()); - this.to_be_sorted.addAll(to_be_sorted); - } - public void change_source(ArrayList to_be_sorted) - { - this.to_be_sorted = new ArrayList<>(to_be_sorted.size()); - this.to_be_sorted.addAll(to_be_sorted); - } - - void swap(int index_1, int index_2) - { - int temp = to_be_sorted.get(index_1); - to_be_sorted.set(index_1, to_be_sorted.get(index_2)); - to_be_sorted.set(index_2, temp); - } - public ArrayList sort_out(ArrayList to_be_sorted) - { - change_source(to_be_sorted); - return sort_out(); - } - - abstract public ArrayList sort_out(); -} diff --git a/searching_methods/src/testing/main.java b/searching_methods/src/testing/main.java deleted file mode 100644 index 14206c2f..00000000 --- a/searching_methods/src/testing/main.java +++ /dev/null @@ -1,56 +0,0 @@ -package testing; - -import algorithms.*; - -import java.util.ArrayList; -import java.util.Random; - -public class main -{ - static Random seed = new Random(); - static ArrayList randomize_new_array(int size) - { - ArrayList new_array = new ArrayList(size); - for (int i = 0; i < size; i++) new_array.add(seed.nextInt()); - return new_array; - } - - static void print_array_out(ArrayList array) - { - for (Integer integer : array) - System.out.println(integer); - - System.out.println("🧶-----------------------🐈"); - } - - static float estimate_sorting_duration(sorting_algorithm algorithm, ArrayList> arrays) - { - if(arrays.isEmpty()) return -1; - - long start_time = System.currentTimeMillis(); - - for (ArrayList array : arrays) - algorithm.sort_out(array); - - return (float)(System.currentTimeMillis() - start_time) / arrays.size(); - } - - public static void main(String args[]) - { - int number_of_tests = 10, test_size = 10000; - ArrayList> set_of_tests = new ArrayList>(number_of_tests); - - for (int i = 0; i < number_of_tests; i++) - set_of_tests.add(randomize_new_array(test_size)); - - bubble bubble_sort = new bubble(); - insert insert_sort = new insert(); - select select_sort = new select(); - quick quick_sort = new quick(); - - System.out.println(estimate_sorting_duration(bubble_sort, set_of_tests) + " [ms]"); - System.out.println(estimate_sorting_duration(insert_sort, set_of_tests) + " [ms]"); - System.out.println(estimate_sorting_duration(select_sort, set_of_tests) + " [ms]"); - System.out.println(estimate_sorting_duration(quick_sort, set_of_tests) + " [ms]"); - } -} From ae4fa6a66f7ed506c8843c4025ca3ee60f03dd3d Mon Sep 17 00:00:00 2001 From: Kuf <250875@student.pwr.edu.pl> Date: Tue, 9 Jun 2020 21:05:17 -0500 Subject: [PATCH 2/2] Lets make bitcoin great again! --- l4.py | 219 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 l4.py diff --git a/l4.py b/l4.py new file mode 100644 index 00000000..98b7808a --- /dev/null +++ b/l4.py @@ -0,0 +1,219 @@ +import json +import time +import requests +import string +from operator import itemgetter + +market_names = ['bitstamp.net','cex.io','bitbay.net','bittrex.com'] + +trade_pairs = ["BTCPLN","BTCUSD","ETHEUR","ETHBTC"] + +fees = {'bitstamp.net': 0.005,'cex.io': 0.0025, 'bitbay.net': 0.0043,'bittrex.com':0.002} + + #bid to kupno, ask to sprzedaz pierwszej z walut w parze +def accept_bid(wallet, trade_pair, rate, ammount, fee): + wallet[trade_pair[3:]] = wallet[trade_pair[3:]] - (ammount * 1 / rate) + wallet[trade_pair[:3]] = wallet[trade_pair[:3]] + (ammount - ammount * fee) + +def accept_ask(wallet, trade_pair, rate, ammount, fee): + wallet[trade_pair[3:]] = wallet[trade_pair[3:]] + (ammount * rate - ammount * rate * fee) + wallet[trade_pair[:3]] = wallet[trade_pair[:3]] - ammount + + + #operation_name np. "ticker" albo "orderbook" - zwraca odpowiedni slownik otrzymany od API +def get_resources(operation_name, market_name, trade_pair): + + url = "" + + if market_name == 'bitbay.net': + url = 'https://bitbay.net/API/Public/' + trade_pair + '/' + operation_name + '.json' + + if market_name == 'bittrex.com': + url = 'https://api.bittrex.com/api/v1.1/public/get' + operation_name + '?market=' + trade_pair[:3]+ '-' + trade_pair[3:] + if operation_name == "orderbook": + url = url + "&type=both" + + if market_name == 'bitstamp.net': + url = 'https://www.bitstamp.net/api/v2/' + if operation_name == "orderbook": + url = url + "order_book" + else: + url = url + operation_name + url = url + '/' + trade_pair[:3].lower() + trade_pair[3:].lower() + '/' + + if market_name == 'cex.io': + url = 'https://cex.io/api/' + if operation_name == "orderbook": + url = url + "order_book" + else: + url = url + operation_name + url = url +'/' + trade_pair[:3]+ "/" + trade_pair[3:] + if url == "": + print("URL failed") + return {} + + try: + recieved_dict = json.loads(str(requests.get(url).json()).replace('\'', '\"')) + except ValueError as e: + return {} + #print(str(recieved_dict)) + return recieved_dict + + +def best_offers(market, trade_pair): + resource_dict = get_resources("ticker",market,trade_pair) + if 'bid' not in resource_dict.keys(): + return {} + amounts_pair = ammounts(market, trade_pair) + return{"ask":resource_dict['ask'],"ask_amount":amounts_pair[0],"bid":resource_dict['bid'],"bid_amount":amounts_pair[1]} + +def sort_offers(list_to_sort,reverseOrder=False): + sorted_list = sorted(list_to_sort,key=itemgetter(0),reverse=reverseOrder) + return sorted_list + + +def ammounts(market, trade_pair): #Zwraca pare - (ilosc w najlepszym asku,ilosc w najlepszym bidzie) + + resource_dict = get_resources("orderbook",market, trade_pair) + if 'asks' not in resource_dict.keys(): + return (-1,-1) + sorted_asks = sort_offers(resource_dict['asks']) + sorted_bids = sort_offers(resource_dict['bids'],reverseOrder=True) + return (sorted_asks[0][1],sorted_bids[0][1]) + + +def arbitrage(market1, market2, trade_pair): #zwraca trojke(x,y,z, ilosc) x -> -1 jesli + #niemozliwy arbitraz oraz 1 lub 2 mowiace + market1_values = best_offers(market1, trade_pair) # w ktora strone zadziala arbitraz (gdzie kupic) + market2_values = best_offers(market2, trade_pair) #y i z to odpowiednio ceny kupna i sprzedazy + if market1_values == {} or market2_values == {}: # ilosc to ilosc waluty jaka mozna zamienic + return (-1,0,0,0) + fee1 = fees[market1] + fee2 = fees[market2] +# print(str(market1_values['bid'])) + sell1 = float(market1_values['bid'])-float(market1_values['bid'])*fee1 + sell2 = float(market2_values['bid'])-float(market2_values['bid'])*fee2 + buy1 = float(market1_values['ask'])+float(market1_values['ask'])*fee1 + buy2 = float(market2_values['ask'])+float(market2_values['ask'])*fee2 + if buy1 < sell2: + return (1,buy1,sell2,min(float(market1_values['bid_amount']),float(market2_values['ask_amount']))) + if buy2 < sell1: + return (2,buy2,sell1,min(float(market1_values['ask_amount']),float(market2_values['bid_amount']))) + return (-1,0,0,0) + + +def print_arbitrage(arbitrage_tuple, market1, market2, pair): + if arbitrage_tuple[0] == 1: + source_market = market1 + destination_market = market2 + else: + source_market = market2 + destination_market = market1 + print("Arbitrage possible with fees included! Buy on market " + source_market + " " + str(arbitrage_tuple[3]) + " of " + str(pair[:3]) + " with ") + print(pair[3:] + " with price " + str(arbitrage_tuple[1]) + " and sell on market " + destination_market + " with price " + str(arbitrage_tuple[2])) + +def run_arbitrage_checker(): + while True: + for x in range(len(market_names)): + for y in range(x+1,len(market_names)): + for pair in trade_pairs: + arb = arbitrage(market_names[x],market_names[y],pair) + if arb[0] != -1: + print_arbitrage(arb,market_names[x],market_names[y],pair) + time.sleep(0.5) + + + +# Z4 + +#Najpierw znalezc pare z duzymi odchyleniami +#Dzialac to bedzie na tej zasadzie ze liczy srednia i liczy laczne odchylenia od tej sredniej +# +#Po znalezieniu takiej pary +#Pozniej, jezeli od 10 iteracji lub wiecej cena best aska spada to czekamy az zacznie rosnac (minimum lokalne) i kupujemy +#Jednoczesnie, jezeli od 10 iteracji lub wiecej cena best bida rosnie to czekamy az zacznie spadac (maximum lokalne) i sprzedajemy +#Jednoczesnie drugim wymogiem jest ze wartosci musza byc odpowiednoi minimum i maximum na przedziale ostatnich 1000 ofert + +def find_pair_with_biggest_deviation(market,iterations): + #Algorytm dziala w obrebie 100 iteracji + asks = {} + bids = {} + for pair in trade_pairs: + asks[pair] = [] + bids[pair] = [] + for i in range(iterations): + for pair in trade_pairs: + resources_dict = best_offers(market,pair) + asks[pair].append(resources_dict['ask']) + bids[pair].append(resources_dict['bid']) + time.sleep(0.1) + best_pair = trade_pairs[0] + biggest_deviation_sum = 0.0 + for pair in trade_pairs: + average_ask = sum(asks[pair]) / len(asks[pair]) + average_bid= sum(bids[pair]) / len(bids[pair]) + sum_of_deviations_asks = 0.0 + sum_of_deviations_bids = 0.0 + for ask in asks[pair]: + sum_of_deviations_asks += abs(ask - average_ask) + for bid in bids[pair]: + sum_of_deviations_bids += abs(bid - average_bid) + if sum_of_deviations_asks + sum_of_deviations_bids > biggest_deviation_sum: + biggest_deviation_sum = sum_of_deviations_asks + sum_of_deviations_bids + best_pair = pair + return pair + + #Step - czas w sekundach miedzy iteracjami +def run_speculation_agent(wallet, market,iterations_to_specify_deviated_pair,iterations_to_start,step): + pair = find_pair_with_biggest_deviation(market,iterations_to_specify_deviated_pair) + print("Chosen pair with biggest deviation! The chosen pair is " + pair) + asks = [] + bids = [] + for step in range(iterations_to_start): + resources_dict = best_offers(market,pair) + asks.append(resources_dict['ask']) + bids.append(resources_dict['bid']) + last_iteration_ask = asks[len(asks)-1] + last_iteration_bid = bids[len(bids)-1] + times_ask_is_lowering = 0 + times_bid_is_increasing = 0 + while True: + resources_dict = best_offers(market,pair) + ask = resources_dict['ask'] + bid = resources_dict['bid'] + asks.append(ask) + bids.append(bid) + + if times_ask_is_lowering > 10: + if ask < last_iteration_ask and ask == min(asks[-1000:]): + accept_ask(wallet, pair, ask, min(wallet[pair[:3]],resources_dict['ask_amount']) , fees[market]) + print("Minimum of asks found! Decision made - accepting ask for price: " + ask + " ammount possible to buy: " + min(wallet[pair[:3]],resources_dict['ask_ammount'])) + print("Wallet state after transaction: " + str(wallet)) + + if times_bid_is_increasing > 10: + if bid > last_iteration_bid and bid == max(bids[-1000:]): + accept_bid(wallet, pair, bid, min(wallet[pair[3:]],resources_dict['bid_amount']) , fees[market]) + + print("Maximum of bids found! Decision made - accepting bid for price: " + bid + " ammount possible to buy: " + min(wallet[pair[3:]],resources_dict['bid_ammount'])) + print("Wallet state after transaction: " + str(wallet)) + if ask < last_iteration_ask: + times_ask_is_lowering += 1 + else: + times_ask_is_lowering = 0 + + if bid > last_iteration_bid: + times_bid_is_increasing += 1 + else: + times_bid_is_increasing = 0 + + last_iteration_ask = ask + last_iteration_bid = bid + + time.sleep(step) + +def main(): + wallet = {"ETH": 100.0, "USD": 100.0, "LTC": 100.0, "BTC": 100.0, "EUR": 100.0,"PLN" : 100.0} +# run_arbitrage_checker() + run_speculation_agent(wallet,"bitbay.net",10,10,0.1) + +main() \ No newline at end of file