alpacahq / Alpaca-API

The Alpaca API is a developer interface for trading operations and market data reception through the Alpaca platform.
https://alpaca.markets/
141 stars 13 forks source link

Submitted order does not appear on "trade_updates" event stream #74

Open d-e-s-o opened 5 years ago

d-e-s-o commented 5 years ago

(paper trading; v1)

In my experimentation an order submitted to the system reliably does not appear in the "trade_updates" stream of events until it is cancelled.

I listen to trade updates like this:

import alpaca_trade_api as tradeapi
conn = tradeapi.StreamConn()

@conn.on(r'trade_updates')
async def on_account_updates(conn, channel, account):
    print('trade', account)

conn.run(['trade_updates'])

Then I submit an order which is accepted

01:02:56 HTTP status: 200 OK
01:02:56 HTTP response: {"id":"e861a45a-ec58-4c54-99c6-e0001c936db8","client_order_id":"7567a582-234d-43e1-ad59-6ea1d2e6acf0","created_at":"2019-06-22T01:02:57.66207875Z","updated_at":"2019-06-22T01:02:57.680978319Z","submitted_at":"2019-06-22T01:02:57.65003418Z","filled_at":null,"expired_at":null,"canceled_at":null,"failed_at":null,"asset_id":"b28f4066-5c6d-479b-a2af-85dc1a8f16fb","symbol":"SPY","asset_class":"us_equity","qty":"1","filled_qty":"0","filled_avg_price":null,"order_type":"market","type":"market","side":"buy","time_in_force":"gtc","limit_price":null,"stop_price":null,"status":"new"}

I don't receive any events.

It's only when I cancel the order that I suddenly get informed about the cancellation as well as the creation:

trade Entity({   'event': 'new',
    'order': {   'asset_class': 'us_equity',
                 'asset_id': 'b28f4066-5c6d-479b-a2af-85dc1a8f16fb',
                 'canceled_at': None,
                 'client_order_id': '7567a582-234d-43e1-ad59-6ea1d2e6acf0',
                 'created_at': '2019-06-22T01:02:57.662079Z',
                 'expired_at': None,
                 'failed_at': None,
                 'filled_at': None,
                 'filled_avg_price': None,
                 'filled_qty': '0',
                 'id': 'e861a45a-ec58-4c54-99c6-e0001c936db8',
                 'limit_price': None,
                 'order_type': 'market',
                 'qty': '1',
                 'side': 'buy',
                 'status': 'new',
                 'stop_price': None,
                 'submitted_at': '2019-06-22T01:02:57.650034Z',
                 'symbol': 'SPY',
                 'time_in_force': 'gtc',
                 'type': 'market',
                 'updated_at': '2019-06-22T01:04:41.81251274Z'}})
trade Entity({   'event': 'canceled',
    'order': {   'asset_class': 'us_equity',
                 'asset_id': 'b28f4066-5c6d-479b-a2af-85dc1a8f16fb',
                 'canceled_at': '2019-06-22T01:04:41.809220438Z',
                 'client_order_id': '7567a582-234d-43e1-ad59-6ea1d2e6acf0',
                 'created_at': '2019-06-22T01:02:57.662079Z',
                 'expired_at': None,
                 'failed_at': None,
                 'filled_at': None,
                 'filled_avg_price': None,
                 'filled_qty': '0',
                 'id': 'e861a45a-ec58-4c54-99c6-e0001c936db8',
                 'limit_price': None,
                 'order_type': 'market',
                 'qty': '1',
                 'side': 'buy',
                 'status': 'canceled',
                 'stop_price': None,
                 'submitted_at': '2019-06-22T01:02:57.650034Z',
                 'symbol': 'SPY',
                 'time_in_force': 'gtc',
                 'type': 'market',
                 'updated_at': '2019-06-22T01:04:41.826689194Z'},
    'timestamp': '2019-06-22T01:04:41.809220438Z'})

Why?

umitanuki commented 5 years ago

This is probably happening only during the night time. Your order doesn't hit the simulator until next morning and therefore the 'new' event is not sent until the decision is made such as cancel. This behavior is also due to internal architecture and we have been discussing what is the right behavior during the night.

d-e-s-o commented 5 years ago

Thanks for the explanation. Do you have insight into how would a real order behave?