Closed ydm closed 3 years ago
This bug also exists in 1.4.1 and 1.4.0.
After some investigation the issue is that for daily simulations orders don't get cancelled. Currently the EODCancel is only implemented for minutely simulations.
In summary, what is happening is that the orders keep getting recorded until the limit_order is satisfied then it fills all the orders. The following day the order_target sells back to fill the target.
Unfortunately, due to the zipline implementation logic, I don't think there is a clean and easy fix to this daily transactions issue
See PR 20 that, thanks to @MBounouar, should help. Please feel free to reopen if he issues persists regardless.
Well, it fixes it for daily frequency, but not for minute, since EODCancel and CancelPolicy in general can only be called on SESSION_END (gens/tradesimulation.py:~240). Another problem is self.clock does never fire MINUTE_END, otherwise a fix would be a piece of cake.
It can be fixed in the userspace by placing an order only if there isn't already an open order for the same asset:
def limit_order(asset, amount, limit):
exists = False
for order in api.get_open_orders().get(asset, []):
if np.allclose(order['amount'], amount) and np.allclose(order['limit'], limit):
exists = True
if not exists:
api.order_target(asset, amount, limit)
I would'nt mind trying to implement a better solution, if you put together a few examples of what you expect. Otherwise, I do agree that the default order placing logic is heavy handed and its up to the user to be careful and implement his logic.
@MBounouar , thanks for paying attention to that! I'll do my best to describe the problem. Perhaps in another issue, I'll leave a link here in a sec.
Tested against: 9e57abd37f0749a0f152fc3092b162f36370ab4b https://github.com/ydm/zipline-limit-order-bug/blob/main/limit_order_bug.ipynb
Dear Zipline Maintainers,
Before I tell you about my issue, let me describe my environment:
Environment
Now that you know a little about me, let me tell you about the issue I am having:
Description of Issue
I expect a limit order to respect the specified
---capital-base
and theorder_target(target=X)
target setting.On a custom test with BTC data and 24/7 market it's totally broken. In a simpler test with AAPL, it buys 13 shares at first.
Here is how you can reproduce this issue on your machine:
https://exchange.ml4trading.io/uploads/default/original/1X/4cb658a17618f5c4f373a0422443ce9f8d1f7a05.png
Reproduction Steps
What steps have you taken to resolve this already?
I pray to the universe for help.
...
Anything else?
...
Sincerely, ydm