Introduction
After building the the Java web socket client for Kraken’s WebSocket API, the immediate thought was wouldn’t it be good if this market data could generate some basic trading signals. Obviously this wouldn’t happen in Java so it’d need a offer a WebSocket server for this. Ultimately, I got a bit carried away and decided to build a full hobby-grade trading platform that can handle real-time data and execute trades based on live market conditions. The architecture is shown the below diagram, the post just covers the need for a WebSocket server and position management.
The code for all this is on GitHub.
Real-Time WebSocket Server
Originally, the intention was to run analytics within the Java application itself, but the reality this was all happening in Python. Thus, the decision was made to implement a WebSocket server that allows external clients to connect and interact with the trading application. This server acts as a bridge between the trading application and external clients (such as Python scripts or web dashboards). Its main responsibilities are:
- Forwarding Market Data: As the application receives real-time market data (ticker, order book, trades, etc.) from Kraken, it immediately forwards this data to all connected WebSocket clients. This ensures that clients always have the latest information for analysis or visualisation. Currently, the server only forwards OHLC (candlestick) data and ticker data, but it can be extended to include other data types like trades or order book updates.
- Accepting Trading Signals: Clients can also send trading signals (e.g., buy/sell instructions) back to the application via the WebSocket connection. This two-way communication enables automated or manual trading strategies to be executed in real time.
This architecture allows for flexible integration with various client applications, making it easy to build custom trading bots or dashboards on top of the core platform.
Position Manager
Receiving trading signals is only half the battle. The position manager is responsible for ensuring that only valid and safe trades are executed. Its key features include:
- Signal Filtering: The position manager evaluates incoming trading signals against predefined risk limits (such as maximum position size, exposure, or frequency). For now, this is just position count, later position size, and global position size. Signals that would violate these limits are filtered out, protecting the account from excessive risk.
- Position Exit Management: Beyond just entering trades, the position manager also monitors open positions and applies exit logic. This could include stop-loss, take-profit, or other custom exit strategies, ensuring that positions are closed according to the user’s risk management rules. With this design, clients can be many and also transient since position exit is handled centrally by the position manager. This means clients can connect, send signals, and disconnect without worrying about managing positions themselves.
By combining real-time data delivery with intelligent trade management, the application provides a robust foundation for building automated trading systems that is both responsive and safe.
Summary
It really is not hard to build a simple trading platform that can handle real-time data and execute trades based on live market conditions. An obvious next step is connect to more venues, add more data sources, and more sophisticated position management.