def setUp(self): """ Set up the Position object that will store the PnL. """ self.position = Position("BOT", "XIV", 4352, PriceParser.parse(15.01), PriceParser.parse(1.00), PriceParser.parse(15.01), PriceParser.parse(15.01), datetime(2011, 2, 4))
def setUp(self): """ Set up the Position object that will store the PnL. """ self.position = Position( "BOT", "SPY", 100, PriceParser.parse(239.08), PriceParser.parse(1.00), PriceParser.parse(220.45), PriceParser.parse(220.45), datetime(2016, 1, 1) )
def setUp(self): """ Set up the Position object that will store the PnL. """ print "Setup initial position BOT 2 ES_0_I0B at 2180.50 with $4.06 commission:" self.position = Position("BOT", "ES_0_I0B", 2, PriceParser.parse(2180.50), PriceParser.parse(4.06), PriceParser.parse(2181.50), PriceParser.parse(2181.75), datetime(2016, 1, 1)) print self.position, '\n'
class TestLongRoundTripESPosition(unittest.TestCase): """ Test a round-trip trade in S&P e-mini futures contract where the initial trade is a buy/long of 1 contract of ES, at a price of 2155.25, with $2.00 commission. """ def setUp(self): """ Set up the Position object that will store the PnL. """ print "Setup initial position BOT 2 ES_0_I0B at 2180.50 with $4.06 commission:" self.position = Position("BOT", "ES_0_I0B", 2, PriceParser.parse(2180.50), PriceParser.parse(4.06), PriceParser.parse(2181.50), PriceParser.parse(2181.75), datetime(2016, 1, 1)) print self.position, '\n' def test_transact_position(self): """ Update market value of current position """ print "Update market value with bid/ask of 2178.50/2178.75:" self.position.update_market_value(PriceParser.parse(2190.00), PriceParser.parse(2190.25), datetime(2016, 1, 2)) print self.position, '\n' self.assertEqual(self.position.action, "BOT") self.assertEqual(self.position.ticker, "ES_0_I0B") self.assertEqual(self.position.quantity, 2) self.assertEqual(self.position.buys, 2) self.assertEqual(self.position.sells, 0) self.assertEqual(self.position.net, 2) self.assertEqual(PriceParser.display(self.position.avg_bot, 5), 2180.50) self.assertEqual(PriceParser.display(self.position.avg_sld, 5), 0) self.assertEqual(PriceParser.display(self.position.total_bot), 218050.00) self.assertEqual(PriceParser.display(self.position.total_sld), 0.0) self.assertEqual(PriceParser.display(self.position.net_total), 218050.00) self.assertEqual(PriceParser.display(self.position.total_commission), 4.06) self.assertEqual(PriceParser.display(self.position.net_incl_comm), 218050.00 - 4.06) self.assertEqual(PriceParser.display(self.position.avg_price, 5), (218050.00 - 4.06) / 2 / 50) self.assertEqual(PriceParser.display(self.position.cost_basis), 218050 - 4.06) self.assertEqual(PriceParser.display(self.position.market_value), (2190 + 2190.25) / 2 * 2 * 50) self.assertEqual( PriceParser.display(self.position.unrealised_pnl), round((((2190 + 2190.25) / 2 * 2 * 50) - (218050 - 4.06)), 2) * 1) self.assertEqual(PriceParser.display(self.position.realised_pnl), 0.00) print "Sell 1 contract of current position at 2187.50 with 2.03 commission. Market at 2193.50/2193.50:" self.position.transact_shares("SLD", 1, PriceParser.parse(2187.50), PriceParser.parse(2.03)) self.position.update_market_value(PriceParser.parse(2193.50), PriceParser.parse(2193.50), datetime(2016, 1, 3)) print self.position, '\n' self.assertEqual(self.position.quantity, 1) self.assertEqual(self.position.buys, 2) self.assertEqual(self.position.sells, 1) self.assertEqual(self.position.net, 1) self.assertEqual(PriceParser.display(self.position.avg_bot, 5), 2180.50) self.assertEqual(PriceParser.display(self.position.avg_sld, 5), 2187.50) self.assertEqual(PriceParser.display(self.position.total_bot), 218050.00) self.assertEqual(PriceParser.display(self.position.total_sld), 2187.5 * 50) self.assertEqual(PriceParser.display(self.position.net_total), (2187.5 * 50) - 218050.00) self.assertEqual(PriceParser.display(self.position.total_commission), 6.09) self.assertEqual(PriceParser.display(self.position.net_incl_comm), (2187.5 * 50) - 218050.00 - 6.09) self.assertEqual(PriceParser.display(self.position.avg_price, 5), (218050.00 - 4.06) / 2 / 50) self.assertEqual(PriceParser.display(self.position.cost_basis), round(1 * (218050.00 - 4.06) / 2 / 50 * 50, 2)) self.assertEqual(PriceParser.display(self.position.market_value), (2193.50 + 2193.50) / 2 * 1 * 50) self.assertEqual(PriceParser.display(self.position.unrealised_pnl), 652.03) self.assertEqual(PriceParser.display(self.position.realised_pnl), 347.97) print "Sell 1 contract TO CLOSE position at 2199.00 with 2.03 commission. Market at 2199.50/2199.75:" self.position.transact_shares("SLD", 1, PriceParser.parse(2199.00), PriceParser.parse(2.03)) self.position.update_market_value(PriceParser.parse(2199.50), PriceParser.parse(2199.75), datetime(2016, 1, 3)) print self.position, '\n'
class TestLongRoundTripSPYPosition(unittest.TestCase): """ Test a round-trip trade in SPY ETF where the initial trade is a buy/long of 100 shares of SPY, at a price of $220.45, with $1.00 commission. """ def setUp(self): """ Set up the Position object that will store the PnL. """ self.position = Position( "BOT", "SPY", 100, PriceParser.parse(239.08), PriceParser.parse(1.00), PriceParser.parse(220.45), PriceParser.parse(220.45), datetime(2016, 1, 1) ) def test_calculate_round_trip(self): """ After the subsequent purchase, carry out two more buys/longs and then close the position out with two additional sells/shorts. """ print("Buy 100 SPY at 239.08 with $1.00 commission. Update market value with bid/ask of 239.95/239.96:") self.position.update_market_value( PriceParser.parse(239.95), PriceParser.parse(239.96), datetime(2016, 1, 2) ) print(self.position, '\n') self.assertEqual(self.position.action, "BOT") self.assertEqual(self.position.ticker, "SPY") self.assertEqual(self.position.quantity, 100) self.assertEqual(self.position.open_quantity, 100) self.assertEqual(PriceParser.display(self.position.entry_price, 5), (239.08*100 + 1)/100) self.assertEqual(PriceParser.display(self.position.exit_price, 5), 0) self.assertEqual(PriceParser.display(self.position.total_commission), 1.00) self.assertEqual(PriceParser.display(self.position.cost_basis), 239.08*100 + 1.00) self.assertEqual(PriceParser.display(self.position.market_value), 239.95*100 * 1, 2) self.assertEqual(PriceParser.display(self.position.unrealised_pnl), (239.95*100 * 1) - (239.08*100 + 1.00), 2) self.assertEqual(PriceParser.display(self.position.realised_pnl), 0.00) print("Buy 300 shares to add to current position at 238.90 with 1.50 commission. Market at 238.96/238.97:") self.position.transact_shares( "BOT", 300, PriceParser.parse(238.90), PriceParser.parse(1.50) ) self.position.update_market_value( PriceParser.parse(238.96), PriceParser.parse(238.97), datetime(2016, 1, 3) ) print(self.position, '\n') self.assertEqual(self.position.quantity, 400) self.assertEqual(self.position.open_quantity, 400) self.assertEqual(PriceParser.display(self.position.entry_price, 5), (239.08*100+1 + 238.90*300+1.5)/400) self.assertEqual(PriceParser.display(self.position.exit_price, 5), 0) self.assertEqual(PriceParser.display(self.position.total_commission), 2.50) self.assertEqual(PriceParser.display(self.position.cost_basis), (239.08*100+1 + 238.90*300+1.5)) self.assertEqual(PriceParser.display(self.position.market_value), 238.96 * 400, 2) self.assertEqual(PriceParser.display(self.position.unrealised_pnl), (238.96 * 400) - (239.08*100+1 + 238.90*300+1.5), 2) self.assertEqual(PriceParser.display(self.position.realised_pnl), 0.00) print("Sell 150 shares from current position at 239.05 with 1.80 commission. Market at 239.19/239.20:") self.position.transact_shares( "SLD", 150, PriceParser.parse(239.05), PriceParser.parse(1.80) ) self.position.update_market_value( PriceParser.parse(239.19), PriceParser.parse(239.20), datetime(2016, 1, 4) ) print(self.position, '\n') self.assertEqual(self.position.quantity, 400) self.assertEqual(self.position.open_quantity, 100+300-150) self.assertEqual(PriceParser.display(self.position.entry_price, 5), (239.08*100+1 + 238.90*300+1.5)/400) self.assertEqual(PriceParser.display(self.position.exit_price, 5), (239.05*150+1.80)/150) self.assertEqual(PriceParser.display(self.position.total_commission), 4.30) self.assertEqual(PriceParser.display(self.position.cost_basis), (238.90*250)+(250/300*1.5)) self.assertEqual(PriceParser.display(self.position.market_value), 239.19 * 250) self.assertEqual(PriceParser.display(self.position.unrealised_pnl), (239.19 * 250) - ((238.90*250)+(250/300*1.5)), 2) self.assertEqual(PriceParser.display(self.position.realised_pnl), 1.45) print("Buy 250 shares adding to current position at 239.39 with 1.25 commission. Market at 245.24/245.25:") self.position.transact_shares( "BOT", 250, PriceParser.parse(239.39), PriceParser.parse(1.25) ) self.position.update_market_value( PriceParser.parse(245.24), PriceParser.parse(245.25), datetime(2016, 1, 5) ) print(self.position, '\n') self.assertEqual(self.position.quantity, 650) self.assertEqual(self.position.open_quantity, 100+300-150+250) self.assertEqual(PriceParser.display(self.position.entry_price, 5), round((239.08*100+1 + 238.90*300+1.5 + 239.39*250+1.25)/650, 5)) self.assertEqual(PriceParser.display(self.position.exit_price, 5), (239.05*150+1.80)/150) self.assertEqual(PriceParser.display(self.position.total_commission), 5.55) self.assertEqual(PriceParser.display(self.position.cost_basis), (238.90*250)+(250/300*1.5)+(239.39*250+1.25)) self.assertEqual(PriceParser.display(self.position.market_value), 245.24 * 500) self.assertEqual(PriceParser.display(self.position.unrealised_pnl), (245.24 * 500) - ((238.90*250)+(250/300*1.5)+(239.39*250+1.25)), 2) self.assertEqual(PriceParser.display(self.position.realised_pnl), 1.45) print("Sell 500 shares to close position at 244.09 with 5.22 commission. Market at 244.09/244.10:") self.position.transact_shares( "SLD", 500, PriceParser.parse(244.09), PriceParser.parse(5.22) ) self.position.update_market_value( PriceParser.parse(244.09), PriceParser.parse(244.10), datetime(2016, 1, 6) ) print(self.position, '\n') self.assertEqual(self.position.quantity, 650) self.assertEqual(self.position.open_quantity, 100+300-150+250-500) self.assertEqual(PriceParser.display(self.position.entry_price, 5), round((239.08*100+1 + 238.90*300+1.5 + 239.39*250+1.25)/650, 5)) self.assertEqual(PriceParser.display(self.position.exit_price, 5), round((239.05*150+1.80 + 244.09*500+5.22)/(150+500), 5)) self.assertEqual(PriceParser.display(self.position.total_commission), 5.55+5.22) self.assertEqual(PriceParser.display(self.position.cost_basis), 0) self.assertEqual(PriceParser.display(self.position.market_value), 0) self.assertEqual(PriceParser.display(self.position.unrealised_pnl), 0) self.assertEqual(PriceParser.display(self.position.realised_pnl), round(1.45+(244.09-238.90)*250-250/300*1.5+(244.09-239.39)*250-1.25-5.22, 2))
class TestLongRoundTripXIVPosition(unittest.TestCase): """ Tests a long position with multiple entries and exits during the trade """ def setUp(self): """ Set up the Position object that will store the PnL. """ self.position = Position("BOT", "XIV", 4352, PriceParser.parse(15.01), PriceParser.parse(1.00), PriceParser.parse(15.01), PriceParser.parse(15.01), datetime(2011, 2, 4)) def test_enter_long_position(self): """ Initial entry """ print( "Buy 4352 XIV at 15.01 with $1.00 commission. Update market value with bid/ask of 15.01/15.01:" ) self.position.update_market_value(PriceParser.parse(15.01), PriceParser.parse(15.01), datetime(2011, 2, 4)) print(self.position, '\n') self.assertEqual(self.position.action, "BOT") self.assertEqual(self.position.ticker, "XIV") self.assertEqual(self.position.quantity, 4352) self.assertEqual(self.position.buys, 4352) self.assertEqual(self.position.sells, 0) self.assertEqual(self.position.net, 4352) self.assertEqual(PriceParser.display(self.position.avg_bot, 5), 15.01) self.assertEqual(PriceParser.display(self.position.avg_sld, 5), 0) self.assertEqual(PriceParser.display(self.position.total_bot), 15.01 * 4352) self.assertEqual(PriceParser.display(self.position.total_sld), 0) self.assertEqual(PriceParser.display(self.position.net_total), 15.01 * 4352) self.assertEqual(PriceParser.display(self.position.total_commission), 1.00) self.assertEqual(PriceParser.display(self.position.net_incl_comm), 15.01 * 4352 - 1.00) self.assertEqual(PriceParser.display(self.position.avg_price, 5), round((15.01 * 4352 + 1.00) / 4352 / 1, 5)) self.assertEqual(PriceParser.display(self.position.cost_basis, 2), 15.01 * 4352 + 1.00) self.assertEqual(PriceParser.display(self.position.market_value), round(((15.01 + 15.01) / 2 * 4352 * 1), 2)) self.assertEqual( PriceParser.display(self.position.unrealised_pnl, 2), round(((15.01 + 15.01) / 2 * 4352 * 1) - (15.01 * 4352 + 1.00), 2)) self.assertEqual(PriceParser.display(self.position.realised_pnl, 2), 0.00) print( "Sell 2188 shares of current position at 15.24 with 1.00 commission. Market at 15.24/15.24:" ) self.position.transact_shares("SLD", 2188, PriceParser.parse(15.24), PriceParser.parse(1.00)) self.position.update_market_value(PriceParser.parse(15.24), PriceParser.parse(15.24), datetime(2011, 2, 7)) print(self.position, '\n') self.assertEqual(self.position.buys, 4352) self.assertEqual(self.position.sells, 2188) self.assertEqual(self.position.net, 4352 - 2188) self.assertEqual(PriceParser.display(self.position.avg_bot, 5), round((15.01 * 4352) / 4352, 5)) self.assertEqual(PriceParser.display(self.position.avg_sld, 5), round((15.24 * 2188) / 2188, 5)) self.assertEqual(PriceParser.display(self.position.total_bot), 15.01 * 4352) self.assertEqual(PriceParser.display(self.position.total_sld), 15.24 * 2188) self.assertEqual(PriceParser.display(self.position.net_total), round(15.24 * 2188 - (15.01 * 4352), 2)) self.assertEqual(PriceParser.display(self.position.total_commission), 2.00) self.assertEqual(PriceParser.display(self.position.net_incl_comm), round(((15.24 * 2188) - (15.01 * 4352)) - 2.00, 2)) self.assertEqual(PriceParser.display(self.position.avg_price, 5), round((15.01 * 4352 + 1) / 4352, 5)) self.assertEqual(PriceParser.display(self.position.cost_basis, 2), 32482.14) #15.01*(4352-2188) + 1) self.assertEqual(PriceParser.display(self.position.market_value), round((15.24 * (4352 - 2188) * 1), 2)) self.assertEqual(PriceParser.display(self.position.unrealised_pnl, 2), round((15.24 * (4352 - 2188) * 1) - (32482.14), 2)) self.assertEqual(PriceParser.display(self.position.realised_pnl), (15.24 - 15.01) * 2188 - 2) print( "Sell 75 shares from current position at 220.85 with 1.37 commission. Market at 220.85/220.86:" ) self.position.transact_shares("SLD", 75, PriceParser.parse(220.85), PriceParser.parse(1.37)) self.position.update_market_value(PriceParser.parse(220.85), PriceParser.parse(220.86), datetime(2016, 1, 3)) print(self.position, '\n') self.assertEqual(self.position.buys, 150) self.assertEqual(self.position.sells, 75) self.assertEqual(self.position.net, 75) self.assertEqual(PriceParser.display(self.position.avg_bot, 5), round((220.45 * 100 + 220.35 * 50) / 150, 5)) self.assertEqual(PriceParser.display(self.position.avg_sld, 5), 220.85 * 75 / 75) self.assertEqual(PriceParser.display(self.position.total_bot), 220.45 * 100 + 220.35 * 50) self.assertEqual(PriceParser.display(self.position.total_sld), 220.85 * 75) self.assertEqual(PriceParser.display(self.position.net_total), (220.85 * 75) - (220.45 * 100 + 220.35 * 50)) self.assertEqual(PriceParser.display(self.position.total_commission), 3.37) self.assertEqual(PriceParser.display(self.position.net_incl_comm), ((220.85 * 75) - (220.45 * 100 + 220.35 * 50)) - 3.37) self.assertEqual(PriceParser.display(self.position.avg_price, 5), (220.45 * 100 + 220.35 * 50 + 2.00) / 150 / 1) self.assertEqual(PriceParser.display(self.position.cost_basis), ((220.45 * 100 + 220.35 * 50 + 2.00) / 150) * 75) self.assertEqual(PriceParser.display(self.position.market_value), round(((220.85 + 220.86) / 2 * 75 * 1), 2)) self.assertEqual( PriceParser.display(self.position.unrealised_pnl), round(((220.85 + 220.86) / 2 * 75 * 1) - ((220.45 * 100 + 220.35 * 50 + 2.00) / 150) * 75, 2)) self.assertEqual( PriceParser.display(self.position.realised_pnl), (220.85 * 75) - ((220.45 * 100 + 220.35 * 50 + 2.00) / 150) * 75 - 1.37) print( "Sell 75 shares from current position at 221.24 with 1.37 commission. Market at 221.24/221.25:" ) self.position.transact_shares("SLD", 75, PriceParser.parse(221.24), PriceParser.parse(1.37)) self.position.update_market_value(PriceParser.parse(221.24), PriceParser.parse(221.25), datetime(2016, 1, 3)) print(self.position, '\n') self.assertEqual(self.position.buys, 150) self.assertEqual(self.position.sells, 150) self.assertEqual(self.position.net, 0) self.assertEqual(PriceParser.display(self.position.avg_bot, 5), round((220.45 * 100 + 220.35 * 50) / 150, 5)) self.assertEqual(PriceParser.display(self.position.avg_sld, 5), (220.85 * 75 + 221.24 * 75) / 150) self.assertEqual(PriceParser.display(self.position.total_bot), 220.45 * 100 + 220.35 * 50) self.assertEqual(PriceParser.display(self.position.total_sld), 220.85 * 75 + 221.24 * 75) self.assertEqual(PriceParser.display(self.position.net_total), (220.85 * 75 + 221.24 * 75) - (220.45 * 100 + 220.35 * 50)) self.assertEqual(PriceParser.display(self.position.total_commission), 3.37 + 1.37) self.assertEqual( PriceParser.display(self.position.net_incl_comm), round(((220.85 * 75 + 221.24 * 75) - (220.45 * 100 + 220.35 * 50)) - 3.37 - 1.37, 2)) self.assertEqual(PriceParser.display(self.position.avg_price, 5), (220.45 * 100 + 220.35 * 50 + 2.00) / 150 / 1) self.assertEqual(PriceParser.display(self.position.cost_basis), 0) self.assertEqual(PriceParser.display(self.position.market_value), 0) self.assertEqual(PriceParser.display(self.position.unrealised_pnl), 0) self.assertEqual( PriceParser.display(self.position.realised_pnl), round((220.85 * 75) - ((220.45 * 100 + 220.35 * 50 + 2.00) / 150) * 75 - 1.37 + (221.24 * 75) - ((220.45 * 100 + 220.35 * 50 + 2.00) / 150) * 75 - 1.37, 2))
class TestLongRoundTripSPYPosition(unittest.TestCase): """ Test a round-trip trade in SPY ETF where the initial trade is a buy/long of 100 shares of SPY, at a price of $220.45, with $1.00 commission. """ def setUp(self): """ Set up the Position object that will store the PnL. """ self.position = Position( "BOT", "SPY", 100, PriceParser.parse(239.08), PriceParser.parse(1.00), PriceParser.parse(220.45), PriceParser.parse(220.45), datetime(2016, 1, 1) ) def test_initial_bot(self): """ After the subsequent purchase, carry out two more buys/longs and then close the position out with two additional sells/shorts. """ print("Buy 100 SPY at 239.08 with $1.00 commission. Update market value with bid/ask of 239.95/239.96:") self.position.update_market_value( PriceParser.parse(239.95), PriceParser.parse(239.96), datetime(2016, 1, 2) ) print(self.position, '\n') self.assertIs(type(self.position.quantity), int) self.assertIs(type(self.position.open_quantity), int) self.assertIs(type(self.position.mul), int) self.assertIs(type(self.position.realised_pnl), int) self.assertIs(type(self.position.unrealised_pnl), int) self.assertIs(type(self.position.entry_price), int) self.assertIs(type(self.position.exit_price), int) self.assertIs(type(self.position.total_commission), int) self.assertIs(type(self.position.market_value), int) self.assertIs(type(self.position.cost_basis), int) print("Buy 300 shares to add to current position at 238.90 with 1.50 commission. Market at 238.96/238.97:") self.position.transact_shares( "BOT", 300, PriceParser.parse(238.90), PriceParser.parse(1.50) ) self.position.update_market_value( PriceParser.parse(238.96), PriceParser.parse(238.97), datetime(2016, 1, 3) ) print(self.position, '\n') self.assertIs(type(self.position.quantity), int) self.assertIs(type(self.position.open_quantity), int) self.assertIs(type(self.position.mul), int) self.assertIs(type(self.position.realised_pnl), int) self.assertIs(type(self.position.unrealised_pnl), int) self.assertIs(type(self.position.entry_price), int) self.assertIs(type(self.position.exit_price), int) self.assertIs(type(self.position.total_commission), int) self.assertIs(type(self.position.market_value), int) self.assertIs(type(self.position.cost_basis), int) print("Sell 150 shares from current position at 239.05 with 1.80 commission. Market at 239.19/239.20:") self.position.transact_shares( "SLD", 150, PriceParser.parse(239.05), PriceParser.parse(1.80) ) self.position.update_market_value( PriceParser.parse(239.19), PriceParser.parse(239.20), datetime(2016, 1, 4) ) print(self.position, '\n') self.assertIs(type(self.position.quantity), int) self.assertIs(type(self.position.open_quantity), int) self.assertIs(type(self.position.mul), int) self.assertIs(type(self.position.realised_pnl), int) self.assertIs(type(self.position.unrealised_pnl), int) self.assertIs(type(self.position.entry_price), int) self.assertIs(type(self.position.exit_price), int) self.assertIs(type(self.position.total_commission), int) self.assertIs(type(self.position.market_value), int) self.assertIs(type(self.position.cost_basis), int) print("Sell 250 shares to close position at 244.09 with 5.22 commission. Market at 244.09/244.10:") self.position.transact_shares( "SLD", 250, PriceParser.parse(244.09), PriceParser.parse(5.22) ) self.position.update_market_value( PriceParser.parse(244.09), PriceParser.parse(244.10), datetime(2016, 1, 6) ) print(self.position, '\n') self.assertIs(type(self.position.quantity), int) self.assertIs(type(self.position.open_quantity), int) self.assertIs(type(self.position.mul), int) self.assertIs(type(self.position.realised_pnl), int) self.assertIs(type(self.position.unrealised_pnl), int) self.assertIs(type(self.position.entry_price), int) self.assertIs(type(self.position.exit_price), int) self.assertIs(type(self.position.total_commission), int) self.assertIs(type(self.position.market_value), int) self.assertIs(type(self.position.cost_basis), int)