From f88aacc21447f0b21659ea28ddd4ec503fb308b2 Mon Sep 17 00:00:00 2001 From: Stephen Diehl Date: Wed, 16 May 2012 17:03:16 -0400 Subject: [PATCH] Fix some more oop on transforms. --- zipline/core/devsimulator.py | 8 +++++- zipline/finance/movingaverage.py | 4 +-- zipline/finance/vwap.py | 14 ++++++---- zipline/transitions.py | 48 ++++++++++++++++---------------- 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/zipline/core/devsimulator.py b/zipline/core/devsimulator.py index 37418d06..b1aee965 100644 --- a/zipline/core/devsimulator.py +++ b/zipline/core/devsimulator.py @@ -1,11 +1,16 @@ """ -Simulator hosts all the components necessary to execute a simluation. See :py:method"" +Simulator hosts all the components necessary to execute a simluation. +See :py:method"" """ import threading from zipline.core import ComponentHost class AddressAllocator(object): + """ + Produces a iterator of 10000 sockets to allocate as needed. + Emulates the API of Qexec's socket allocator. + """ def __init__(self, ns): self.idx = 0 @@ -28,6 +33,7 @@ class Simulator(ComponentHost): zmq_flavor = 'thread' def __init__(self, addresses): + # TODO: rethink this ComponentHost.__init__(self, addresses) self.subthreads = [] self.running = False diff --git a/zipline/finance/movingaverage.py b/zipline/finance/movingaverage.py index bdd3bfc8..ce5b2827 100644 --- a/zipline/finance/movingaverage.py +++ b/zipline/finance/movingaverage.py @@ -20,7 +20,7 @@ class MovingAverageTransform(BaseTransform): class MovingAverage(object): - def __init__(self, daycount): + def init(self, daycount): self.window = EventWindow(daycount) self.total = 0.0 self.average = 0.0 @@ -43,7 +43,7 @@ class EventWindow(object): Tracks a window of the event history. Use an instance to track the events inside your window to efficiently calculate rolling statistics. """ - def __init__(self, daycount): + def init(self, daycount): self.ticks = [] self.dropped_ticks = [] self.delta = timedelta(days=daycount) diff --git a/zipline/finance/vwap.py b/zipline/finance/vwap.py index 7b435fb0..14ea98ad 100644 --- a/zipline/finance/vwap.py +++ b/zipline/finance/vwap.py @@ -1,27 +1,29 @@ -from collections import defaultdict from datetime import timedelta +from collections import defaultdict from zipline.transforms.base import BaseTransform from zipline.finance.movingaverage import EventWindow class VWAPTransform(BaseTransform): - + def init(self, daycount=3): self.daycount = daycount self.by_sid = defaultdict(self.create_vwap) - + def transform(self, event): cur = self.by_sid[event.sid] cur.update(event) self.state['value'] = cur.vwap return self.state - + def create_vwap(self): return DailyVWAP(self.daycount) class DailyVWAP: - """A class that tracks the volume weighted average price - based on tick updates.""" + """ + A class that tracks the volume weighted average price based on tick + updates. + """ def __init__(self, daycount): self.window = EventWindow(daycount) self.flux = 0.0 diff --git a/zipline/transitions.py b/zipline/transitions.py index 9a68926f..2e546fc5 100644 --- a/zipline/transitions.py +++ b/zipline/transitions.py @@ -35,27 +35,6 @@ class Workflow(Container, Callable): else: return False -class Flowable: - - @property - def state(self): - if not hasattr(self, '_state'): - self._state = self.initial_state - else: - return self._state - - @state.setter - def state(self, new): - if not hasattr(self, '_state'): - self._state = self.initial_state - - old = self._state - - if (old, new) in self.workflow: - self._state = new - else: - raise RuntimeError("Invalid State Transition : %s -> %s" %(old, new)) - class WorkflowMeta(type): """ Base metaclass component workflows. @@ -79,9 +58,30 @@ class WorkflowMeta(type): if not transitions: raise RuntimeError('Must specify initial_state') - new_class = super(WorkflowMeta, cls).__new__( - cls, name, mro+(Flowable,), attrs - ) + new_class = super(WorkflowMeta, cls).__new__(cls, name, mro, attrs) new_class.workflow = Workflow(state, transitions, initial_state) return new_class + +class Flowable(object): + + __metaclass__ = WorkflowMeta + + @property + def state(self): + if not hasattr(self, '_state'): + self._state = self.initial_state + else: + return self._state + + @state.setter + def state(self, new): + if not hasattr(self, '_state'): + self._state = self.initial_state + + old = self._state + + if (old, new) in self.workflow: + self._state = new + else: + raise RuntimeError("Invalid State Transition : %s -> %s" %(old, new))