Streaming Real-Time Barcharts

Follow along with this article by checking out the Java sample code on GitHub. Note: Java is just an example, not necessarily the preferred programming language for consuming the TradeStation WebAPI. Please let us know if you would like an equivalent article in another language.

Pulling data from the TradeStation WebAPI empowers your applications with quotes, balances, and positions with relative ease. It is slightly more of a challenge to get streaming data though. Although there are many ways to do this, we’ve had good success with the method we’ll talk about here.

Prepare the HTTP client to open streaming requests indefinitely


For streaming requests, the response will not end. Ensure that the HTTP client will not throw a timeout exception during execution.

Build the HTTP Request

This code snippet shows that the HTTP request is being generated and then passed to the StreamSource class that will execute the request.

Execute the Request & Parse the JSON response in chunks as you receive them & Deserialize into Java POJOs

This is the run method within the StreamSource object (which extends from Observable in Java). This run method executes the HTTP request using the ning.com Async Http Client. As you can see, the httpResponseBodyPart is encoded to a String that will represent a chunk of JSON. The chunk may only be a partial JSON object that will be complete on the next chunk, so the full body is appended to remainingPartialJsonData to be evaluated over multiple response chunks. Each response character is evaluated to determine if a JSON object is detected. If the JSON object is valid, it will then be deserialized into the plain old java object that we’ve specified the Object Mapper to use. Upon success, the observers are notified of new data available and remainingPartialJsonData is updated to remove the the successful object to process the next one.

Use Observers to process the data

When the observer is notified of changes, the update method will execute the data using the new object and running any appropriate processes.

Need Help?

I hope this makes more sense, but in case you would like to dialog about this further, please send us an e-mail at webapi@tradestation.com.