Skip to content

Commit f249f59

Browse files
committed
etrade python client
1 parent 545b320 commit f249f59

File tree

14 files changed

+1417
-0
lines changed

14 files changed

+1417
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
"""This Python script provides examples on using the E*TRADE API endpoints"""
2+
from __future__ import print_function
3+
import webbrowser
4+
import json
5+
import logging
6+
import configparser
7+
import sys
8+
import requests
9+
from rauth import OAuth1Service
10+
from logging.handlers import RotatingFileHandler
11+
from accounts.accounts import Accounts
12+
from market.market import Market
13+
14+
# loading configuration file
15+
config = configparser.ConfigParser()
16+
config.read('config.ini')
17+
18+
# logger settings
19+
logger = logging.getLogger('my_logger')
20+
logger.setLevel(logging.DEBUG)
21+
handler = RotatingFileHandler("python_client.log", maxBytes=5*1024*1024, backupCount=3)
22+
FORMAT = "%(asctime)-15s %(message)s"
23+
fmt = logging.Formatter(FORMAT, datefmt='%m/%d/%Y %I:%M:%S %p')
24+
handler.setFormatter(fmt)
25+
logger.addHandler(handler)
26+
27+
28+
def oauth():
29+
"""Allows user authorization for the sample application with OAuth 1"""
30+
etrade = OAuth1Service(
31+
name="etrade",
32+
consumer_key=config["DEFAULT"]["CONSUMER_KEY"],
33+
consumer_secret=config["DEFAULT"]["CONSUMER_SECRET"],
34+
request_token_url="https://api.etrade.com/oauth/request_token",
35+
access_token_url="https://api.etrade.com/oauth/access_token",
36+
authorize_url="https://us.etrade.com/e/t/etws/authorize?key={}&token={}",
37+
base_url="https://api.etrade.com")
38+
39+
menu_items = {"1": "Sandbox Consumer Key",
40+
"2": "Live Consumer Key",
41+
"3": "Exit"}
42+
while True:
43+
print("")
44+
options = menu_items.keys()
45+
for entry in options:
46+
print(entry + ")\t" + menu_items[entry])
47+
selection = input("Please select Consumer Key Type: ")
48+
if selection == "1":
49+
base_url = config["DEFAULT"]["SANDBOX_BASE_URL"]
50+
break
51+
elif selection == "2":
52+
base_url = config["DEFAULT"]["PROD_BASE_URL"]
53+
break
54+
elif selection == "3":
55+
break
56+
else:
57+
print("Unknown Option Selected!")
58+
print("")
59+
60+
# Step 1: Get OAuth 1 request token and secret
61+
request_token, request_token_secret = etrade.get_request_token(
62+
params={"oauth_callback": "oob", "format": "json"})
63+
64+
# Step 2: Go through the authentication flow. Login to E*TRADE.
65+
# After you login, the page will provide a text code to enter.
66+
authorize_url = etrade.authorize_url.format(etrade.consumer_key, request_token)
67+
webbrowser.open(authorize_url)
68+
text_code = input("Please accept agreement and enter text code from browser: ")
69+
70+
# Step 3: Exchange the authorized request token for an authenticated OAuth 1 session
71+
session = etrade.get_auth_session(request_token,
72+
request_token_secret,
73+
params={"oauth_verifier": text_code})
74+
75+
main_menu(session, base_url)
76+
77+
78+
def main_menu(session, base_url):
79+
"""
80+
Provides the different options for the sample application: Market Quotes, Account List
81+
82+
:param session: authenticated session
83+
"""
84+
85+
menu_items = {"1": "Market Quotes",
86+
"2": "Account List",
87+
"3": "Exit"}
88+
89+
while True:
90+
print("")
91+
options = menu_items.keys()
92+
for entry in options:
93+
print(entry + ")\t" + menu_items[entry])
94+
selection = input("Please select an option: ")
95+
if selection == "1":
96+
market = Market(session, base_url)
97+
market.quotes()
98+
elif selection == "2":
99+
accounts = Accounts(session, base_url)
100+
accounts.account_list()
101+
elif selection == "3":
102+
break
103+
else:
104+
print("Unknown Option Selected!")
105+
106+
107+
if __name__ == "__main__":
108+
oauth()
Binary file not shown.

etrade_python_client/accounts/_init_.py

Whitespace-only changes.

0 commit comments

Comments
 (0)