def do_wait(): try: print '.', time.sleep(2) f_currency.refresh_and_get_free_currency() except: f_print.print_exception(sys._getframe().f_code.co_name)
def get_zone(last_preis, avg, low, high): try: # Zone 5 # # xPrice[4]------------------------------high # # Zone 4 # # xPrice[3]-----------------------------------# # Zone 3 # # xPrice[2]-------------------------------avg # # Zone 2 # # xPrice[1]-----------------------------------# # Zone 1 # # xPrice[0]-------------------------------low # # Zone 0 # xPrice = {} xPrice[0] = low xPrice[1] = low + (avg - low) * 0.5 xPrice[2] = avg xPrice[3] = high - (avg - low) * 0.5 xPrice[4] = high z = 0 if (last_preis > xPrice[0]) and (last_preis <= xPrice[1]): z = 1 elif (last_preis > xPrice[1]) and (last_preis <= xPrice[2]): z = 2 elif (last_preis > xPrice[2]) and (last_preis <= xPrice[3]): z = 3 elif (last_preis > xPrice[3]) and (last_preis <= xPrice[4]): z = 4 elif (last_preis > xPrice[4]): z = 5 except: f_print.print_exception(sys._getframe().f_code.co_name) return z
def do_trade(active_trade_pair): try: ini(active_trade_pair) # refresh for new pair #f_print.print_info() f_trade.start_trade() except: f_print.print_exception(sys._getframe().f_code.co_name)
def add_fix_prices(pair_nr, price_to_trade): try: if (f_currency.get_first_currency(pair_nr) == BTC) and (f_currency.get_second_currency(pair_nr) == USD): price_to_trade[len(price_to_trade)] = BTC_USD_FIX_1 price_to_trade[len(price_to_trade)] = BTC_USD_FIX_2 elif (f_currency.get_first_currency(pair_nr) == BTC) and (f_currency.get_second_currency(pair_nr) == RUB): price_to_trade[len(price_to_trade)] = BTC_RUB_FIX_1 price_to_trade[len(price_to_trade)] = BTC_RUB_FIX_2 elif (f_currency.get_first_currency(pair_nr) == ETH) and (f_currency.get_second_currency(pair_nr) == USD): price_to_trade[len(price_to_trade)] = ETH_USD_FIX_1 price_to_trade[len(price_to_trade)] = ETH_USD_FIX_2 elif (f_currency.get_first_currency(pair_nr) == ETH) and (f_currency.get_second_currency(pair_nr) == RUB): price_to_trade[len(price_to_trade)] = ETH_RUB_FIX_1 price_to_trade[len(price_to_trade)] = ETH_RUB_FIX_2 elif (f_currency.get_first_currency(pair_nr) == USD) and (f_currency.get_second_currency(pair_nr) == RUB): price_to_trade[len(price_to_trade)] = USD_RUB_FIX_1 price_to_trade[len(price_to_trade)] = USD_RUB_FIX_2 except: f_print.print_exception(sys._getframe().f_code.co_name) return price_to_trade
def get_max_and_min(pair_currency): try: depth = _ExmoAPI.get_depth(g.exmoAPI_instance, pair_currency) pairs_nr = pair_currency.get_pair_nr() pairs_url = PAIR_STRING[pairs_nr] pair = PAIR_STRING[pairs_nr] #.upper() typ = 'asks' rate = depth[pair][typ][0][0] aAsks[pairs_nr] = depth[pair][typ][0][0] typ = 'bids' rate = depth[pair][typ][0][0] aBids[pairs_nr] = depth[pair][typ][0][0] min = (aAsks[pairs_nr] + aBids[pairs_nr]) * 0.5 max = (aAsks[pairs_nr] + aBids[pairs_nr]) * 0.5 counter = 0 for orders in depth[pair][typ]: rate = orders[0] print rate if (rate > max): max = rate if (rate < min): min = rate print min, max return min, max except: f_print.print_exception(sys._getframe().f_code.co_name)
def ini(pair_nr): try: g.pair_instance = Pair( pair_nr) # make objekt from class Pair (price-data) _ExmoAPI.read_exmoAPI() # read API-Daten EXMO, push in pair_currency except: f_print.print_exception(sys._getframe().f_code.co_name)
def buy(avg_bid_ask, min_currency, pair, price): try: type_trade = 'buy' pair_nr, first_currency, second_currency = f_currency.get_current_currency( ) params = { "pair": pair.upper(), 'quantity': 0.999999 * min_currency / price, 'price': price, 'type': type_trade } id = _ExmoAPI.trade(g.exmoAPI_instance, params) list_open_new = _ExmoAPI.get_my_orders_id(g.exmoAPI_instance) diff_list = list(set(g.list_open_old) - set(list_open_new)) f_print.print_my_order_id(diff_list) g.list_open_old = list_open_new _ExmoAPI.get_trading_hystory(g.exmoAPI_instance, diff_list) f_string.write_str_trade(avg_bid_ask, first_currency, id, min_currency, pair_nr, price, second_currency, type_trade) except: f_print.print_exception(sys._getframe().f_code.co_name)
def refresh_and_get_free_currency(): try: _ExmoAPI.get_status(g.exmoAPI_instance, g.pair_instance) # refresh data free_currency = g.pair_instance.get_free_currency() except: f_print.print_exception(sys._getframe().f_code.co_name) return free_currency
def get_bid_and_ask(): try: avg_bid_ask = g.pair_instance.get_bid_ask()[AVG_BID_ASK_NR] aBid = g.pair_instance.get_bid_ask()[BID_NR] aAsk = g.pair_instance.get_bid_ask()[ASK_NR] except: f_print.print_exception(sys._getframe().f_code.co_name) return aBid, aAsk, avg_bid_ask
def start_sell_buy(type_trade): try: pair_nr, first_currency, second_currency = f_currency.get_current_currency( ) if (type_trade == 'sell'): currency = first_currency else: # 'buy' currency = second_currency free_currency = g.pair_instance.get_free_currency() aBid, aAsk, avg_bid_ask = f_currency.get_bid_and_ask() pair = PAIR_STRING[pair_nr] price_to_trade = {} price_to_trade = g.pair_instance.get_price_to_trade() price_to_trade[0] = g.pair_instance.get_min_price_to_sell() if (TACTIC_USE_ORDER_BOOK == 1): price_to_trade = add_ask_and_bid(pair_nr, price_to_trade) if (TACTIC_USE_FIX_PRICE == 1): price_to_trade = add_fix_prices(pair_nr, price_to_trade) len_price = len(price_to_trade) if (free_currency[currency] >= MIN_TO_TRADE[currency]): for i in range(100): if (i >= len_price): i = i - len_price if (price_to_trade[i] >= price_to_trade[0]): price = f_currency.check_aAsk(aAsk, price_to_trade[i], currency) min_currency = f_currency.check_free_min( free_currency[currency], MIN_TO_TRADE[currency]) if (type_trade == 'sell'): quantity = min_currency else: # buy quantity = 0.999999 * min_currency / price sell_buy(avg_bid_ask, min_currency, pair, price) free_currency = f_currency.refresh_and_get_free_currency() if (free_currency[currency] < MIN_TO_TRADE[currency]): break print '> sale-order added successfully' except: f_print.print_exception(sys._getframe().f_code.co_name) return
def get_current_currency(): pair_nr = -1 first_currency = -1 second_currency = -1 try: pair_nr = g.pair_instance.get_pair_nr() first_currency = get_first_currency(pair_nr) second_currency = get_second_currency(pair_nr) except: f_print.print_exception(sys._getframe().f_code.co_name) return pair_nr, first_currency, second_currency
def get_free_currency_nr(): try: free_currency_nr = -1 free_currency = g.pair_instance.free_currency nr = 0 # check all for nxt in free_currency: if (free_currency[nr] >= MIN_TO_TRADE[nr]): free_currency_nr = nr break nr += 1 except: f_print.print_exception(sys._getframe().f_code.co_name) return free_currency_nr
def add_ask_and_bid(pair_nr, price_to_trade): try: if (f_currency.get_first_currency(pair_nr) == BTC): lim_quantity = BTC_MAX_ASK_QUANTITY elif (f_currency.get_first_currency(pair_nr) == ETH): lim_quantity = ETH_MAX_ASK_QUANTITY else: lim_quantity = am_lim bid_price = get_vBids(lim_quantity) ask_price = get_vAsks(lim_quantity) price_to_trade[len(price_to_trade)] = bid_price price_to_trade[len(price_to_trade)] = ask_price except: f_print.print_exception(sys._getframe().f_code.co_name) return price_to_trade
def get_second_pair(pair_nr): try: f_currency = get_first_currency(pair_nr) s_currency = get_second_currency(pair_nr) second_pair = -1 if (f_currency == BTC) and (s_currency == USD): second_pair = ETH_RUB elif (f_currency == ETH) and (s_currency == USD): second_pair = BTC_RUB elif (f_currency == BTC) and (s_currency == RUB): second_pair = ETH_USD elif (f_currency == ETH) and (s_currency == RUB): second_pair = BTC_USD except: f_print.print_exception(sys._getframe().f_code.co_name) return second_pair
def get_active_trade_pair(pair_nr): try: active_trade_pair = -1 free_currency_nr = get_free_currency_nr() if (free_currency_nr >= 0): print print '> free currency ', f_string.get_str_currency( free_currency_nr) active_trade_pair = f_tactic.check_tactic(pair_nr, free_currency_nr) if (active_trade_pair >= 0): print '> active_trade_pair:', f_string.get_str_pairs_nr( active_trade_pair) else: time.sleep(10) else: active_trade_pair = -1 time.sleep(10) except: f_print.print_exception(sys._getframe().f_code.co_name) return active_trade_pair
def buy_currency(type_trade): try: pair_nr, first_currency, second_currency = f_currency.get_current_currency( ) free_currency = g.pair_instance.get_free_currency() aBid, aAsk, avg_bid_ask = f_currency.get_bid_and_ask() pair = PAIR_STRING[pair_nr] price_to_trade = {} price_to_trade = g.pair_instance.get_price_to_trade() price_to_trade[0] = g.pair_instance.get_max_price_to_buy() if (TACTIC_USE_ORDER_BOOK == 1): price_to_trade = add_ask_and_bid(pair_nr, price_to_trade) if (TACTIC_USE_FIX_PRICE == 1): price_to_trade = add_fix_prices(pair_nr, price_to_trade) len_price = len(price_to_trade) if (free_currency[second_currency] >= MIN_TO_TRADE[second_currency]): for i in range(100): if (i >= len_price): i = i - len_price if (price_to_trade[i] <= price_to_trade[0]): price = f_currency.check_aBid(aBid, price_to_trade[i], second_currency) min_currency = f_currency.check_free_min( free_currency[second_currency], MIN_TO_TRADE[second_currency]) buy(avg_bid_ask, min_currency, pair, price) free_currency = f_currency.refresh_and_get_free_currency() if (free_currency[second_currency] < MIN_TO_TRADE[second_currency]): break print '> buy-order added successfully' except: f_print.print_exception(sys._getframe().f_code.co_name) return 0
def start_trade(): try: pair_nr, first_currency, second_currency = f_currency.get_current_currency( ) free_currency = g.pair_instance.get_free_currency() print '>', time.strftime( "%d.%m.%Y %H:%M:%S"), #, 'start Trade', get_str_pairs_nr(pair_nr) if (free_currency[second_currency] >= MIN_TO_TRADE[second_currency]): type_trade = 'buy' print ' start buy', f_string.get_str_pairs_nr(pair_nr) buy_currency(type_trade) if (free_currency[first_currency] >= MIN_TO_TRADE[first_currency]): type_trade = 'sell' print ' start sell', f_string.get_str_pairs_nr(pair_nr) sell_currency(type_trade) print ' start', type_trade #start_sell_buy(type_trade) except: f_print.print_exception(sys._getframe().f_code.co_name)
def main(): try: g.exmoAPI_instance = ExmoAPI(MY_API_KEY, MY_API_SECRET) # EXMO-API-Connection except: f_print.print_exception(sys._getframe().f_code.co_name + ' ExmoAPI()') try: # make pair-number from first and second currency (ted.py) pair_nr = f_currency.get_pairs_nr(FIRST_CURRENCY, SECOND_CURRENCY) f_main.ini(pair_nr) f_print.print_pair() f_print.print_tactic(pair_nr) # active_trade_pair = pair_nr except: f_print.print_exception(sys._getframe().f_code.co_name + 'ini() + print()') print '> Bot: I am ready . . .', while (True): pair_nr = f_currency.get_pairs_nr(FIRST_CURRENCY, SECOND_CURRENCY) f_main.ini(pair_nr) # my_func.__name__ try: # active_trade_pair depending on the chosen tactics and depending on the free currency active_trade_pair = f_currency.get_active_trade_pair(pair_nr) if (active_trade_pair < 0): f_main.do_wait() else: try: f_main.do_trade(active_trade_pair) except: f_print.print_exception(sys._getframe().f_code.co_name + ' f_main.do_trade') except: f_print.print_exception(sys._getframe().f_code.co_name + ' while-loop') # raise sys.exit() # sys.exit("Exit") main()