def setUp(self):
        self.market1 = MockMarket(price_currency="USD", amount_currency="BTC")
        self.market1.set_mock_depth(depthUSDBTC)

        self.market2 = MockMarket(price_currency="BTC", amount_currency="LTC")
        self.market2.set_mock_depth(depthLTCBTC)

        self.market3 = MockMarket(amount_currency="LTC", price_currency="USD")
        self.market3.set_mock_depth(depthLTCUSD)

        self.chain = MarketChain(
            self.market1, self.market2, self.market3
        )
class TestMarketChain(unittest.TestCase):
    def setUp(self):
        self.market1 = MockMarket(price_currency="USD", amount_currency="BTC")
        self.market1.set_mock_depth(depthUSDBTC)

        self.market2 = MockMarket(price_currency="BTC", amount_currency="LTC")
        self.market2.set_mock_depth(depthLTCBTC)

        self.market3 = MockMarket(amount_currency="LTC", price_currency="USD")
        self.market3.set_mock_depth(depthLTCUSD)

        self.chain = MarketChain(
            self.market1, self.market2, self.market3
        )

    def test_profit_functions(self):
        self.chain.begin_transaction()

        tradechain = self.chain.next()
        assert tradechain.profit == 0.72
        assert tradechain.percentage == 21.95
        
        tradechain = self.chain.next()
        assert tradechain.profit == 0.34
        assert tradechain.percentage == 20.73
        
        # Make sure we're still locked.
        assert self.chain.locked

        tradechain = self.chain.next()
        assert tradechain.profit == -0.804
        assert tradechain.percentage == -44.57

        self.chain.end_transaction()

        # Make sure we're unlocked now.
        assert not self.chain.locked

    def test_market_depth_integrity(self):
        """Make sure Markets shared across MarketChains don't suffer depth
        depletion in the course of normal trade chain exploration.

        """
        chain_copy = self.chain.copy()

        self.chain.begin_transaction()
        tradechain = self.chain.next()
        self.chain.end_transaction()

        chain_copy.begin_transaction()
        tradechain2 = chain_copy.next()
        chain_copy.end_transaction()

        # Make sure that the original MarketChain and its copy both
        # produce the same TradeChain when the first one was produced
        # inside a transaction. Transactions protect Market.depth from
        # permanent mutation.
        self.assertEqual(tradechain.profit, tradechain2.profit)
class TestMarketChain(unittest.TestCase):
    def setUp(self):
        self.market1 = MockMarket(price_currency="USD", amount_currency="BTC")
        self.market1.set_mock_depth(depthUSDBTC)

        self.market2 = MockMarket(price_currency="BTC", amount_currency="LTC")
        self.market2.set_mock_depth(depthLTCBTC)

        self.market3 = MockMarket(amount_currency="LTC", price_currency="USD")
        self.market3.set_mock_depth(depthLTCUSD)

        self.chain = MarketChain(self.market1, self.market2, self.market3)

    def test_profit_functions(self):
        self.chain.begin_transaction()

        tradechain = self.chain.next()
        assert tradechain.profit == 0.72
        assert tradechain.percentage == 21.95

        tradechain = self.chain.next()
        assert tradechain.profit == 0.34
        assert tradechain.percentage == 20.73

        # Make sure we're still locked.
        assert self.chain.locked

        tradechain = self.chain.next()
        assert tradechain.profit == -0.804
        assert tradechain.percentage == -44.57

        self.chain.end_transaction()

        # Make sure we're unlocked now.
        assert not self.chain.locked

    def test_market_depth_integrity(self):
        """Make sure Markets shared across MarketChains don't suffer depth
        depletion in the course of normal trade chain exploration.

        """
        chain_copy = self.chain.copy()

        self.chain.begin_transaction()
        tradechain = self.chain.next()
        self.chain.end_transaction()

        chain_copy.begin_transaction()
        tradechain2 = chain_copy.next()
        chain_copy.end_transaction()

        # Make sure that the original MarketChain and its copy both
        # produce the same TradeChain when the first one was produced
        # inside a transaction. Transactions protect Market.depth from
        # permanent mutation.
        self.assertEqual(tradechain.profit, tradechain2.profit)
Exemple #4
0
    def setUp(self):
        self.market1 = MockMarket(price_currency="USD", amount_currency="BTC")
        self.market1.set_mock_depth(depthUSDBTC)

        self.market2 = MockMarket(price_currency="BTC", amount_currency="LTC")
        self.market2.set_mock_depth(depthLTCBTC)

        self.market3 = MockMarket(amount_currency="LTC", price_currency="USD")
        self.market3.set_mock_depth(depthLTCUSD)

        self.chain = MarketChain(self.market1, self.market2, self.market3)
Exemple #5
0
class TestMarketChain(unittest.TestCase):
    def setUp(self):
        self.market1 = MockMarket(price_currency="USD", amount_currency="BTC")
        self.market1.set_mock_depth(depthUSDBTC)

        self.market2 = MockMarket(price_currency="BTC", amount_currency="LTC")
        self.market2.set_mock_depth(depthLTCBTC)

        self.market3 = MockMarket(amount_currency="LTC", price_currency="USD")
        self.market3.set_mock_depth(depthLTCUSD)

        self.chain = MarketChain(self.market1, self.market2, self.market3)

    def test_profit_functions(self):
        tradechain = self.chain.next()
        assert tradechain.profit == 0.72
        assert tradechain.percentage == 21.95

        # Make sure we're unlocked.
        assert not self.chain.locked

        self.chain.begin_transaction()

        tradechain = self.chain.next()
        assert tradechain.profit == 0.34
        assert tradechain.percentage == 20.73

        # Make sure we're still locked.
        assert self.chain.locked

        tradechain = self.chain.next()
        assert tradechain.profit == -0.804
        assert tradechain.percentage == -44.57

        self.chain.end_transaction()

        # Make sure we're unlocked now.
        assert not self.chain.locked
 def setUp(self):
     self.market = MockMarket()
class TestMarket(unittest.TestCase):
    def setUp(self):
        self.market = MockMarket()


    def test_getdepth_invaliddata(self):
        self.market.set_mock_depth(depth_bad)
        self.market.get_depth()
        assert self.market.depth['asks'][0]['price'] == 0


    def test_getdepth_validdata(self):
        self.market.set_mock_depth(depth_ok)
        self.market.get_depth()
        assert self.market.depth['asks'][0]['price'] == 32.8
        assert self.market.depth['bids'][0]['price'] == 31.8


    def test_validation_functions(self):
        assert self.market.uses("USD")
        assert self.market.uses("BTC")
        assert not self.market.uses("LTC")


    def test_invalid_execution(self):
        try:
            self.market.execute_trade(2, "BTC")
            assert False
        except Exception:
            assert True

    def test_divide_by_zero_bug(self):
        self.market.set_mock_depth(depth_div_by_zero)
        self.market.get_depth()
        assert self.market.value_of("USD") == 0
        assert self.market.r_value_of("BTC") == 0

    def test_volume_functions(self):
        self.market.set_mock_depth(depth_profit)

        # Lock the order book.
        self.market.begin_transaction()

        # Should match the figures from the first order in the list.
        assert self.market.volume_to_next_price_as("BTC") == 2
        assert self.market.volume_to_next_price_as("USD") == 131.2

        # Make sure it returns the amount of USD that 0.1 BTC would obtain.
        assert self.market.value_of("BTC", volume = 0.1) == 3.18   
 
        # Make sure it returns the amount of BTC that 65.60 USD would obtain.
        assert self.market.value_of("USD", volume = 65.60) == 2       
 
        # Make sure it returns the amount of BTC obtained from 65.60 USD.
        assert self.market.execute_trade(65.60, "USD").to_volume == 2
        
        # Should have half the original figure left in USD...
        assert self.market.volume_to_next_price_as("USD") == 65.6

        # ...and all the original figure left in BTC.        
        assert self.market.volume_to_next_price_as("BTC") == 2

        # Make sure it returns the amount of USD obtained from 2 BTC.
        assert self.market.execute_trade(2, "BTC").to_volume == 63.6

        # Clear out the top ask order.
        assert self.market.execute_trade(65.60, "USD").to_volume == 2

        # Should match the figures from the second order in the list now.
        assert self.market.volume_to_next_price_as("BTC") == 4 
        assert self.market.volume_to_next_price_as("USD") == 263.2

        self.market.end_transaction()
 def setUp(self):
     self.market = MockMarket()
class TestMarket(unittest.TestCase):
    def setUp(self):
        self.market = MockMarket()

    def test_getdepth_invaliddata(self):
        self.market.set_mock_depth(depth_bad)
        self.market.get_depth()
        assert self.market.depth['asks'][0]['price'] == 0

    def test_getdepth_validdata(self):
        self.market.set_mock_depth(depth_ok)
        self.market.get_depth()
        assert self.market.depth['asks'][0]['price'] == 32.8
        assert self.market.depth['bids'][0]['price'] == 31.8

    def test_validation_functions(self):
        assert self.market.uses("USD")
        assert self.market.uses("BTC")
        assert not self.market.uses("LTC")

    def test_invalid_execution(self):
        try:
            self.market.execute_trade(2, "BTC")
            assert False
        except Exception:
            assert True

    def test_divide_by_zero_bug(self):
        self.market.set_mock_depth(depth_div_by_zero)
        self.market.get_depth()
        assert self.market.value_of("USD") == 0
        assert self.market.r_value_of("BTC") == 0

    def test_volume_functions(self):
        self.market.set_mock_depth(depth_profit)

        # Lock the order book.
        self.market.begin_transaction()

        # Should match the figures from the first order in the list.
        assert self.market.volume_to_next_price_as("BTC") == 2
        assert self.market.volume_to_next_price_as("USD") == 131.2

        # Make sure it returns the amount of USD that 0.1 BTC would obtain.
        assert self.market.value_of("BTC", volume=0.1) == 3.18

        # Make sure it returns the amount of BTC that 65.60 USD would obtain.
        assert self.market.value_of("USD", volume=65.60) == 2

        # Make sure it returns the amount of BTC obtained from 65.60 USD.
        assert self.market.execute_trade(65.60, "USD").to_volume == 2

        # Should have half the original figure left in USD...
        assert self.market.volume_to_next_price_as("USD") == 65.6

        # ...and all the original figure left in BTC.
        assert self.market.volume_to_next_price_as("BTC") == 2

        # Make sure it returns the amount of USD obtained from 2 BTC.
        assert self.market.execute_trade(2, "BTC").to_volume == 63.6

        # Clear out the top ask order.
        assert self.market.execute_trade(65.60, "USD").to_volume == 2

        # Should match the figures from the second order in the list now.
        assert self.market.volume_to_next_price_as("BTC") == 4
        assert self.market.volume_to_next_price_as("USD") == 263.2

        self.market.end_transaction()
class TestMarket(unittest.TestCase):
    def setUp(self):
        self.market = MockMarket()

    # Invalidated following @weidenrinde's (temporary?) removal of the
    # market depth sanity check on the grounds that some markets have
    # valid states where bid price is greater than ask price.
    # Leaving this in here until the originator of the sanity check,
    # @mdespriee, can weigh in on the check's removal.
    # def test_getdepth_invaliddata(self):
    #    self.market.set_mock_depth(depth_bad)
    #    self.market.get_depth()
    #    assert self.market.depth['asks'][0]['price'] == 0

    def test_getdepth_validdata(self):
        self.market.set_mock_depth(depth_ok)
        self.market.get_depth()
        assert self.market.depth["asks"][0]["price"] == 32.8
        assert self.market.depth["bids"][0]["price"] == 31.8

    def test_validation_functions(self):
        assert self.market.uses("USD")
        assert self.market.uses("BTC")
        assert not self.market.uses("LTC")

    def test_invalid_execution(self):
        try:
            self.market.execute_trade(2, "BTC")
            assert False
        except Exception:
            assert True

    def test_divide_by_zero_bug(self):
        self.market.set_mock_depth(depth_div_by_zero)
        self.market.get_depth()
        assert self.market.value_of("USD") == 0
        assert self.market.r_value_of("BTC") == 0

    def test_volume_functions(self):
        self.market.set_mock_depth(depth_profit)

        # Lock the order book.
        self.market.begin_transaction()

        # Should match the figures from the first order in the list.
        assert self.market.volume_to_next_price_as("BTC") == 2
        assert self.market.volume_to_next_price_as("USD") == 131.2

        # Make sure it returns the amount of USD that 0.1 BTC would obtain.
        assert self.market.value_of("BTC", volume=0.1) == 3.18

        # Make sure it returns the amount of BTC that 65.60 USD would obtain.
        assert self.market.value_of("USD", volume=65.60) == 2

        # Make sure it returns the amount of BTC obtained from 65.60 USD.
        assert self.market.execute_trade(65.60, "USD").to_volume == 2

        # Should have half the original figure left in USD...
        assert self.market.volume_to_next_price_as("USD") == 65.6

        # ...and all the original figure left in BTC.
        assert self.market.volume_to_next_price_as("BTC") == 2

        # Make sure it returns the amount of USD obtained from 2 BTC.
        assert self.market.execute_trade(2, "BTC").to_volume == 63.6

        # Clear out the top ask order.
        assert self.market.execute_trade(65.60, "USD").to_volume == 2

        # Should match the figures from the second order in the list now.
        assert self.market.volume_to_next_price_as("BTC") == 4
        assert self.market.volume_to_next_price_as("USD") == 263.2

        self.market.end_transaction()

        # Make sure the original market depth was restored
        # after the transaction ended.
        assert self.market.depth == depth_profit
class TestMarket(unittest.TestCase):
    def setUp(self):
        self.market = MockMarket()

    # Invalidated following @weidenrinde's (temporary?) removal of the
    # market depth sanity check on the grounds that some markets have
    # valid states where bid price is greater than ask price.
    # Leaving this in here until the originator of the sanity check,
    # @mdespriee, can weigh in on the check's removal.
    #def test_getdepth_invaliddata(self):
    #    self.market.set_mock_depth(depth_bad)
    #    self.market.get_depth()
    #    assert self.market.depth['asks'][0]['price'] == 0

    def test_getdepth_validdata(self):
        self.market.set_mock_depth(depth_ok)
        self.market.get_depth()
        assert self.market.depth['asks'][0]['price'] == 32.8
        assert self.market.depth['bids'][0]['price'] == 31.8

    def test_validation_functions(self):
        assert self.market.uses("USD")
        assert self.market.uses("BTC")
        assert not self.market.uses("LTC")

    def test_invalid_execution(self):
        try:
            self.market.execute_trade(2, "BTC")
            assert False
        except Exception:
            assert True

    def test_divide_by_zero_bug(self):
        self.market.set_mock_depth(depth_div_by_zero)
        self.market.get_depth()
        assert self.market.value_of("USD") == 0
        assert self.market.r_value_of("BTC") == 0

    def test_volume_functions(self):
        self.market.set_mock_depth(depth_profit)

        # Lock the order book.
        self.market.begin_transaction()

        # Should match the figures from the first order in the list.
        assert self.market.volume_to_next_price_as("BTC") == 2
        assert self.market.volume_to_next_price_as("USD") == 131.2

        # Make sure it returns the amount of USD that 0.1 BTC would obtain.
        assert self.market.value_of("BTC", volume=0.1) == 3.18

        # Make sure it returns the amount of BTC that 65.60 USD would obtain.
        assert self.market.value_of("USD", volume=65.60) == 2

        # Make sure it returns the amount of BTC obtained from 65.60 USD.
        assert self.market.execute_trade(65.60, "USD").to_volume == 2

        # Should have half the original figure left in USD...
        assert self.market.volume_to_next_price_as("USD") == 65.6

        # ...and all the original figure left in BTC.
        assert self.market.volume_to_next_price_as("BTC") == 2

        # Make sure it returns the amount of USD obtained from 2 BTC.
        assert self.market.execute_trade(2, "BTC").to_volume == 63.6

        # Clear out the top ask order.
        assert self.market.execute_trade(65.60, "USD").to_volume == 2

        # Should match the figures from the second order in the list now.
        assert self.market.volume_to_next_price_as("BTC") == 4
        assert self.market.volume_to_next_price_as("USD") == 263.2

        self.market.end_transaction()

        # Make sure the original market depth was restored
        # after the transaction ended.
        assert self.market.depth == depth_profit