def production_classic_futures_system( data, config_filename, log=logtoscreen("futures_system"), notional_trading_capital=None, base_currency=None, ): log_level = "on" # ugly but once you've established a pattern... sim_data = dataSimData(data).sim_data() config = Config(config_filename) # Overwrite capital if notional_trading_capital is not None: config.notional_trading_capital = notional_trading_capital if base_currency is not None: config.base_currency = base_currency system = futures_system(data=sim_data, config=config) system.log = log system.set_logging_level(log_level) return system
def production_futures_system(config_filename, log=logger("futures_system"), notional_trading_capital=1000000, base_currency="USD"): """ :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 log_level: How much logging to do :type log_level: str """ log_level = "on" data = arcticFuturesSimData() config = Config( config_filename) # Overwrite capital config.notional_trading_capital = notional_trading_capital config.base_currency = base_currency system = futures_system(data=data, config=config) system.log = log system.set_logging_level(log_level) return system
def production_classic_futures_system( data: dataBlob, config_filename: str, log=logtoscreen("futures_system"), notional_trading_capital: float = arg_not_supplied, base_currency: str = arg_not_supplied, ) -> System: log_level = "on" sim_data = get_sim_data_object_for_production(data) config = set_up_config(data, config_filename) # Overwrite capital and base currency if notional_trading_capital is not arg_not_supplied: config.notional_trading_capital = notional_trading_capital if base_currency is not arg_not_supplied: config.base_currency = base_currency system = futures_system(data=sim_data, config=config) system._log = log system.set_logging_level(log_level) return system
def system(): """test fixture creates a system with start and end dates""" system = futures_system( data=CsvFuturesSimTestData(), config=Config("systems.provided.futures_chapter15.futuresconfig.yaml"), ) # speed things up system.config.forecast_weight_estimate["method"] = "shrinkage" system.config.forecast_weight_estimate["date_method"] = "in_sample" system.config.instrument_weight_estimate["date_method"] = "in_sample" system.config.instrument_weight_estimate["method"] = "shrinkage" return system
def production_classic_futures_system(config_filename, log=logtoscreen("futures_system"), notional_trading_capital=1000000, base_currency="USD"): log_level = "on" data = arcticFuturesSimData() config = Config( config_filename) # Overwrite capital config.notional_trading_capital = notional_trading_capital config.base_currency = base_currency system = futures_system(data=data, config=config) system.log = log system.set_logging_level(log_level) return system
from systems.provided.futures_chapter15.basesystem import futures_system sys = futures_system() save_absolute_carry_rule = sys.config.trading_rules['carry'] # equal weighting across instruments del (sys.config.instrument_weights) # but estimated IDM sys.config.use_instrument_div_mult_estimates = True sys.config.trading_rules = dict( carry=save_absolute_carry_rule, relativecarry=dict( function='systems.provided.moretradingrules.morerules.relative_carry', data=[ 'rawdata.smoothed_carry', 'rawdata.median_carry_for_asset_class' ])) # going to estimate forecast weights sys.config.use_forecast_scale_estimates = True sys.config.use_forecast_weight_estimates = True sys.config.use_forecast_div_mult_estimates = True del (sys.config.forecast_weights) ans = sys.accounts.pandl_for_all_trading_rules() ans2 = sys.accounts.pandl_for_all_trading_rules_unweighted()
from systems.provided.futures_chapter15.basesystem import futures_system from systems.forecasting import create_variations, TradingRule from copy import copy sys = futures_system() del(sys.config.instrument_weights) save_absolute_carry_rule = sys.config.trading_rules['carry'] # Trading rules short_vol=dict(shortbias=dict(function='systems.provided.moretradingrules.morerules.short_bias')) mean_reversion=dict(mr=dict(function='systems.provided.moretradingrules.morerules.cross_sectional_mean_reversion', data=['rawdata.cumulative_norm_return', 'rawdata.normalised_price_for_asset_class'],other_args=dict(horizon=250))) relative_carry_rule = dict(relativecarry=dict(function='systems.provided.moretradingrules.morerules.relative_carry', data=['rawdata.smoothed_carry','rawdata.median_carry_for_asset_class'])) absolute_carry_rule =dict(carry=save_absolute_carry_rule) dict_of_speeds= [dict(Lfast=4, Lslow=16), dict(Lfast=8, Lslow=32), dict(Lfast=16, Lslow=64), dict(Lfast=32, Lslow=128), dict(Lfast=64, Lslow=256)] normmom_base_rule= TradingRule('systems.provided.futures_chapter15.rules.ewmac', data=['rawdata.cumulative_norm_return', 'rawdata.daily_returns_volatility']) normmom_variations = create_variations(normmom_base_rule, dict_of_speeds, key_argname='Lfast', nameformat="normmom_%s:%s")
from matplotlib.pyplot import show from systems.provided.futures_chapter15.basesystem import futures_system system = futures_system(log_level="on") system.config.instrument_weights = dict(EDOLLAR=1.0) system.config.capital_multiplier['func'] = 'syscore.capital.fixed_capital' """ system.accounts.portfolio().curve().plot() show() system.accounts.portfolio().percent().curve().plot() show() system.accounts.portfolio().cumulative().curve().plot() show() """ pandl_fixed = system.accounts.portfolio() print(system.accounts.portfolio().capital) """ system = futures_system(log_level="on") system.config.instrument_weights=dict(EDOLLAR=1.0) system.config.capital_multiplier['func']='syscore.capital.full_compounding' system.accounts.capital_multiplier().plot() show()
from systems.provided.futures_chapter15.basesystem import futures_system from systems.provided.moretradingrules.morerules import long_bias, short_bias from copy import copy import numpy as np import pandas as pd base_system = futures_system() config = copy(base_system.config) config.percentage_vol_target = 16.0 ## long lived vol estimate. comment out to use standard one #config.volatility_calculation['days']=999999999 config.volatility_calculation['days'] = 35 config.trading_rules = dict(long_bias=long_bias, short_bias=short_bias) config.instrument_weights = dict(US10=0.5, US5=0.5) #config.instrument_weights=dict(US10=0.5, SP500=0.5) # #config.forecast_weights=dict(US10=dict(long_bias=1.0), SP500=dict(long_bias=1.0)) #config.forecast_weights=dict(US10=dict(long_bias=1.0), US5=dict(long_bias=1.0)) # or for relative value config.forecast_weights = dict(US10=dict(long_bias=1.0), US5=dict(short_bias=1.0)) config.forecast_div_multiplier = 1.0 system = futures_system(config=config) pandl = system.accounts.portfolio().percent() pandl = pandl[pd.datetime(1990, 1, 1):] #pandl=pandl[pd.datetime(1998,1,1):]
""" Try it out (this isn't properly scaled at this stage of course) """ instrument_code = 'EDOLLAR' price = data.daily_prices(instrument_code) ewmac = calc_ewmac_forecast(price, 32, 128) ewmac.columns = ['forecast'] print(ewmac.tail(5)) from matplotlib.pyplot import show ewmac.plot() show() """ Did we make money? """ from syscore.accounting import accountCurve account = accountCurve(price, forecast=ewmac) account.curve() account.curve().plot() show() print(account.percent().stats()) from systems.provided.futures_chapter15.basesystem import futures_system system = futures_system() system
from systems.provided.futures_chapter15.basesystem import futures_system from systems.forecasting import create_variations, TradingRule from copy import copy sys = futures_system() del (sys.config.instrument_weights) save_absolute_carry_rule = sys.config.trading_rules['carry'] # Trading rules short_vol = dict(shortbias=dict( function='systems.provided.moretradingrules.morerules.short_bias')) mean_reversion = dict(mr=dict( function= 'systems.provided.moretradingrules.morerules.cross_sectional_mean_reversion', data=[ 'rawdata.cumulative_norm_return', 'rawdata.normalised_price_for_asset_class' ], other_args=dict(horizon=250))) relative_carry_rule = dict(relativecarry=dict( function='systems.provided.moretradingrules.morerules.relative_carry', data=['rawdata.smoothed_carry', 'rawdata.median_carry_for_asset_class'])) absolute_carry_rule = dict(carry=save_absolute_carry_rule) dict_of_speeds = [ dict(Lfast=4, Lslow=16), dict(Lfast=8, Lslow=32),
import matplotlib matplotlib.use("TkAgg") from systems.provided.futures_chapter15.basesystem import futures_system sys = futures_system() sys.config.forecast_floor = 0 sys.forecastScaleCap.get_capped_forecast("SP500", "ewmac2_8").plot() from systems.forecasting import create_variations, TradingRule dict_of_speeds = [ dict(Lfast=4, Lslow=16), dict(Lfast=8, Lslow=32), dict(Lfast=16, Lslow=64), dict(Lfast=32, Lslow=128), dict(Lfast=64, Lslow=256), ] normmom_base_rule = TradingRule( "systems.provided.futures_chapter15.rules.ewmac_calc_vol", data=["rawdata.cumulative_norm_return"], ) normmom_variations = create_variations(normmom_base_rule, dict_of_speeds, key_argname="Lfast", nameformat="normmom_%s:%s") TradingRule( rule="systems.provided.futures_chapter15.rules.ewmac_calc_vol", data=["rawdata.cumulative_norm_return"],
from systems.provided.futures_chapter15.basesystem import futures_system sys=futures_system() save_absolute_carry_rule = sys.config.trading_rules['carry'] # equal weighting across instruments del(sys.config.instrument_weights) # but estimated IDM sys.config.use_instrument_div_mult_estimates=True sys.config.trading_rules=dict(carry=save_absolute_carry_rule, relativecarry=dict(function='systems.provided.moretradingrules.morerules.relative_carry', data=['rawdata.smoothed_carry','rawdata.median_carry_for_asset_class'])) # going to estimate forecast weights sys.config.use_forecast_scale_estimates=True sys.config.use_forecast_weight_estimates=True sys.config.use_forecast_div_mult_estimates=True del(sys.config.forecast_weights) ans = sys.accounts.pandl_for_all_trading_rules() ans2 = sys.accounts.pandl_for_all_trading_rules_unweighted() ans2.to_frame().cumsum().plot() ans2.get_stats("sharpe",freq="monthly") ans.monthly.stats()
from systems.provided.futures_chapter15.basesystem import futures_system from copy import copy from pickle import dump, load base_system = futures_system() base_config = base_system.config ## run all possible combinations of TF to get base performance instruments = base_system.get_instrument_list() results = dict() wlist = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 125, 150, 175, 200, 250 ] #wlist = [1,250] instrument_list = base_system.get_instrument_list() from syscore.genutils import progressBar thing = progressBar(len(wlist) * len(wlist) * len(instrument_list)) for Aspeed in wlist: for Bspeed in wlist: if Aspeed == Bspeed: continue config = copy(base_config) trading_rules = dict(rule=dict( function='systems.provided.futures_chapter15.rules.ewmac',
from matplotlib.pyplot import plot, scatter import numpy as np from scipy.stats import linregress import statsmodels.formula.api as sm from systems.provided.futures_chapter15.basesystem import futures_system import pandas as pd from systems.forecasting import TradingRule from systems.provided.moretradingrules.morerules import long_bias from copy import copy system = futures_system() carry_acc = system.accounts.pandl_for_instrument_forecast("US10", "carry") ewmac8_acc = system.accounts.pandl_for_instrument_forecast("US10", "ewmac8_32") ewmac16_acc = system.accounts.pandl_for_instrument_forecast( "US10", "ewmac16_64") all_rets = pd.concat([carry_acc, ewmac8_acc, ewmac16_acc], axis=1) # benchmarking all_curve = system.accounts.pandl_for_instrument("MXP").percent() all_curve = 1.5 * all_curve new_rule = TradingRule(long_bias) config = copy(system.config) config.trading_rules['long_bias'] = new_rule ## If you're using fixed weights and scalars config.forecast_scalar = 1.0 config.forecast_weights = dict(
from matplotlib.pyplot import show from systems.provided.futures_chapter15.basesystem import futures_system system = futures_system(log_level="on") system.config.instrument_weights = dict(EDOLLAR=1.0) system.config.capital_multiplier['func'] = 'syscore.capital.fixed_capital' """ system.accounts.portfolio().curve().plot() show() system.accounts.portfolio().percent().curve().plot() show() system.accounts.portfolio().cumulative().curve().plot() show() """ pandl_fixed = system.accounts.portfolio() print(system.accounts.portfolio().capital) """ system = futures_system(log_level="on") system.config.instrument_weights=dict(EDOLLAR=1.0) system.config.capital_multiplier['func']='syscore.capital.full_compounding' system.accounts.capital_multiplier().plot() show() system.accounts.portfolio_with_multiplier().capital.plot()
import sys; sys.path.append('..') from systems.provided.futures_chapter15.basesystem import futures_system from matplotlib.pyplot import show system = futures_system() system.accounts.portfolio().sharpe() system.accounts.portfolio().curve().plot() show()
from systems.provided.futures_chapter15.basesystem import futures_system from systems.provided.example.rules import ewmac_forecast_with_defaults as ewmac from systems.forecasting import TradingRule from systems.forecasting import Rules from sysdata.configdata import Config my_config = Config("interface.futuresconfig.yaml") # Config object system = futures_system(log_level="on", config=my_config) # We parse the config object acording the class, for basesystem, the keyword is config, this change # accordding to class, as you can see we don't need to parse the stages.It makes everything # futures_system does but with customs atributes. portfolio = system.accounts.portfolio() print(system.positionSize.get_subsystem_position("EDOLLAR").tail(2)) # The output is somenthing like below # ss_position # 2015-12-10 1.811465 # 2015-12-11 2.544598