robcarver17 / pysystemtrade

Systematic Trading in python
GNU General Public License v3.0
2.6k stars 822 forks source link

Repost: AttributeError related to system.accounts #42

Closed eurekar closed 7 years ago

eurekar commented 7 years ago

I post the code below. It is the same appeared in the introduction of pysystemtrade.


# coding: utf-8

import syscore

from syscore.algos import robust_vol_calc

import pandas as pd
import numpy as np
import matplotlib as plt
#import pyyaml
import scipy

from sysdata.csvdata import csvFuturesData
data=csvFuturesData()

from systems.provided.example.rules import ewmac_forecast_with_defaults as ewmac
from systems.forecasting import Rules
my_rules=Rules(dict(ewmac=ewmac))
my_rules.trading_rules()

from systems.basesystem import System
my_system=System([my_rules], data)
my_system

# We can now get forecasts:

my_system.rules.get_raw_forecast("EDOLLAR", "ewmac").tail(5)

from systems.forecasting import TradingRule
ewmac_rule=TradingRule(ewmac)
my_rules=Rules(dict(ewmac=ewmac_rule))
ewmac_rule

ewmac_8=TradingRule((ewmac, [], dict(Lfast=8, Lslow=32))) ## as a tuple (function, data, other_args) notice the empty element in the middle
ewmac_32=TradingRule(dict(function=ewmac, other_args=dict(Lfast=32, Lslow=128)))  ## as a dict
my_rules=Rules(dict(ewmac8=ewmac_8, ewmac32=ewmac_32))
my_rules.trading_rules()['ewmac32']

from sysdata.configdata import Config
my_config=Config()
my_config
empty_rules=Rules()
my_config.trading_rules=dict(ewmac8=ewmac_8, ewmac32=ewmac_32)
my_system=System([empty_rules], data, my_config)

from systems.forecast_scale_cap import ForecastScaleCap
## By default we pool esimates across instruments. It's worth telling the system what instruments we want to use:
#
my_config.instruments=["EDOLLAR", "US10", "EDOLLAR", "CORN", "SP500"]

## this parameter ensures we estimate:
my_config.use_forecast_scale_estimates=True

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

my_config.forecast_scalars=dict(ewmac8=5.3, ewmac32=2.65)

## this parameter ensures we don't estimate:
#my_config.use_forecast_scale_estimates=False

## we need a new object
fcs=ForecastScaleCap()

my_system=System([fcs, empty_rules], data, my_config)
my_system.forecastScaleCap.get_capped_forecast("EDOLLAR", "ewmac32")

from systems.forecast_combine import ForecastCombine
combiner=ForecastCombine()
my_system=System([fcs, empty_rules, combiner], data, my_config)
my_system.combForecast.get_forecast_weights("EDOLLAR").tail(5)
my_system.combForecast.get_forecast_diversification_multiplier("EDOLLAR").tail(5)

from systems.account import Account
my_account = Account()

## let's use naive markowitz to get more interesting results...
my_config.forecast_weight_estimate=dict(method="one_period") 
my_config.use_forecast_weight_estimates=True

combiner = ForecastCombine()
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("EDOLLAR").tail(5))
print(my_system.combForecast.get_forecast_diversification_multiplier("EDOLLAR").tail(5))

my_config.forecast_weights=dict(ewmac8=0.5, ewmac32=0.5)
my_config.forecast_div_multiplier=1.1
my_system=System([fcs, empty_rules, combiner], data, my_config)
my_system.combForecast.get_combined_forecast("EDOLLAR").tail(5)

from systems.positionsizing import PositionSizing
possizer=PositionSizing()

my_config.percentage_vol_target=25
my_config.notional_trading_capital=500000
my_config.base_currency="GBP"

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

my_system.positionSize.get_subsystem_position("EDOLLAR").tail(5)
eurekar commented 7 years ago

The output and errors. Thank you.

Calculating raw forecast EDOLLAR for ewmac Loading csv data for EDOLLAR Adding config defaults Getting forecast scalar for ewmac32 over CORN, EDOLLAR, SP500, US10 Calculating raw forecast CORN for ewmac32 Loading csv data for CORN Calculating raw forecast EDOLLAR for ewmac32 Loading csv data for EDOLLAR Calculating raw forecast SP500 for ewmac32 Loading csv data for SP500 Calculating raw forecast US10 for ewmac32 Loading csv data for US10 2016-11-07 2.852933 2016-11-08 2.853191 2016-11-09 2.853442 2016-11-10 2.853675 2016-11-11 2.853896 Freq: B, dtype: float64 Adding config defaults Calculating capped forecast for EDOLLAR ewmac32 Calculating raw forecast EDOLLAR for ewmac32 Loading csv data for EDOLLAR Getting forecast scalar for ewmac32 over CORN, EDOLLAR, SP500, US10 Calculating raw forecast CORN for ewmac32 Loading csv data for CORN Calculating raw forecast SP500 for ewmac32 Loading csv data for SP500 Calculating raw forecast US10 for ewmac32 Loading csv data for US10 Adding config defaults Calculating forecast weights for EDOLLAR Calculating raw forecast weights for EDOLLAR WARNING: No forecast weights - using equal weights of 0.5000 over all 2 trading rules in system Calculating capped forecast for EDOLLAR ewmac32 Calculating raw forecast EDOLLAR for ewmac32 Loading csv data for EDOLLAR Getting forecast scalar for ewmac32 over CORN, EDOLLAR, SP500, US10 Calculating raw forecast CORN for ewmac32 Loading csv data for CORN Calculating raw forecast SP500 for ewmac32 Loading csv data for SP500 Calculating raw forecast US10 for ewmac32 Loading csv data for US10 Calculating capped forecast for EDOLLAR ewmac8 Calculating raw forecast EDOLLAR for ewmac8 Loading csv data for EDOLLAR Getting forecast scalar for ewmac8 over CORN, EDOLLAR, SP500, US10 Calculating raw forecast CORN for ewmac8 Loading csv data for CORN Calculating raw forecast SP500 for ewmac8 Loading csv data for SP500 Calculating raw forecast US10 for ewmac8 Loading csv data for US10 Calculating diversification multiplier for EDOLLAR Adding config defaults Calculating forecast weights for EDOLLAR Calculating raw forecast weights for EDOLLAR Calculating capped forecast for CORN ewmac32 Calculating raw forecast CORN for ewmac32 Loading csv data for CORN Getting forecast scalar for ewmac32 over CORN, EDOLLAR, SP500, US10 Calculating raw forecast EDOLLAR for ewmac32 Loading csv data for EDOLLAR Calculating raw forecast SP500 for ewmac32 Loading csv data for SP500 Calculating raw forecast US10 for ewmac32 Loading csv data for US10 Calculating capped forecast for EDOLLAR ewmac32 Calculating capped forecast for SP500 ewmac32 Calculating capped forecast for US10 ewmac32 Loading csv cost file Loading csv instrument config Loading csv data for EDOLLAR Calculating capped forecast for CORN ewmac8 Calculating raw forecast CORN for ewmac8 Loading csv data for CORN Getting forecast scalar for ewmac8 over CORN, EDOLLAR, SP500, US10 Calculating raw forecast EDOLLAR for ewmac8 Loading csv data for EDOLLAR Calculating raw forecast SP500 for ewmac8 Loading csv data for SP500 Calculating raw forecast US10 for ewmac8 Loading csv data for US10 Calculating capped forecast for EDOLLAR ewmac8 Calculating capped forecast for SP500 ewmac8 Calculating capped forecast for US10 ewmac8 Only this set of rules ['ewmac32'] is cheap enough to trade for EDOLLAR Loading csv cost file Loading csv instrument config Loading csv data for CORN Only this set of rules ['ewmac32', 'ewmac8'] is cheap enough to trade for CORN Loading csv cost file Loading csv instrument config Loading csv data for SP500 Only this set of rules ['ewmac32', 'ewmac8'] is cheap enough to trade for SP500 Loading csv cost file Loading csv instrument config Loading csv data for US10 Only this set of rules ['ewmac32', 'ewmac8'] is cheap enough to trade for US10 Calculating raw forecast weights for EDOLLAR, over EDOLLAR Calculating pandl for instrument rules for EDOLLAR Calculating pandl for instrument forecast for EDOLLAR ewmac32 Using cost multiplier on optimisation of 0.00 Optimising... Optimising for data from 1983-10-02 00:00:00 to 1984-10-31 00:00:00 Optimising for data from 1984-10-31 00:00:00 to 1985-10-31 00:00:00 Optimising for data from 1985-10-31 00:00:00 to 1986-10-31 00:00:00 Optimising for data from 1986-10-31 00:00:00 to 1987-10-31 00:00:00 Optimising for data from 1987-10-31 00:00:00 to 1988-10-31 00:00:00 Optimising for data from 1988-10-31 00:00:00 to 1989-10-31 00:00:00 Optimising for data from 1989-10-31 00:00:00 to 1990-10-31 00:00:00 Optimising for data from 1990-10-31 00:00:00 to 1991-10-31 00:00:00 Optimising for data from 1991-10-31 00:00:00 to 1992-10-31 00:00:00 Optimising for data from 1992-10-31 00:00:00 to 1993-10-31 00:00:00 Optimising for data from 1993-10-31 00:00:00 to 1994-10-31 00:00:00 Optimising for data from 1994-10-31 00:00:00 to 1995-10-31 00:00:00 Optimising for data from 1995-10-31 00:00:00 to 1996-10-31 00:00:00 Optimising for data from 1996-10-31 00:00:00 to 1997-10-31 00:00:00 Optimising for data from 1997-10-31 00:00:00 to 1998-10-31 00:00:00 Optimising for data from 1998-10-31 00:00:00 to 1999-10-31 00:00:00 Optimising for data from 1999-10-31 00:00:00 to 2000-10-31 00:00:00 Optimising for data from 2000-10-31 00:00:00 to 2001-10-31 00:00:00 Optimising for data from 2001-10-31 00:00:00 to 2002-10-31 00:00:00 Optimising for data from 2002-10-31 00:00:00 to 2003-10-31 00:00:00 Optimising for data from 2003-10-31 00:00:00 to 2004-10-31 00:00:00 Optimising for data from 2004-10-31 00:00:00 to 2005-10-31 00:00:00 Optimising for data from 2005-10-31 00:00:00 to 2006-10-31 00:00:00 Optimising for data from 2006-10-31 00:00:00 to 2007-10-31 00:00:00 Optimising for data from 2007-10-31 00:00:00 to 2008-10-31 00:00:00 Optimising for data from 2008-10-31 00:00:00 to 2009-10-31 00:00:00 Optimising for data from 2009-10-31 00:00:00 to 2010-10-31 00:00:00 Optimising for data from 2010-10-31 00:00:00 to 2011-10-31 00:00:00 Optimising for data from 2011-10-31 00:00:00 to 2012-10-31 00:00:00 Optimising for data from 2012-10-31 00:00:00 to 2013-10-31 00:00:00 Optimising for data from 2013-10-31 00:00:00 to 2014-10-31 00:00:00 Optimising for data from 2014-10-31 00:00:00 to 2015-10-31 00:00:00 Optimising for data from 2015-10-31 00:00:00 to 2016-10-31 00:00:00 Optimising for data from 2016-10-31 00:00:00 to 2016-11-13 00:00:00 Applying cost weighting to optimisation results ewmac32 2016-11-07 1.0 2016-11-08 1.0 2016-11-09 1.0 2016-11-10 1.0 2016-11-11 1.0 Calculating forecast div multiplier for EDOLLAR Calculating forecast correlations over EDOLLAR Correlation estimate Estimating from 1981-09-27 00:00:00 to 1982-09-30 00:00:00 Estimating from 1982-09-30 00:00:00 to 1983-09-30 00:00:00 Estimating from 1983-09-30 00:00:00 to 1984-09-30 00:00:00 Estimating from 1984-09-30 00:00:00 to 1985-09-30 00:00:00 Estimating from 1985-09-30 00:00:00 to 1986-09-30 00:00:00 Estimating from 1986-09-30 00:00:00 to 1987-09-30 00:00:00 Estimating from 1987-09-30 00:00:00 to 1988-09-30 00:00:00 Estimating from 1988-09-30 00:00:00 to 1989-09-30 00:00:00 Estimating from 1989-09-30 00:00:00 to 1990-09-30 00:00:00 Estimating from 1990-09-30 00:00:00 to 1991-09-30 00:00:00 C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\syscore\correlations.py:178: RuntimeWarning: invalid value encountered in less corrmat[corrmat < 0] = 0.0 Estimating from 1991-09-30 00:00:00 to 1992-09-30 00:00:00 Estimating from 1992-09-30 00:00:00 to 1993-09-30 00:00:00 Estimating from 1993-09-30 00:00:00 to 1994-09-30 00:00:00 Estimating from 1994-09-30 00:00:00 to 1995-09-30 00:00:00 Estimating from 1995-09-30 00:00:00 to 1996-09-30 00:00:00 Estimating from 1996-09-30 00:00:00 to 1997-09-30 00:00:00 Estimating from 1997-09-30 00:00:00 to 1998-09-30 00:00:00 Estimating from 1998-09-30 00:00:00 to 1999-09-30 00:00:00 Estimating from 1999-09-30 00:00:00 to 2000-09-30 00:00:00 Estimating from 2000-09-30 00:00:00 to 2001-09-30 00:00:00 Estimating from 2001-09-30 00:00:00 to 2002-09-30 00:00:00 Estimating from 2002-09-30 00:00:00 to 2003-09-30 00:00:00 Estimating from 2003-09-30 00:00:00 to 2004-09-30 00:00:00 Estimating from 2004-09-30 00:00:00 to 2005-09-30 00:00:00 Estimating from 2005-09-30 00:00:00 to 2006-09-30 00:00:00 Estimating from 2006-09-30 00:00:00 to 2007-09-30 00:00:00 Estimating from 2007-09-30 00:00:00 to 2008-09-30 00:00:00 Estimating from 2008-09-30 00:00:00 to 2009-09-30 00:00:00 Estimating from 2009-09-30 00:00:00 to 2010-09-30 00:00:00 Estimating from 2010-09-30 00:00:00 to 2011-09-30 00:00:00 Estimating from 2011-09-30 00:00:00 to 2012-09-30 00:00:00 Estimating from 2012-09-30 00:00:00 to 2013-09-30 00:00:00 Estimating from 2013-09-30 00:00:00 to 2014-09-30 00:00:00 Estimating from 2014-09-30 00:00:00 to 2015-09-30 00:00:00 Estimating from 2015-09-30 00:00:00 to 2016-09-30 00:00:00 Estimating from 2016-09-30 00:00:00 to 2016-11-13 00:00:00 2016-11-07 1.0 2016-11-08 1.0 2016-11-09 1.0 2016-11-10 1.0 2016-11-11 1.0 Freq: B, dtype: float64 Adding config defaults Calculating combined forecast for EDOLLAR Calculating forecast weights for EDOLLAR Calculating raw forecast weights for EDOLLAR

AttributeError Traceback (most recent call last)

in () 109 my_config.forecast_div_multiplier=1.1 110 my_system=System([fcs, empty_rules, combiner], data, my_config) --> 111 my_system.combForecast.get_combined_forecast("EDOLLAR").tail(5) 112 113 C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\forecast_combine.py in get_combined_forecast(self, instrument_code) 541 542 combined_forecast = self.parent.calc_or_cache( --> 543 'get_combined_forecast', instrument_code, _get_combined_forecast, self) 544 return combined_forecast 545 C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\basesystem.py in calc_or_cache(self, itemname, instrument_code, func, this_stage, flags, *args, **kwargs) 633 634 if value is None: --> 635 value = func(self, instrument_code, this_stage, *args, **kwargs) 636 self.set_item_in_cache(value, cache_ref, instrument_code) 637 C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\forecast_combine.py in _get_combined_forecast(system, instrument_code, this_stage) 513 instrument_code=instrument_code) 514 --> 515 forecast_weights = this_stage.get_forecast_weights(instrument_code) 516 rule_variation_list = list(forecast_weights.columns) 517 C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\forecast_combine.py in get_forecast_weights(self, instrument_code) 392 393 forecast_weights = self.parent.calc_or_cache( --> 394 'get_forecast_weights', instrument_code, _get_forecast_weights, self) 395 return forecast_weights 396 C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\basesystem.py in calc_or_cache(self, itemname, instrument_code, func, this_stage, flags, *args, **kwargs) 633 634 if value is None: --> 635 value = func(self, instrument_code, this_stage, *args, **kwargs) 636 self.set_item_in_cache(value, cache_ref, instrument_code) 637 C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\forecast_combine.py in _get_forecast_weights(system, instrument_code, this_stage) 375 376 forecast_weights = this_stage.get_raw_forecast_weights( --> 377 instrument_code) 378 rule_variation_list = list(forecast_weights.columns) 379 forecasts = this_stage.get_all_forecasts( C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\forecast_combine.py in get_raw_forecast_weights(self, instrument_code) 1132 'get_raw_forecast_weights', instrument_code, 1133 _get_raw_forecast_weights, -> 1134 self) 1135 1136 return raw_forecast_weights C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\basesystem.py in calc_or_cache(self, itemname, instrument_code, func, this_stage, flags, *args, **kwargs) 633 634 if value is None: --> 635 value = func(self, instrument_code, this_stage, *args, **kwargs) 636 self.set_item_in_cache(value, cache_ref, instrument_code) 637 C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\forecast_combine.py in _get_raw_forecast_weights(system, instrument_code, this_stage) 1126 1127 return this_stage.calculation_of_raw_forecast_weights( -> 1128 instrument_code).weights 1129 1130 ## C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\forecast_combine.py in calculation_of_raw_forecast_weights(self, instrument_code) 894 # could still be using pooled returns 895 return self.calculation_of_raw_forecast_weights_for_instrument( --> 896 instrument_code) 897 898 def calculation_of_raw_forecast_weights_for_instrument( C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\forecast_combine.py in calculation_of_raw_forecast_weights_for_instrument(self, instrument_code) 976 if pooling_returns: 977 # find set of instruments with same trading rules as I have --> 978 codes_to_use = self.has_same_cheap_rules_as_code(instrument_code) 979 else: 980 codes_to_use = [instrument_code] C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\forecast_combine.py in has_same_cheap_rules_as_code(self, instrument_code) 861 """ 862 --> 863 my_rules = self.apply_cost_weighting(instrument_code) 864 instrument_list = self.parent.get_instrument_list() 865 C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\forecast_combine.py in apply_cost_weighting(self, instrument_code) 846 'apply_cost_weighting', instrument_code, 847 _apply_cost_weighting, --> 848 self, ceiling_cost_SR) 849 850 return cheap_rules C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\basesystem.py in calc_or_cache(self, itemname, instrument_code, func, this_stage, flags, *args, **kwargs) 633 634 if value is None: --> 635 value = func(self, instrument_code, this_stage, *args, **kwargs) 636 self.set_item_in_cache(value, cache_ref, instrument_code) 637 C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\forecast_combine.py in _apply_cost_weighting(system, instrument_code, this_stage, ceiling_cost_SR) 824 rule_list = this_stage.get_trading_rule_list(instrument_code) 825 SR_cost_list = [this_stage.get_SR_cost_for_instrument_forecast(instrument_code, rule_variation_name) \ --> 826 for rule_variation_name in rule_list] 827 828 cheap_rule_list = [rule_name for (rule_name, rule_cost) in zip(rule_list, SR_cost_list) C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\forecast_combine.py in (.0) 824 rule_list = this_stage.get_trading_rule_list(instrument_code) 825 SR_cost_list = [this_stage.get_SR_cost_for_instrument_forecast(instrument_code, rule_variation_name) \ --> 826 for rule_variation_name in rule_list] 827 828 cheap_rule_list = [rule_name for (rule_name, rule_cost) in zip(rule_list, SR_cost_list) C:\Users\mih\WinPython-64bit-3.5.2.3Qt5\notebooks\pysystemtrade\systems\forecast_combine.py in get_SR_cost_for_instrument_forecast(self, instrument_code, rule_variation_name) 804 """ 805 --> 806 return self.parent.accounts.get_SR_cost_for_instrument_forecast(instrument_code, rule_variation_name) 807 808 def apply_cost_weighting(self, instrument_code): AttributeError: 'System' object has no attribute 'accounts'
robcarver17 commented 7 years ago

The code is correct (also the file https://github.com/robcarver17/pysystemtrade/blob/master/examples/introduction/simplesystem.py) but the documentation is wrong

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 ## new line that was missing in the doc
my_system=System([fcs, empty_rules, combiner], data, my_config)
my_system.combForecast.get_combined_forecast("EDOLLAR").tail(5)
eurekar commented 7 years ago

Thanks. It worked after I added two lines.

my_config.use_forecast_weight_estimates = False 

combiner = ForecastCombine()