STY: Move order_value from blotter to TradingAlgorithm.

This commit is contained in:
Thomas Wiecki
2013-11-25 17:38:30 -05:00
parent 579cb56663
commit c199a0d956
2 changed files with 22 additions and 28 deletions
+22 -3
View File
@@ -382,10 +382,29 @@ class TradingAlgorithm(object):
return self.blotter.order(sid, amount, limit_price, stop_price)
def order_value(self, sid, value, limit_price=None, stop_price=None):
"""
Place an order by desired value rather than desired number of shares.
If the requested sid is found in the universe, the requested value is
divided by its price to imply the number of shares to transact.
value > 0 :: Buy/Cover
value < 0 :: Sell/Short
Market order: order(sid, value)
Limit order: order(sid, value, limit_price)
Stop order: order(sid, value, None, stop_price)
StopLimit order: order(sid, value, limit_price, stop_price)
"""
last_price = self.trading_client.current_data[sid].price
return self.blotter.order_value(sid, value, last_price,
limit_price=limit_price,
stop_price=stop_price)
if np.allclose(last_price, 0):
zero_message = "Price of 0 for {psid}; can't infer value".format(
psid=sid
)
log.debug(zero_message)
# Don't place any order
return
else:
amount = value / last_price
return self.order(sid, amount, limit_price, stop_price)
@property
def recorded_vars(self):
-25
View File
@@ -136,31 +136,6 @@ class Blotter(object):
return order.id
def order_value(self, sid, value, last_price,
limit_price=None, stop_price=None):
"""
Place an order by desired value rather than desired number of shares.
If the requested sid is found in the universe, the requested value is
divided by its price to imply the number of shares to transact.
value > 0 :: Buy/Cover
value < 0 :: Sell/Short
Market order: order(sid, value)
Limit order: order(sid, value, limit_price)
Stop order: order(sid, value, None, stop_price)
StopLimit order: order(sid, value, limit_price, stop_price)
"""
if np.allclose(last_price, 0):
zero_message = "Price of 0 for {psid}; can't infer value".format(
psid=sid
)
log.debug(zero_message)
# Don't place any order
return
else:
amount = value / last_price
return self.order(sid, amount, limit_price, stop_price)
def cancel(self, order_id):
if order_id not in self.orders:
return