def setUp(self): self.sym = "EURUSD" self.aEq = self.assertEqual self.aIn = self.assertIn self.aRaise = self.assertRaises self.pl = PositionList()
class TestPositionList(unittest.TestCase): def setUp(self): self.sym = "EURUSD" self.aEq = self.assertEqual self.aIn = self.assertIn self.aRaise = self.assertRaises self.pl = PositionList() #self.ob = OrderBook(debug=False) def tearDown(self): pass def testAddEmpty(self): o = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p = self.pl.add(o, datetime.now()) self.aEq(self.pl.open, [p]) self.aEq(p.order_id, o.id) def testAddEmptyLevel(self): o = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p = self.pl.add(o, datetime.now(), level=2.0) self.aEq(p.entry, 2.0) self.aEq(p.mark, 2.0) self.aEq(self.pl.open, [p]) def testAddTriggered(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o2 = Order(self.sym, dir=Order.SELL, type=Order.LIMIT, level=1.1000, size=-10000) o1.trigger(o2) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.add(o2, datetime.now()) self.aEq([p1], self.pl.closed) self.aEq([], self.pl.open) def testAddLink(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o2 = Order(self.sym, dir=Order.SELL, type=Order.LIMIT, level=1.1000, size=-10000, link=o1.id) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.add(o2, datetime.now()) self.aEq([p1], self.pl.closed) self.aEq([], self.pl.open) def testMark(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o2 = Order("NOPE", dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.add(o1, datetime.now()) p3 = self.pl.add(o2, datetime.now()) b1 = Bar(self.sym, "20010102-230000,EURUSD,0.9507,0.9509,0.9505,0.9506") self.pl.mark(b1) #mark should update p1 & p2 but not p3 self.aEq(p1.mark, b1.cl) self.aEq(p2.mark, b1.cl) self.aEq(p3.mark, o2.level) def testRewind(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.rewind(o1.id) self.aEq(p1, p2) self.aEq([], self.pl.open) self.aEq([p1], self.pl.rewinded) def testClose(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p1 = self.pl.add(o1, datetime.now()) self.pl.close(p1) self.aEq([], self.pl.open) self.aEq([p1], self.pl.closed) self.aEq(p1.exit, o1.level) self.aEq(p1.exit, p1.mark) def testCloseMark(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p1 = self.pl.add(o1, datetime.now()) #close @ a specified mark level self.pl.close(p1, mark_level=2.0) self.aEq([], self.pl.open) self.aEq([p1], self.pl.closed) self.aEq(p1.exit, 2.0) def testCloseAll(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o2 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o3 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o4 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.add(o2, datetime.now()) p3 = self.pl.add(o2, datetime.now()) p4 = self.pl.add(o2, datetime.now()) self.pl.close_all() self.aEq([], self.pl.open) self.aEq([p1, p2, p3, p4], self.pl.closed) def testNetSize(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=100) o2 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=1000) o3 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) for o in (o1, o2, o3): self.pl.add(o, datetime.now()) self.aEq(self.pl.net_size(), 11100) def testValue(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o2 = Order(self.sym, dir=Order.SELL, type=Order.MARKET, level=1.0000, size=-10000) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.add(o2, datetime.now()) #mark = entry self.aEq(self.pl.value(), 0) p1.mark = 1.0001 self.aEq(round(self.pl.value()), 1) p2.mark = 0.9998 self.aEq(round(self.pl.value()), 3) p1.mark = 0.9999 self.aEq(round(self.pl.value()), 1) p2.mark = 1.0001 self.aEq(round(self.pl.value()), -2) def testSymOpen(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o2 = Order("NOPE", dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o3 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.add(o2, datetime.now()) p3 = self.pl.add(o3, datetime.now()) self.aEq(self.pl.sym_open(self.sym), [p1, p3]) self.aEq(self.pl.sym_open("NOPE"), [p2]) self.aEq(self.pl.sym_open("PETE"), []) self.aEq(self.pl.sym_open(None), [])
class TestPositionList(unittest.TestCase): def setUp(self): self.sym = "EURUSD" self.aEq = self.assertEqual self.aIn = self.assertIn self.aRaise = self.assertRaises self.pl = PositionList() #self.ob = OrderBook(debug=False) def tearDown(self): pass def testAddEmpty(self): o = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p = self.pl.add(o, datetime.now()) self.aEq(self.pl.open, [p]) self.aEq(p.order_id, o.id) def testAddEmptyLevel(self): o = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p = self.pl.add(o, datetime.now(), level=2.0) self.aEq(p.entry, 2.0) self.aEq(p.mark, 2.0) self.aEq(self.pl.open, [p]) def testAddTriggered(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o2 = Order(self.sym, dir=Order.SELL, type=Order.LIMIT, level=1.1000, size=-10000) o1.trigger(o2) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.add(o2, datetime.now()) self.aEq([p1], self.pl.closed) self.aEq([], self.pl.open) def testAddLink(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o2 = Order(self.sym, dir=Order.SELL, type=Order.LIMIT, level=1.1000, size=-10000, link=o1.id) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.add(o2, datetime.now()) self.aEq([p1], self.pl.closed) self.aEq([], self.pl.open) def testMark(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o2 = Order("NOPE", dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.add(o1, datetime.now()) p3 = self.pl.add(o2, datetime.now()) b1 = Bar(self.sym, "20010102-230000,EURUSD,0.9507,0.9509,0.9505,0.9506") self.pl.mark(b1) #mark should update p1 & p2 but not p3 self.aEq(p1.mark, b1.cl) self.aEq(p2.mark, b1.cl) self.aEq(p3.mark, o2.level) def testRewind(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.rewind(o1.id) self.aEq(p1, p2) self.aEq([], self.pl.open) self.aEq([p1], self.pl.rewinded) def testClose(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p1 = self.pl.add(o1, datetime.now()) self.pl.close(p1) self.aEq([], self.pl.open) self.aEq([p1], self.pl.closed) self.aEq(p1.exit, o1.level) self.aEq(p1.exit, p1.mark) def testCloseMark(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p1 = self.pl.add(o1, datetime.now()) #close @ a specified mark level self.pl.close(p1, mark_level=2.0) self.aEq([], self.pl.open) self.aEq([p1], self.pl.closed) self.aEq(p1.exit, 2.0) def testCloseAll(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o2 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o3 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o4 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.add(o2, datetime.now()) p3 = self.pl.add(o2, datetime.now()) p4 = self.pl.add(o2, datetime.now()) self.pl.close_all() self.aEq([], self.pl.open) self.aEq([p1, p2, p3, p4], self.pl.closed) def testNetSize(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=100) o2 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=1000) o3 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) for o in (o1, o2, o3): self.pl.add(o, datetime.now()) self.aEq(self.pl.net_size(), 11100) def testValue(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o2 = Order(self.sym, dir=Order.SELL, type=Order.MARKET, level=1.0000, size=-10000) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.add(o2, datetime.now()) #mark = entry self.aEq(self.pl.value(), 0) p1.mark = 1.0001 self.aEq(self.pl.value(), 1) p2.mark = 0.9998 self.aEq(self.pl.value(), 3) p1.mark = 0.9999 self.aEq(self.pl.value(), 1) p2.mark = 1.0001 self.aEq(self.pl.value(), -2) def testSymOpen(self): o1 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o2 = Order("NOPE", dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) o3 = Order(self.sym, dir=Order.BUY, type=Order.MARKET, level=1.0000, size=10000) p1 = self.pl.add(o1, datetime.now()) p2 = self.pl.add(o2, datetime.now()) p3 = self.pl.add(o3, datetime.now()) self.aEq(self.pl.sym_open(self.sym), [p1, p3]) self.aEq(self.pl.sym_open("NOPE"), [p2]) self.aEq(self.pl.sym_open("PETE"), []) self.aEq(self.pl.sym_open(None), [])