Modified the Position handling to use long/short instead of buy/sell for side/position_type. Also modified the unit tests for both Portfolio and Position to reflect these changes. Added a basic historical backtesting capability via backtest.py and using CSV tick data for currency pairs.

This commit is contained in:
Michael Halls-Moore
2015-04-17 12:34:31 +01:00
parent d9a7444fc2
commit e74777802b
12 changed files with 519 additions and 192 deletions
+166 -91
View File
@@ -18,200 +18,199 @@ class TestPortfolio(unittest.TestCase):
)
def test_add_position_long(self):
side = "LONG"
position_type = "long"
market = "GBP/USD"
units = 2000
exposure = Decimal(str(units))
add_price = Decimal("1.51819")
remove_price = Decimal("1.51770")
units = Decimal("2000")
exposure = Decimal("2000.00")
bid = Decimal("1.51770")
ask = Decimal("1.51819")
self.port.add_new_position(
side, market, units, exposure,
add_price, remove_price
position_type, market, units,
exposure, bid, ask
)
ps = self.port.positions[market]
self.assertEquals(ps.side, side)
self.assertEquals(ps.position_type, position_type)
self.assertEquals(ps.market, market)
self.assertEquals(ps.units, units)
self.assertEquals(ps.exposure, exposure)
self.assertEquals(ps.avg_price, add_price)
self.assertEquals(ps.cur_price, remove_price)
self.assertEquals(ps.avg_price, ask)
self.assertEquals(ps.cur_price, bid)
def test_add_position_short(self):
side = "SHORT"
position_type = "short"
market = "GBP/USD"
units = 2000
exposure = Decimal(str(units))
add_price = Decimal("1.51770")
remove_price = Decimal("1.51819")
units = Decimal("2000")
exposure = Decimal("2000.00")
bid = Decimal("1.51770")
ask = Decimal("1.51819")
self.port.add_new_position(
side, market, units, exposure,
add_price, remove_price
position_type, market, units,
exposure, bid, ask
)
ps = self.port.positions[market]
self.assertEquals(ps.side, side)
self.assertEquals(ps.position_type, position_type)
self.assertEquals(ps.market, market)
self.assertEquals(ps.units, units)
self.assertEquals(ps.exposure, exposure)
self.assertEquals(ps.avg_price, add_price)
self.assertEquals(ps.cur_price, remove_price)
self.assertEquals(ps.avg_price, bid)
self.assertEquals(ps.cur_price, ask)
def test_add_position_units_long(self):
side = "LONG"
position_type = "long"
market = "GBP/USD"
units = 2000
exposure = Decimal(str(units))
add_price = Decimal("1.51819")
remove_price = Decimal("1.51770")
units = Decimal("2000")
exposure = Decimal("2000.00")
bid = Decimal("1.51770")
ask = Decimal("1.51819")
# Test for no position
market = "EUR/USD"
apu = self.port.add_position_units(
market, units, exposure,
add_price, remove_price
bid, ask
)
self.assertFalse(apu)
# Add a position and test for real position
market = "GBP/USD"
self.port.add_new_position(
side, market, units, exposure,
add_price, remove_price
position_type, market, units,
exposure, bid, ask
)
ps = self.port.positions[market]
# Test for addition of units
add_price = Decimal("1.51928")
remove_price = Decimal("1.51878")
bid = Decimal("1.51878")
ask = Decimal("1.51928")
apu = self.port.add_position_units(
market, units, exposure,
add_price, remove_price
bid, ask
)
self.assertTrue(apu)
self.assertEqual(ps.avg_price, Decimal("1.518735"))
def test_add_position_units_short(self):
side = "SHORT"
position_type = "short"
market = "GBP/USD"
units = 2000
exposure = Decimal(str(units))
add_price = Decimal("1.51770")
remove_price = Decimal("1.51819")
units = Decimal("2000")
exposure = Decimal("2000.00")
bid = Decimal("1.51770")
ask = Decimal("1.51819")
# Test for no position
market = "EUR/USD"
apu = self.port.add_position_units(
market, units, exposure,
add_price, remove_price
bid, ask
)
self.assertFalse(apu)
# Add a position and test for real position
market = "GBP/USD"
self.port.add_new_position(
side, market, units, exposure,
add_price, remove_price
position_type, market, units,
exposure, bid, ask
)
ps = self.port.positions[market]
# Test for addition of units
add_price = Decimal("1.51878")
remove_price = Decimal("1.51928")
bid = Decimal("1.51878")
ask = Decimal("1.51928")
apu = self.port.add_position_units(
market, units, exposure,
add_price, remove_price
bid, ask
)
self.assertTrue(apu)
self.assertEqual(ps.avg_price, Decimal("1.51824"))
def test_remove_position_units_long(self):
side = "LONG"
units = 2000
exposure = Decimal(str(units))
add_price = Decimal("1.51819")
remove_price = Decimal("1.51770")
position_type = "long"
units = Decimal("2000")
exposure = Decimal("2000.00")
bid = Decimal("1.51770")
ask = Decimal("1.51819")
# Test for no position
market = "EUR/USD"
apu = self.port.remove_position_units(
market, units, remove_price
market, units, bid, ask
)
self.assertFalse(apu)
# Add a position and then add units to it
market = "GBP/USD"
self.port.add_new_position(
side, market, units, exposure,
add_price, remove_price
position_type, market, units,
exposure, bid, ask
)
ps = self.port.positions[market]
add_price = Decimal("1.51928")
remove_price = Decimal("1.51878")
bid = Decimal("1.51878")
ask = Decimal("1.51928")
add_units = 8000
add_exposure = Decimal(str(add_units))
apu = self.port.add_position_units(
market, add_units, add_exposure,
add_price, remove_price
bid, ask
)
self.assertEqual(ps.units, 10000)
self.assertEqual(ps.exposure, Decimal("10000.00"))
self.assertEqual(ps.avg_price, Decimal("1.519062"))
# Test removal of (some) of the units
add_price = Decimal("1.52134")
remove_price = Decimal("1.52017")
bid = Decimal("1.52017")
ask = Decimal("1.52134")
remove_units = 3000
rpu = self.port.remove_position_units(
market, remove_units, remove_price
market, remove_units, bid, ask
)
self.assertTrue(rpu)
self.assertEqual(ps.units, 7000)
self.assertEqual(ps.exposure, Decimal("7000.00"))
self.assertEqual(ps.profit_base, Decimal("2.19054"))
self.assertEqual(self.port.balance, Decimal("100002.19"))
def test_remove_position_units_short(self):
side = "SHORT"
units = 2000
exposure = Decimal(str(units))
add_price = Decimal("1.51770")
remove_price = Decimal("1.51819")
position_type = "short"
units = Decimal("2000")
exposure = Decimal("2000.00")
bid = Decimal("1.51770")
ask = Decimal("1.51819")
# Test for no position
market = "EUR/USD"
apu = self.port.remove_position_units(
market, units, remove_price
market, units, bid, ask
)
self.assertFalse(apu)
# Add a position and then add units to it
market = "GBP/USD"
self.port.add_new_position(
side, market, units, exposure,
add_price, remove_price
position_type, market, units,
exposure, bid, ask
)
ps = self.port.positions[market]
add_price = Decimal("1.51878")
remove_price = Decimal("1.51928")
bid = Decimal("1.51878")
ask = Decimal("1.51928")
add_units = 8000
add_exposure = Decimal(str(add_units))
apu = self.port.add_position_units(
market, add_units, add_exposure,
add_price, remove_price
bid, ask
)
self.assertEqual(ps.units, 10000)
self.assertEqual(ps.exposure, Decimal("10000.00"))
self.assertEqual(ps.avg_price, Decimal("1.518564"))
# Test removal of (some) of the units
add_price = Decimal("1.52017")
remove_price = Decimal("1.52134")
bid = Decimal("1.52017")
ask = Decimal("1.52134")
remove_units = 3000
rpu = self.port.remove_position_units(
market, remove_units, remove_price
market, remove_units, bid, ask
)
self.assertTrue(rpu)
self.assertEqual(ps.units, 7000)
@@ -220,16 +219,16 @@ class TestPortfolio(unittest.TestCase):
self.assertEqual(self.port.balance, Decimal("99994.52"))
def test_close_position_long(self):
side = "LONG"
units = 2000
exposure = Decimal(str(units))
add_price = Decimal("1.51819")
remove_price = Decimal("1.51770")
position_type = "long"
units = Decimal("2000")
exposure = Decimal("2000.00")
bid = Decimal("1.51770")
ask = Decimal("1.51819")
# Test for no position
market = "EUR/USD"
cp = self.port.close_position(
market, remove_price
market, bid, ask
)
self.assertFalse(cp)
@@ -237,12 +236,12 @@ class TestPortfolio(unittest.TestCase):
# Will lose money on the spread
market = "GBP/USD"
self.port.add_new_position(
side, market, units, exposure,
add_price, remove_price
position_type, market, units,
exposure, bid, ask
)
ps = self.port.positions[market]
cp = self.port.close_position(
market, remove_price
market, bid, ask
)
self.assertTrue(cp)
self.assertRaises(ps) # Key doesn't exist
@@ -252,38 +251,114 @@ class TestPortfolio(unittest.TestCase):
# close the position. Balance should be as expected
# for a multi-leg transaction.
self.port.add_new_position(
side, market, units, exposure,
add_price, remove_price
position_type, market, units,
exposure, bid, ask
)
ps = self.port.positions[market]
add_price = Decimal("1.51928")
remove_price = Decimal("1.51878")
# Add 8000 units
bid = Decimal("1.51878")
ask = Decimal("1.51928")
add_units = 8000
add_exposure = Decimal(str(add_units))
apu = self.port.add_position_units(
market, add_units, add_exposure,
add_price, remove_price
market, add_units,
add_exposure, bid, ask
)
self.assertEqual(ps.units, 10000)
self.assertEqual(ps.exposure, Decimal("10000.00"))
self.assertEqual(ps.avg_price, Decimal("1.519062"))
add_price = Decimal("1.52134")
remove_price = Decimal("1.52017")
# Remove 3000 units
bid = Decimal("1.52017")
ask = Decimal("1.52134")
remove_units = 3000
rpu = self.port.remove_position_units(
market, remove_units, remove_price
market, remove_units, bid, ask
)
self.assertEqual(ps.units, 7000)
self.assertEqual(ps.exposure, Decimal("7000.00"))
self.assertEqual(ps.profit_base, Decimal("2.19054"))
self.assertEqual(self.port.balance, Decimal("100001.54"))
# Close the position
cp = self.port.close_position(
market, remove_price
market, bid, ask
)
self.assertTrue(cp)
self.assertRaises(ps) # Key doesn't exist
self.assertEqual(self.port.balance, Decimal("100006.65"))
def test_close_position_short(self):
position_type = "short"
units = Decimal("2000")
exposure = Decimal("2000.00")
bid = Decimal("1.51770")
ask = Decimal("1.51819")
# Test for no position
market = "EUR/USD"
cp = self.port.close_position(
market, bid, ask
)
self.assertFalse(cp)
# Add a position and then close it
# Will lose money on the spread
market = "GBP/USD"
self.port.add_new_position(
position_type, market, units,
exposure, bid, ask
)
ps = self.port.positions[market]
cp = self.port.close_position(
market, bid, ask
)
self.assertTrue(cp)
self.assertRaises(ps) # Key doesn't exist
self.assertEqual(self.port.balance, Decimal("99999.35"))
# Add 2000, add another 8000, remove 3000 and then
# close the position. Balance should be as expected
# for a multi-leg transaction.
self.port.add_new_position(
position_type, market, units,
exposure, bid, ask
)
ps = self.port.positions[market]
# Add 8000 units
bid = Decimal("1.51878")
ask = Decimal("1.51928")
add_units = 8000
add_exposure = Decimal(str(add_units))
apu = self.port.add_position_units(
market, add_units,
add_exposure, bid, ask
)
self.assertEqual(ps.units, 10000)
self.assertEqual(ps.exposure, Decimal("10000.00"))
self.assertEqual(ps.avg_price, Decimal("1.518564"))
# Remove 3000 units
bid = Decimal("1.52017")
ask = Decimal("1.52134")
remove_units = 3000
rpu = self.port.remove_position_units(
market, remove_units, bid, ask
)
self.assertEqual(ps.units, 7000)
self.assertEqual(ps.exposure, Decimal("7000.00"))
self.assertEqual(ps.profit_base, Decimal("-5.48201"))
self.assertEqual(self.port.balance, Decimal("99993.87"))
# Close the position
cp = self.port.close_position(
market, bid, ask
)
self.assertTrue(cp)
self.assertRaises(ps) # Key doesn't exist
self.assertEqual(self.port.balance, Decimal("99981.08"))
if __name__ == "__main__":
unittest.main()