Ruby · REST + WebSocket · NinjaTrader 8

Ruby NinjaTrader 8 API examples

Ruby wrapper for the CrossTrade NinjaTrader 8 API. net/http for REST, faye-websocket for streaming.

Why Ruby

Using Ruby against the CrossTrade NinjaTrader API

Ruby's net/http stdlib handles CrossTrade's REST surface cleanly. For WebSocket, faye-websocket gives you an event-driven client compatible with EventMachine if needed. GetExecution returns the matched-trade record for a specific order — useful for reconciliation.

REST net/http (stdlib)
WebSocket faye-websocket (gem)
REST · animated

Watch the request get written

A pseudo-developer types out a real GET GetExecutionByOrderId call against your NinjaTrader 8 account. The animation runs on a loop; the static snippet below is yours to copy.

getexecutionbyorderid.rb
Copy this

Complete Ruby GetExecutionByOrderId example

require "net/http"
require "json"
require "uri"

token = "your-bearer-token"
order_id = "cb1fc8d4e1a84d29ae38fea964aaac8c"

uri = URI("https://app.crosstrade.io/v1/api/accounts/Sim101/executions/#{order_id}")
req = Net::HTTP::Get.new(uri)
req["Authorization"] = "Bearer #{token}"

res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |h| h.request(req) }
pp JSON.parse(res.body)
WebSocket · animated

Streaming data with faye-websocket (gem)

Same flow on a persistent connection: open the socket, send a subscribe message, receive market data or P&L pushes in real time. CrossTrade's WebSocket is at wss://app.crosstrade.io/ws/stream with the same Bearer token in the request header.

stream.rb
Copy this

Complete Ruby WebSocket example

require "faye/websocket"
require "eventmachine"
require "json"

EM.run do
  ws = Faye::WebSocket::Client.new(
    "wss://app.crosstrade.io/ws/stream",
    nil,
    { headers: { "Authorization" => "Bearer YOUR_TOKEN" } }
  )

  ws.on(:open) { ws.send(JSON.dump(action: "subscribe", instruments: ["ES 12-25"])) }
  ws.on(:message) { |e| puts JSON.parse(e.data).inspect }
  ws.on(:close) { EM.stop }
end

The full Ruby-compatible API surface

Every endpoint documented with request/response schemas, error codes, rate-limit notes, and language-specific code samples.

FAQ

Common questions

Is Ruby a good language for algorithmic trading?

Yes, especially for teams already running on Rails or Sidekiq. Ruby's readability suits the documentation-style nature of trading rules ("if RSI crosses 70 and we're flat, enter long"), and the gem ecosystem covers everything you need: ActiveSupport for time math, Sidekiq for scheduled jobs, ActionMailer for trade alerts.

Rails integration?

Yes — wrap CrossTrade as a service object (app/services/crosstrade.rb) and inject it into controllers and jobs. Put the bearer token in Rails credentials (rails credentials:edit), not config.yml. For the WebSocket, run a separate Sidekiq worker or a dedicated daemon (Foreman process type "stream") that holds the long-lived connection.

HTTParty, Faraday, or net/http?

All three work. net/http is in stdlib (the example above), no gem needed. Faraday is the most flexible — middleware for retries, JSON parsing, logging. HTTParty is the friendliest API. Pick based on whether the rest of your codebase already commits to one.

Async patterns: ActiveJob, Sidekiq, or async-http?

For scheduled tasks (EOD reports, hourly risk checks): Sidekiq + sidekiq-cron is the production-grade choice. For ad-hoc parallelism: ActiveJob with a thread-pool adapter. For high-concurrency WebSocket fan-out: async-http or the async gem with fibers (Ruby 3.x).

Performance compared to Python or Node?

Ruby 3.3+ with YJIT is roughly comparable to Python (sometimes faster on CPU-bound work). Network-bound CrossTrade calls (~50ms round-trip) don't exercise the JIT much, so the language overhead is invisible. For high-throughput trading specifically, look at JRuby or TruffleRuby — both give you a serious JIT boost.

IRB / Pry for live exploration?

Yes — Ruby's REPL story is one of its strengths. Open IRB or Pry, require your CrossTrade wrapper, and you can interactively poke at accounts, positions, orders. Great for "I wonder what this endpoint returns" exploration before writing code against it.

RSpec mocking CrossTrade for tests?

Use WebMock to stub HTTP calls in unit tests. For integration tests, hit Sim101 against a dedicated test bearer token. CrossTrade's error responses are well-structured ({ "error": "..." }), so happy-path and error-path tests are straightforward.

JRuby or TruffleRuby?

Both work and both give serious performance gains for CPU-heavy backtest code. JRuby integrates with the JVM ecosystem (useful if your team also runs Java services). TruffleRuby (GraalVM) is faster on raw Ruby but with rougher gem compatibility.

Hanami or Roda instead of Rails?

Yes — both are excellent. The CrossTrade integration is identical: net/http or Faraday for REST, faye-websocket for streaming. Smaller frameworks make for leaner trading services if you don't need Rails' full stack.

Build your first NinjaTrader 8 trading bot in Ruby

One bearer token, one URL, every NinjaTrader endpoint. Ship to Sim101 first.