• Invest: 1-855-880-2559
  • Open 24/7

Ally Invest API

Streaming with Java

This uses the evented Jetty HTTP Client to make a request to the API and oauth-signpost library to OAuth sign the request.

import java.io.IOException;
import org.mortbay.jetty.client.*;
import org.mortbay.io.Buffer;
import oauth.signpost.*;
import oauth.signpost.jetty.*;
public class Streaming {
  public static void main(String[] args) throws Exception {
        String CONSUMER_KEY = "<CONSUMER_KEY>";
        String CONSUMER_SECRET = "<CONSUMER_SECRET>";
        String ACCESS_TOKEN = "<ACCESS_TOKEN>";
        String ACCESS_TOKEN_SECRET = "<ACCESS_TOKEN_SECRET>";
        // create a consumer object and configure it with the access
        // token and token secret obtained from the service provider
        OAuthConsumer consumer = new JettyOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        consumer.setTokenWithSecret(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
        // create an HTTP request to a protected resource
        ContentExchange request = new ContentExchange(true) {
          // tell me what kind of response code we got
            protected void onResponseComplete() throws IOException {
                int status = getResponseStatus();
                if (status == 200)
                    System.out.println("Successfully connected");
                else
                    System.out.println("Error Code Received: " + status);
            }
            // print out any response data we get along the stream
            protected void onResponseContent(Buffer data) {
              System.out.println(data);
            }
        };
        // setup the request
        request.setMethod("GET");
        request.setURL("https://stream.tradeking.com/v1/market/quotes.xml?symbols=F");
        // sign the request
        consumer.sign(request);
        // send the request
        HttpClient client = new HttpClient();
        client.start();
        client.send(request);
        request.waitForDone();
  }
}
Disclaimer:

Any mention of actual symbols are to be used for coding purposes only and do not imply a recommendation or solicitation to buy or sell a particular security or to engage in any particular investment strategy.

At the time of publication and in the preceding month, Ally Invest did not have ownership greater than 1% in any stocks mentioned here and does not have any other actual, material conflict of interest known at the time of publication.

Ally Invest did not receive compensation from a public offering or from investment banking services related to any companies mentioned here within the past 12 months, or expects to receive any in the next 3 months. Ally Invest did not engage in market making in the securities mentioned here.

Ally Invest Securities' background can be found at FINRA's BrokerCheck. Options involve risk and are not suitable for all investors.

Review the Characteristics and Risks of Standardized Options brochure (PDF) before you begin trading options. Options investors may lose the entire amount of their investment in a relatively short period of time.