Exemple #1
0
def futures_system(data=None, config=None, trading_rules=None, log_level="on"):
    """

    :param data: data object (defaults to reading from csv files)
    :type data: sysdata.data.Data, or anything that inherits from it

    :param config: Configuration object (defaults to futuresconfig.yaml in this directory)
    :type config: sysdata.configdata.Config

    :param trading_rules: Set of trading rules to use (defaults to set specified in config object)
    :type trading_rules: list or dict of TradingRules, or something that can be parsed to that

    :param log_level: How much logging to do
    :type log_level: str


    >>> system=futures_system(log_level="off")
    >>> system
    System with stages: accounts, portfolio, positionSize, rawdata, combForecast, forecastScaleCap, rules
    >>> system.rules.get_raw_forecast("EDOLLAR", "ewmac2_8").dropna().head(2)
                ewmac2_8
    1983-10-10  0.695929
    1983-10-11 -0.604704

                ewmac2_8
    2015-04-21  0.172416
    2015-04-22 -0.477559
    >>> system.rules.get_raw_forecast("EDOLLAR", "carry").dropna().head(2)
                   carry
    1983-10-10  0.952297
    1983-10-11  0.854075

                   carry
    2015-04-21  0.350892
    2015-04-22  0.350892
    """

    if data is None:
        data = csvFuturesData()

    if config is None:
        config = Config(
            "pysystemtrade.systems.provided.futures_chapter15.futuresconfig.yaml")

    rules = Rules(trading_rules)

    system = System([
        Account(), Portfolios(), PositionSizing(), FuturesRawData(),
        ForecastCombine(), ForecastScaleCap(), rules
    ], data, config)

    system.set_logging_level(log_level)

    return system
Exemple #2
0
    def setUpWithEstimatedReturns(self):
        config = copy.copy(self.config)
        config.use_forecast_weight_estimates = True
        config.use_forecast_div_mult_estimates = True
        new_system = System([
            self.rawdata, self.rules, self.fcs,
            self.forecast_combine(),
            Account()
        ], self.data, config)

        return new_system
Exemple #3
0
def get_test_object_futures_with_rules_and_capping_estimate():
    """
    Returns some standard test data
    """
    data = csvFuturesData("sysdata.tests")
    rawdata = FuturesRawData()
    rules = Rules()
    config = Config("systems.provided.example.estimateexampleconfig.yaml")
    capobject = ForecastScaleCap()
    account = Account()
    return (account, capobject, rules, rawdata, data, config)
Exemple #4
0
 def test_estimated_instrument_weights(self):
     config = copy.copy(self.config)
     config.use_instrument_weight_estimates = True
     system2 = System([
         self.rawdata, self.rules, self.possizing, self.forecast_combine,
         self.fcs,
         Account(),
         self.portfolios()
     ], self.data, config)
     ans = system2.portfolio.get_instrument_weights()
     self.assertAlmostEqual(ans.BUND.values[-1], 0.541, places=2)
     self.assertAlmostEqual(ans.EDOLLAR.values[-1], 0.346, places=2)
     self.assertAlmostEqual(ans.US10.values[-1], 0.1121, places=2)
Exemple #5
0
    def test_actual_positions(self):
        config = copy.copy(self.config)
        config.use_instrument_weight_estimates = True
        system2 = System([
            self.rawdata, self.rules, self.possizing, self.forecast_combine,
            self.fcs,
            Account(),
            self.portfolios()
        ], self.data, config)

        ans = system2.portfolio.get_actual_position("EDOLLAR")
        self.assertAlmostEqual(ans.values[-1], 1.058623, places=4)

        ans = system2.portfolio.get_actual_buffers_for_position("EDOLLAR")
        self.assertAlmostEqual(ans.values[-1][0], 1.164485, places=4)
        self.assertAlmostEqual(ans.values[-1][1], 0.952761, places=4)
Exemple #6
0
    def test_estimated_dm(self):
        config = copy.copy(self.config)
        config.use_instrument_weight_estimates = True
        system2 = System([
            self.rawdata, self.rules, self.possizing, self.forecast_combine,
            self.fcs,
            Account(),
            self.portfolios()
        ], self.data, config)
        ans = system2.portfolio.get_instrument_correlation_matrix(
        ).corr_list[-1]

        self.assertAlmostEqual(ans[0][1], 0.3889, places=3)
        self.assertAlmostEqual(ans[0][2], 0.5014, places=3)
        self.assertAlmostEqual(ans[1][2], 0.8771, places=3)

        ans = system2.portfolio.get_estimated_instrument_diversification_multiplier(
        )
        self.assertAlmostEqual(ans.values[-1], 1.1855, places=3)
Exemple #7
0
def futures_system(data=None,
                   config=None,
                   trading_rules=None,
                   log_level="terse"):
    """

    :param data: data object (defaults to reading from csv files)
    :type data: sysdata.data.Data, or anything that inherits from it

    :param config: Configuration object (defaults to futuresconfig.yaml in this directory)
    :type config: sysdata.configdata.Config

    :param trading_rules: Set of trading rules to use (defaults to set specified in config object)
    :param trading_rules: list or dict of TradingRules, or something that can be parsed to that

    :param log_level: Set of trading rules to use (defaults to set specified in config object)
    :type log_level: str

    """

    if data is None:
        data = csvFuturesData()

    if config is None:
        config = Config(
            "systems.provided.futures_chapter15.futuresestimateconfig.yaml")

    rules = Rules(trading_rules)

    system = System([
        Account(),
        Portfolios(),
        PositionSizing(),
        FuturesRawData(),
        ForecastCombine(),
        ForecastScaleCap(), rules
    ], data, config)

    system.set_logging_level(log_level)

    return system
Exemple #8
0
def simplesystem(data=None, config=None, log_level="on"):
    """
    Example of how to 'wrap' a complete system
    """
    if config is None:
        config = Config("systems.provided.example.simplesystemconfig.yaml")
    if data is None:
        data = csvFuturesData()

    my_system = System([
        Account(),
        Portfolios(),
        PositionSizing(),
        ForecastCombine(),
        ForecastScaleCap(),
        Rules()
    ], data, config)

    my_system.set_logging_level(log_level)

    return my_system
Exemple #9
0
    def test_get_returns_for_optimisation(self):
        # Note: More thorough tests will be run inside optimisation module (FIXME next refactoring)
        # At this point we don't run proper tests but just check all the plumbing works with new caching code
        # FIXME rewrite proper tests once refactored optimisation generally

        system = self.setUpWithEstimatedReturns()

        print(
            system.combForecast.get_SR_cost_for_instrument_forecast(
                "EDOLLAR", "ewmac8"))
        print(
            system.combForecast.get_SR_cost_for_instrument_forecast(
                "BUND", "ewmac8"))
        print(
            system.combForecast.get_SR_cost_for_instrument_forecast(
                "US10", "ewmac8"))

        print(system.combForecast.has_same_cheap_rules_as_code("EDOLLAR"))
        print(system.combForecast.has_same_cheap_rules_as_code("BUND"))
        print(system.combForecast.has_same_cheap_rules_as_code("US10"))

        print(
            system.combForecast.get_returns_for_optimisation(
                "EDOLLAR").to_frame())
        print(
            system.combForecast.get_returns_for_optimisation(
                "BUND").to_frame())
        print(
            system.combForecast.get_returns_for_optimisation(
                "US10").to_frame())

        print(system.combForecast.has_same_cheap_rules_as_code("EDOLLAR"))
        print(system.combForecast.has_same_cheap_rules_as_code("BUND"))
        print(system.combForecast.has_same_cheap_rules_as_code("US10"))

        # default - don't pool costs, pool gross
        print(system.combForecast.get_raw_forecast_weights("BUND"))

        # pool neithier gross or costs
        config = copy.copy(system.config)
        config.forecast_weight_estimate['pool_gross_returns'] = False
        config.forecast_weight_estimate['forecast_cost_estimates'] = False

        system2 = System([
            self.rawdata, self.rules, self.fcs,
            self.forecast_combine(),
            Account()
        ], self.data, config)
        print(system2.combForecast.get_raw_forecast_weights("BUND"))

        # pool gross, not costs
        config = copy.copy(system.config)
        config.forecast_weight_estimate['pool_gross_returns'] = True
        config.forecast_weight_estimate['forecast_cost_estimates'] = False

        system2 = System([
            self.rawdata, self.rules, self.fcs,
            self.forecast_combine(),
            Account()
        ], self.data, config)
        print(system2.combForecast.get_raw_forecast_weights("BUND"))

        # pool both (special function)
        config = copy.copy(system.config)
        config.forecast_weight_estimate['pool_gross_returns'] = True
        config.forecast_weight_estimate['forecast_cost_estimates'] = True

        system2 = System([
            self.rawdata, self.rules, self.fcs,
            self.forecast_combine(),
            Account()
        ], self.data, config)
        print(system2.combForecast.get_raw_forecast_weights("BUND"))