Right after I created the TFS Build Monitor for Android, I started to dream up new, ingenious ideas that would bring fame and fortune (I know this is why everyone gets into software development
). The TFS app didn’t sell a single download when it was 99 cents. When it was free it did a bit better but advertising in an app is like putting a billboard in the middle of the jungle. I would most likely need thousands of downloads to achieve any kind of profit. Even then it probably wouldn’t account for the hours and hours I spent working on the app.
After looking at the app landscape for Android, I came to the conclusion that people with money would most certainly be the ones to pay for an app. What better way than to integrate with a platform that manages money? I have had an eTrade account for a while so I decided this would be a good avenue to explore. Better yet, they didn’t offer an Android app. Gold mine!
Not really…
Exploring the eTrade API
The eTrade API is something you need to apply for. Like many APIs that manage real, physical assets (I guess that could be argued…) a developer key is required. This way they have all your information in case you were to create an app that was malicious in any way. The API was pretty rich. It allows you to look up quotes, return portfolio data, and even buy stocks and options. There were a few things lacking at the time I was working on the API. Mainly, there was no way to return a set of stock data over a period of time. This type of data is necessary for drawing charts. What good is a stock app that can’t draw charts?
The eTrade API uses OAuth to authenticate the application and user. Once the user logs in to the app, they are redirected to the eTrade website within the browser. After accepting some terms and agreements, they can permit the app to access their data. It’s a pretty easy system to get up and running. They even offer a Sandbox URL so you can test your app without acting on any live data.
Here is the code I used to initiate this communication. I’ve removed error handling to make it more succinct.
public String userAuthorize(Context ctx)
{
OAuthAccessor accessor = defaultAccessor();
OAuthClient client = new OAuthClient(new HttpClient4());
ArrayList> params = new ArrayList>();
params.add(new OAuth.Parameter(OAuth.OAUTH_CALLBACK, "oob"));
client.getRequestToken(accessor, OAuthMessage.GET, params);
Config.setRequestToken(accessor.requestToken, ctx);
Config.setRequestTokenSecret(accessor.tokenSecret, ctx);
return accessor.consumer.serviceProvider.userAuthorizationURL+
"?key="+accessor.consumer.consumerKey+
"&token="+accessor.requestToken;
}
Once the we receive an access token we are free to access the protected resources on the eTrade server. This is done through a RESTful API. I boiled down the actual accessing of the resources into a single method. Again, I’ve removed error handling.
public T getProtectedResource(RestRequest request, Boolean isRetry, Context ctx) throws EtradeException
{
ArrayList params = new ArrayList();
OAuthClient oclient = new OAuthClient(new HttpClient4());
OAuthAccessor accessor = defaultAccessor();
accessor.tokenSecret = Config.getAccessTokenSecret(ctx).trim();
OAuth.Parameter param = new OAuth.Parameter("oauth_token", Config.getAccessToken(ctx));
params.add(param);
OAuthMessage omessage = null;
if (request.hasBody())
{
omessage = accessor.newRequestMessage(request.getRequestType(), request.getSandboxUrl(), params, new ByteArrayInputStream(request.getBody().getBytes("UTF-8")));
omessage = oclient.invoke(omessage, ParameterStyle.AUTHORIZATION_HEADER);
}
else
{
omessage = oclient.invoke(accessor, request.getRequestType(), request.getSandboxUrl(), params);
}
return request.getResponse(omessage.readBodyAsString());
}
For the generic type parameter in this method, I would specify the object that I would like returned from the REST service (like a Quote object) . The getReponse method of the RestRequest class would return it for me. The RestRequest class is the base class for all the different types of requests I could make to the eTrade API. I had a class for Quotes, Accounts, AccountPositions, etc.
For example, if I wanted to query the Sandbox for a particular quote, I would access the Sandbox URL of the QuoteRequest class. The QuoteRequest class returns Quote objects.
public String getSandboxUrl() {
String url = "https://etwssandbox.etrade.com/market/sandbox/rest/quote/";
if (strTickers != null)
{
url += strTickers;
}
else
{
for(String ticker : tickers)
{
url += ticker + ",";
}
url = url.substring(0, url.length() - 2);
}
Log.d("QuoteRequest", url);
return (url + "?detailFlag=ALL");
}
Responses are returned as XML and can be easily parsed into objects. The QuoteRequest class also has a method for parsing the returned XML into a Quote object. Here is a list of some of the objects that you can access through the API.
- Quotes
- Accounts
- Account Positions
- Orders
- Alerts
















