Пример #1
0
    def test_simple_trading_rule(self):
        """
        This is (mostly) the code from 'examples.introduction.asimpletradingrule',
        but without graph plotting
        """
        # Get some data
        data = csvFuturesSimData()

        print(data)
        print(data.get_instrument_list())
        print(data.get_raw_price("EDOLLAR").tail(5))

        print(data["VIX"])
        print(data.keys())
        print(data.get_instrument_raw_carry_data("EDOLLAR").tail(6))

        instrument_code = "VIX"
        price = data.daily_prices(instrument_code)
        ewmac = self.calc_ewmac_forecast(price, 32, 128)
        ewmac2 = self.calc_ewmac_forecast(price, 16, 64)

        ewmac.columns = ["forecast"]
        print(ewmac.tail(5))

        account = accountCurve(price, forecast=ewmac)
        account2 = accountCurve(price, forecast=ewmac2)

        account.curve()
        account2.curve()

        print(account.percent().stats())
        print(account2.percent().stats())
Пример #2
0
def generate_roll_calendars_from_provided_multiple_csv_prices(
    output_datapath=arg_not_supplied, ):
    if output_datapath is arg_not_supplied:
        print(
            "USING DEFAULT DATAPATH WILL OVERWRITE PROVIDED DATA in /data/futures/"
        )
    else:
        print("Writing to %s" % output_datapath)
    input(
        "This will overwrite any existing roll calendars to %s: CRTL-C if you aren't sure!"
        % str(output_datapath))
    csv_roll_calendars = csvRollCalendarData(datapath=output_datapath)
    sim_futures_data = csvFuturesSimData()

    instrument_list = sim_futures_data.get_instrument_list()

    for instrument_code in instrument_list:
        print(instrument_code)
        multiple_prices = sim_futures_data.get_multiple_prices(instrument_code)

        roll_calendar = rollCalendar.back_out_from_multiple_prices(
            multiple_prices)
        print("Calendar:")
        print(roll_calendar)

        # We ignore duplicates since this is run regularly
        csv_roll_calendars.add_roll_calendar(instrument_code,
                                             roll_calendar,
                                             ignore_duplication=True)
Пример #3
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 = csvFuturesSimData()

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

    my_system.set_logging_level(log_level)

    return my_system
Пример #4
0
def get_test_object_futures():
    """
    Returns some standard test data
    """
    data = csvFuturesSimData()
    rawdata = FuturesRawData()
    config = Config("systems.provided.example.exampleconfig.yaml")
    return (rawdata, data, config)
Пример #5
0
 def test_prebaked_from_confg(self):
     """
     This is the config system from 'examples.introduction.prebakedsimplesystems'
     """
     my_config = Config("systems.provided.example.simplesystemconfig.yaml")
     my_data = csvFuturesSimData()
     my_system = simplesystem(config=my_config, data=my_data)
     print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5))
Пример #6
0
def get_test_object_futures_with_rules_and_capping():
    """
    Returns some standard test data
    """
    data = csvFuturesSimData()
    rawdata = FuturesRawData()
    rules = Rules()
    config = Config("systems.provided.example.exampleconfig.yaml")
    capobject = ForecastScaleCap()
    return (capobject, rules, rawdata, data, config)
Пример #7
0
def get_test_object_futures_with_pos_sizing():
    """
    Returns some standard test data
    """
    data = csvFuturesSimData()
    rawdata = FuturesRawData()
    rules = Rules()
    config = Config("systems.provided.example.exampleconfig.yaml")
    capobject = ForecastScaleCap()
    combobject = ForecastCombine()
    posobject = PositionSizing()
    return (posobject, combobject, capobject, rules, rawdata, data, config)
Пример #8
0
def get_test_object_futures():
    """
    Returns some standard test data
    """
    data = csvFuturesSimData(datapath_dict=dict(
        config_data="sysdata.tests.configtestdata",
        adjusted_prices="sysdata.tests.adjtestdata",
        spot_fx_data="sysdata.tests.fxtestdata",
        multiple_price_data="sysdata.tests.multiplepricestestdata",
    ))
    rawdata = FuturesRawData()
    config = Config("systems.provided.example.exampleconfig.yaml")
    return (rawdata, data, config)
Пример #9
0
def get_test_object_futures_with_rules_and_capping():
    """
    Returns some standard test data
    """
    data = csvFuturesSimData(datapath_dict=dict(
        config_data="sysdata.tests.configtestdata",
        adjusted_prices="sysdata.tests.adjtestdata",
        spot_fx_data="sysdata.tests.fxtestdata",
        multiple_price_data="sysdata.tests.multiplepricestestdata",
    ))
    rawdata = FuturesRawData()
    rules = Rules()
    config = Config("systems.provided.example.exampleconfig.yaml")
    capobject = ForecastScaleCap()
    return (capobject, rules, rawdata, data, config)
Пример #10
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.simData, 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 = csvFuturesSimData()

    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
Пример #11
0
def get_test_object_futures_with_pos_sizing_estimates():
    """
    Returns some standard test data
    """
    data = csvFuturesSimData(datapath_dict=dict(
        config_data="sysdata.tests.configtestdata",
        adjusted_prices="sysdata.tests.adjtestdata",
        spot_fx_data="sysdata.tests.fxtestdata",
        multiple_price_data="sysdata.tests.multiplepricestestdata",
    ))
    rawdata = FuturesRawData()
    rules = Rules()
    config = Config("systems.provided.example.estimateexampleconfig.yaml")
    capobject = ForecastScaleCap()
    combobject = ForecastCombine()
    posobject = PositionSizing()
    account = Account()
    return (account, posobject, combobject, capobject, rules, rawdata, data,
            config)
Пример #12
0
def generate_roll_calendars_from_provided_multiple_csv_prices(output_datapath):
    input(
        "This will overwrite the roll calendars in %s : CRTL-C if you aren't sure!"
        % output_datapath)
    csv_roll_calendars = csvRollCalendarData(datapath=output_datapath)
    sim_futures_data = csvFuturesSimData()

    instrument_list = sim_futures_data.get_instrument_list()

    for instrument_code in instrument_list:
        print(instrument_code)
        multiple_prices = sim_futures_data.get_multiple_prices(instrument_code)

        roll_calendar = rollCalendar.back_out_from_multiple_prices(
            multiple_prices)
        print("Calendar:")
        print(roll_calendar)

        # We ignore duplicates since this is run regularly
        csv_roll_calendars.add_roll_calendar(instrument_code,
                                             roll_calendar,
                                             ignore_duplication=True)
Пример #13
0
def data():
    data = csvFuturesSimData()
    return data
Пример #14
0
Work up a minimum example of a trend following system

"""

# Get some data

from sysdata.sim.csv_futures_sim_data import csvFuturesSimData

""""
Let's get some data

We can get data from various places; however for now we're going to use
prepackaged 'legacy' data stored in csv files
"""

data = csvFuturesSimData()

print(data)
"""
We get stuff out of data with methods
"""
print(data.get_instrument_list())
print(data.get_raw_price("EDOLLAR").tail(5))
"""
data can also behave in a dict like manner (though it's not a dict)
"""

print(data["VIX"])
print(data.keys())
"""
Пример #15
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.simData, 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 = csvFuturesSimData()

    if config is None:
        config = Config(
            "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
Пример #16
0
from sysdata.sim.csv_futures_sim_data import csvFuturesSimData
from sysobjects.roll_calendars import rollCalendar
from sysdata.csv.csv_roll_calendars import csvRollCalendarData
from sysdata.mongodb.mongo_roll_data import mongoRollParametersData
"""
Generate the roll calendars from existing data
"""

if __name__ == "__main__":
    csv_roll_calendars = csvRollCalendarData()
    sim_futures_data = csvFuturesSimData()
    mongo_rollparameters = mongoRollParametersData()

    instrument_list = sim_futures_data.get_instrument_list()

    for instrument_code in instrument_list:
        print(instrument_code)
        multiple_prices = sim_futures_data.get_all_multiple_prices(
            instrument_code)

        roll_parameters = mongo_rollparameters.get_roll_parameters(
            instrument_code)
        roll_calendar = rollCalendar.back_out_from_multiple_prices(
            multiple_prices)
        print("Calendar:")
        print(roll_calendar)

        # We ignore duplicates since this is run regularly
        csv_roll_calendars.add_roll_calendar(instrument_code,
                                             roll_calendar,
                                             ignore_duplication=True)
Пример #17
0
    def test_simple_system(self):
        """
        This is (mostly) the code from 'examples.introduction.simplesystem',
        but without graph plotting
        """
        data = csvFuturesSimData()
        my_rules = Rules(ewmac)
        print(my_rules.trading_rules())

        my_rules = Rules(dict(ewmac=ewmac))
        print(my_rules.trading_rules())

        my_system = System([my_rules], data)
        print(my_system)
        print(my_system.rules.get_raw_forecast("EDOLLAR", "ewmac").tail(5))

        ewmac_rule = TradingRule(ewmac)
        my_rules = Rules(dict(ewmac=ewmac_rule))
        print(ewmac_rule)

        ewmac_8 = TradingRule((ewmac, [], dict(Lfast=8, Lslow=32)))
        ewmac_32 = TradingRule(
            dict(function=ewmac, other_args=dict(Lfast=32, Lslow=128)))
        my_rules = Rules(dict(ewmac8=ewmac_8, ewmac32=ewmac_32))
        print(my_rules.trading_rules()["ewmac32"])

        my_system = System([my_rules], data)
        my_system.rules.get_raw_forecast("EDOLLAR", "ewmac32").tail(5)

        my_config = Config()
        print(my_config)

        empty_rules = Rules()
        my_config.trading_rules = dict(ewmac8=ewmac_8, ewmac32=ewmac_32)
        my_system = System([empty_rules], data, my_config)
        my_system.rules.get_raw_forecast("EDOLLAR", "ewmac32").tail(5)

        # we can estimate these ourselves
        my_config.instruments = ["US10", "EDOLLAR", "CORN", "SP500"]
        my_config.use_forecast_scale_estimates = True

        fcs = ForecastScaleCap()
        my_system = System([fcs, my_rules], data, my_config)
        my_config.forecast_scalar_estimate["pool_instruments"] = False
        print(
            my_system.forecastScaleCap.get_forecast_scalar(
                "EDOLLAR", "ewmac32").tail(5))

        # or we can use the values from the book
        my_config.forecast_scalars = dict(ewmac8=5.3, ewmac32=2.65)
        my_config.use_forecast_scale_estimates = False
        fcs = ForecastScaleCap()
        my_system = System([fcs, my_rules], data, my_config)
        print(
            my_system.forecastScaleCap.get_capped_forecast(
                "EDOLLAR", "ewmac32").tail(5))

        # defaults
        combiner = ForecastCombine()
        my_system = System([fcs, my_rules, combiner], data, my_config)
        print(my_system.combForecast.get_forecast_weights("EDOLLAR").tail(5))
        print(
            my_system.combForecast.get_forecast_diversification_multiplier(
                "EDOLLAR").tail(5))

        # estimates:
        my_account = Account()
        combiner = ForecastCombine()

        my_config.forecast_weight_estimate = dict(method="one_period")
        my_config.use_forecast_weight_estimates = True
        my_config.use_forecast_div_mult_estimates = True

        my_system = System([my_account, fcs, my_rules, combiner], data,
                           my_config)

        # this is a bit slow, better to know what's going on
        my_system.set_logging_level("on")

        print(my_system.combForecast.get_forecast_weights("US10").tail(5))
        print(
            my_system.combForecast.get_forecast_diversification_multiplier(
                "US10").tail(5))

        # fixed:
        my_config.forecast_weights = dict(ewmac8=0.5, ewmac32=0.5)
        my_config.forecast_div_multiplier = 1.1
        my_config.use_forecast_weight_estimates = False
        my_config.use_forecast_div_mult_estimates = False

        combiner = ForecastCombine()
        my_system = System(
            [fcs, empty_rules, combiner], data,
            my_config)  # no need for accounts if no estimation done
        my_system.combForecast.get_combined_forecast("EDOLLAR").tail(5)

        # size positions
        possizer = PositionSizing()
        my_config.percentage_vol_target = 25
        my_config.notional_trading_capital = 500000
        my_config.base_currency = "GBP"

        my_system = System([fcs, my_rules, combiner, possizer], data,
                           my_config)

        print(my_system.positionSize.get_price_volatility("EDOLLAR").tail(5))
        print(my_system.positionSize.get_block_value("EDOLLAR").tail(5))
        print(my_system.positionSize.get_underlying_price("EDOLLAR"))
        print(
            my_system.positionSize.get_instrument_value_vol("EDOLLAR").tail(5))
        print(my_system.positionSize.get_volatility_scalar("EDOLLAR").tail(5))
        print(my_system.positionSize.get_vol_target_dict())
        print(my_system.positionSize.get_subsystem_position("EDOLLAR").tail(5))

        # portfolio - estimated
        portfolio = Portfolios()

        my_config.use_instrument_weight_estimates = True
        my_config.use_instrument_div_mult_estimates = True
        my_config.instrument_weight_estimate = dict(method="shrinkage",
                                                    date_method="in_sample")

        my_system = System(
            [my_account, fcs, my_rules, combiner, possizer, portfolio], data,
            my_config)

        my_system.set_logging_level("on")

        print(my_system.portfolio.get_instrument_weights().tail(5))
        print(my_system.portfolio.get_instrument_diversification_multiplier().
              tail(5))

        # or fixed
        portfolio = Portfolios()
        my_config.use_instrument_weight_estimates = False
        my_config.use_instrument_div_mult_estimates = False
        my_config.instrument_weights = dict(US10=0.1,
                                            EDOLLAR=0.4,
                                            CORN=0.3,
                                            SP500=0.2)
        my_config.instrument_div_multiplier = 1.5

        my_system = System([fcs, my_rules, combiner, possizer, portfolio],
                           data, my_config)

        print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5))

        my_system = System(
            [fcs, my_rules, combiner, possizer, portfolio, my_account], data,
            my_config)
        profits = my_system.accounts.portfolio()
        print(profits.percent().stats())

        # have costs data now
        print(profits.gross.percent().stats())
        print(profits.net.percent().stats())

        my_config = Config(
            dict(trading_rules=dict(ewmac8=ewmac_8, ewmac32=ewmac_32),
                 instrument_weights=dict(US10=0.1,
                                         EDOLLAR=0.4,
                                         CORN=0.3,
                                         SP500=0.2),
                 instrument_div_multiplier=1.5,
                 forecast_scalars=dict(ewmac8=5.3, ewmac32=2.65),
                 forecast_weights=dict(ewmac8=0.5, ewmac32=0.5),
                 forecast_div_multiplier=1.1,
                 percentage_vol_target=25.00,
                 notional_trading_capital=500000,
                 base_currency="GBP"))
        print(my_config)
        my_system = System(
            [
                Account(),
                Portfolios(),
                PositionSizing(),
                ForecastCombine(),
                ForecastScaleCap(),
                Rules()
            ],
            data,
            my_config,
        )
        print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5))

        my_config = Config("systems.provided.example.simplesystemconfig.yaml")
        print(my_config)
        my_system = System(
            [
                Account(),
                Portfolios(),
                PositionSizing(),
                ForecastCombine(),
                ForecastScaleCap(),
                Rules()
            ],
            data,
            my_config,
        )
        print(my_system.rules.get_raw_forecast("EDOLLAR", "ewmac32").tail(5))
        print(my_system.rules.get_raw_forecast("EDOLLAR", "ewmac8").tail(5))
        print(
            my_system.forecastScaleCap.get_capped_forecast(
                "EDOLLAR", "ewmac32").tail(5))
        print(
            my_system.forecastScaleCap.get_forecast_scalar(
                "EDOLLAR", "ewmac32"))
        print(my_system.combForecast.get_combined_forecast("EDOLLAR").tail(5))
        print(my_system.combForecast.get_forecast_weights("EDOLLAR").tail(5))

        print(my_system.positionSize.get_subsystem_position("EDOLLAR").tail(5))

        print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5))