Skip to content

Commit 4e27134

Browse files
author
Rob Carver
committed
small refactor of stage init and system init
1 parent 5094df8 commit 4e27134

File tree

17 files changed

+128
-26
lines changed

17 files changed

+128
-26
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,9 @@ This package isn't hosted on pip. So to get the code the easiest way is to use g
5656

5757
```
5858
git clone https://github.com/robcarver17/pysystemtrade.git
59+
python3 setup.py develop
5960
```
60-
61-
A setup.py file is provided, however as the installation is trivial this shouldn't be neccessary. Just add the relevant ibsystemtrade directory to the path that python searches in, and off you go.
62-
63-
61+
Notice that develop mode is required so that ipython sessions can see files inside subdirectories which would otherwise be inaccessible.
6462

6563
### A note on support
6664

examples/smallaccountsize/generatesystems.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
for idx in [15, 20, 25, 38]:
1919
instrument_sets.append(instrument_list[:idx])
2020

21-
from systems.provided.futures_chapter15.estimatedsystem import PortfoliosEstimated
21+
from systems.portfolio import PortfoliosEstimated
2222
from systems.provided.futures_chapter15.basesystem import *
2323
from syscore.correlations import get_avg_corr
2424
from copy import copy

sysbrokers/baseClient.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class brokerClient(object):
2+
"""
3+
4+
Broker server classes are called by the brokers server application (eg IB Gateway)
5+
6+
We inherit from this for specific brokers and over ride the methods in the base class to ensure a consistent API
7+
8+
"""
9+
10+
def __init__(self):
11+
pass
12+
13+
def speakingClock(self):
14+
print("Method needs to be overriden to do anything interesting")

sysbrokers/baseServer.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class brokerServer(object):
2+
"""
3+
4+
Broker server classes are called by the brokers server application (eg IB Gateway)
5+
6+
We inherit from this and then write hooks from the servers native methods into the methods in this base class
7+
8+
"""
9+
10+
def __init__(self):
11+
pass
12+
13+
def action_to_take_when_time_set(self, time_received):
14+
print("Do something with time!")

sysbrokers/ibClient.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from sysbrokers.baseClient import brokerClient
2+
from ibapi import eclient
3+
4+
class ibClient(brokerClient):
5+
"""
6+
Client specific to interactive brokers
7+
8+
Overrides the methods in the base class specifically for IB
9+
10+
"""
11+
12+
def __init__(self):
13+
pass

sysbrokers/ibServer.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from sysbrokers.baseServer import brokerServer
2+
from ibapi import wrapper
3+
4+
class ibServer(wrapper.EWrapper):
5+
"""
6+
Server specific to interactive brokers
7+
8+
Overrides the methods in the base class specifically for IB
9+
10+
"""
11+
12+
def __init__(self):
13+
pass

sysdata/configdata.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,31 @@ def __init__(self, config_object=dict()):
5151
"""
5252
setattr(self, "_elements", []) # will be populated later
5353

54+
# this will normally be overriden by the base system
55+
setattr(self, "log", logtoscreen(stage="config"))
56+
5457
if isinstance(config_object, list):
5558
# multiple configs
5659
for config_item in config_object:
5760
self._create_config_from_item(config_item)
5861
else:
5962
self._create_config_from_item(config_object)
6063

61-
# this will normally be overriden by the base system
62-
setattr(self, "log", logtoscreen(stage="config"))
64+
65+
def _system_init(self, base_system):
66+
"""
67+
This is run when added to a base system
68+
69+
:param base_system
70+
:return: nothing
71+
"""
72+
73+
## inherit the log
74+
setattr(self, "log", base_system.log.setup(stage="config"))
75+
76+
## fill with defaults
77+
self.fill_with_defaults()
78+
6379

6480
def _create_config_from_item(self, config_item):
6581
if isinstance(config_item, dict):

sysdata/data.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ def __repr__(self):
4141
return "Data object with %d instruments" % len(
4242
self.get_instrument_list())
4343

44+
def _system_init(self, base_system):
45+
"""
46+
This is run when added to a base system
47+
48+
:param base_system
49+
:return: nothing
50+
"""
51+
52+
## inherit the log
53+
setattr(self, "log", base_system.log.setup(stage="data"))
54+
55+
4456
def methods(self):
4557
return get_methods(self)
4658

systems/account.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def __init__(self):
7979
8080
8181
"""
82+
super().__init__()
83+
8284
setattr(self, "name", "accounts")
8385
setattr(self, "description", "Account()")
8486

systems/basesystem.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,21 @@ def __init__(self, stage_list, data, config=None,
5555
# Default - for very dull systems this is sufficient
5656
config = Config()
5757

58-
config.fill_with_defaults()
59-
6058
setattr(self, "data", data)
6159
setattr(self, "config", config)
6260
setattr(self, "log", log)
6361

64-
setattr(data, "log", log.setup(stage="data"))
65-
setattr(config, "log", log.setup(stage="config"))
62+
self.config._system_init(self)
63+
self.data._system_init(self)
6664

6765
protected = []
6866
nopickle = []
6967
stage_names = []
7068

71-
assert isinstance(stage_list, list)
69+
try:
70+
iter(stage_list)
71+
except AssertionError:
72+
raise Exception("You didn't pass a list into this System instance; even just one stage should be System([stage_instance])")
7273

7374
for stage in stage_list:
7475

@@ -82,12 +83,9 @@ def __init__(self, stage_list, data, config=None,
8283
sub_name = stage.name
8384

8485
# Each stage has a link back to the parent system
86+
# This init sets this, and also passes the log
8587
stage._system_init(self)
8688

87-
# and a log
88-
log = log.setup(stage=sub_name)
89-
setattr(stage, "log", log)
90-
9189
if sub_name in stage_names:
9290
raise Exception(
9391
"You have duplicate subsystems with the name %s. Remove "
@@ -98,6 +96,7 @@ def __init__(self, stage_list, data, config=None,
9896
stage_names.append(sub_name)
9997

10098
# list of attributes / methods of the stage which are protected
99+
# FIXME more graceful way of doing this
101100
stage_protected = getattr(stage, "_protected", [])
102101
stage_protected = [(sub_name, protected_item, "*")
103102
for protected_item in stage_protected]

0 commit comments

Comments
 (0)