Пример #1
0
    def test_get_costs_zero(self):
        _asset_dict = {
            'ticker': 'test_ticker',
            'quotes': self.quotes,
        }
        a = Asset(**_asset_dict)

        self.assertEqual((0, 0), a.get_costs(pd.Timestamp('2018-01-21'), 1))
        self.assertEqual((0, 0), a.get_costs(pd.Timestamp('2018-01-21'), -1))
Пример #2
0
    def test_get_costs_percent(self):
        _asset_dict = {
            'ticker': 'test_ticker',
            'quotes': self.quotes,
            'costs': {
                'type': 'percent',  # percent or dollar or dynamic
                'value': 0.001,  # 0.1%
            }
        }
        a = Asset(**_asset_dict)

        # Default point value is 1.0
        self.assertEqual((-0.002, -0.004),
                         a.get_costs(pd.Timestamp('2018-01-01'), 2))
        self.assertEqual((-0.002, -0.004),
                         a.get_costs(pd.Timestamp('2018-01-01'), -2))

        # Error checks
        _asset_dict = {
            'ticker': 'test_ticker',
            'quotes': self.quotes,
            'costs': {
                'type': 'percent',  # percent or dollar or dynamic
                'value': [0.001],  # 0.1%
            }
        }
        # raise ValueError("'costs' value of 'percent' type must be a single float number")
        self.assertRaises(ValueError, Asset, **_asset_dict)

        _asset_dict = {
            'ticker': 'test_ticker',
            'quotes': self.quotes,
            'costs': {
                'type': 'percent',  # percent or dollar or dynamic
                'value': -0.001,  # 0.1%
            }
        }
        # raise ValueError("'costs' value of 'percent' type must be positive")
        self.assertRaises(ValueError, Asset, **_asset_dict)
Пример #3
0
    def test_get_costs_dynamic(self):
        costs = pd.DataFrame(
            {
                'c': [1, 1, 1, 2, 2, 2],
                'exec': [-2, 3, 4, 5, 2, -3],
            },
            index=[
                pd.Timestamp(d) for d in [
                    '2018-01-01', '2018-01-02', '2018-01-03', '2018-01-07',
                    '2018-01-08', '2018-01-09'
                ]
            ])

        _asset_dict = {
            'ticker': 'test_ticker',
            'quotes': self.quotes,
            'costs': {
                'type': 'dynamic',  # percent or dollar or dynamic
                'value': costs,
            }
        }
        a = Asset(**_asset_dict)

        self.assertEqual((-2, -4), a.get_costs(pd.Timestamp('2018-01-01'), 2))
        self.assertEqual((-2, -4), a.get_costs(pd.Timestamp('2018-01-01'), -2))

        # Data holes
        self.assertEqual((-2, -8), a.get_costs(pd.Timestamp('2018-01-04'), 2))
        self.assertEqual((-2, -8), a.get_costs(pd.Timestamp('2018-01-04'), -2))

        self.assertRaises(KeyError, a.get_costs, pd.Timestamp('2017-01-04'),
                          -2)

        #
        # Error checks
        #
        #
        _asset_dict = {
            'ticker': 'test_ticker',
            'quotes': self.quotes,
            'costs': {
                'type': 'dynamic',  # percent or dollar or dynamic
                'value': [1, 2, 3],
            }
        }
        # raise ValueError("'costs' value of 'dynamic' type must be a Pandas.DataFrame with columns ['c', 'exec']")
        self.assertRaises(ValueError, Asset, **_asset_dict)
        #
        #
        #
        costs = pd.DataFrame({
            'exec': [-2, 3, 4, 5, 2, -3],
        },
                             index=[
                                 pd.Timestamp(d) for d in [
                                     '2018-01-01', '2018-01-02', '2018-01-03',
                                     '2018-01-07', '2018-01-08', '2018-01-09'
                                 ]
                             ])
        _asset_dict = {
            'ticker': 'test_ticker',
            'quotes': self.quotes,
            'costs': {
                'type': 'dynamic',  # percent or dollar or dynamic
                'value': costs,
            }
        }
        # raise ValueError("'costs' value of 'dynamic' type must be a Pandas.DataFrame with columns ['c', 'exec']")
        self.assertRaises(ValueError, Asset, **_asset_dict)
        #
        #
        #
        costs = pd.DataFrame({
            'c': [1, 1, 1, 2, 2, 2],
        },
                             index=[
                                 pd.Timestamp(d) for d in [
                                     '2018-01-01', '2018-01-02', '2018-01-03',
                                     '2018-01-07', '2018-01-08', '2018-01-09'
                                 ]
                             ])
        _asset_dict = {
            'ticker': 'test_ticker',
            'quotes': self.quotes,
            'costs': {
                'type': 'dynamic',  # percent or dollar or dynamic
                'value': costs,
            }
        }
        # raise ValueError("'costs' value of 'dynamic' type must be a Pandas.DataFrame with columns ['c', 'exec']")
        self.assertRaises(ValueError, Asset, **_asset_dict)

        #
        # Inconsistent length
        #
        costs = pd.DataFrame({
            'exec': [-2, 3, 4, 5, 2],
            'c': [1, 1, 1, 2, 2],
        },
                             index=[
                                 pd.Timestamp(d) for d in [
                                     '2018-01-01', '2018-01-02', '2018-01-03',
                                     '2018-01-07', '2018-01-08'
                                 ]
                             ])
        _asset_dict = {
            'ticker': 'test_ticker',
            'quotes': self.quotes,
            'costs': {
                'type': 'dynamic',  # percent or dollar or dynamic
                'value': costs,
            }
        }
        #raise ValueError("'costs' value of 'dynamic' dataframe must be the same length and index as quotes")
        self.assertRaises(ValueError, Asset, **_asset_dict)
        #
        # Incosistent index of quotes and costs dataframes
        #
        costs = pd.DataFrame(
            {
                'exec': [-2, 3, 4, 5, 2, -3],
                'c': [1, 1, 1, 2, 2, 2],
            },
            index=[
                pd.Timestamp(d) for d in [
                    '2018-01-01', '2018-01-02', '2018-01-03', '2018-01-07',
                    '2018-01-08', '2018-01-10'
                ]
            ])
        _asset_dict = {
            'ticker': 'test_ticker',
            'quotes': self.quotes,
            'costs': {
                'type': 'dynamic',  # percent or dollar or dynamic
                'value': costs,
            }
        }
        # raise ValueError("'costs' value of 'dynamic' dataframe must be the same length and index as quotes")
        self.assertRaises(ValueError, Asset, **_asset_dict)