mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-29 10:26:10 +08:00
alias for dt as datetime.
This commit is contained in:
@@ -160,6 +160,9 @@ class TradeSimulationClient(Component):
|
||||
|
||||
def get_data(self):
|
||||
for event in self.event_queue:
|
||||
#alias the dt as datetime
|
||||
event.datetime = event.dt
|
||||
self.event_data[event['sid']] = event
|
||||
|
||||
self.event_queue = []
|
||||
return self.event_data
|
||||
|
||||
+36
-37
@@ -2,23 +2,23 @@
|
||||
Algorithm Protocol
|
||||
===================
|
||||
|
||||
For a class to be passed as a trading algorithm to the
|
||||
For a class to be passed as a trading algorithm to the
|
||||
:py:class:`zipline.lines.SimulatedTrading` zipline
|
||||
it must follow an implementation protocol. Examples of this algorithm protocol
|
||||
are provided below.
|
||||
|
||||
The algorithm must expose methods:
|
||||
|
||||
- initialize: method that takes no args, no returns. Simply called to
|
||||
- initialize: method that takes no args, no returns. Simply called to
|
||||
enable the algorithm to set any internal state needed.
|
||||
|
||||
- get_sid_filter: method that takes no args, and returns a list
|
||||
of valid sids. List must have a length between 1 and 10. If None is returned
|
||||
the filter will block all events.
|
||||
|
||||
- handle_data: method that accepts a :py:class:`zipline.protocol_utils.ndict`
|
||||
|
||||
- handle_data: method that accepts a :py:class:`zipline.protocol_utils.ndict`
|
||||
of the current state of the simulation universe. An example data ndict::
|
||||
|
||||
|
||||
+-----------------+--------------+----------------+--------------------+
|
||||
| | sid(133) | sid(134) | sid(135) |
|
||||
+=================+==============+================+====================+
|
||||
@@ -30,23 +30,22 @@ The algorithm must expose methods:
|
||||
+-----------------+--------------+----------------+--------------------+
|
||||
| dt | 6/30/2012 | 6/30/2011 | 6/29/2012 |
|
||||
+-----------------+--------------+----------------+--------------------+
|
||||
|
||||
- set_order: method that accepts a callable. Will be set as the value of the
|
||||
order method of trading_client. An algorithm can then place orders with a
|
||||
|
||||
- set_order: method that accepts a callable. Will be set as the value of the
|
||||
order method of trading_client. An algorithm can then place orders with a
|
||||
valid sid and a number of shares::
|
||||
|
||||
|
||||
self.order(sid(133), share_count)
|
||||
|
||||
- set_performance: property which can be set equal to the
|
||||
cumulative_trading_performance property of the trading_client. An
|
||||
|
||||
- set_performance: property which can be set equal to the
|
||||
cumulative_trading_performance property of the trading_client. An
|
||||
algorithm can then check position information with the
|
||||
Portfolio object::
|
||||
|
||||
|
||||
self.Portfolio[sid(133)]['cost_basis']
|
||||
|
||||
"""
|
||||
|
||||
import zipline.protocol as zp
|
||||
|
||||
class TestAlgorithm():
|
||||
"""
|
||||
@@ -54,7 +53,7 @@ class TestAlgorithm():
|
||||
to verify the orders sent/received, transactions created, and positions
|
||||
at the close of a simulation.
|
||||
"""
|
||||
|
||||
|
||||
def __init__(self, sid, amount, order_count):
|
||||
self.count = order_count
|
||||
self.sid = sid
|
||||
@@ -64,26 +63,26 @@ class TestAlgorithm():
|
||||
self.order = None
|
||||
self.frame_count = 0
|
||||
self.portfolio = None
|
||||
|
||||
|
||||
def initialize(self):
|
||||
pass
|
||||
|
||||
|
||||
def set_order(self, order_callable):
|
||||
self.order = order_callable
|
||||
|
||||
|
||||
def set_portfolio(self, portfolio):
|
||||
self.portfolio = portfolio
|
||||
|
||||
|
||||
def handle_data(self, data):
|
||||
self.frame_count += 1
|
||||
#place an order for 100 shares of sid
|
||||
if self.incr < self.count:
|
||||
self.order(self.sid, self.amount)
|
||||
self.incr += 1
|
||||
|
||||
|
||||
def get_sid_filter(self):
|
||||
return [self.sid]
|
||||
|
||||
return [self.sid]
|
||||
|
||||
#
|
||||
class HeavyBuyAlgorithm():
|
||||
"""
|
||||
@@ -91,7 +90,7 @@ class HeavyBuyAlgorithm():
|
||||
to verify the orders sent/received, transactions created, and positions
|
||||
at the close of a simulation.
|
||||
"""
|
||||
|
||||
|
||||
def __init__(self, sid, amount):
|
||||
self.sid = sid
|
||||
self.amount = amount
|
||||
@@ -100,41 +99,41 @@ class HeavyBuyAlgorithm():
|
||||
self.order = None
|
||||
self.frame_count = 0
|
||||
self.portfolio = None
|
||||
|
||||
|
||||
def initialize(self):
|
||||
pass
|
||||
|
||||
pass
|
||||
|
||||
def set_order(self, order_callable):
|
||||
self.order = order_callable
|
||||
|
||||
|
||||
def set_portfolio(self, portfolio):
|
||||
self.portfolio = portfolio
|
||||
|
||||
|
||||
def handle_data(self, data):
|
||||
self.frame_count += 1
|
||||
#place an order for 100 shares of sid
|
||||
self.order(self.sid, self.amount)
|
||||
self.incr += 1
|
||||
|
||||
|
||||
def get_sid_filter(self):
|
||||
return [self.sid]
|
||||
|
||||
return [self.sid]
|
||||
|
||||
class NoopAlgorithm(object):
|
||||
"""
|
||||
Dolce fa niente.
|
||||
"""
|
||||
|
||||
|
||||
def initialize(self):
|
||||
pass
|
||||
|
||||
|
||||
def set_order(self, order_callable):
|
||||
pass
|
||||
|
||||
|
||||
def set_portfolio(self, portfolio):
|
||||
pass
|
||||
|
||||
|
||||
def handle_data(self, data):
|
||||
pass
|
||||
|
||||
|
||||
def get_sid_filter(self):
|
||||
return None
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user