Skip to content

Commit

Permalink
blacked codebase see #484
Browse files Browse the repository at this point in the history
  • Loading branch information
rob committed Dec 13, 2021
1 parent 4ed1b9c commit 6898f02
Show file tree
Hide file tree
Showing 383 changed files with 16,185 additions and 12,050 deletions.
5 changes: 3 additions & 2 deletions examples/introduction/asimpletradingrule.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import matplotlib

matplotlib.use("TkAgg")
"""
Expand Down Expand Up @@ -104,8 +105,8 @@ def calc_ewmac_forecast(price, Lfast, Lslow=None):

from systems.accounts.account_forecast import pandl_for_instrument_forecast

account = pandl_for_instrument_forecast(forecast=ewmac, price = price)
account2 = pandl_for_instrument_forecast(forecast=ewmac, price = price)
account = pandl_for_instrument_forecast(forecast=ewmac, price=price)
account2 = pandl_for_instrument_forecast(forecast=ewmac, price=price)

account.curve()

Expand Down
46 changes: 19 additions & 27 deletions examples/introduction/simplesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@
"""

ewmac_8 = TradingRule((ewmac, [], dict(Lfast=8, Lslow=32)))
ewmac_32 = TradingRule(
dict(
function=ewmac,
other_args=dict(
Lfast=32,
Lslow=128)))
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"])

Expand All @@ -85,20 +80,14 @@
fcs = ForecastScaleCap()
my_system = System([fcs, my_rules], data, my_config)
my_config.forecast_scalar_estimate["pool_instruments"] = False
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
my_config.forecast_scalars = dict(ewmac8=5.3, ewmac32=2.65)
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))
print(my_system.forecastScaleCap.get_capped_forecast("EDOLLAR", "ewmac32").tail(5))
"""
combine some rules
"""
Expand Down Expand Up @@ -126,7 +115,9 @@
my_config.use_forecast_weight_estimates = True
my_config.use_forecast_div_mult_estimates = True

my_system = System([my_account, fcs, my_rules, combiner, raw_data, position_size], data, my_config)
my_system = System(
[my_account, fcs, my_rules, combiner, raw_data, position_size], data, my_config
)

# this is a bit slow, better to know what's going on
my_system.set_logging_level("on")
Expand Down Expand Up @@ -170,11 +161,12 @@

my_config.use_instrument_weight_estimates = True
my_config.use_instrument_div_mult_estimates = True
my_config.instrument_weight_estimate = dict(
method="shrinkage", date_method="in_sample")
my_config.instrument_weight_estimate = dict(method="shrinkage", date_method="in_sample")

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

my_system.set_logging_level("on")
Expand All @@ -189,16 +181,19 @@
my_config.instrument_weights = dict(US10=0.1, EDOLLAR=0.4, CORN=0.3, SP500=0.2)
my_config.instrument_div_multiplier = 1.5

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

print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5))
"""
Have we made some dosh?
"""

my_system = System(
[fcs, my_rules, combiner, possizer, portfolio, my_account, raw_data], data, my_config
[fcs, my_rules, combiner, possizer, portfolio, my_account, raw_data],
data,
my_config,
)
profits = my_system.accounts.portfolio()
print(profits.percent.stats())
Expand Down Expand Up @@ -231,7 +226,7 @@
ForecastCombine(),
ForecastScaleCap(),
Rules(),
RawData()
RawData(),
],
data,
my_config,
Expand All @@ -250,17 +245,14 @@
ForecastCombine(),
ForecastScaleCap(),
Rules(),
RawData()
RawData(),
],
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))
Expand Down
33 changes: 19 additions & 14 deletions examples/production/example_of_custom_run_system.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

from syscore.objects import arg_not_supplied

from sysdata.data_blob import dataBlob

from sysproduction.data.sim_data import get_sim_data_object_for_production
from sysproduction.strategy_code.run_dynamic_optimised_system import runSystemCarryTrendDynamic
from sysproduction.strategy_code.run_dynamic_optimised_system import (
runSystemCarryTrendDynamic,
)

from syslogdiag.log_to_screen import logtoscreen

Expand All @@ -15,9 +16,9 @@ class runMySystemCarryTrendDynamic(runSystemCarryTrendDynamic):

# DO NOT CHANGE THE NAME OF THIS FUNCTION; IT IS HARDCODED INTO CONFIGURATION FILES
# BECAUSE IT IS ALSO USED TO LOAD BACKTESTS
def system_method(self,
notional_trading_capital: float=None,
base_currency: str=None) -> System:
def system_method(
self, notional_trading_capital: float = None, base_currency: str = None
) -> System:
data = self.data
backtest_config_filename = self.backtest_config_filename

Expand All @@ -36,8 +37,8 @@ def production_carry_trend_dynamic_system(
data: dataBlob,
config_filename: str,
log=logtoscreen("futures_system"),
notional_trading_capital: float=arg_not_supplied,
base_currency: str=arg_not_supplied,
notional_trading_capital: float = arg_not_supplied,
base_currency: str = arg_not_supplied,
) -> System:

log_level = "on"
Expand All @@ -59,6 +60,7 @@ def production_carry_trend_dynamic_system(

return system


from sysdata.config.configdata import Config

from systems.forecasting import Rules
Expand All @@ -68,10 +70,17 @@ def production_carry_trend_dynamic_system(
from private.systems.carrytrend.rawdata import myFuturesRawData
from systems.positionsizing import PositionSizing
from systems.portfolio import Portfolios
from systems.provided.dynamic_small_system_optimise.portfolio_weights_stage import portfolioWeightsStage
from systems.provided.dynamic_small_system_optimise.optimised_positions_stage import optimisedPositions
from systems.provided.dynamic_small_system_optimise.portfolio_weights_stage import (
portfolioWeightsStage,
)
from systems.provided.dynamic_small_system_optimise.optimised_positions_stage import (
optimisedPositions,
)
from systems.provided.dynamic_small_system_optimise.risk import Risk
from systems.provided.dynamic_small_system_optimise.accounts_stage import accountForOptimisedStage
from systems.provided.dynamic_small_system_optimise.accounts_stage import (
accountForOptimisedStage,
)


def futures_system(data, config):

Expand All @@ -87,14 +96,10 @@ def futures_system(data, config):
ForecastCombine(),
volAttenForecastScaleCap(),
Rules(),

],
data,
config,
)
system.set_logging_level("on")

return system



14 changes: 8 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
sys.exit(1)

if StrictVersion(platform.python_version()) >= StrictVersion("3.9.0"):
print("pysystemtrade requires Python 3.8.* or earlier (pandas issue). Exiting.", file=sys.stderr)
print(
"pysystemtrade requires Python 3.8.* or earlier (pandas issue). Exiting.",
file=sys.stderr,
)
sys.exit(1)


Expand Down Expand Up @@ -75,7 +78,8 @@ def dir_this_file():
author="Robert Carver",
description=(
"Python framework for running systems as in Robert Carver's book Systematic Trading"
" (https://www.systematicmoney.org/systematic-trading)"),
" (https://www.systematicmoney.org/systematic-trading)"
),
license="GNU GPL v3",
keywords="systematic trading interactive brokers",
url="https://qoppac.blogspot.com/p/pysystemtrade.html",
Expand All @@ -95,11 +99,9 @@ def dir_this_file():
"pytest>6.2",
"Flask>=2.0.1",
"Werkzeug>=2.0.1",
"statsmodels==0.12.2"
"statsmodels==0.12.2",
],
tests_require=[
"nose",
"flake8"],
tests_require=["nose", "flake8"],
extras_require=dict(),
test_suite="nose.collector",
include_package_data=True,
Expand Down
48 changes: 26 additions & 22 deletions sysbrokers/IB/client/ib_accounting_client.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import datetime

from sysbrokers.IB.client.ib_client import ibClient, STALE_SECONDS_ALLOWED_ACCOUNT_SUMMARY
from sysbrokers.IB.client.ib_client import (
ibClient,
STALE_SECONDS_ALLOWED_ACCOUNT_SUMMARY,
)

from syscore.objects import arg_not_supplied

from sysobjects.spot_fx_prices import currencyValue, listOfCurrencyValues

class ibAccountingClient(ibClient):

class ibAccountingClient(ibClient):
def broker_get_account_value_across_currency(
self,
account_id: str = arg_not_supplied
self, account_id: str = arg_not_supplied
) -> listOfCurrencyValues:

list_of_currencies = self._get_list_of_currencies_for_liquidation_values()
list_of_values_per_currency = list(
[
currencyValue(
currency,
self._get_liquidation_value_for_currency_across_accounts(currency,
account_id = account_id),
self._get_liquidation_value_for_currency_across_accounts(
currency, account_id=account_id
),
)
for currency in list_of_currencies
]
Expand All @@ -29,9 +32,9 @@ def broker_get_account_value_across_currency(

return list_of_values_per_currency

def _get_liquidation_value_for_currency_across_accounts(self,
currency: str,
account_id:str = arg_not_supplied) -> float:
def _get_liquidation_value_for_currency_across_accounts(
self, currency: str, account_id: str = arg_not_supplied
) -> float:
liquidiation_values_across_accounts_dict = (
self._get_net_liquidation_value_across_accounts()
)
Expand Down Expand Up @@ -65,7 +68,10 @@ def _get_net_liquidation_value_across_accounts(self) -> dict:
accounts = account_summary_dict.keys()
liquidiation_values_across_accounts_dict = dict(
[
(account_id, self._get_liquidation_values_for_single_account(account_id))
(
account_id,
self._get_liquidation_values_for_single_account(account_id),
)
for account_id in accounts
]
)
Expand All @@ -77,7 +83,6 @@ def _get_liquidation_values_for_single_account(self, account_id: str) -> dict:
account_summary_dict = self._ib_get_account_summary()
return account_summary_dict[account_id]["NetLiquidation"]


def _ib_get_account_summary(self) -> dict:
data_stale = self._is_ib_account_summary_cache_stale()
if data_stale:
Expand All @@ -99,13 +104,15 @@ def _is_ib_account_summary_cache_stale(self) -> bool:
return False

def _get_elapsed_seconds_since_last_cache_update(self) -> float:
account_summary_data_update = getattr(self, "_account_summary_data_update", None)
account_summary_data_update = getattr(
self, "_account_summary_data_update", None
)
if account_summary_data_update is None:
# force an update
return STALE_SECONDS_ALLOWED_ACCOUNT_SUMMARY*9999
return STALE_SECONDS_ALLOWED_ACCOUNT_SUMMARY * 9999

elapsed_seconds = (
datetime.datetime.now() - account_summary_data_update
datetime.datetime.now() - account_summary_data_update
).total_seconds()

return elapsed_seconds
Expand All @@ -115,7 +122,6 @@ def _ib_update_account_summary_cache(self):
self._record_cache_update()
self._account_summary_data = account_summary_dict


def _record_cache_update(self):
self._account_summary_data_update = datetime.datetime.now()

Expand All @@ -124,15 +130,13 @@ def _ib_get_account_summary_from_broker(self) -> dict:
account_summary_rawdata = self.ib.accountSummary()

# Weird format let's clean it up
account_summary_dict = clean_up_account_summary(
account_summary_rawdata)
account_summary_dict = clean_up_account_summary(account_summary_rawdata)

return account_summary_dict


def clean_up_account_summary(account_summary_rawdata: list) -> dict:
list_of_accounts = _unique_list_from_total(
account_summary_rawdata, "account")
list_of_accounts = _unique_list_from_total(account_summary_rawdata, "account")
list_of_tags = _unique_list_from_total(account_summary_rawdata, "tag")

account_summary_dict = {}
Expand All @@ -154,8 +158,8 @@ def clean_up_account_summary(account_summary_rawdata: list) -> dict:


def _unique_list_from_total(account_summary_data: list, tag_name: str):
list_of_items = [getattr(account_value, tag_name)
for account_value in account_summary_data]
list_of_items = [
getattr(account_value, tag_name) for account_value in account_summary_data
]
list_of_items = list(set(list_of_items))
return list_of_items

Loading

0 comments on commit 6898f02

Please sign in to comment.