def run(key_file, buy_floor, sell_ceiling, live_trades): # Load the keys and create an API object from the first one. handler = btceapi.KeyHandler(key_file) key = handler.getKeys()[0] print "Trading with key %s" % key api = btceapi.TradeAPI(key, handler) # Create a trader that handles LTC/USD trades in the given range. trader = RangeTrader(api, "ltc_usd", buy_floor, sell_ceiling, live_trades) # Create a bot and add the trader to it. bot = btcebot.Bot() bot.addTrader(trader) # Add an error handler so we can print info about any failures bot.addErrorHandler(onBotError) # The bot will provide the traders with updated information every # 15 seconds. bot.setCollectionInterval(15) bot.start() print "Running; press Ctrl-C to stop" try: while 1: # you can do anything else you prefer in this loop while # the bot is running in the background time.sleep(3600) except KeyboardInterrupt: print "Stopping..." finally: bot.stop()
def __init__(self): #get params #TODO: make this a param self.key_file = "/home/bob/Documents/btce_bot/trade_api.key" self.handler = btceapi.KeyHandler(self.key_file, resaveOnDeletion=True) self.conn = btceapi.BTCEConnection() self.topic_names_init = False self.topics = deque([]) # self.msg = get_info() #intiate a bunch of services get_info_service = rospy.Service('get_info_service', get_info_all, self.call_get_info) trans_history_service = rospy.Service('trans_history_service', trans_history_all, self.call_trans_history) active_orders_service = rospy.Service('active_orders_service', active_orders_all, self.call_active_orders) cancel_order_service = rospy.Service('cancel_order_service', cancel_order, self.call_cancel_order) make_trade_service = rospy.Service('make_trade_service', make_trade, self.call_make_trade) #trade_history_service = rospy.Service('trade_history_service', trade_history_all, self.call_trade_history) rospy.spin()
def __init__(self, keyfile, shared_data): self.handler = btceapi.KeyHandler(keyfile) try: self.key = self.handler.getKeys()[0] except IndexError: print( "Error: something's wrong with keyfile. Looks like it's empty") exit(1) self.api = btceapi.TradeAPI(self.key, self.handler) self.update_balance(prnt=True) # Trade all available money on the following condition if shared_data.trading_sum >= self.usd or shared_data.trading_sum <= 0: print("Trading all available money") self.trade_all = True else: self.trade_all = False # Check if we are able to trade at all with current sums if self.usd < shared_data.trading_sum \ and self.btc < self.min_amount("sell", shared_data.price): print("Not enough funds for real trading. Activating simulation") shared_data.real_trading = False else: # Define initial action. Buy has priority. # If enough USD if self.usd >= shared_data.trading_sum \ and self.usd >= self.min_amount("buy", shared_data.price): print("Looking to buy") self.next_action = "buy" # Else, if enough BTC - sell elif self.btc >= self.min_amount("sell", shared_data.price): print("Looking to sell") self.next_action = "sell"
def run(key_file, pair, buy_margin, sell_margin, logging, loggingTime, printing, live_trades, gamma, eraSec, alfa, beta, buyChunk, sellChunk, buyBudget, sellBudget, interval, lastMinutes, epsilonSize, singleMode): #Load the keys and create an API object from the first one. handler = btceapi.KeyHandler(key_file, resaveOnDeletion=True) key = handler.getKeys()[0] print "Trading with key %s" % key api = btceapi.TradeAPI(key, handler=handler) # Create a trader trader = RangeTrader(api, pair, buy_margin, sell_margin, logging, loggingTime, printing, live_trades, gamma, eraSec, alfa, beta, buyChunk, sellChunk, buyBudget, sellBudget, lastMinutes, epsilonSize, singleMode) # Create a bot and add the trader to it. bot = btcebot.Bot() bot.addTrader(trader) # Add an error handler so we can iPrint( info about any failures bot.addErrorHandler(onBotError) # The bot will provide the traders with updated information every # 15 seconds. bot.setCollectionInterval(interval) bot.start() print "Running; press Ctrl-C to stop" try: while 1: # you can do anything else you prefer in this loop while # the bot is running in the background time.sleep(96000) except KeyboardInterrupt: print "Stopping..." finally: bot.stop() cancel_all_active_orders_of_pair(api, pair)
def __init__(self, keyfile): keyfile = os.path.abspath(keyfile) self.keyhandler = btceapi.KeyHandler(keyfile) key = self.keyhandler.getKeys()[0] self.conn = btceapi.BTCEConnection() self.api = btceapi.TradeAPI(key, self.keyhandler) super(BTCE, self).__init__() self.name = 'BTCE' self.trading_fee = 0.002
def __init__(self, pair, key, collectionInterval=1, bufferSpanMinutes=10): self.bufferSpanMinutes = bufferSpanMinutes handler = btceapi.KeyHandler(key) key = handler.getKeys()[0] print "Trading with key %s" % key self.api = btceapi.TradeAPI(key, handler) self.type = pair self.fee_adjustment = 1 - float(btceapi.getTradeFee(self.type)) / 10 self.collectionInterval = collectionInterval self.running = False self.bidHistory = {} self.askHistory = {} self.asks = [] self.bids = []
def __init__(self, keypath): keyfile = os.path.abspath(keypath) self.keyhandler = btceapi.KeyHandler(keyfile) key = self.keyhandler.getKeys()[0] self.conn = btceapi.BTCEConnection() self.api = btceapi.TradeAPI(key, self.keyhandler) self.name = 'BTCE' self.trading_fee = 0.002 # The fee is 0.2% for all pairs, maker and taker self.bid_fee = self.trading_fee self.ask_fee = self.trading_fee self.tradeable_pairs = self.get_tradeable_pairs() self.minimum_amount = {} self.decimal_places = {} self.get_info()
def trade(opportunity, max_volume, key_file, prices, volumes, tax): handler = btceapi.KeyHandler(key_file, resaveOnDeletion=True) volume = max_volume keys = handler.getKeys() key = keys[0] t = btceapi.TradeAPI(key, handler=handler) for operation in opportunity: pair = operation[1] price = float(prices[operation[0]][operation[1]]) if operation[0] == "bid": results = t.trade(pair, "sell", price, volume) volume *= price * tax if operation[0] == "ask": volume /= price results = t.trade(pair, "buy", price, volume) volume *= tax
#!/usr/bin/python import sys import btceapi if len(sys.argv) < 2: print "Usage: compute-account-value.py <key file>" print " key file - Path to a file containing key/secret/nonce data" sys.exit(1) key_file = sys.argv[1] with btceapi.KeyHandler(key_file, resaveOnDeletion=True) as handler: for key in handler.getKeys(): print "Computing value for key %s" % key # NOTE: In future versions, the handler argument will be required. conn = btceapi.BTCEConnection() t = btceapi.TradeAPI(key, handler) try: r = t.getInfo(connection=conn) exchange_rates = {} for pair in btceapi.all_pairs: asks, bids = btceapi.getDepth(pair) exchange_rates[pair] = bids[0][0] btc_total = 0 for currency in btceapi.all_currencies: balance = getattr(r, "balance_" + currency) if currency == "btc": print "\t%s balance: %s" % (currency.upper(), balance)
# This sample shows use of a KeyHandler. For each API key in the file # passed in as the first argument, all pending orders for the specified # pair and type will be canceled. if len(sys.argv) < 4: print "Usage: cancel_orders.py <key file> <pair> <order type>" print " key file - Path to a file containing key/secret/nonce data" print " pair - A currency pair, such as btc_usd" print " order type - Type of orders to process, either 'buy' or 'sell'" sys.exit(1) key_file = sys.argv[1] pair = sys.argv[2] order_type = unicode(sys.argv[3]) handler = btceapi.KeyHandler(key_file) for key in handler.keys: print "Canceling orders for key %s" % key t = btceapi.TradeAPI(key, handler) try: # Get a list of orders for the given pair, and cancel the ones # with the correct order type. orders = t.orderList(pair=pair) for o in orders: if o.type == order_type: print " Canceling %s %s order for %f @ %f" % ( pair, order_type, o.amount, o.rate) t.cancelOrder(o.order_id)
"Usage: place-order.py <key file> <pair> <order type> <amount> <price>" ) print(" key file - Path to a file containing key/secret/nonce data") print(" pair - A currency pair, such as btc_usd") print(" order type - Type of orders to process, either 'buy' or 'sell'") print(" amount - Amount of currency in order") print(" price - Order price") sys.exit(1) key_file = sys.argv[1] pair = sys.argv[2] order_type = sys.argv[3] amount = decimal.Decimal(sys.argv[4]) price = decimal.Decimal(sys.argv[5]) with btceapi.KeyHandler(key_file) as handler: if not handler.keys: print("No keys in key file.") else: for key in handler.keys: print("Placing order for key {}".format(key)) with btceapi.BTCEConnection() as connection: t = btceapi.TradeAPI(key, handler, connection) try: result = t.trade(pair, order_type, price, amount) print("Trade result:") print(" received: {0}".format(result.received)) print(" remains: {0}".format(result.remains))
def setUp(self): self.key_handler = btceapi.KeyHandler(TEST_KEY_FILE) self.connection = btceapi.BTCEConnection()