Ejemplo n.º 1
0
 def test_are_dependent_set_two_depends(self):
     m = StubModel()
     rf = ReactiveFramework(m)
     self.assertFalse(rf._are_dependents_set('settle_date'))
     rf.set_value('trade_date', date(2015, 1, 1))
     self.assertFalse(rf._are_dependents_set('settle_date'))
     rf.set_value('calendar', 'USD')
     self.assertTrue(rf._are_dependents_set('settle_date'))
Ejemplo n.º 2
0
 def test_are_dependent_set_two_depends(self):
     m = StubModel()
     rf = ReactiveFramework(m)
     self.assertFalse(rf._are_dependents_set( 'settle_date' ) )
     rf.set_value('trade_date', date(2015,1,1))
     self.assertFalse(rf._are_dependents_set( 'settle_date' ) )
     rf.set_value('calendar', 'USD')
     self.assertTrue(rf._are_dependents_set( 'settle_date' ) )
Ejemplo n.º 3
0
    def test_save(self):
        m = StubModel()
        rf = ReactiveFramework(m)
        rf.set_value('quantity', 100)
        rf.validate()
        rf.save()

        trade = m.get_domain_object('Trade')
        self.assertEquals(trade.quantity, rf.get_value('quantity'))
        self.assertTrue(m.save_called)
Ejemplo n.º 4
0
    def test_save(self):
        m = StubModel()
        rf = ReactiveFramework(m)
        rf.set_value('quantity', 100)
        rf.validate()
        rf.save()

        trade = m.get_domain_object('Trade')
        self.assertEquals(trade.quantity, rf.get_value('quantity'))
        self.assertTrue(m.save_called)
Ejemplo n.º 5
0
 def test_when_recalculation_method_error_is_recorded_but_not_raised(self):
     class FailedStub(StubModel):
         def calc_settle_date(self):
             raise Exception("I failed")
     m = FailedStub()
     rf = ReactiveFramework(m)
     rf.set_value('trade_date', date(2015,1,1))
     rf.set_value('calendar', 'USD')
     self.assertEquals(None, rf.get_value('settle_date' ) )
     f =  rf.get_field('settle_date' )
     self.assertEquals("I failed", f.error)
Ejemplo n.º 6
0
    def test_when_recalculation_method_error_is_recorded_but_not_raised(self):
        class FailedStub(StubModel):
            def calc_settle_date(self):
                raise Exception("I failed")

        m = FailedStub()
        rf = ReactiveFramework(m)
        rf.set_value('trade_date', date(2015, 1, 1))
        rf.set_value('calendar', 'USD')
        self.assertEquals(None, rf.get_value('settle_date'))
        f = rf.get_field('settle_date')
        self.assertEquals("I failed", f.error)
Ejemplo n.º 7
0
    def test_when_mapper_fails_multiple_errors_are_returned(self):
        class FailedStub(StubModel):
            FIELD_DEPENDS = {'currency': ['instrument'], 'instrument': []}

            def map_instrument(self, field, direction):
                raise Exception("I failed")

        m = FailedStub()
        rf = ReactiveFramework(m)
        rf.set_value('instrument', "ABC")
        rf.set_value('currency', "USD")
        status = rf.validate()
        expect = {'instrument': 'I failed', 'currency': 'I failed'}
        self.assertEquals(expect, status)
Ejemplo n.º 8
0
 def test_when_mapper_fails_multiple_errors_are_returned(self):
     class FailedStub(StubModel):
         FIELD_DEPENDS = { 
                          'currency':['instrument'],
                          'instrument':[]
                         }
         def map_instrument(self, field, direction):
             raise Exception("I failed")
     m = FailedStub()
     rf = ReactiveFramework(m)
     rf.set_value('instrument', "ABC")
     rf.set_value('currency', "USD")
     status = rf.validate()
     expect = {'instrument': 'I failed', 'currency': 'I failed'}
     self.assertEquals(expect, status)
Ejemplo n.º 9
0
 def test_set_field_recalcs(self):
     m = StubModel()
     rf = ReactiveFramework(m)
     self.assertEquals([], rf.set_value('quantity', 100))
     self.assertEquals(['commission'], rf.set_value('trade_date', date(2015,2,1)))
     self.assertEquals(['commission'], rf.set_value('quantity', 150))
     self.assertListEqual(sorted(['calendar', 'settle_date']), sorted(rf.set_value('currency', 'USD')))
     self.assertListEqual(sorted(['commission', 'settle_date']), sorted(rf.set_value('trade_date', date(2015,2,1))))
     self.assertEquals([], rf.set_value('commission', 4))
     self.assertEquals(['settle_date'], rf.set_value('trade_date', date(2015,2,1)))
     self.assertEquals('USD', rf.get_value('currency'))
     self.assertEquals( date(2015,2,1), rf.get_value('trade_date'))
     self.assertEquals( date(2015,2,2), rf.get_value('settle_date'))
     self.assertEquals( 4, rf.get_value('commission'))
     self.assertEquals( "US", rf.get_value('calendar'))
Ejemplo n.º 10
0
 def test_set_field_recalcs(self):
     m = StubModel()
     rf = ReactiveFramework(m)
     self.assertEquals([], rf.set_value('quantity', 100))
     self.assertEquals(['commission'],
                       rf.set_value('trade_date', date(2015, 2, 1)))
     self.assertEquals(['commission'], rf.set_value('quantity', 150))
     self.assertListEqual(sorted(['calendar', 'settle_date']),
                          sorted(rf.set_value('currency', 'USD')))
     self.assertListEqual(
         sorted(['commission', 'settle_date']),
         sorted(rf.set_value('trade_date', date(2015, 2, 1))))
     self.assertEquals([], rf.set_value('commission', 4))
     self.assertEquals(['settle_date'],
                       rf.set_value('trade_date', date(2015, 2, 1)))
     self.assertEquals('USD', rf.get_value('currency'))
     self.assertEquals(date(2015, 2, 1), rf.get_value('trade_date'))
     self.assertEquals(date(2015, 2, 2), rf.get_value('settle_date'))
     self.assertEquals(4, rf.get_value('commission'))
     self.assertEquals("US", rf.get_value('calendar'))
Ejemplo n.º 11
0
 def test_are_dependent_set_one_depends(self):
     m = StubModel()
     rf = ReactiveFramework(m)
     self.assertFalse(rf._are_dependents_set('calendar'))
     rf.set_value('currency', 'USD')
     self.assertTrue(rf._are_dependents_set('calendar'))
Ejemplo n.º 12
0
 def test_calc_settle_date_basic(self):
     rf = ReactiveFramework(VanillaModel())
     rf.set_value('trade_date', date(2015,5,6))
     self.assertEquals(date(2015,5,8), rf.get_value('settle_date'))
Ejemplo n.º 13
0
 def book_a_trade(self):
     rf = ReactiveFramework(VanillaModel())
     rf.set_value('quantity', 100)
     rf.set_value('price', 600)
     rf.set_value('action', "Buy")
     rf.set_value('instrument', "GOOGL.O")
     rf.set_value('trade_date', date(2015, 5, 5))
     rf.set_value('fund', "Fund1")
     rf.set_value('trader', "Trader1")
     rf.set_value('analyst', "Analyst1")
     rf.set_value('broker', "Broker1")
     rf.set_value('clearer', "Clearer1")
     rf.set_value('sector', "sec1")
     rf.set_value('strategy', "strat1")
     rf.set_value('trade_id', "ZZ{}".format(get_next_id()))
     self.assertEquals({},  rf.validate() )
     trade_id = rf.save()
     return trade_id
Ejemplo n.º 14
0
 def book_a_trade(self):
     rf = ReactiveFramework(VanillaModel())
     rf.set_value('quantity', 100)
     rf.set_value('price', 600)
     rf.set_value('action', "Buy")
     rf.set_value('instrument', "GOOGL.O")
     rf.set_value('trade_date', date(2015, 5, 5))
     rf.set_value('fund', "Fund1")
     rf.set_value('trader', "Trader1")
     rf.set_value('analyst', "Analyst1")
     rf.set_value('broker', "Broker1")
     rf.set_value('clearer', "Clearer1")
     rf.set_value('sector', "sec1")
     rf.set_value('strategy', "strat1")
     rf.set_value('trade_id', "ZZ{}".format(get_next_id()))
     self.assertEquals({}, rf.validate())
     trade_id = rf.save()
     return trade_id
Ejemplo n.º 15
0
 def test_are_dependent_set_one_depends(self):
     m = StubModel()
     rf = ReactiveFramework(m)
     self.assertFalse(rf._are_dependents_set( 'calendar' ) )
     rf.set_value('currency', 'USD')
     self.assertTrue(rf._are_dependents_set( 'calendar' ) )
Ejemplo n.º 16
0
 def test_calc_settle_date_basic(self):
     rf = ReactiveFramework(VanillaModel())
     rf.set_value('trade_date', date(2015, 5, 6))
     self.assertEquals(date(2015, 5, 8), rf.get_value('settle_date'))
Ejemplo n.º 17
0
    def test_integration(self):
        s = Session()
        s.query(Trade).delete()
        s.query(Portfolio).delete()
        trade_id = self.book_a_trade()
        trades = s.query(Trade).all()
        self.assertEquals( 1, len(trades) )
        self.assertEquals( trade_id, trades[0].trade_id)
        self.assertEquals( 100, trades[0].quantity )
        self.assertEquals( 600, trades[0].price )
        self.assertEquals( "Buy", trades[0].action.value )
        self.assertEquals( "GOOGL.O", trades[0].instrument.name )
        self.assertEquals( "USD", trades[0].currency.name )
        self.assertEquals( date(2015,5,5), trades[0].trade_date )
        self.assertEquals( date(2015,5,7), trades[0].settle_date )
        self.assertEquals( "Fund1", trades[0].portfolio.fund.name )   

        s = Session()
        ports = s.query(Portfolio).all()
        self.assertEquals( 1, len(ports) )
        self.assertEquals( 'Fund1', ports[0].fund.name )
        self.assertEquals( 'Trader1', ports[0].trader.name )
        self.assertEquals( 'Analyst1', ports[0].analyst.name )
        self.assertEquals( 'Broker1', ports[0].broker.name )
        self.assertEquals( 'Clearer1', ports[0].clearer.name )
        self.assertEquals( 'sec1', ports[0].sector )
        self.assertEquals( 'strat1', ports[0].strategy )

        rf = ReactiveFramework(VanillaModel())
        rf.load(trade_id)
        self.assertEquals( 100, rf.get_value('quantity' ))
        self.assertEquals( 600, rf.get_value('price' ))
        self.assertEquals( "Buy", rf.get_value('action' ))
        self.assertEquals( "GOOGL.O", rf.get_value('instrument' ))
        self.assertEquals( "USD", rf.get_value('currency'))
        self.assertEquals( date(2015,5,5), rf.get_value('trade_date' ))
        self.assertEquals( date(2015,5,7), rf.get_value('settle_date' ))
        self.assertEquals( "Fund1", rf.get_value('fund' ))
        self.assertEquals( "Trader1", rf.get_value('trader' ))
        self.assertEquals( "Analyst1", rf.get_value('analyst' ))
        self.assertEquals( "Broker1", rf.get_value('broker' ))
        self.assertEquals( "Clearer1", rf.get_value('clearer' ))
        self.assertEquals( "sec1", rf.get_value('sector' ))
        self.assertEquals( "strat1", rf.get_value('strategy' ))
        
        
        rf.set_value( 'quantity', -140)
        rf.set_value( 'price', 600)
        rf.set_value( 'action', "Sell")
        rf.set_value( 'instrument', "GOOGL.O")
        rf.set_value( 'trade_date', date(2015,5,5))
        rf.set_value( 'fund', "Fund2")
        self.assertEquals({}, rf.validate())
        rf.save()
 
        s = Session()
        trades = s.query(Trade).all()
        self.assertEquals( 1, len(trades) )
        self.assertEquals( -140, trades[0].quantity )
        self.assertEquals( 600, trades[0].price )
        self.assertEquals( "Sell", trades[0].action.value )
        self.assertEquals( "GOOGL.O", trades[0].instrument.name )
        self.assertEquals( "USD", trades[0].currency.name )
        self.assertEquals( date(2015,5,5), trades[0].trade_date )
        self.assertEquals( date(2015,5,7), trades[0].settle_date )
        self.assertEquals( "Fund2", trades[0].portfolio.fund.name )
 
        rf.delete()
        trades = s.query(Trade).all()
        self.assertEquals( 0, len(trades) )
Ejemplo n.º 18
0
    def test_integration(self):
        s = Session()
        s.query(Trade).delete()
        s.query(Portfolio).delete()
        trade_id = self.book_a_trade()
        trades = s.query(Trade).all()
        self.assertEquals(1, len(trades))
        self.assertEquals(trade_id, trades[0].trade_id)
        self.assertEquals(100, trades[0].quantity)
        self.assertEquals(600, trades[0].price)
        self.assertEquals("Buy", trades[0].action.value)
        self.assertEquals("GOOGL.O", trades[0].instrument.name)
        self.assertEquals("USD", trades[0].currency.name)
        self.assertEquals(date(2015, 5, 5), trades[0].trade_date)
        self.assertEquals(date(2015, 5, 7), trades[0].settle_date)
        self.assertEquals("Fund1", trades[0].portfolio.fund.name)

        s = Session()
        ports = s.query(Portfolio).all()
        self.assertEquals(1, len(ports))
        self.assertEquals('Fund1', ports[0].fund.name)
        self.assertEquals('Trader1', ports[0].trader.name)
        self.assertEquals('Analyst1', ports[0].analyst.name)
        self.assertEquals('Broker1', ports[0].broker.name)
        self.assertEquals('Clearer1', ports[0].clearer.name)
        self.assertEquals('sec1', ports[0].sector)
        self.assertEquals('strat1', ports[0].strategy)

        rf = ReactiveFramework(VanillaModel())
        rf.load(trade_id)
        self.assertEquals(100, rf.get_value('quantity'))
        self.assertEquals(600, rf.get_value('price'))
        self.assertEquals("Buy", rf.get_value('action'))
        self.assertEquals("GOOGL.O", rf.get_value('instrument'))
        self.assertEquals("USD", rf.get_value('currency'))
        self.assertEquals(date(2015, 5, 5), rf.get_value('trade_date'))
        self.assertEquals(date(2015, 5, 7), rf.get_value('settle_date'))
        self.assertEquals("Fund1", rf.get_value('fund'))
        self.assertEquals("Trader1", rf.get_value('trader'))
        self.assertEquals("Analyst1", rf.get_value('analyst'))
        self.assertEquals("Broker1", rf.get_value('broker'))
        self.assertEquals("Clearer1", rf.get_value('clearer'))
        self.assertEquals("sec1", rf.get_value('sector'))
        self.assertEquals("strat1", rf.get_value('strategy'))

        rf.set_value('quantity', -140)
        rf.set_value('price', 600)
        rf.set_value('action', "Sell")
        rf.set_value('instrument', "GOOGL.O")
        rf.set_value('trade_date', date(2015, 5, 5))
        rf.set_value('fund', "Fund2")
        self.assertEquals({}, rf.validate())
        rf.save()

        s = Session()
        trades = s.query(Trade).all()
        self.assertEquals(1, len(trades))
        self.assertEquals(-140, trades[0].quantity)
        self.assertEquals(600, trades[0].price)
        self.assertEquals("Sell", trades[0].action.value)
        self.assertEquals("GOOGL.O", trades[0].instrument.name)
        self.assertEquals("USD", trades[0].currency.name)
        self.assertEquals(date(2015, 5, 5), trades[0].trade_date)
        self.assertEquals(date(2015, 5, 7), trades[0].settle_date)
        self.assertEquals("Fund2", trades[0].portfolio.fund.name)

        rf.delete()
        trades = s.query(Trade).all()
        self.assertEquals(0, len(trades))