mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-29 10:26:10 +08:00
flattened the finance package. draft test for vwap in a complete zipline.
This commit is contained in:
@@ -13,7 +13,7 @@ class MovingAverageTransform(BaseTransform):
|
||||
def transform(self, event):
|
||||
cur = self.by_sid(event.sid)
|
||||
cur.update(event)
|
||||
self.state['value'] = cur.vwap
|
||||
self.state['value'] = cur.average
|
||||
return self.state
|
||||
|
||||
class MovingAverage(object):
|
||||
@@ -37,6 +37,10 @@ class MovingAverage(object):
|
||||
self.average = 0.0
|
||||
|
||||
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):
|
||||
self.ticks = []
|
||||
self.dropped_ticks = []
|
||||
@@ -3,7 +3,7 @@ from datetime import timedelta
|
||||
from collections import defaultdict
|
||||
|
||||
from zipline.messaging import BaseTransform
|
||||
from zipline.finance.transforms.moving_average import EventWindow, EventHistory
|
||||
from zipline.finance.movingaverage import EventWindow, EventHistory
|
||||
|
||||
class VWAPTransform(BaseTransform):
|
||||
|
||||
@@ -1,15 +1,46 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from collections import defaultdict
|
||||
from unittest2 import TestCase
|
||||
|
||||
import zipline.test.factory as factory
|
||||
from zipline.finance.transforms.vwap import DailyVWAP, DailyVWAP_df
|
||||
from zipline.finance.transforms.returns import ReturnsFromPriorClose
|
||||
from zipline.finance.transforms.moving_average import MovingAverage
|
||||
import zipline.util as qutil
|
||||
from zipline.finance.vwap import DailyVWAP, VWAPTransform, DailyVWAP_df
|
||||
from zipline.finance.returns import ReturnsFromPriorClose
|
||||
from zipline.finance.movingaverage import MovingAverage
|
||||
from zipline.lines import SimulatedTrading
|
||||
from zipline.simulator import AddressAllocator, Simulator
|
||||
|
||||
|
||||
class FinanceTestCase(TestCase):
|
||||
allocator = AddressAllocator(1000)
|
||||
|
||||
class ZiplineWithTransformsTestCase(TestCase):
|
||||
leased_sockets = defaultdict(list)
|
||||
|
||||
def setUp(self):
|
||||
qutil.configure_logging()
|
||||
self.trading_environment = factory.create_trading_environment()
|
||||
self.zipline_test_config = {
|
||||
'allocator':allocator,
|
||||
'sid':133
|
||||
}
|
||||
|
||||
def test_vwap_tnfm(self):
|
||||
zipline = SimulatedTrading.create_test_zipline(
|
||||
**self.zipline_test_config
|
||||
)
|
||||
|
||||
vwap = VWAPTransform("vwap_10", daycount=10)
|
||||
zipline.add_transform(vwap)
|
||||
|
||||
zipline.simulate(blocking=True)
|
||||
|
||||
self.assertTrue(zipline.sim.ready())
|
||||
self.assertFalse(zipline.sim.exception)
|
||||
|
||||
class FinanceTransformsTestCase(TestCase):
|
||||
def setUp(self):
|
||||
self.trading_environment = factory.create_trading_environment()
|
||||
|
||||
def test_vwap(self):
|
||||
|
||||
trade_history = factory.create_trade_history(
|
||||
@@ -58,4 +89,7 @@ class FinanceTestCase(TestCase):
|
||||
ma.update(trade)
|
||||
|
||||
|
||||
self.assertEqual(ma.average, 10.5)
|
||||
self.assertEqual(ma.average, 10.5)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user