def get_balances(self): balances = {} balances['huobi_main'] = huobi_main_client.Huobi_Main_Client( ).getAccountInfo() balances['huobi_eth'] = huobi_eth_client.Huobi_ETH_Client().getBalance( ) balances['poloniex'] = poloniex_client.Poloniex_Client( ).get_available_balances() return balances
def transfer_eth_from_huobi_to_poloniex(self, amount=''): # INVALID: the method of withdraw eth needs trade pw which is not yet supported by the server print('Start Transferring ETH from Huobi to Poloniex...') # get deposit address from poloniex and make sure it is the same as the recorded one print('Getting Poloniex deposit address:') poloniexDepositAddress = poloniex_client.Poloniex_Client( ).client.returnDepositAddresses()['ETH'] print('Deposit address returned from Poloniex: %s' % poloniexDepositAddress) print('Deposit address in local record: %s' % POLONIEX_ETH_DEPOSIT_ADDRESS) isAddressSame = poloniexDepositAddress == POLONIEX_ETH_DEPOSIT_ADDRESS if isAddressSame: print('Deposit address verification result: PASS!') else: print( 'Deposit address verification result: FAILED! Please check Poloniex deposit address manually and update in local record!' ) raise BaseException('Deposit address not the same as record') # transfer amount from Huobi to Poloniex print('Transferring ETH from Huobi to Poloniex:') # get address id in eth reserved withdraw address addresses = huobi_eth_client.Huobi_ETH_Client( ).get_eth_withdraw_addresses() # print(addresses) address_id = '' for address in addresses: if ('0x' + address['address']) == POLONIEX_ETH_DEPOSIT_ADDRESS: address_id = address['id'] break if not address_id: raise BaseException('Address id not found!') # transfer using withdraw address id withdraw_id = huobi_eth_client.Huobi_ETH_Client().withdraw_eth_create( address_id, amount='0.01') status = huobi_eth_client.Huobi_ETH_Client().withdraw_eth_place( withdraw_id) if status['status'] == 'ok': print('Transfer status: OK! Status: %s' % status) else: print('Transfer status: ERROR! Status: %s' % status) raise BaseException('Transfer error! Status: %s' % status) # return status withdraw_id = status['data'] print('Transfer SUCCESS! Amount: %s BTC, Withdraw_ID: %s' % (amount, withdraw_id)) return withdraw_id
def transfer_btc_from_poloniex_to_huobi(self, amount=''): print('Start Transferring BTC from Poloniex to Huobi...') # get deposit address from huobi and make sure it is the same as the recorded one print('Getting Huobi deposit address:') print('Deposit address in local record: %s' % HUOBI_BTC_DEPOSIT_ADDRESS) print( 'Since the address cannot be verified, please go to the website to verify the address!' ) # send email about the transfer address try: print('Sending caution email:') email_client.Email_client().notify_me_by_email( title='Caution about BTC deposit address in Huobi.', content= 'BTC is being transferred from Poloniex to Huobi.\n Amount: %s \nBTC deposit address of Huobi cannot be verified. Please check manually.' % amount) print('Email sent successfully!') except: print('Email sent FAILED.') # transfer amount from Poloniex to Huobi print('Transferring BTC from Poloniex to Huobi:') status = poloniex_client.Poloniex_Client().client.withdraw( currency='BTC', amount=amount, address=HUOBI_BTC_DEPOSIT_ADDRESS) print('Returned response: \"%s\"' % status['response']) print('Expected response: \"%s\"' % 'Withdrew %.8f BTC.' % float(amount)) if status['response'] == 'Withdrew %.8f BTC.' % float(amount): print('Transfer status: OK! Status: %s' % status) else: print('Transfer status: ERROR! Status: %s' % status) raise BaseException('Transfer error! Status: %s' % status) # return status print('Transfer SUCCESS! Amount: %s BTC' % amount) return True
def transfer_btc_from_huobi_to_poloniex(self, amount=''): print('Start Transferring BTC from Huobi to Poloniex...') # get deposit address from poloniex and make sure it is the same as the recorded one print('Getting Poloniex deposit address:') poloniexDepositAddress = poloniex_client.Poloniex_Client( ).client.returnDepositAddresses()['BTC'] print('Deposit address returned from Poloniex: %s' % poloniexDepositAddress) print('Deposit address in local record: %s' % POLONIEX_BTC_DEPOSIT_ADDRESS) isAddressSame = poloniexDepositAddress == POLONIEX_BTC_DEPOSIT_ADDRESS if isAddressSame: print('Deposit address verification result: PASS!') else: print( 'Deposit address verification result: FAILED! Please check Poloniex deposit address manually and update in local record!' ) raise BaseException('Deposit address not the same as record') # transfer amount from Huobi to Poloniex print('Transferring BTC from Huobi to Poloniex:') status = huobi_main_client.Huobi_Main_Client( ).withdraw_btc_to_btc_address(poloniexDepositAddress, amount) if status['code'] == 200 and status['message'] == 'OK': print('Transfer status: OK! Status: %s' % status) else: print('Transfer status: ERROR! Status: %s' % status) raise BaseException('Transfer error! Status: %s' % status) # return status withdraw_id = status['withdraw_coin_id'] print('Transfer SUCCESS! Amount: %s BTC, Withdraw_ID: %s' % (amount, withdraw_id)) return withdraw_id
def execute_positive_premium_arbitrage(self, eth_trade_amount=''): # return execution status print('=' * 50 + ' S T A R T ' + '=' * 50) print('Executing positive premium arbitrage:') # Phase 0: record balances print('\nPhase 0: Record balances:') balances_old = self.get_balances() print('Current balances (before arbitrage): %s' % balances_old) # Phase 1: get balances and calculate amount print('\nPhase 1: Get balances and verify trade amount:') # test_amount = '0.01' # test amount is 1 ETH # eth_trade_amount = test_amount print('ETH trade amount: %s' % eth_trade_amount) eth_balance_huobi = huobi_eth_client.Huobi_ETH_Client().getETHBalance() print('Current ETH balance in Huobi: %s' % eth_balance_huobi) btc_balance_poloniex = poloniex_client.Poloniex_Client().get_balance( 'BTC') print('Current BTC balance in poloniex: %s' % btc_balance_poloniex) eth_affordable_amount = float(btc_balance_poloniex) / float( poloniex_client.Poloniex_Client().client.returnTicker()['BTC_ETH'] ['lowestAsk']) print('Affordable ETH amount in poloniex: %.4f' % eth_affordable_amount) eth_max_amount = min(float(eth_balance_huobi), eth_affordable_amount) print('Maximum ETH purchase amount: %.4f ' % eth_max_amount) if float(eth_trade_amount) < eth_max_amount: print('Balance verification: PASS!') else: print('Not enough balance to trade!') raise BaseException('Insufficient trade balance !') # Phase 2: Sell ETH in huobi and buy ETH in poloniex print('\nPhase 2: Sell ETH in huobi and buy ETH in poloniex') print('Selling ETH in huobi: amount = %s' % eth_trade_amount) eth_sell_order_detail = huobi_eth_operator.ETH_operator().sellETH( amount=eth_trade_amount) cny_transfer_amount = '%.2f' % float( eth_sell_order_detail['field-cash-amount']) print('Transferring CNY to huobi main account: amount = %s' % cny_transfer_amount) huobi_eth_client.Huobi_ETH_Client().transfer_cny_to_main( amount=cny_transfer_amount) print('Buying ETH in poloniex:') eth_purchase_order_status = poloniex_client.Poloniex_Client().buy_ETH( amount=eth_trade_amount) btc_spent_in_eth_purchase = sum([ float(subtrade['total']) for subtrade in eth_purchase_order_status['resultingTrades'] ]) btc_trade_amount = str('%.4f' % float(btc_spent_in_eth_purchase)) print('Buying BTC in huobi: amount = %s' % btc_trade_amount) huobi_main_operator.Huobi_Main_Operator().buy_BTC( amount=btc_trade_amount) # Phase 3: Transfer BTC and ETH back print('\nPhase 3: Transfer BTC and ETC back') print('Transferring BTC back to Poloniex: amount = %s' % btc_trade_amount) if float(btc_trade_amount) <= 0.1: print( 'The BTC amount for transfer is too small! No need to transfer.\n(Huobi requires at least 0.01 BTC for transfer.)' ) else: transfer_btc.Transfer_BTC().transfer_btc_from_huobi_to_poloniex( amount=btc_trade_amount) print('Transferring ETH back to Huobi: amount = %s' % eth_trade_amount) if float(eth_trade_amount) <= 1.0: print( 'The ETH amount for transfer is too small! No need to transfer.\n' ) else: transfer_eth.Transfer_ETH().transfer_eth_from_poloniex_to_huobi( amount=eth_trade_amount) # Report arbitrage print('Arbitrage successfully proceeded! ETH amount = %s' % eth_trade_amount) # Phase 4: Report print('\nPhase 4: Report') print('Getting balances:') balances_new = self.get_balances() print('Current balances (after arbitrage): %s' % balances_new) profit_report = str( self.cal_profits(balances_old=balances_old, balances_new=balances_new)) print('Profit Report: \n%s' % profit_report) print('Report by email...') try: email_client.Email_client().notify_me_by_email( title= 'Arbitrage (ETH, positive premium) successfully proceeded!', content= 'ETH amount: %s \nOld balances: \n%s \nNew balances: \n%s\n\n Profit Report: \n %s' % (eth_trade_amount, balances_old, balances_new, profit_report)) print('Email was sent.') except: print('Email was not sent.') print('=' * 50 + ' E N D ' + '=' * 50) return 'Execution status: SUCCESS'