Created
January 8, 2012 14:15
-
-
Save fawce/1578485 to your computer and use it in GitHub Desktop.
Simple Object Model for Quantopian Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SecurityState(): | |
"""An example class to track persistent state for a | |
specific security. This trivial example just tracks the max | |
price for the security, as well as shares ordered. | |
Modify this class to track individual securities.""" | |
def __init__(self, tick): | |
self.sid = tick.sid | |
self.max_price = tick.price | |
self.new_highs = False | |
def update(self, tick): | |
if(tick.price > self.max_price): | |
self.max_price = tick.price | |
self.new_highs = True | |
else: | |
self.new_highs = False | |
def place_order(self): | |
"""This method is called after the state of the security has | |
been updated with the latest tick. Use the current state to | |
determine whether to buy or sell the security.""" | |
if(self.new_highs): | |
order(self.sid, -100) | |
class MarketState(): | |
"""An example class to track a model state as events arrive. | |
Manages a map of Security ID --> SecurityState objects. | |
Add properties to this class to track properties across | |
all securities. | |
""" | |
def __init__(self): | |
self.state_map = {} | |
def update(self, tick): | |
if self.state_map.has_key(tick.sid): | |
self.state_map[tick.sid].update(tick) | |
else: | |
self.state_map[tick.sid] = SecurityState(tick) | |
return self.state_map[tick.sid] | |
market_state = MarketState() | |
def handle_events(queue): | |
for tick in queue: | |
security_state = market_state.update(tick) | |
security_state.place_order() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment