Alpaca Close order missing

Whilst testing paper trading on Apaca, I noticed that there’s a mismatch between what QR thinks is the case and what Alpaca thinks.

QR entered the market at ~3:30:
quantrocket blotter status

At 3:45 I submitted the MOC close order:
quantrocket blotter close --order-refs ‘first-last-live’ --params ‘OrderType:MKT’ ‘Tif:MOC’ | quantrocket blotter order -f ‘-’

quantrocketec2_houston_1| - - [26/Jun/2020:19:45:03 +0000] “DELETE /blotter/positions.csv?order_refs=first-last-live&params=OrderType%3AMKT&params=Tif%3AMOC HTTP/1.1” 200 5 “-” “-”
quantrocketec2_houston_1| - - [26/Jun/2020:19:45:03 +0000] “POST /blotter/orders HTTP/1.1” 200 3 “-” “-”

But this order doesn’t show up in the blotter status and my positions are empty:

quantrocket blotter positions

However my Alpaca account still shows the 33 short.

When you run

$ quantrocket blotter close --order-refs ‘first-last-live’ --params ‘OrderType:MKT’ ‘Tif:MOC’ | quantrocket blotter order -f ‘-’

it will return the order ID, if an order was placed. You can then use the order ID to check the status:

quantrocket blotter status --order-ids <order id>

I would start there. The houston output you posted doesn’t actually prove an order was placed, just that those commands were run.

I see you’re using a custom project name quantrocketec2. Make sure you’re closing the position on the same deployment as the one where you placed the original order. That’s one scenario I can think of that would explain the sequence of events you saw.

Thanks for the response Brian.


quantrocket blotter close --order-refs ‘first-last-live’

Shows no results at all.

I only have one installation, so I’m sure it was executed on the correct one.

Everything suggests the order was opened and closed, and the only anomaly is the position you see in Alpaca. Any chance you opened the short position twice, then closed it once?

I’m not sure what else to suggest other than to try working through order placement fresh from start to finish.

Here’s the entire order list of the first-last-live strategy:

quantrocket blotter status |grep first-last-live


1c862714b28811ea9fab0242ac120011,alpaca,FIBBG000BDTBL9,SELL,32,PA23J54L7CWT,first-last-live,2020-06-19T23:53:45+00:00,Error,0,32,"[{"“ErrorCode”": 40010001, ““ErrorMsg””: ““invalid order type””}]"

2b53b22ab28811ea9fab0242ac120011,alpaca,FIBBG000BDTBL9,SELL,32,PA23J54L7CWT,first-last-live,2020-06-19T23:54:10+00:00,Error,0,32,"[{"“ErrorCode”": 40010001, ““ErrorMsg””: ““invalid order type””}]"








I would have expected to see a buy 33 on the 26th given the logs above.

If you query the executions for the strategy (quantrocket blotter executions) does it line up with the order statuses?

quantrocket blotter close looks at the LatestPosition table (same as quantrocket blotter positions). The LatestPosition table is updated based on the incoming execution records from Alpaca.

I don’t see the 33 short:

quantrocket blotter executions|grep first-last-live







Since there is no execution record for the SELL 33, when you tried to close the position, QuantRocket thought there was no position and did nothing.

So the question is why was there no execution record for the SELL 33? On the 26th QuantRocket would have been expected to make an API call like the one below to get the latest Alpaca executions.

import alpaca_trade_api as tradeapi
api = tradeapi.REST(api_key, secret_key, base_url="", api_version='v2')

# this is the latest execution QR knew about on the 26th
latest_execution_id = "20200622121648232::530d4499-85c7-4c66-8e59-dd21185976b3"

executions = api.get_activities(
        activity_types=["FILL"], direction="asc",
        page_size=100, page_token=latest_execution_id)

QuantRocket would then look at the order_id field in the returned executions and match the executions to any orders it knows about. Please run this in a notebook or console and see if you find an execution with order_id 1c0b112a-abd7-496d-b6ec-d96ee5727b6c

Looks like there are two:
AccountActivity({ 'activity_type': 'FILL', 'cum_qty': '12', 'id': '20200626033006626::4aa6dcd2-d16b-46c6-aae3-882743bf5085', 'leaves_qty': '21', 'order_id': '1c0b112a-abd7-496d-b6ec-d96ee5727b6c', 'price': '300.35', 'qty': '12', 'side': 'sell_short', 'symbol': 'SPY', 'transaction_time': '2020-06-26T19:30:06.626249Z', 'type': 'partial_fill'}),
AccountActivity({ 'activity_type': 'FILL', 'cum_qty': '33', 'id': '20200626033007109::5e020a37-dfcc-4bf3-96b5-90788e30e8e7', 'leaves_qty': '0', 'order_id': '1c0b112a-abd7-496d-b6ec-d96ee5727b6c', 'price': '300.33', 'qty': '21', 'side': 'sell_short', 'symbol': 'SPY', 'transaction_time': '2020-06-26T19:30:07.109777Z', 'type': 'fill'}),

Would you mind sending your logs to brian at quantrocket dot com?

quantrocket flightlog get app.log
quantrocket flightlog get -d sys.log
gzip app.log
gzip sys.log

I think there are two next steps here.

One, since the logs had rotated out, it would be nice to catch this issue as it’s happening. Among other things, that would help rule out the possibility that Alpaca was delayed in returning that execution. Running this command on the crontab during the period you hold positions will log to flightlog if the blotter and Alpaca disagree about your positions (it could be combined with a Papertrail alert).

quantrocket blotter positions -a <ACCOUNT> --broker --diff | quantrocket flightlog log --level CRITICAL

If that alerts you to a repeat of the issue, a nice first step would be to promptly manually query the Alpaca executions like above and see if the execution is present.

The second next step is that I think the blotter should request a more generous overlap of executions from Alpaca, instead of using the last_execution_id. What if there was some kind of race condition where execution B arrived and got saved before execution A, then execution A was no longer requested because the blotter thought it was up-to-date through B? That could be prevented by always requesting all executions that are later than, say, an hour ago. There’s no downside to doing that, so it seems prudent. Whether a race condition is likely to explain your issue might depend on whether you had other Alpaca executions (from any source, not just QuantRocket) that were close in time to the missed execution. Was that the case?

1 Like

QuantRocket 2.1.0 is now available and includes new behavior to request an overlap of executions from Alpaca.

This topic was automatically closed after 14 days. New replies are no longer allowed.