def testRules(self): # config=Config(dict(trading_rules=dict(ewmac=dict(function="systems.provided.example.rules.ewmac_forecast_with_defaults")))) NOTUSEDrawdata, data, NOTUSEDconfig = get_test_object() rules = Rules( dict( function= "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], -3.280028, 5) config = Config( dict(trading_rules=dict(ewmac=dict( function= "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], -3.28002839, 5) config = Config("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], -2.158634, 5)
def testRules(self): # config=Config(dict(trading_rules=dict(ewmac=dict(function="systems.provided.example.rules.ewmac_forecast_with_defaults")))) data = csvFuturesData("sysdata.tests") rules = Rules( dict( function= "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= "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("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)
def test_simple_system_trading_rule(self, data, raw_data, ewmac_8, ewmac_32): ewmac_rule = TradingRule(ewmac) print(ewmac_rule) my_rules = Rules(dict(ewmac8=ewmac_8, ewmac32=ewmac_32)) print(my_rules.trading_rules()["ewmac32"]) my_system = System([my_rules, raw_data], data) my_system.rules.get_raw_forecast("EDOLLAR", "ewmac32").tail(5)
def test_simple_system_config_import(self, data): my_config = Config("systems.provided.example.simplesystemconfig.yaml") my_config.risk_overlay = arg_not_supplied my_config.exclude_instrument_lists = dict( ignore_instruments=["MILK"], trading_restrictions=["BUTTER"], bad_markets=["CHEESE"], ) print(my_config) my_system = System( [ Account(), Portfolios(), PositionSizing(), ForecastCombine(), ForecastScaleCap(), Rules(), RawData(), ], 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))
def futures_system(sim_data=arg_not_supplied, config_filename="systems.provided.rob_system.config.yaml"): if sim_data is arg_not_supplied: sim_data = dbFuturesSimData() config = Config(config_filename) system = System( [ Risk(), accountForOptimisedStage(), optimisedPositions(), Portfolios(), PositionSizing(), myFuturesRawData(), ForecastCombine(), volAttenForecastScaleCap(), Rules(), ], sim_data, config, ) system.set_logging_level("on") return system
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
def testCallingTradingRule(self): # config=Config(dict(trading_rules=dict(ewmac=dict(function="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(( "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= "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)
def testCallingTradingRule(self): # config=Config(dict(trading_rules=dict(ewmac=dict(function="systems.provided.example.rules.ewmac_forecast_with_defaults")))) NOTUSEDrawdata, data, NOTUSEDconfig = get_test_object() 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], -3.280028, 5) # Change the data source rule = TradingRule(( "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], -1.24349, 5) rule = TradingRule( dict( function= "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.025001057146)
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( "systems.provided.futures_chapter15.futuresconfig.yaml") rules = Rules(trading_rules) system = System([ Account(), PortfoliosFixed(), PositionSizing(), FuturesRawData(), ForecastCombine(), ForecastScaleCap(), rules ], data, config) system.set_logging_level(log_level) return system
def get_test_object_futures_with_rules(): """ Returns some standard test data """ data = csvFuturesData("sysdata.tests") rawdata = FuturesRawData() rules = Rules() config = Config("systems.provided.example.exampleconfig.yaml") return (rules, rawdata, data, config)
def get_test_object_futures_with_rules_and_capping(): """ Returns some standard test data """ data = csvFuturesData("sysdata.tests") rawdata = FuturesRawData() rules = Rules() config = Config("systems.provided.example.exampleconfig.yaml") capobject = ForecastScaleCap() return (capobject, rules, rawdata, data, config)
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.iloc[-1][0], 0.411686026, 5)
def testCarryRule(self): NOTUSEDrawdata, data, NOTUSEDconfig = get_test_object() rawdata = FuturesRawData() rules = Rules() system = System([rawdata, rules], data) rule = TradingRule(carry2, [ "rawdata.daily_annualised_roll", ], dict(smooth_days=90)) ans = rule.call(system, "EDOLLAR") self.assertAlmostEqual(ans.tail(1).values[0], 0.138302, 5)
def get_test_object_futures_with_comb_forecasts(): """ Returns some standard test data """ data = csvFuturesSimData() rawdata = FuturesRawData() rules = Rules() config = Config("systems.provided.example.exampleconfig.yaml") capobject = ForecastScaleCap() combobject = ForecastCombine() return (combobject, capobject, rules, rawdata, data, config)
def get_test_object_futures_with_rules_and_capping_estimate(): """ Returns some standard test data """ data = csvFuturesSimData() rawdata = FuturesRawData() rules = Rules() config = Config("systems.provided.example.estimateexampleconfig.yaml") capobject = ForecastScaleCap() account = Account() return (account, capobject, rules, rawdata, data, config)
def get_test_object_futures_with_pos_sizing(): """ Returns some standard test data """ data = csvFuturesData("sysdata.tests") 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)
def testCarryRule(self): data = csvFuturesData("sysdata.tests") rawdata = FuturesRawData() rules = Rules() system = System([rawdata, rules], data) rule = TradingRule( carry2, [ "rawdata.daily_annualised_roll", ], dict(smooth_days=90)) ans = rule.call(system, "EDOLLAR") self.assertAlmostEqual(ans.tail(1).values[0], 0.37666175, 5)
def get_test_object_futures_with_rules(): """ 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") return (rules, rawdata, data, config)
def test_simple_system_trading_rules_estimated( self, data, raw_data, ewmac_8, ewmac_32, fcs ): my_rules = Rules(dict(ewmac8=ewmac_8, ewmac32=ewmac_32)) 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, raw_data], 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 my_system = System([fcs, my_rules, raw_data], data, my_config) my_config.forecast_scalar_estimate["pool_instruments"] = False print( my_system.forecastScaleCap.get_forecast_scalar("EDOLLAR", "ewmac32").tail(5) )
def test_simple_system_combining_fixed(self, data, raw_data, my_config, fcs): # 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 empty_rules = Rules() combiner = ForecastCombine() my_system = System( [fcs, empty_rules, combiner, raw_data], data, my_config ) # no need for accounts if no estimation done my_system.combForecast.get_combined_forecast("EDOLLAR").tail(5)
def get_test_object_futures_with_rules_and_capping_estimate(): """ 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() account = Account() return (account, capobject, rules, rawdata, data, config)
def get_test_object_futures_with_pos_sizing(): """ 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() combobject = ForecastCombine() posobject = PositionSizing() return (posobject, combobject, capobject, rules, rawdata, data, config)
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
def test_simple_system_rules(self, data, raw_data): 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, raw_data], data) print(my_system) print(my_system.rules.get_raw_forecast("EDOLLAR", "ewmac").tail(5))
def create_rules_for_random_system(): ## create a series of regression trading rules different lookbacks rules_dict=dict() for timewindow in WINDOWS_TO_USE: rule_name="regression%d" % timewindow min_periods = max(2, int(np.ceil(timewindow / 4.0))) new_rule = TradingRule((regression_rule, ["rawdata.get_daily_prices", "rawdata.daily_returns_volatility"], dict(timewindow=timewindow, min_periods=min_periods))) rules_dict[rule_name]=new_rule my_rules = Rules(rules_dict) return my_rules
def futures_system(data, config): system = System( [ Risk(), accountForOptimisedStage(), optimisedPositions(), Portfolios(), PositionSizing(), RawData(), ForecastCombine(), ForecastScaleCap(), Rules(), ], data, config, ) system.set_logging_level("on") return system
def test_simple_system_risk_overlay(self, data, ewmac_8, ewmac_32): 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", risk_overlay=dict( max_risk_fraction_normal_risk=1.4, max_risk_fraction_stdev_risk=3.6, max_risk_limit_sum_abs_risk=3.4, max_risk_leverage=13.0, ), exclude_instrument_lists=dict( ignore_instruments=["MILK"], trading_restrictions=["BUTTER"], bad_markets=["CHEESE"], ), ) ) print(my_config) my_system = System( [ Account(), Portfolios(), PositionSizing(), ForecastCombine(), ForecastScaleCap(), Rules(), RawData(), ], data, my_config, ) print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5))
For this we need to build a system A system is made up of SystemStages - essentially stages in the process, and it needs data, and perhaps a configuration The minimum stage you would have would be Rules - which is where you put trading rules """ from systems.forecasting import Rules """ We can create rules in a number of different ways Note that to make our rule work it needs to have """ my_rules = Rules(ewmac) print(my_rules.trading_rules()) my_rules = Rules(dict(ewmac=ewmac)) print(my_rules.trading_rules()) from systems.basesystem import System my_system = System([my_rules], data) print(my_system) print(my_system.rules.get_raw_forecast("EDOLLAR", "ewmac").tail(5)) """ Define a TradingRule """ from systems.forecasting import TradingRule
from systems.forecast_scale_cap import ForecastScaleCap from systems.basesystem import System from sysdata.csvdata import csvFuturesData from systems.forecasting import Rules from systems.forecasting import TradingRule from systems.portfolio import PortfoliosEstimated from systems.positionsizing import PositionSizing data = csvFuturesData() my_config = Config() my_config.instruments = ["US20", "SP500"] 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)) my_system = System([ Account(), PortfoliosEstimated(), PositionSizing(), ForecastScaleCap(), my_rules, ForecastCombine() ], data, my_config) my_system.config.forecast_weight_estimate['method'] = "equal_weights" my_system.config.instrument_weight_estimate['method'] = "bootstrap" my_system.config.instrument_weight_estimate["monte_runs"] = 1 my_system.config.instrument_weight_estimate["bootstrap_length"] = 250 print(my_system.portfolio.get_instrument_weights()) print(my_system.portfolio.get_instrument_diversification_multiplier())
A system is made up of SystemStages - essentially stages in the process, and it needs data, and perhaps a configuration The minimum stage you would have would be Rules - which is where you put trading rules """ from systems.forecasting import Rules """ We can create rules in a number of different ways Note that to make our rule work it needs to have """ my_rules = Rules(ewmac) print(my_rules.trading_rules()) my_rules = Rules(dict(ewmac=ewmac)) print(my_rules.trading_rules()) from systems.basesystem import System my_system = System([my_rules], data) print(my_system) print(my_system.rules.get_raw_forecast("EDOLLAR", "ewmac").tail(5)) """ Define a TradingRule