diff --git a/README.md b/README.md index 5ab40d42..308e2e9f 100644 --- a/README.md +++ b/README.md @@ -104,13 +104,32 @@ You can access the following information through this object. ### REST.get_account() Calls `GET /account` and returns an `Account` entity. -### REST.list_orders(status=None, limit=None, after=None, until=None, direction=None) +### REST.list_orders(status=None, limit=None, after=None, until=None, direction=None, nested=None) Calls `GET /orders` and returns a list of `Order` entities. `after` and `until` need to be string format, which you can obtain by `pd.Timestamp().isoformat()` -### REST.submit_order(symbol, qty, side, type, time_in_force, limit_price=None, stop_price=None, client_order_id=None) +### REST.submit_order(symbol, qty, side, type, time_in_force, limit_price=None, stop_price=None, client_order_id=None, order_class=None, take_profit=None, stop_loss=None) Calls `POST /orders` and returns an `Order` entity. +Below is an example of submitting a bracket order. +```py +api.submit_order( + symbol='SPY', + side='buy', + type='market', + qty='100', + time_in_force='day', + order_class='bracket', + take_profit=dict( + limit_price='305.0', + ), + stop_loss=dict( + stop_price='295.5', + limit_price='295.5', + ) +) +``` + ### REST.get_order_by_client_order_id(client_order_id) Calls `GET /orders` with client_order_id and returns an `Order` entity. diff --git a/alpaca_trade_api/entity.py b/alpaca_trade_api/entity.py index 057fa834..da06e67a 100644 --- a/alpaca_trade_api/entity.py +++ b/alpaca_trade_api/entity.py @@ -48,7 +48,13 @@ class Asset(Entity): class Order(Entity): - pass + def __init__(self, raw): + super().__init__(raw) + try: + self.legs = [Order(o) for o in self.legs] + except Exception: + # No order legs existed + pass class Position(Entity): diff --git a/alpaca_trade_api/rest.py b/alpaca_trade_api/rest.py index 7deaae37..17c2f453 100644 --- a/alpaca_trade_api/rest.py +++ b/alpaca_trade_api/rest.py @@ -197,7 +197,7 @@ def update_account_configurations( return AccountConfigurations(resp) def list_orders(self, status=None, limit=None, after=None, until=None, - direction=None, params=None): + direction=None, params=None, nested=None): ''' Get a list of orders https://docs.alpaca.markets/web-api/orders/#get-a-list-of-orders @@ -214,12 +214,16 @@ def list_orders(self, status=None, limit=None, after=None, until=None, params['direction'] = direction if status is not None: params['status'] = status - resp = self.get('/orders', params) + if nested is not None: + params['nested'] = nested + url = '/orders' + resp = self.get(url, params) return [Order(o) for o in resp] def submit_order(self, symbol, qty, side, type, time_in_force, limit_price=None, stop_price=None, client_order_id=None, - extended_hours=None): + extended_hours=None, order_class=None, + take_profit=None, stop_loss=None): '''Request a new order''' params = { 'symbol': symbol, @@ -236,19 +240,27 @@ def submit_order(self, symbol, qty, side, type, time_in_force, params['client_order_id'] = client_order_id if extended_hours is not None: params['extended_hours'] = extended_hours + if order_class is not None: + params['order_class'] = order_class + if take_profit is not None: + params['take_profit'] = take_profit + if stop_loss is not None: + params['stop_loss'] = stop_loss resp = self.post('/orders', params) return Order(resp) def get_order_by_client_order_id(self, client_order_id): '''Get an order by client order id''' - resp = self.get('/orders:by_client_order_id', { + params = { 'client_order_id': client_order_id, - }) + } + resp = self.get('/orders:by_client_order_id', params) return Order(resp) def get_order(self, order_id): '''Get an order''' - resp = self.get('/orders/{}'.format(order_id)) + params = {} + resp = self.get('/orders/{}'.format(order_id), params) return Order(resp) def replace_order(