def test_firesale(self): parameterDefs = simulator.define_parameters() eParams = Parameters(parameterDefs) simInfo = SimulationInfo() econ = Economy(simInfo, "Economy1", eParams) inv = [] holders = [] for i in range(5): inv.append(econ.create_investment()) holders.append("holder%s" % i) econ.params.set('fireSaleFactor', 1.0) # price is affected by sales simInfo.updateCount = 1 for i in range(5): inv[0].buy(5, holders[i]) inv[1].buy(5, holders[i]) # one holder sells a bit of the investment simInfo.updateCount = 2 q, v = inv[0].sell(1, holders[0]) assert v < 1, "expected consideration < 1 but is %f" % v eq_(v, inv[0].currentPrice, "expected consideration equal to price but they are %f, %f" % (v, inv[0].currentPrice)) check_asset_holding(inv[0], holders[0], quantity=4, value=4 * v, tag="fs1") check_asset_holding(inv[0], holders[1], quantity=5, value=5 * v, tag="fs1") price1 = inv[0].currentPrice simInfo.updateCount = 3 q, v = inv[0].sell(3, holders[2]) # a total of 4 has now been sold price2 = inv[0].currentPrice assert price2 < price1, "expected price < %f but is %f (fs2)" % (price1, price2) assert approx_equal(v, price2 * q, 0.00001), "expected consideration %f but is %f (fs2)" % (price2 * q, v) simInfo.updateCount = 4 inv[1].sell(4, holders[3]) # sell 4, so price should be same as inv0 price3 = inv[1].currentPrice assert approx_equal(price2, price3, 0.00001), "expected price to be %f but is %f (fs3)" % (price2, price3) pHistory = inv[1].priceHistory when, price = pHistory[-1] eq_(4, when, "expected last item in price history to be at time 4 but got %r" % when) eq_(price3, price, "expected last item in price history to be price %f but got %f" % (price3, price))
def test_investments_buy1(self): parameterDefs = simulator.define_parameters() eParams = Parameters(parameterDefs) econ = Economy(self.simInfo, "Economy1", eParams) newI = econ.create_investment() hLen = len(newI.priceHistory) self.simInfo.updateCount = 1 newI.set_price(1.0) hLen1 = len(newI.priceHistory) eq_(hLen + 1, hLen1, "expected %r items in price history but got %r" % (hLen + 1, hLen1)) when, price = newI.priceHistory[hLen1 - 1] eq_(1, when, "expected last item in price history to be at time 0 but was %r" % when) eq_(1.0, price, "expected last price in price history to be 1.0 but was %r" % price) self.simInfo.updateCount = 2 q, v = newI.buy(1, "buyer1") eq_(q, 1, "expected quantity 1 but got %r (1)" % q) eq_(v, 1, "expected consideration 1 but got %r (1)" % v) check_asset_network(self.simInfo, nodes=2, edges=1, tag="buy1") check_asset_holding(newI, "buyer1", 1, 1.0, tag="buy1") check_asset_totals(self.simInfo, newI, 1, 1.0, "buy1") hHistory = newI.holdingHistory["buyer1"] eq_(1, len(hHistory), "expected 1 item in holding history but got %r" % len(hHistory)) when, amount = hHistory[-1] eq_(2, when, "expected last item in holding history to be at time 1 but got %r" % when) eq_(1, amount, "expected last item in holding history to be amount 1 but got %r" % amount) self.simInfo.updateCount = 3 newI.set_price(2.0) q, v = newI.buy(3, "buyer1") eq_(q, 3, "expected quantity 3 but got %r (2)" % q) eq_(v, 6, "expected consideration 6 but got %r (2)" % v) check_asset_network(self.simInfo, nodes=2, edges=1, tag="buy1") check_asset_holding(newI, "buyer1", 4, 8.0, tag="buy2") check_asset_totals(self.simInfo, newI, 4, 2.0, tag="buy2") eq_(newI.value("buyer2"), 0, "expected buyer2 value to be 0 but is %r" % newI.value("buyer2")) eq_(newI.quantity("buyer2"), 0, "expected buyer2 quantity to be 0 but is %r" % newI.quantity("buyer2")) # now sell some self.simInfo.updateCount = 4 newI.set_price(5.0) econ.params.set('fireSaleFactor', 0.0) # selling has no effect on price q, v = newI.sell(3, "buyer1") eq_(q, 3, "expected quantity 3 but got %r (4)" % q) eq_(v, 15, "expected consideration 15 but got %r (4)" % v) check_asset_network(self.simInfo, nodes=2, edges=1, tag="sell1") check_asset_holding(newI, "buyer1", 1, 5.0, "sell1") check_asset_totals(self.simInfo, newI, 1, 5.0, "sell1") self.simInfo.updateCount = 5 q, v = newI.sell(1, "buyer2") eq_(q, 0, "expected quantity 0 but got %r" % q) eq_(v, 0, "expected consideration 0 but got %r" % v) check_asset_network(self.simInfo, nodes=2, edges=1, tag="sell2") check_asset_holding(newI, "buyer1", 1, 5.0, "sell2") check_asset_totals(self.simInfo, newI, 1, 5.0, "sell2") self.simInfo.updateCount = 6 q, v = newI.sell(-6, "buyer1") eq_(q, 0, "expected quantity 0 but got %r (4.5)" % q) eq_(v, 0, "expected consideration 0 but got %r (4.5)" % v) check_asset_network(self.simInfo, nodes=2, edges=1, tag="sell3") check_asset_holding(newI, "buyer1", 1, 5.0, "sell3") check_asset_totals(self.simInfo, newI, 1, 5.0, "sell3") pHistory = newI.priceHistory when, price = pHistory[-1] eq_(4, when, "expected last item in price history to be at time 4 but got %r" % when) eq_(5.0, price, "expected last item in price history to be price 5 but got %r" % price) self.simInfo.updateCount = 7 newI.set_price(3) q, v = newI.sell(5, "buyer1") eq_(q, 1, "expected quantity 1 but got %r (5)" % q) eq_(v, 3, "expected consideration 3 but got %r (5)" % v) check_asset_network(self.simInfo, nodes=2, edges=0, tag="sell4") check_asset_holding(newI, "buyer1", 0, 0, "sell4") check_asset_totals(self.simInfo, newI, 0, 3, "sell4") self.simInfo.updateCount = 8 q, v = newI.buy(-6, "buyer1") eq_(q, 0, "expected quantity 0 but got %r (6)" % q) eq_(v, 0, "expected consideration 0 but got %r (6)" % v) hHistory = newI.holdingHistory["buyer1"] when, amount = hHistory[-1] eq_(7, when, "expected last item in holding history to be at time 7 but got %r" % when) eq_(0.0, amount, "expected last item in holding history to be amount 0 but got %r" % amount) pHistory = newI.priceHistory when, price = pHistory[-1] eq_(7, when, "expected last item in price history to be at time 7 but got %r" % when) eq_(3.0, price, "expected last item in price history to be price 3 but got %r" % price)
def test_investments_buy2(self): parameterDefs = simulator.define_parameters() eParams = Parameters(parameterDefs) econ = Economy(self.simInfo, "Economy1", eParams) newI = econ.create_investment() econ.params.set('fireSaleFactor', 0.0) self.simInfo.updateCount = 1 newI.set_price(2) q, v = newI.buy(3, "buyer1") eq_(q, 3, "expected quantity 3 but got %r (1)" % q) eq_(v, 6, "expected consideration 6 but got %r (1)" % v) check_asset_network(self.simInfo, nodes=2, edges=1, tag="buy3") check_asset_holding(newI, "buyer1", quantity=3, value=6, tag="buy3") check_asset_totals(self.simInfo, newI, 3, 2, tag="buy3") self.simInfo.updateCount = 2 q, v = newI.buy(4, "buyer2") eq_(q, 4, "expected quantity 4 but got %r (2)" % q) eq_(v, 8, "expected consideration 8 but got %r (2)" % v) check_asset_network(self.simInfo, nodes=3, edges=2, tag="buy4") check_asset_holding(newI, "buyer1", quantity=3, value=6, tag="buy4") check_asset_holding(newI, "buyer2", quantity=4, value=8, tag="buy4") check_asset_totals(self.simInfo, newI, 7, 2, tag="buy4") self.simInfo.updateCount = 3 q, v = newI.buy(2, "buyer3") eq_(q, 2, "expected quantity 2 but got %r (3)" % q) eq_(v, 4, "expected consideration 4 but got %r (3)" % v) check_asset_network(self.simInfo, nodes=4, edges=3, tag="buy5") check_asset_holding(newI, "buyer1", quantity=3, value=6, tag="buy5") check_asset_holding(newI, "buyer2", quantity=4, value=8, tag="buy5") check_asset_holding(newI, "buyer3", quantity=2, value=4, tag="buy5") check_asset_totals(self.simInfo, newI, 9, 2, tag="buy3") self.simInfo.updateCount = 4 q, v = newI.sell(5, "buyer2") eq_(q, 4, "expected quantity 4 but got %r (4)" % q) eq_(v, 8, "expected consideration 8 but got %r (4)" % v) check_asset_network(self.simInfo, nodes=4, edges=2, tag="buy5") check_asset_holding(newI, "buyer1", quantity=3, value=6, tag="buy5") check_asset_holding(newI, "buyer2", quantity=0, value=0, tag="buy5") check_asset_holding(newI, "buyer3", quantity=2, value=4, tag="buy5") check_asset_totals(self.simInfo, newI, 5, 2, tag="buy3")