Error Handling

Most of the errors you will see while using LimeTrader SDK are errors returned from REST API. These errors are not modified and are returned with details from the REST API.

Handle order place error:

 1 from _decimal import Decimal
 2 from http import HTTPStatus
 3 from pprint import pprint
 4
 5 from lime_trader import LimeClient
 6 from lime_trader.exceptions.api_error import ApiError
 7 from lime_trader.models.trading import Order, TimeInForce, OrderType, OrderSide
 8
 9 client = LimeClient.from_file(file_path="credentials.json")
10
11 accounts = client.account.get_balances()  # need to get account numbers first
12
13 account_number = accounts[0].account_number  # get account number of first account in a list
14
15 order = Order(account_number=account_number,
16               symbol="AAPADL",
17               quantity=Decimal(1),
18               price=Decimal("140.0"),
19               stop_price=Decimal("159.00"),
20               time_in_force=TimeInForce.DAY,
21               order_type=OrderType.LIMIT,
22               side=OrderSide.BUY,
23               exchange="auto",
24               )
25 try:
26     placed_order_response = client.trading.place_order(order=order)
27     pprint(placed_order_response)
28 except ApiError as e:
29     if e.error.status_code == HTTPStatus.NOT_FOUND:
30         print(f"Not found: {e.error.message}")
31 except Exception as e:
32     print("Unexpected error")