def wait_cancel_orders(): global reb_state global cycle_count global trigger_time orders_canceled_bool = True try: bc = BTCChina(access_key,secret_key) btcc_orders = bc.get_orders() for item in btcc_orders['order']: orders_canceled_bool = bc.cancel(item['id']) except: orders_canceled_bool = False if orders_canceled_bool: print('all orders canceled or no orders present') else: reb_state = 'r' cycle_count = rebalance_interval print('not all orders canceled (try loop)') trigger_time = time.time() + 1
def rebalance(): global trigger_time strftime("%Y-%m-%d %H:%M:%S", gmtime()) try: bc = BTCChina(access_key,secret_key) btcc_market_depth = bc.get_market_depth2() btcc_account_info = bc.get_account_info() best_ask_depth = btcc_market_depth['market_depth']['ask'] best_bid_depth = btcc_market_depth['market_depth']['bid'] best_bid = Decimal(best_bid_depth[0]['price']) best_ask = Decimal(best_ask_depth[0]['price']) frozen_cny = Decimal(btcc_account_info['frozen']['cny']['amount']) balance_cny = Decimal(btcc_account_info['balance']['cny']['amount']) frozen_btc= Decimal(btcc_account_info['frozen']['btc']['amount']) balance_btc = Decimal(btcc_account_info['balance']['btc']['amount']) print("best bid: " ,ceil(best_bid*100)/100.0, " best ask: ", ceil(best_ask*100)/100.0, " total cny: ", frozen_cny + balance_cny, " totab btc: ",frozen_btc + balance_btc) total_btc = frozen_btc + balance_btc total_cny = frozen_cny + balance_cny mid = Decimal(ceil( ( ( best_bid + best_ask ) /2 ) * 100 ) / 100.0) #pdb.set_trace() bidq = ( total_cny - ( ( total_cny + total_btc * mid )/ 2 ) ) / mid askq = ( - total_cny + ( ( total_cny + total_btc * mid ) / 2 ) ) / mid bidp = mid askp = mid current_buyp = floor(bidp*1000)/Decimal(1000.0) current_buyq = floor(bidq*1000)/Decimal(1000.0) if Decimal(current_buyq) >= Decimal(0.00099): bc.buy(current_buyp ,current_buyq) print('price:',current_buyp ,' quantity: ', current_buyq) current_sellp = ceil(askp*1000)/Decimal(1000.0) current_sellq = floor(askq*1000)/Decimal(1000.0) if Decimal(current_sellq) >= Decimal(0.00099): bc.sell(current_sellp ,current_sellq ) print('price:', current_sellp, ' quantity: ', current_sellq ) except: print('not (completely) rebalanced') trigger_time = time.time() + 1