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)
def setUp(self): stage = SystemStage() stage.name = "test" data = Data() config = Config(dict(instruments=["another_code", "code"])) system = System([stage], data=data, config=config) self.system = system
def get_test_object_futures(): """ Returns some standard test data """ data = csvFuturesData("sysdata.tests") config = Config( "pysystemtrade.systems.provided.example.exampleconfig.yaml") rawdata = FuturesRawData() return (rawdata, data, config)
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
def get_test_object_futures_with_rules_and_capping(): """ Returns some standard test data """ data = csvFuturesData("sysdata.tests") rawdata = FuturesRawData() rules = Rules() config = Config( "pysystemtrade.systems.provided.example.exampleconfig.yaml") capobject = ForecastScaleCap() return (capobject, rules, rawdata, data, config)
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)
def get_test_object_futures_with_pos_sizing(): """ 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() 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.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
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
PortfoliosFixed(), PositionSizing(), ForecastCombineEstimated(), ForecastScaleCapEstimated(), rules, RawData() ], csvFuturesData(), config) my_system.set_logging_level(log_level) return my_system rules = create_rules_for_random_system() config = Config( dict(use_forecast_scale_estimates=True, use_forecast_weight_estimates=True, forecast_scalar_estimate=dict(pool_instruments=True), instrument_weights=dict(EDOLLAR=.25, US10=.25, CORN=.25, SP500=.25), instrument_div_multiplier=1.5)) system = random_system_for_regression(config, rules) system.accounts.portfolio().cumsum().plot() show() ans = system.accounts.pandl_for_all_trading_rules_unweighted() ans.gross.to_frame().cumsum().plot() legend() show() ans.costs.to_frame().cumsum().plot()
def __init__(self, stage_list, data, config=None, log=logtoscreen("base_system")): """ Create a system object for doing simulations or live trading :param stage_list: A list of stages :type stage_list: list of systems.stage.SystemStage (or anything that inherits from it) :param data: data for doing simulations :type data: sysdata.data.Data (or anything that inherits from that) :param config: Optional configuration :type config: sysdata.configdata.Config :returns: new system object >>> from pysystemtrade.systems.stage import SystemStage >>> stage=SystemStage() >>> from pysystemtrade.sysdata.csvdata import csvFuturesData >>> data=csvFuturesData() >>> System([stage], data) System with stages: unnamed """ if config is None: # Default - for very dull systems this is sufficient config = Config() setattr(self, "data", data) setattr(self, "config", config) setattr(self, "log", log) self.config._system_init(self) self.data._system_init(self) stage_names = [] try: iter(stage_list) except AssertionError: raise Exception( "You didn't pass a list into this System instance; even just one stage should be System([stage_instance])" ) for stage in stage_list: """ This is where we put the methods to store various stages of the process """ # Stages have names, which are also how we find them in the system # attributes sub_name = stage.name # Each stage has a link back to the parent system # This init sets this, and also passes the system logging object stage._system_init(self) if sub_name in stage_names: raise Exception( "You have duplicate subsystems with the name %s. Remove " "one of them, or change a name." % sub_name) setattr(self, sub_name, stage) stage_names.append(sub_name) setattr(self, "_stage_names", stage_names) """ The cache hides all intermediate results We call optimal_positions and then that propogates back finding all the data we need The results are then cached in the object. Should we call delete_instrument_data (in base class system) then everything related to a particular instrument is removed from these 'nodes' except for protected items This is very useful in live trading when we don't want to update eg cross sectional data every sample """ setattr(self, "cache", systemCache(self)) self.name = "base_system" # makes caching work and for general consistency
def setUp(self): system = System([testStage1(), testStage2()], Data(), Config(dict(instruments=["code", "another_code"]))) self.system = system
Account(), PortfoliosFixed(), PositionSizing(), ForecastCombineEstimated(), ForecastScaleCapEstimated(), rules, RawData() ], data, config) my_system.set_logging_level(log_level) return my_system data = create_data_for_random_system() rules = create_rules_for_random_system() config = Config(dict(use_forecast_scale_estimates=True)) system = random_system_for_regression(data, config, rules) Volscale = 0.05 Tlength = 64 print("Correlation for Vol scale %.2f Trend length %d" % (Volscale, Tlength)) instrument_code = "fake_T%.2fV_%d" % (Volscale, Tlength) ans = system.combForecast.get_forecast_correlation_matrices( instrument_code).corr_list print(ans[-1]) for Volscale in VOLS_TO_USE: for Tlength in LENGTHS_TO_USE:
from pysystemtrade.systems.provided.futures_chapter15.estimatedsystem import futures_system from pysystemtrade.sysdata.configdata import Config my_config = Config("examples.breakout.breakoutfuturesestimateconfig.yaml") system = futures_system(config=my_config) system.config.notional_trading_capital = 100000 system.set_logging_level("on") system.accounts.portfolio().stats() system.portfolio.get_instrument_weights().plot() from matplotlib.pyplot import show show()
allrulespandl=system.accounts.pandl_for_all_trading_rules() ## ewmac_all=allrulespandl.to_frame().loc[:,evariations].sum(axis=1) break_all=allrulespandl.to_frame().loc[:,bvariations].sum(axis=1) both_plot=pd.concat([ewmac_all, break_all], axis=1) print(both_plot.corr()) both_plot.plot() show() """ # full backtest compare my_config = Config("examples.breakout.breakoutfuturesestimateconfig.yaml") # will do all instruments we have data for del (my_config.instruments) # temporarily remove breakout rules my_config.rule_variations = evariations my_config.forecast_weight_estimate["method"] = "equal_weights" system_old = futures_system(config=my_config, log_level="on") # new system has all trading rules new_config = Config("examples.breakout.breakoutfuturesestimateconfig.yaml") new_config.rule_variations = bvariations new_config.forecast_weight_estimate["method"] = "equal_weights" del (new_config.instruments) system_new = futures_system(config=new_config, log_level="on")
from systems.provided.example.simplesystem import simplesystem my_system = simplesystem(log_level="on") print(my_system) print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5)) from sysdata.csvdata import csvFuturesData from pysystemtrade.sysdata.configdata import Config """ Now loading config and data """ my_config = Config("systems.provided.example.simplesystemconfig.yaml") my_data = csvFuturesData() my_system = simplesystem(config=my_config, data=my_data) print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5)) """ Let's get the chapter 15 system """ from systems.provided.futures_chapter15.basesystem import futures_system from matplotlib.pyplot import show system = futures_system(log_level="on") print(system.accounts.portfolio().sharpe()) system.accounts.portfolio().curve().plot() show() """ Same for estimated system """