def test_validateOrder(self): info = btceapi.APIInfo(self.connection) for pair in info.pair_names: t = random.choice(("buy", "sell")) a = random.random() if pair[4] == "btc": info.validate_order(pair, t, a, decimal.Decimal("0.001")) else: info.validate_order(pair, t, a, decimal.Decimal("0.1")) t = random.choice(("buy", "sell")) a = decimal.Decimal(str(random.random())) if pair[:4] == "btc_": self.assertRaises(btceapi.InvalidTradeAmountException, info.validate_order, pair, t, a, decimal.Decimal("0.0009999")) else: self.assertRaises(btceapi.InvalidTradeAmountException, info.validate_order, pair, t, a, decimal.Decimal("0.009999")) self.assertRaises(btceapi.InvalidTradePairException, info.validate_order, "foo_bar", "buy", decimal.Decimal("1.0"), decimal.Decimal("1.0")) self.assertRaises(btceapi.InvalidTradeTypeException, info.validate_order, "btc_usd", "foo", decimal.Decimal("1.0"), decimal.Decimal("1.0"))
def test_validateOrder(self): info = btceapi.APIInfo(self.connection) for pair_name, pair_data in info.pairs.items(): for t in ("buy", "sell"): info.validate_order(pair_name, t, pair_data.min_price, pair_data.min_amount) info.validate_order(pair_name, t, pair_data.max_price, pair_data.min_amount) self.assertRaises(btceapi.InvalidTradeAmountException, info.validate_order, pair_name, t, pair_data.min_price, pair_data.min_amount - decimal.Decimal("0.0001")) self.assertRaises(btceapi.InvalidTradePriceException, info.validate_order, pair_name, t, pair_data.min_price - decimal.Decimal("0.0001"), pair_data.min_amount) self.assertRaises(btceapi.InvalidTradePriceException, info.validate_order, pair_name, t, pair_data.max_price + decimal.Decimal("0.0001"), pair_data.min_amount) self.assertRaises(btceapi.InvalidTradePairException, info.validate_order, "foo_bar", "buy", decimal.Decimal("1.0"), decimal.Decimal("1.0")) self.assertRaises(btceapi.InvalidTradeTypeException, info.validate_order, "btc_usd", "foo", decimal.Decimal("1.0"), decimal.Decimal("1.0"))
def run(database_path): conn = btceapi.BTCEConnection() api = btceapi.APIInfo(conn) #logger = MarketDataLogger(api.pair_names, database_path) logger = MarketDataLogger(api, ("btc_usd", "ltc_usd"), database_path) # Create a bot and add the logger to it. bot = btcebot.Bot(api) bot.addTrader(logger) # Add an error handler so we can print info about any failures bot.addErrorHandler(onBotError) # The bot will provide the logger with updated information every # 60 seconds. bot.setCollectionInterval(60) 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 test_truncateAmount(self): info = btceapi.APIInfo(self.connection) for i in info.pairs.values(): d = i.decimal_places self.assertEqual(i.truncate_amount(1.12), btceapi.truncateAmountDigits(1.12, d)) self.assertEqual(i.truncate_amount(44.0), btceapi.truncateAmountDigits(44.0, d))
def test_getTicker(self): connection = btceapi.BTCEConnection() info = btceapi.APIInfo(connection) for pair in info.pair_names: btceapi.getTicker(pair, connection, info) btceapi.getTicker(pair, connection) btceapi.getTicker(pair, info=info) btceapi.getTicker(pair)
def test_currency_sets(self): currencies_from_pairs = set() info = btceapi.APIInfo(self.connection) for pair in info.pair_names: c1, c2 = pair.split("_") currencies_from_pairs.add(c1) currencies_from_pairs.add(c2) self.assertEqual(len(info.currencies), len(set(info.currencies))) self.assertEqual(currencies_from_pairs, set(info.currencies))
def test_scrape_main_page(self): with btceapi.BTCEConnection() as connection: info = btceapi.APIInfo(connection) mainPage = info.scrapeMainPage() for message in mainPage.messages: msgId, user, time, text = message assert type(time) is datetime if sys.version_info[0] == 2: # python2.x assert type(msgId) in (str, unicode) assert type(user) in (str, unicode) assert type(text) in (str, unicode) else: # python3.x self.assertIs(type(msgId), str) self.assertIs(type(user), str) self.assertIs(type(text), str)
#!/usr/bin/env python import btceapi attrs = ('high', 'low', 'avg', 'vol', 'vol_cur', 'last', 'buy', 'sell', 'updated') print("Tickers:") connection = btceapi.BTCEConnection() info = btceapi.APIInfo(connection) for pair in info.pair_names: ticker = btceapi.getTicker(pair, connection) print(pair) for a in attrs: print("\t%s %s" % (a, getattr(ticker, a)))
def test_validate_pair_suggest(self): info = btceapi.APIInfo(self.connection) self.assertRaises(btceapi.InvalidTradePairException, info.validate_pair, "usd_btc")
def test_validatePair(self): info = btceapi.APIInfo(self.connection) for pair in info.pair_names: info.validate_pair(pair) self.assertRaises(btceapi.InvalidTradePairException, info.validate_pair, "not_a_real_pair")
def test_pair_identity(self): info = btceapi.APIInfo(self.connection) self.assertEqual(len(info.pair_names), len(set(info.pair_names))) self.assertEqual(set(info.pairs.keys()), set(info.pair_names))