pyetrade package¶
Submodules¶
pyetrade.accounts module¶
Accounts - ETrade Accounts API Calls
- TODO:
- Fix init doc string
- Check request response for error
-
class
pyetrade.accounts.ETradeAccounts(client_key, client_secret, resource_owner_key, resource_owner_secret, dev=True)[source]¶ Bases:
objectDescription: Accounts object to access account information
Parameters: - client_key (str, required) – Client key provided by Etrade
- client_secret (str, required) – Client secret provided by Etrade
- resource_owner_key (str, required) – Resource key from
pyetrade.authorization.ETradeOAuth - resource_owner_secret (str, required) – Resource secret from
pyetrade.authorization.ETradeOAuth - dev (bool, optional) – Defines Sandbox (True) or Live (False) ETrade, defaults to True
EtradeRef: https://apisb.etrade.com/docs/api/account/api-account-v1.html
-
get_account_balance(account_id_key: str, account_type=None, real_time=True, resp_format='xml') → dict[source]¶ Description: Retrieves account balanace for an account
Parameters: - account_id_key (str, required) – AccountIDkey retrived from
list_accounts - resp_format (str, optional) – Desired Response format, defaults to xml
Returns: Balance of account with key
account_id_keyReturn type: xml or json based on
resp_formatEtradeRef: https://apisb.etrade.com/docs/api/account/api-balance-v1.html
- account_id_key (str, required) – AccountIDkey retrived from
-
get_account_portfolio(account_id_key: str, resp_format='xml', **kwargs) → dict[source]¶ Description: Retrieves account portfolio for an account
Parameters: - account_id_key (str, required) – AccountIDkey retrived from
list_accounts - resp_format (str, optional) – Desired Response format, defaults to xml
- kwargs (
**kwargs, optional) – Parameters for api
Returns: Account portfolio of account with key
account_id_keyReturn type: xml or json based on
resp_formatEtradeRef: https://apisb.etrade.com/docs/api/account/api-portfolio-v1.html
- account_id_key (str, required) – AccountIDkey retrived from
-
list_accounts(resp_format='xml') → dict[source]¶ Description: Lists accounts in Etrade Parameters: resp_format (str, optional) – Desired Response format, defaults to xml Returns: List of accounts Return type: xml or json based on resp_formatEtradeRef: https://apisb.etrade.com/docs/api/account/api-account-v1.html
-
list_transaction_details(account_id_key: str, transaction_id: int, resp_format='xml', **kwargs) → dict[source]¶ Description: Retrieves transaction details for an account
Parameters: - account_id_key (str, required) – AccountIDKey retrived from
list_accounts - resp_format (str, optional) – Desired Response format, defaults to xml
- transaction_id (int, required) – Numeric transaction ID obtained from
list_transactions - kwargs (
**kwargs, optional) – Parameters for api
Returns: Transaction Details for
transaction_idfor account keyaccount_id_keyReturn type: xml or json based on
resp_formatEtradeRef: https://apisb.etrade.com/docs/api/account/api-transaction-v1.html
- account_id_key (str, required) – AccountIDKey retrived from
-
list_transactions(account_id_key: str, resp_format='xml', **kwargs) → dict[source]¶ Description: Retrieves transactions for an account
Parameters: - account_id_key (str, required) – AccountIDKey retrived from
list_accounts - resp_format (str, optional) – Desired Response format, defaults to xml
- kwargs (
**kwargs, optional) – Parameters for api
Returns: Transactions list for account with key
account_id_keyReturn type: xml or json based on
resp_formatEtradeRef: https://apisb.etrade.com/docs/api/account/api-transaction-v1.html
- account_id_key (str, required) – AccountIDKey retrived from
pyetrade.authorization module¶
Authorization - ETrade Authorization API Calls
- TODO:
- Lint this messy code
- Catch events
Bases:
objectDescription: Renews and revokes ETrade OAuth access tokens
Parameters: - client_key (str, required) – Client key provided by Etrade
- client_secret (str, required) – Client secret provided by Etrade
- resource_owner_key (str, required) – Resource key from
ETradeOAuth - resource_owner_secret (str, required) – Resource secret from
ETradeOAuth
EtradeRef: https://apisb.etrade.com/docs/api/authorization/renew_access_token.html
Description: Renews access tokens obtained from ETradeOAuthParameters: None – Takes no parameters Returns: Success or failure Return type: bool (True or False) EtradeRef: https://apisb.etrade.com/docs/api/authorization/renew_access_token.html
Description: Revokes access tokens obtained from ETradeOAuthParameters: None – Takes no parameters Returns: Success or failure Return type: bool (True or False) EtradeRef: https://apisb.etrade.com/docs/api/authorization/revoke_access_token.html
Bases:
objectDescription: Performs authorization for OAuth 1.0a
Parameters: - client_key (str, required) – Client key provided by Etrade
- client_secret (str, required) – Client secret provided by Etrade
- callback_url (str, optional) – Callback URL passed to OAuth mod, defaults to “oob”
EtradeRef: https://apisb.etrade.com/docs/api/authorization/request_token.html
Description: Obtains access token. Requires token URL from get_request_tokenParameters: verifier (str, required) – OAuth Verification Code from Etrade Returns: OAuth access tokens Return type: dict EtradeRef: https://apisb.etrade.com/docs/api/authorization/get_access_token.html
Description: Obtains the token URL from Etrade. Parameters: None – Takes no parameters Returns: Formatted Authorization URL (Access this to obtain taken) Return type: str EtradeRef: https://apisb.etrade.com/docs/api/authorization/request_token.html
pyetrade.alerts module¶
Alerts - ETrade Alerts API
- TODO:
- list_alerts - add args
- list_alert_details - add arg
-
class
pyetrade.alerts.ETradeAlerts(client_key, client_secret, resource_owner_key, resource_owner_secret, dev=True)[source]¶ Bases:
objectDescription: Object to retrieve alerts
Parameters: - client_key (str, required) – Client key provided by Etrade
- client_secret (str, required) – Client secret provided by Etrade
- resource_owner_key (str, required) – Resource key from
pyetrade.authorization.ETradeOAuth - resource_owner_secret (str, required) – Resource secret from
pyetrade.authorization.ETradeOAuth - dev (bool, optional) – Defines Sandbox (True) or Live (False) ETrade, defaults to True
EtradeRef: -
delete_alert(alert_id, resp_format='xml')[source]¶ Description: Deletes specified alert
Parameters: - alert_id (int, required) – Alert ID obtained from
list_alerts - resp_format (str, optional) – Desired Response format, defaults to xml
Returns: List of alert details
Return type: xml or json based on
resp_formatEtradeRef: - alert_id (int, required) – Alert ID obtained from
-
list_alert_details(alert_id, resp_format='xml') → dict[source]¶ Description: Provides details for an alert
Parameters: - alert_id (int, required) – Alert ID obtained from
list_alerts - resp_format (str, optional) – Desired Response format, defaults to xml
Returns: List of alert details
Return type: xml or json based on
resp_formatEtradeRef: - alert_id (int, required) – Alert ID obtained from
-
list_alerts(resp_format='xml') → dict[source]¶ Description: Lists alerts in Etrade Parameters: resp_format (str, optional) – Desired Response format, defaults to xml Returns: List of alerts Return type: xmlorjsonbased onresp_formatEtradeRef: https://apisb.etrade.com/docs/api/user/api-alert-v1.html
pyetrade.maket module¶
Market - ETrade Market API V1
TODO: * move logger into object under self.logger
-
class
pyetrade.market.ETradeMarket(client_key, client_secret, resource_owner_key, resource_owner_secret, dev=True)[source]¶ Bases:
objectDescription: Performs Market functions
Parameters: - client_key (str, required) – Client key provided by Etrade
- client_secret (str, required) – Client secret provided by Etrade
- resource_owner_key (str, required) – Resource key from
pyetrade.authorization.ETradeOAuth - resource_owner_secret (str, required) – Resource secret from
pyetrade.authorization.ETradeOAuth - dev (bool, optional) – Defines Sandboxi (True) or Live (False) ETrade, defaults to True
EtradeRef: -
get_option_chains(underlier: str, expiry_date, skip_adjusted=None, chain_type=None, strike_price_near=None, no_of_strikes=None, option_category=None, price_type=None, resp_format='xml') → dict[source]¶ Description: Returns the option chain information for the requested expiry_date and chaintype in the desired format. This should be a list of dictionaries, one for each option chain.
Parameters: - underlier (str, required) – Market Symbol
- expiry_date (datetime.date(year, month, day), optional) – Contract expiration date, None produces closest to today
- skip_adjusted (str, optional) – Specifies whether to show (True) or not show (False) adjusted options, defaults to True
- chain_type (str, optional) – Type of option chain, defaults to callput
- strike_price_near (int, optional) – Optionchains fetched will have strike price close to this value
- no_of_strikes (int, optional) – Indicates number of strikes for which the optionchain needs to be fetched, defaults to None
- option_category (str, optional) – The option category, defaults to
standard - price_type (str, optional) – The price type, defaults to
atnm - resp_format (str, optional) – Desired Response format, defaults to
xml
Returns: Returns list of option chains for a specific underlying instrument
Return type: xml or json based on
resp_formatChain_type values: - put
- call
- callput (default)
Option_category values: - standard (default)
- all
- mini
Price_type values: - atnm
- all
SampleURL: EtradeRef:
-
get_option_expire_date(underlier: str, resp_format='xml') → dict[source]¶ Description: Returns a list of dates suitable for structuring an option table display
Parameters: - underlier (str, required) – Market Symbol
- resp_format (str, optional) – Desired Response format, defaults to xml
Returns: Returns expiry of options for symbol
Return type: xml or json based on
resp_formatSampleURL: https://api.etrade.com/v1/market/optionexpiredate?symbol=GOOG&expiryType=ALL
EtradeRef:
-
get_quote(symbols, detail_flag=None, require_earnings_date=None, skip_mini_options_check=None, resp_format='xml') → dict[source]¶ Description: Get quote data on symbols provided in the list args.
Parameters: - symbols (list[], required) – Symbols in list args format. Limit 25.
- detail_flag (enum, optional) – Market fields returned from a quote request, defaults to None
- require_earnings_date (str, optional) – Provides Earnings date if True, defaults to None
- skip_mini_options_check (str, optional) – Skips mini options check if True, defaults to None
- resp_format (str, optional) – Desired Response format, defaults to xml
Returns: Returns quote data on symbols provided
Return type: xml or json based on
resp_formatSymbols values: - Limited to 25. If exceeded, first 25 will be processed with warnings
- Equities format -
symbolname sufficient, e.g. GOOGL. - Options format -
underlier:year:month:day:optionType:strikePrice
Detailflag values: - fundamental - Instrument fundamentals and latest price
- intraday - Performance for the current of most recent trading day
- options - Information on a given option offering
- week_52 - 52-week high and low (highest high and lowest low)
- mf_detail - MutualFund structure gets displayed
- all (default) - All of the above information and more
- None - Defaults to all.
SkipMiniOptionsCheck values: - True - Call is NOT made to check whether the symbol has mini options
- False - Call is made to check whether the symbol has mini options
- None - Call is made to check whether the symbol has mini options (default)
EtradeRef:
-
look_up_product(search_str: str, resp_format='xml') → dict[source]¶ Description: Performs a look up product
Parameters: - search_str (str, required) – Full or partial name of the company.
- resp_format (str, optional) – Desired Response format, defaults to xml
Returns: Product lookup
Return type: xmlorjsonas defined byresp_formatNote: Etrade abbreviates common words such as company, industry and systems and generally skips punctuation.
EtradeRef: https://apisb.etrade.com/docs/api/market/api-market-v1.html#/definition/Lookup
pyetrade.order module¶
Order - ETrade Order API
- TODO:
- Preview equity order change
- Place equity order change
- Preview option order
- Place option order
- Preview option order change
- Place option order change
-
class
pyetrade.order.ETradeOrder(client_key, client_secret, resource_owner_key, resource_owner_secret, dev=True, timeout=30)[source]¶ Bases:
objectDescription: Object to perform Orders
Parameters: - client_key (str, required) – Client key provided by Etrade
- client_secret (str, required) – Client secret provided by Etrade
- resource_owner_key (str, required) – Resource key from
pyetrade.authorization.ETradeOAuth - resource_owner_secret (str, required) – Resource secret from
pyetrade.authorization.ETradeOAuth - dev (bool, optional) – Defines Sandbox (True) or Live (False) ETrade, defaults to True
- timeout (int, optional) – Timeout value for OAuth, defaults to 30
EtradeRef: -
build_order_payload(order_type: str, **kwargs) → dict[source]¶ Description: Builds the POST payload of a preview or place order (Used internally) Parameters: order_type (str, required) – PreviewOrderRequest or PlaceOrderRequest SecurityType: EQ or OPTN OrderAction: for OPTN: BUY_OPEN, SELL_CLOSE CallPut: CALL or PUT ExpiryDate: string, e.g. “2022-02-18” Returns: Builds Order Payload Return type: xmlorjsonbased onresp_formatEtradeRef: https://apisb.etrade.com/docs/api/order/api-order-v1.html
-
cancel_order(account_id_key: str, order_num: int, resp_format: str = 'xml') → dict[source]¶ Description: Cancels a specific order for a given account
Parameters: - account_id_key (str, required) – AccountIDkey retrived from
pyetrade.accounts.ETradeAccounts.list_accounts - order_num (int, required) – Numeric id for this order listed in
list_orders - resp_format (str, required) – Desired Response format, defaults to xml
Returns: Confirmation of cancellation
Return type: dict/jsonEtradeRef: - account_id_key (str, required) – AccountIDkey retrived from
-
change_preview_equity_order(account_id_key: str, order_id: str, **kwargs)[source]¶ Description: Same as
preview_equity_orderwith orderIdParameters: - order_id (str, required) – order_id to modify, refer
list_orders - account_id_key (str, required) – account_id_key retrieved from
list_accounts
Returns: Previews Changed order with orderId for account with account_id_key
Return type: dict/json
EtradeRef: - order_id (str, required) – order_id to modify, refer
-
static
check_order(**kwargs)[source]¶ Description: Check that required params for preview or place order are there and correct (Used internally)
-
find_option_orders(account_id_key: str, symbol: str, call_put: str, expiry_date: str, strike_price: float) → list[source]¶ Description: Lists option orders for a specific account ID Key
Parameters: - account_id_key (str, required) – AccountIDKey from
pyetrade.accounts.ETradeAccounts.list_accounts - symbol (str, required) – ticker symbol for options chain
- call_put (str, required) – whether the option is a call or put
- expiry_date (str, required) – desired expiry of option (ex: 12-05-2021)
- strike_price (str, required) – strike price of desired option
Returns: List of matching option orders in an account
- account_id_key (str, required) – AccountIDKey from
-
list_orders(account_id_key: str, resp_format: str = 'json', **kwargs) → dict[source]¶ Description: Lists orders for a specific account ID Key
Parameters: - account_id_key (str, required) – AccountIDKey from
pyetrade.accounts.ETradeAccounts.list_accounts - resp_format (str, optional) – Desired Response format, defaults to xml
- kwargs (
**kwargs, optional) – Parameters for api. Refer to EtradeRef for options
Returns: List of orders for an account
Return type: xmlorjsonbased onresp_formatEtradeRef: Returns: List of orders in an account
- account_id_key (str, required) – AccountIDKey from
-
perform_request(method, api_url: str, payload: Union[dict, str], resp_format: str = 'xml') → dict[source]¶ Description: POST or PUT request with json or xml used by preview, place and cancel
Parameters: - method (session, required) – PUT or POST method
- resp_format (str, required) – Desired Response format, defaults to xml
- api_url (str, required) – API URL
- payload (json/dict or str xml, required) – Payload
Returns: Return request
Return type: xml or json based on
resp_formatEtradeRef:
-
place_changed_equity_order(**kwargs) → dict[source]¶ Description: Places changes to equity orders NOTE: the ETrade server will actually cancel the old orderId, and create a new orderId Parameters: kwargs ( **kwargs, required) – Parameters for api, referchange_preview_equity_orderReturns: Returns confirmation similar to preview_equity_orderReturn type: xml or json based on resp_formatEtradeRef: https://apisb.etrade.com/docs/api/order/api-order-v1.html
-
place_changed_option_order(**kwargs) → dict[source]¶ Description: Places Option Order, only single leg CALL or PUT is supported for now Returns: Returns confirmation of the equity order
-
place_equity_order(**kwargs) → dict[source]¶ Description: Places Equity Order Parameters: kwargs ( **kwargs, required) – Parameters for api, referpreview_equity_orderReturns: Returns confirmation of the equity order Return type: xml or json based on resp_formatEtradeRef: https://apisb.etrade.com/docs/api/order/api-order-v1.html
-
place_option_order(**kwargs) → dict[source]¶ Description: Places Option Order, only single leg CALL or PUT is supported for now Returns: Returns confirmation of the equity order
-
preview_equity_order(**kwargs) → dict[source]¶ API is used to submit an order request for preview before placing it
Parameters: - accountIdKey (str, required) – AccountIDkey retrived from
list_accounts - symbol (str) – Market symbol for the security being bought or sold
- orderAction (str, required) – Action that the broker is requested to perform
- previewId (long, conditional) – Required only if order was previewed. Numeric preview ID from preview. Note - Other parameters much match that of preview
- clientOrderId (str, required) – Reference number generated by developer. Used to ensure duplicate order is not submitted. Value can be of 20 alphanmeric characters or less Must be uniquewithin this account. Does not appear in any API responses.
- priceType (str, required) – Type of pricing specified in equity order
- limitPrice (double, conditional) – Highest to buy or lowest to sell. Required if priceType is STOP or STOP_LIMIT
- stopPrice (double, conditional) – Price to buy or sell if specified in a stop order. Required if priceType is STOP or STOP_LIMIT
- allOrNone (bool, optional) – Specifies if order must be executed all at once. TRUE triggers allOrNone, defaults to FALSE
- quantity (int, required) – Number of shares to buy or sell
- reserveOrder (bool, optional) – If set to TRUE, publicly displays only a limited number of shares (the reserve quantity), instead of the entire order, to avoid influencing other traders. If TRUE, must also specify the reserveQuantity, defaults to FALSE
- reserveQuantity (int, conditional) – Number of shares to be publicly displayed if this is a reserve order. Required if reserveOrder is TRUE.
- marketSession (str, required) – Session to place the equity order
- orderTerm (str, required) – Term for which the order is in effect.
- routingDestination (str, optional) – Exchange where the order should be executed.
- estimatedCommission (double) – Cost billed to the user to preform requested action
- estimatedTotalAmount (double) – Cost including commission
- messageList (dict) – Container for messages describing the result of the action
- msgDesc (str) – Text of the result message, indicating order status, success or failure, additional requirements that must be met before placing the order, etc. Applications typically display this message to the user, which may result in further user action
- msgCode (int) – Standard numeric code of the result message. Refer to the Error Messages documentation for examples. May optionally be displayed to the user, but is primarily intended for internal use.
- orderNum (int) – Numeric ID for this order in the E*TRADE system
- orderTime (long) – The epoch time the order was submitted.
- symbolDesc (str) – Text description of the security
- symbol – The market symbol for the underlier
OrderAction values: - BUY
- SELL
- BUY_TO_COVER
- SELL_SHORT
PriceType values: - MARKET
- LIMIT - Requires limitPrice
- STOP - Requires stopPrice
- STOP_LIMIT - Requires limitPrice
- MARKET_ON_CLOSE
MarketSession values: - REGULAR
- EXTENDED
OrderTerm values: - GOOD_UNTIL_CANCEL
- GOOD_FOR_DAY
- IMMEDIATE_OR_CANCEL (only for LIMIT orders)
- FILL_OR_KILL (only for LIMIT orders)
RoutingDestination values: - AUTO (default)
- ARCA
- NSDQ
- NYSE
Returns: Confirmation of the Preview Equity Order
Return type: xmlorjsonbased onresp_formatEtradeRef: - accountIdKey (str, required) – AccountIDkey retrived from
-
exception
pyetrade.order.OrderException(explanation=None, params=None)[source]¶ Bases:
ExceptionDescription: Exception raised when giving bad args to a method not from Etrade calls
-
pyetrade.order.get_request_result(req: requests.sessions.Session.request, empty_json: dict, resp_format: str = 'xml') → dict[source]¶