def test_as_dict(self):
        exo_engine = EXOTestEngine('', '', self.date, self.datasource)
        trans = Transaction(self.fut_contract, self.date, 4.0, 12.3)
        exo_engine.position.add(trans)

        tr_list = exo_engine._transactions
        tr_list.append(trans)

        date = pd.date_range("2015-01-01 00:00:00", "2015-01-01 00:00:10", freq="1S")
        price = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.float64)

        exo_engine._series = pd.DataFrame(price, index=date, columns=['exo'])
        exo_engine._series.index = pd.to_datetime(exo_engine._series.index)


        exo_dic = exo_engine.as_dict()

        self.assertEqual(True, 'position' in exo_dic)
        self.assertEqual(exo_engine.position.as_dict(), exo_dic['position'])

        self.assertEqual(True, 'transactions' in exo_dic)
        self.assertEqual([trans.as_dict()], exo_dic['transactions'])

        self.assertEqual(exo_dic['name'], self.exo_engine.name)

        self.assertEqual(True, 'series' in exo_dic)
        self.assertEqual(pickle.dumps(exo_engine.series), exo_dic['series'])
Esempio n. 2
0
class TransactionTestCase(unittest.TestCase):
    def setUp(self):
        self.assetindex = AssetIndexDicts()
        self.symbol = 'EP'
        self.date = datetime(2014, 1, 5, 0, 0, 0)
        self.futures_limit = 12
        self.instrument_dbid = 11
        self.datasource = DataSourceForTest(self.assetindex, self.futures_limit, 0)
        self.instrument = self.datasource.get(self.symbol, self.date)

        self.contract_dict = {'_id': '577a4fa34b01f47f84cab23c',
                              'contractname': 'F.EPZ16',
                              'cqgsymbol': 'F.EPZ16',
                              'expirationdate': datetime(2016, 12, 16, 0, 0),
                              'idcontract': 6570,
                              'idinstrument': 11,
                              'month': 'Z',
                              'monthint': 12,
                              'year': 2016}
        self.fut_contract = FutureContract(self.contract_dict, self.instrument)
        self.trans = Transaction(self.fut_contract, self.date, 4.0, 12.3, leg_name='leg1')

    def test_constructor(self):
        t = Transaction(self.fut_contract, self.date, 4.0, 12.3)

        self.assertEqual(t._asset, self.fut_contract)
        self.assertEqual(t._date, self.date)
        self.assertEqual(t._qty, 4.0)
        self.assertEqual(t._price, 12.3)
        self.assertEqual(t._leg_name, '')

    def test_has_leg_name(self):
        self.assertEqual(self.trans._leg_name, 'leg1')
        self.assertEqual(self.trans.leg_name, 'leg1')

    def test_has_asset(self):
        self.assertEqual(self.trans.asset, self.fut_contract)

    def test_has_date(self):
        self.assertEqual(self.trans.date, self.date)

    def test_has_qty(self):
        self.assertEqual(self.trans.qty, 4.0)

    def test_has_price(self):
        self.assertEqual(self.trans.price, 12.3)

    def test_has_usdvalue(self):
        self.assertEqual(self.trans.usdvalue, 4*12.3*self.fut_contract.pointvalue)

    def test_has_as_dict(self):
        self.assertEqual({'date': self.trans.date,
                          'qty': self.trans.qty,
                          'price': self.trans.price,
                          'asset': self.fut_contract.as_dict(),
                          'usdvalue': self.trans.usdvalue}, self.trans.as_dict())