mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-29 01:25:54 +08:00
Fix some more oop on transforms.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
+24
-24
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user