Skip to content

Commit

Permalink
Merge pull request #28 from cmorgan/autopep8
Browse files Browse the repository at this point in the history
autopep8 -a -i -r .
  • Loading branch information
Robert Carver committed Sep 5, 2016
2 parents e6274e4 + 2c911ae commit f899ab1
Show file tree
Hide file tree
Showing 59 changed files with 4,012 additions and 3,557 deletions.
58 changes: 29 additions & 29 deletions examples/breakout/breakout.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import numpy as np
from matplotlib.pyplot import show, legend, matshow

bvariations=["breakout"+str(ws) for ws in [10, 20, 40, 80, 160, 320]]
evariations=[
"ewmac%d_%d" % (fast, fast*4) for fast in [2,4,8,16,32, 64]]
bvariations = ["breakout" + str(ws) for ws in [10, 20, 40, 80, 160, 320]]
evariations = [
"ewmac%d_%d" % (fast, fast * 4) for fast in [2, 4, 8, 16, 32, 64]]

"""
my_config = Config("examples.breakout.breakoutfuturesestimateconfig.yaml")
Expand Down Expand Up @@ -64,13 +64,13 @@
## check window size correlation and also turnover properties
outputall=[]
wslist=[4,5,6,7,8,9,10,15,20,25,30,35,40, 50, 60, 70, 80, 90, 100, 120, 140, 160,
wslist=[4,5,6,7,8,9,10,15,20,25,30,35,40, 50, 60, 70, 80, 90, 100, 120, 140, 160,
180, 200, 240, 280, 320, 360, 500]
for ws in wslist:
smoothed_output = breakout(price, ws)
##
##
avg_forecast=float(smoothed_output.abs().mean())
print("WS %d turnover %.2f" % (ws, turnover(smoothed_output, avg_forecast)))
outputall.append(smoothed_output)
Expand Down Expand Up @@ -112,7 +112,7 @@
all_scalar_values=[x[1] for x in all_scalars]
print("mean %.3f std %.3f min %.3f max %.3f" % (np.nanmean(all_scalar_values), np.nanstd(all_scalar_values),
np.nanmin(all_scalar_values), np.nanmax(all_scalar_values)))
## reload config so we get scalars estimated with pooling behaviour
my_config = Config("examples.breakout.breakoutfuturesestimateconfig.yaml")
Expand All @@ -123,7 +123,7 @@
scalar=float(system.forecastScaleCap.get_forecast_scalar(instrument, rule_name).tail(1).values)
print("%s: %.3f" % (rule_name, scalar))
## now turnovers
## now turnovers
for rule_name in variations:
all_turnovers=[]
for instrument in instr_list:
Expand All @@ -133,8 +133,8 @@
print("%s: %s %s" % (rule_name, str(all_turnovers[0]), str(all_turnovers[-1])))
all_turnover_values=[x[1] for x in all_turnovers]
print("mean %.3f std %.3f" % (np.nanmean(all_turnover_values), np.nanstd(all_turnover_values)))
## limit the rules to just breakout for now
my_config = Config("examples.breakout.breakoutfuturesestimateconfig.yaml")
my_config.trading_rules = dict([(rule_name, my_config.trading_rules[rule_name]) for rule_name in variations])
Expand Down Expand Up @@ -178,9 +178,9 @@
allpandl=[]
for rule_variation_name in variations:
allpandl.append(system.accounts.pandl_for_trading_rule(rule_variation_name).as_df())
allpandl=pd.concat(allpandl, axis=1)
allpandl.columns=variations
Expand All @@ -190,9 +190,9 @@
allpandl=[]
for rule_variation_name in bvariations:
allpandl.append(system.accounts.pandl_for_trading_rule_unweighted(rule_variation_name).as_df())
allpandl=pd.concat(allpandl, axis=1)
allpandl.columns=bvariations
Expand All @@ -201,9 +201,9 @@
allpandl=[]
for rule_variation_name in bvariations:
allpandl.append(system.accounts.pandl_for_trading_rule(rule_variation_name).as_df())
allpandl=pd.concat(allpandl, axis=1)
allpandl.cumsum().sum(axis=1).plot()
Expand All @@ -217,7 +217,7 @@
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)
Expand All @@ -227,36 +227,36 @@
show()
"""
## full backtest compare
# full backtest compare

my_config = Config("examples.breakout.breakoutfuturesestimateconfig.yaml")
## will do all instruments we have data for
# 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"
# 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 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"
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")

curve1=system_old.accounts.portfolio()
curve2=system_new.accounts.portfolio()
curve1 = system_old.accounts.portfolio()
curve2 = system_new.accounts.portfolio()

print(curve1.stats())
print(curve2.stats())

print(account_test(curve2, curve1))


curves_to_plot=pd.concat([curve1.as_df(), curve2.as_df()], axis=1)
curves_to_plot.columns=["ewmac", "breakout"]
curves_to_plot = pd.concat([curve1.as_df(), curve2.as_df()], axis=1)
curves_to_plot.columns = ["ewmac", "breakout"]

print(curves_to_plot.corr())
curves_to_plot.cumsum().plot()
Expand Down
52 changes: 30 additions & 22 deletions examples/forecastscaling/forecastscaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,64 @@
cross sectional
"""

system=futures_system()
system = futures_system()

## don't pool
system.config.forecast_scalar_estimate['pool_instruments']=False
# don't pool
system.config.forecast_scalar_estimate['pool_instruments'] = False

instrument_list=system.get_instrument_list()
instrument_list = system.get_instrument_list()
print(instrument_list)

results=[]
results = []
for instrument_code in instrument_list:
results.append(round(float(system.forecastScaleCap.get_forecast_scalar(instrument_code, "ewmac2_8").tail(1).values),2))
results.append(
round(
float(
system.forecastScaleCap.get_forecast_scalar(
instrument_code,
"ewmac2_8").tail(1).values),
2))
print(results)

results=[]
results = []
for instrument_code in instrument_list:
results.append(round(float(system.forecastScaleCap.get_forecast_scalar(instrument_code, "carry").tail(1).values),2))
results.append(
round(
float(
system.forecastScaleCap.get_forecast_scalar(
instrument_code,
"carry").tail(1).values),
2))
print(results)


"""
Use an expanding window
"""

system=futures_system()
system = futures_system()

## Let's use a one year rolling window instead
system.config.forecast_scalar_estimate['window']=250
system.config.forecast_scalar_estimate['min_periods']=250
# Let's use a one year rolling window instead
system.config.forecast_scalar_estimate['window'] = 250
system.config.forecast_scalar_estimate['min_periods'] = 250


system.forecastScaleCap.get_forecast_scalar("EDOLLAR", "ewmac64_256").plot()
show()




"""
Goldilocks amount of minimum data - not too much, not too little
"""


system = futures_system()

# stupidly small number of min periods
system.config.forecast_scalar_estimate['min_periods'] = 50

system=futures_system()

## stupidly small number of min periods
system.config.forecast_scalar_estimate['min_periods']=50

## don't pool
system.config.forecast_scalar_estimate['pool_instruments']=False
# don't pool
system.config.forecast_scalar_estimate['pool_instruments'] = False


system.forecastScaleCap.get_forecast_scalar("EDOLLAR", "ewmac64_256").plot()
Expand All @@ -67,4 +75,4 @@

"""
"""
"""
8 changes: 4 additions & 4 deletions examples/introduction/asimpletradingrule.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def calc_ewmac_forecast(price, Lfast, Lslow=None):
# And we'll miss out on the rolldown. See
# http://qoppac.blogspot.co.uk/2015/05/systems-building-futures-rolling.html

price=price.resample("1B", how="last")
price = price.resample("1B", how="last")

if Lslow is None:
Lslow = 4 * Lfast
Expand All @@ -90,7 +90,7 @@ def calc_ewmac_forecast(price, Lfast, Lslow=None):

vol = robust_vol_calc(price.diff())

return raw_ewmac / vol
return raw_ewmac / vol

"""
Try it out
Expand All @@ -100,7 +100,7 @@ def calc_ewmac_forecast(price, Lfast, Lslow=None):
instrument_code = 'EDOLLAR'
price = data.daily_prices(instrument_code)
ewmac = calc_ewmac_forecast(price, 32, 128)
ewmac.columns=['forecast']
ewmac.columns = ['forecast']
print(ewmac.tail(5))

from matplotlib.pyplot import show
Expand All @@ -118,4 +118,4 @@ def calc_ewmac_forecast(price, Lfast, Lslow=None):
account.curve().plot()
show()

print(account.percent().stats())
print(account.percent().stats())
5 changes: 3 additions & 2 deletions examples/introduction/prebakedsystems.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
system.pickle_cache("private.this_system_name.pck")
show()

del(system) ## just to make sure
del(system) # just to make sure
system = futures_system(log_level="on")
system.unpickle_cache("private.this_system_name.pck")
system.accounts.portfolio().sharpe() ## this will run much faster and reuse previous calculations
# this will run much faster and reuse previous calculations
system.accounts.portfolio().sharpe()
48 changes: 25 additions & 23 deletions examples/introduction/simplesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,20 @@
from systems.forecast_scale_cap import ForecastScaleCap


## we can estimate these ourselves
my_config.instruments=[ "US10", "EDOLLAR", "CORN", "SP500"]
my_config.use_forecast_scale_estimates=True
fcs=ForecastScaleCap()
# 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)
print(my_system.forecastScaleCap.get_forecast_scalar("EDOLLAR", "ewmac32").tail(5))
print(
my_system.forecastScaleCap.get_forecast_scalar(
"EDOLLAR",
"ewmac32").tail(5))

## or we can use the values from the book
# 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_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))
Expand All @@ -99,36 +102,34 @@

from systems.forecast_combine import ForecastCombine

## defaults
# 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:
# estimates:
from systems.account import Account
my_account = Account()
combiner = ForecastCombine()

my_config.forecast_weight_estimate=dict(method="one_period")
my_config.use_forecast_weight_estimates=True
my_config.forecast_weight_estimate = dict(method="one_period")
my_config.use_forecast_weight_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
# 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))




## fixed:
# 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_weight_estimates = False

combiner = ForecastCombine()
my_system = System([fcs, empty_rules, combiner], data, my_config)
Expand All @@ -155,10 +156,11 @@

# portfolio - estimated
from systems.portfolio import Portfolios
portfolio= Portfolios()
portfolio = Portfolios()

my_config.use_instrument_weight_estimates=True
my_config.instrument_weight_estimate=dict(method="shrinkage", date_method="in_sample")
my_config.use_instrument_weight_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)
Expand All @@ -168,9 +170,9 @@
print(my_system.portfolio.get_instrument_weights().tail(5))
print(my_system.portfolio.get_instrument_diversification_multiplier().tail(5))

## or fixed
# or fixed
portfolio = Portfolios()
my_config.use_instrument_weight_estimates=False
my_config.use_instrument_weight_estimates = False
my_config.instrument_weights = dict(US10=.1, EDOLLAR=.4, CORN=.3, SP500=.2)
my_config.instrument_div_multiplier = 1.5

Expand All @@ -188,7 +190,7 @@
profits = my_system.accounts.portfolio()
print(profits.percent().stats())

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

Expand Down
Loading

0 comments on commit f899ab1

Please sign in to comment.