Beispiel #1
0
    def testRules(self):

        # config=Config(dict(trading_rules=dict(ewmac=dict(function="pysystemtrade.systems.provided.example.rules.ewmac_forecast_with_defaults"))))
        data = csvFuturesData("sysdata.tests")

        rules = Rules(
            dict(
                function=
                "pysystemtrade.systems.provided.example.rules.ewmac_forecast_with_defaults"
            ))
        system = System([rules], data)

        ans = system.rules.get_raw_forecast("EDOLLAR", "rule0")
        self.assertAlmostEqual(ans.tail(1).values[0], 2.1384223788141838, 5)

        config = Config(
            dict(trading_rules=dict(ewmac=dict(
                function=
                "pysystemtrade.systems.provided.example.rules.ewmac_forecast_with_defaults"
            ))))
        rules = Rules()
        system = System([rules], data, config)
        ans = system.rules.get_raw_forecast("EDOLLAR", "ewmac")
        self.assertAlmostEqual(ans.tail(1).values[0], 2.1384223788141838, 5)

        config = Config(
            "pysystemtrade.systems.provided.example.exampleconfig.yaml")
        rawdata = RawData()

        rules = Rules()
        system = System([rules, rawdata], data, config)
        ans = system.rules.get_raw_forecast("EDOLLAR", "ewmac8")
        self.assertAlmostEqual(ans.tail(1).values[0], 0.16438313875, 5)
Beispiel #2
0
    def testCallingTradingRule(self):

        # config=Config(dict(trading_rules=dict(ewmac=dict(function="pysystemtrade.systems.provided.example.rules.ewmac_forecast_with_defaults"))))
        data = csvFuturesData("sysdata.tests")

        rawdata = RawData()
        rules = Rules()
        system = System([rawdata, rules], data)

        # Call with default data and config
        rule = TradingRule(ewmac_forecast_with_defaults)
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], 2.1384223788141838, 5)

        # Change the data source
        rule = TradingRule((
            "pysystemtrade.systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
            ["rawdata.get_daily_prices",
             "rawdata.daily_returns_volatility"], dict()))

        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], 0.029376, 5)

        rule = TradingRule(
            dict(
                function=
                "pysystemtrade.systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
                data=[
                    "rawdata.get_daily_prices",
                    "rawdata.daily_returns_volatility"
                ],
                other_args=dict(Lfast=50, Lslow=200)))
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], 3.84426755)
Beispiel #3
0
def get_test_object_futures_with_rules():
    """
    Returns some standard test data
    """
    data = csvFuturesData("sysdata.tests")
    rawdata = FuturesRawData()
    rules = Rules()
    config = Config(
        "pysystemtrade.systems.provided.example.exampleconfig.yaml")
    return (rules, rawdata, data, config)
Beispiel #4
0
    def testCarryRule(self):
        data = csvFuturesData("sysdata.tests")

        rawdata = FuturesRawData()
        rules = Rules()
        system = System([rawdata, rules], data)
        rule = TradingRule(carry, [
            "rawdata.daily_annualised_roll", "rawdata.daily_returns_volatility"
        ], dict(smooth_days=90))
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], 0.411686026, 5)
Beispiel #5
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
Beispiel #6
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)
Beispiel #7
0
    def update(self, message):
        """
        Subscriber pattern main method. Will be called each time a registered
        event occurs.
        
        :param message: dict with instrument names as keys and pd.Dataframe
                        as values.
        """
        data = self.get_data(message)
        # Ib data Object. This is the Object that manage the data from ibAPI.
        my_data = ib_Data(data)

        # create a list with the instruments for the config object
        my_config = Config("private.config.yaml")  # create a config object.
        my_config.instruments = my_data.get_instruments_list()

        # Setting the rules.
        my_rules = Rules(dict(ewmac=ewmac))
        my_rules.trading_rules()

        # Initializing the system with all the stages.
        my_stages = [
            Account(),
            Portfolios(),
            PositionSizing(),
            ForecastCombine(),
            ForecastScaleCap(), my_rules
        ]
        my_system = System(stage_list=my_stages,
                           data=my_data,
                           config=my_config)

        # Forecast for each instrument.
        for i in message.keys():
            print("\n{} forecast:\n".format(i))
            position = my_system.portfolio.get_notional_position(i)

            # Publishing forecast.
            message = dict(ticker=i, forecast=int(position.iloc[-1]))
            print(position.tail(5))
            self.pub.dispatch(i, message)
Beispiel #8
0
def get_test_object_futures_with_comb_forecasts():
    """
    Returns some standard test data
    """
    data = csvFuturesData("sysdata.tests")
    rawdata = FuturesRawData()
    rules = Rules()
    config = Config(
        "pysystemtrade.systems.provided.example.exampleconfig.yaml")
    capobject = ForecastScaleCap()
    combobject = ForecastCombine()
    return (combobject, capobject, rules, rawdata, data, config)
Beispiel #9
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
Beispiel #10
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