mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-06 13:00:45 +08:00
BUG: Apply integer truncation to order amounts earlier in the pipeline.
Truncate non-integer order amounts in `TradingAlgorithm.order` instead of `Blotter.order`. This fixes an issue where non-integer orders coming out of order_value can spuriously trigger a `LongOnly` trading guard. Example: sid.price == 2.0 order_value(sid, 5) -> order(sid, 2.5) -> truncated to order(sid, 2.0) order_value(sid, -5) -> order(sid, -2.5) -> LongOnlyViolation b/c 2.0 - 2.5 < 0
This commit is contained in:
@@ -499,6 +499,22 @@ class TradingAlgorithm(object):
|
||||
"""
|
||||
Place an order using the specified parameters.
|
||||
"""
|
||||
|
||||
def round_if_near_integer(a, epsilon=1e-4):
|
||||
"""
|
||||
Round a to the nearest integer if that integer is within an epsilon
|
||||
of a.
|
||||
"""
|
||||
if abs(a - round(a)) <= epsilon:
|
||||
return round(a)
|
||||
else:
|
||||
return a
|
||||
|
||||
# Truncate to the integer share count that's either within .0001 of
|
||||
# amount or closer to zero.
|
||||
# E.g. 3.9999 -> 4.0; 5.5 -> 5.0; -5.5 -> -5.0
|
||||
amount = int(round_if_near_integer(amount))
|
||||
|
||||
# Raises a ZiplineError if invalid parameters are detected.
|
||||
self.validate_order_params(sid,
|
||||
amount,
|
||||
|
||||
Reference in New Issue
Block a user