Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ttt733 committed Feb 6, 2020
2 parents 4856169 + 320e1f1 commit 86056bf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 7 additions & 1 deletion alpaca_trade_api/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
24 changes: 18 additions & 6 deletions alpaca_trade_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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(
Expand Down

0 comments on commit 86056bf

Please sign in to comment.