Skip to content

Commit

Permalink
Merge pull request #1 from cmorgan/master
Browse files Browse the repository at this point in the history
autopep8 -i -r -a .
  • Loading branch information
robcarver17 committed Dec 17, 2015
2 parents c4867b0 + 30406e5 commit a4ec342
Show file tree
Hide file tree
Showing 38 changed files with 2,407 additions and 2,266 deletions.
49 changes: 24 additions & 25 deletions examples/introduction/asimpletradingrule.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

## Get some data
# Get some data

from sysdata.csvdata import csvFuturesData

Expand All @@ -13,10 +13,10 @@
We can get data from various places; however for now we're going to use prepackaged 'legacy' data stored
in csv files
"""

data=csvFuturesData()
data = csvFuturesData()

print(data)

Expand Down Expand Up @@ -66,39 +66,40 @@
from syscore.algos import robust_vol_calc
from syscore.pdutils import divide_df_single_column


def calc_ewmac_forecast(price, Lfast, Lslow=None):


"""
Calculate the ewmac trading fule forecast, given a price and EWMA speeds Lfast, Lslow and vol_lookback
Assumes that 'price' is daily data
"""
## price: This is the stitched price series
## We can't use the price of the contract we're trading, or the volatility will be jumpy
## And we'll miss out on the rolldown. See http://qoppac.blogspot.co.uk/2015/05/systems-building-futures-rolling.html
# price: This is the stitched price series
# We can't use the price of the contract we're trading, or the volatility will be jumpy
# And we'll miss out on the rolldown. See
# http://qoppac.blogspot.co.uk/2015/05/systems-building-futures-rolling.html

if Lslow is None:
Lslow=4*Lfast

## We don't need to calculate the decay parameter, just use the span directly

fast_ewma=pd.ewma(price, span=Lfast)
slow_ewma=pd.ewma(price, span=Lslow)
raw_ewmac=fast_ewma - slow_ewma

vol=robust_vol_calc(price.diff())

Lslow = 4 * Lfast

# We don't need to calculate the decay parameter, just use the span
# directly

fast_ewma = pd.ewma(price, span=Lfast)
slow_ewma = pd.ewma(price, span=Lslow)
raw_ewmac = fast_ewma - slow_ewma

vol = robust_vol_calc(price.diff())

return divide_df_single_column(raw_ewmac, vol)

"""
Try it out
(this isn't properly scaled at this stage of course)
"""
instrument_code='EDOLLAR'
price=data.get_instrument_price(instrument_code)
ewmac=calc_ewmac_forecast(price, 32, 128)
instrument_code = 'EDOLLAR'
price = data.get_instrument_price(instrument_code)
ewmac = calc_ewmac_forecast(price, 32, 128)
print(ewmac.tail(5))

from matplotlib.pyplot import show
Expand All @@ -110,8 +111,6 @@ def calc_ewmac_forecast(price, Lfast, Lslow=None):
"""

from syscore.accounting import pandl
account=pandl(price, forecast=ewmac)
account = pandl(price, forecast=ewmac)
account.curve().plot()
show()


10 changes: 5 additions & 5 deletions examples/introduction/prebakedsystems.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from systems.provided.example.simplesystem import simplesystem

my_system=simplesystem()
my_system = simplesystem()
print(my_system)
print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5))

Expand All @@ -11,9 +11,9 @@
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)
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))


Expand All @@ -23,6 +23,6 @@

from systems.provided.futures_chapter15.basesystem import futures_system

system=futures_system()
system = futures_system()

print(system.portfolio.get_notional_position("EUROSTX").tail(5))
112 changes: 57 additions & 55 deletions examples/introduction/simplesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
We got some data and created a trading rule
"""
from sysdata.csvdata import csvFuturesData
data=csvFuturesData()
data = csvFuturesData()

from systems.provided.example.rules import ewmac_forecast_with_defaults as ewmac

Expand All @@ -21,91 +21,91 @@
"""



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
Note that to make our rule work it needs to have
"""
my_rules=Rules(ewmac)
my_rules = Rules(ewmac)
print(my_rules.trading_rules())

my_rules=Rules(dict(ewmac=ewmac))
my_rules = Rules(dict(ewmac=ewmac))
print(my_rules.trading_rules())

from systems.basesystem import System
my_system=System([my_rules], data)
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
ewmac_rule=TradingRule(ewmac)
my_rules=Rules(dict(ewmac=ewmac_rule))
ewmac_rule = TradingRule(ewmac)
my_rules = Rules(dict(ewmac=ewmac_rule))
ewmac_rule


"""
... or two...
"""

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))
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))
print(my_rules.trading_rules()['ewmac32'])

my_system=System([my_rules], data)
my_system = System([my_rules], data)
my_system.rules.get_raw_forecast("EDOLLAR", "ewmac32").tail(5)

from sysdata.configdata import Config
my_config=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)
empty_rules = Rules()
my_config.trading_rules = dict(ewmac8=ewmac_8, ewmac32=ewmac_32)
my_system = System([empty_rules], data, my_config)
my_system.rules.get_raw_forecast("EDOLLAR", "ewmac32").tail(5)


from systems.forecast_scale_cap import ForecastScaleCapFixed
my_config.forecast_scalars=dict(ewmac8=5.3, ewmac32=2.65)
fcs=ForecastScaleCapFixed()
my_system=System([fcs, my_rules], data, my_config)
print(my_system.forecastScaleCap.get_capped_forecast("EDOLLAR", "ewmac32").tail(5))
my_config.forecast_scalars = dict(ewmac8=5.3, ewmac32=2.65)
fcs = ForecastScaleCapFixed()
my_system = System([fcs, my_rules], data, my_config)
print(my_system.forecastScaleCap.get_capped_forecast(
"EDOLLAR", "ewmac32").tail(5))

"""
combine some rules
"""

from systems.forecast_combine import ForecastCombineFixed
#forecast_weights=dict(ewmac8=0.5, ewmac32=0.5), forecast_div_multiplier=1.1
combiner=ForecastCombineFixed()
my_system=System([fcs, my_rules, combiner], data, my_config)
# forecast_weights=dict(ewmac8=0.5, ewmac32=0.5), forecast_div_multiplier=1.1
combiner = ForecastCombineFixed()
my_system = System([fcs, my_rules, combiner], data, my_config)
print(my_system.combForecast.get_combined_forecast("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_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)

## size positions
# size positions

from systems.positionsizing import PositionSizing
possizer=PositionSizing()
my_config.percentage_vol_target=25
my_config.notional_trading_capital=500000
my_config.base_currency="GBP"
possizer = PositionSizing()
my_config.percentage_vol_target = 25
my_config.notional_trading_capital = 500000
my_config.base_currency = "GBP"

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

print(my_system.positionSize.get_price_volatility("EDOLLAR").tail(5))
print(my_system.positionSize.get_block_value("EDOLLAR").tail(5))
Expand All @@ -115,13 +115,14 @@
print(my_system.positionSize.get_daily_cash_vol_target())
print(my_system.positionSize.get_subsystem_position("EDOLLAR").tail(5))

## portfolio
# portfolio
from systems.portfolio import PortfoliosFixed
portfolio=PortfoliosFixed()
my_config.instrument_weights=dict(US10=.1, EDOLLAR=.4, CORN=.3, SP500=.2)
my_config.instrument_div_multiplier=1.5
portfolio = PortfoliosFixed()
my_config.instrument_weights = dict(US10=.1, EDOLLAR=.4, CORN=.3, SP500=.2)
my_config.instrument_div_multiplier = 1.5

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

print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5))

Expand All @@ -130,41 +131,42 @@
"""

from systems.account import Account
my_account=Account()
my_system=System([ fcs, my_rules, combiner, possizer, portfolio, my_account], data, my_config)
profits=my_system.account.portfolio()
my_account = Account()
my_system = System([fcs, my_rules, combiner, possizer,
portfolio, my_account], data, my_config)
profits = my_system.account.portfolio()
profits.stats()


"""
Another approach is to create a config object
Another approach is to create a config object
"""
my_config=Config(dict(trading_rules=dict(ewmac8=ewmac_8, ewmac32=ewmac_32),
instrument_weights=dict(US10=.1, EDOLLAR=.4, CORN=.3, SP500=.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"))
my_config = Config(dict(trading_rules=dict(ewmac8=ewmac_8, ewmac32=ewmac_32),
instrument_weights=dict(
US10=.1, EDOLLAR=.4, CORN=.3, SP500=.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"))
print(my_config)
my_system=System([Account(), PortfoliosFixed(), PositionSizing(), ForecastCombineFixed(), ForecastScaleCapFixed(), Rules()
], data, my_config)
my_system = System([Account(), PortfoliosFixed(), PositionSizing(), ForecastCombineFixed(), ForecastScaleCapFixed(), Rules()
], data, my_config)
print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5))

"""
... or to import one
"""
my_config=Config("systems.provided.example.simplesystemconfig.yaml")
my_config = Config("systems.provided.example.simplesystemconfig.yaml")
print(my_config)
my_system=System([Account(), PortfoliosFixed(), PositionSizing(), ForecastCombineFixed(), ForecastScaleCapFixed(), Rules()
], data, my_config)
my_system = System([Account(), PortfoliosFixed(), PositionSizing(), ForecastCombineFixed(), ForecastScaleCapFixed(), Rules()
], 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_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))


24 changes: 13 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
from setuptools import setup

# Utility function to read the README file.


def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
name = "pysystemtrade",
version = "0.0.1",
author = "Robert Carver",
description = ("Python framework for running systems as in Robert Carver's book Systematic Trading"
" (www.systematictrading.org)"),
license = "GNU GPL v3",
keywords = "systematic trading interactive brokers",
url = "qoppac.blogspot.com/pysystemtrade.html",
packages=['examples','syscore','sysdata','systems','syssims'],
name="pysystemtrade",
version="0.0.1",
author="Robert Carver",
description=("Python framework for running systems as in Robert Carver's book Systematic Trading"
" (www.systematictrading.org)"),
license="GNU GPL v3",
keywords="systematic trading interactive brokers",
url="qoppac.blogspot.com/pysystemtrade.html",
packages=['examples', 'syscore', 'sysdata', 'systems', 'syssims'],
long_description=read('README.md'),
install_requires=["pandas >= 0.17.0", "numpy >= 1.10.1", "python >= 3.4.3", "matlotplib > 1.4.3",
"yaml > 3.11"],
"yaml > 3.11"],
extras_require=dict(),
include_package_data=True
)
)
Loading

0 comments on commit a4ec342

Please sign in to comment.