How AI Helps Us Write Connectors for Crypto Exchanges

How AI Helps Us Write Connectors for Crypto Exchanges (Pinned)
Atom
9/5/2024
StockSharp


Or how AI can save you hours of work, but not from the necessity of verifying every step.

claudeai-vs-chatgpt.jpg


September 2024. We, the StockSharp team, are actively using AI to write connectors for crypto exchanges. But let me warn you — if you are reading this in 2025 or later, all of this might already be outdated. If you’re from the future, welcome to the past! And don’t forget to check if our methods are still relevant.

Our journey with AI started with ChatGPT 3.5, which, frankly, could not write even a simple trading strategy, let alone a crypto exchange connector. However, with the arrival of ChatGPT 4.0 and Claude Sonnet 3.5, things drastically changed. Now, AI can write complex code modules, though with caveats: you still have to intervene, clarify, and fix bugs, which has become a normal part of our process.

-----

Step 1. Starting a project in Claude.ai



Before we start writing a new connector, the first thing we do is create a project in Claude.ai. This is not just a chat that will forget everything once you close it. The project allows you to save everything you upload: code, documents, comments. It’s like Custom GPT, where AI "learns" from your examples and instructions, not just answering questions.

project.png

Project creation interface in Claude.ai. Here, all data and examples are saved, allowing you to maintain the context of the work.

Claude.ai is like your personal developer, who understands some things, but without your supervision, can write something that might make your hair stand on end. So keep your documentation and vigilance close.

-----

Step 2. Copying existing code



To avoid reinventing the wheel every time, we base our work on an already existing connector, for example, for Coinbase. We copy the project structure and adapt all the key classes and methods for the new exchange. This is just the beginning — the real fun starts now.

-----

Step 3. Adapting the WebSocket client



Now it’s time to set up the WebSocket client for the new exchange. For example, let's take GateIO Spot WebSocket v4. Claude.ai helps generate code, but this is where our ongoing battle with inaccuracies begins. The AI often generates code that theoretically seems correct, but in practice, needs to be supplemented and fixed.

1.png

Original WebSocket client code for one of the exchanges, generated by Claude.ai. The AI suggests expanding the data types and events.

When it comes to authentication, AI often makes mistakes, and we have to manually add the missing elements. Claude can forget even basic things like authorization before subscribing to WebSocket channels.

2.png

Claude.ai made a mistake in implementing WebSocket authentication. We corrected the code and added proper authentication before connecting.

We also rewrite the code to make it more flexible by passing the WebSocket address as a parameter. This allows us to easily adapt to various exchanges and their APIs.

3.png

Example code where the WebSocket address is passed as a parameter, making the architecture more flexible.

-----

Step 4. Setting up the REST client and adapter



After setting up the WebSocket client, we move on to the REST client. Claude.ai generates basic requests, but we have to manually clarify whether they are correct. Each exchange's API differs, and you need to be careful not to miss important details in the documentation.

Once the client is ready, we move on to the adapter for StockSharp, where the main magic happens — data processing and bringing them to a common format. Here, AI helps generate classes, but every step must be double-checked. This is especially important when working with JSON data transmitted via the exchange’s API.

5.png

Generated domain model classes. We manually supplemented them with attributes and corrected the data types.

Claude can offer interesting solutions, but you need to add attributes like JsonProperty to correctly process exchange data. If you don't, be prepared for surprises like incorrect data in reports.

6.png

We added JsonProperty attributes for proper work with JSON data.

Processing trade data is another important step where AI doesn’t always manage on its own. For example, trade data and their processing can lead to errors.

7.png

Claude fixes code for processing trade data transmitted via WebSocket and REST API.

-----

Step 5. Optimization and new methods



Claude.ai does a decent job of writing basic methods like working with candlesticks and trade data. However, its suggestions require optimization. We often refactor logic into separate classes for better structure.

8.png

Optimized methods for working with candlesticks and trade data via WebSocket, refactored into separate classes.

We also have to refine methods for working with trade operations via WebSocket. Even though AI helps automate such processes, the code still requires final polishing.

9.png

Claude.ai added methods for trade operations in SocketClient, but they had to be optimized and corrected for errors.

-----

Step 6. Testing — the code doesn’t always work on the first try



When the code is ready, it's time to test it. This is where AI comes to the rescue again, but all of its suggestions need to be thoroughly checked because things often don’t work correctly the first time. For example, when testing the exchange’s API, we encounter errors in instrument and position requests.

10.png

Claude.ai suggested code for working with REST API Gate.io, but it needed to be tested and refined.

-----

Step 7. Data conversion for spot and derivatives



Claude.ai helps generate classes for working with spot and derivatives, but again — be prepared to manually adjust methods and data types. For example, working with derivatives on Gate.io requires additional checks and adjustments.

11.png

Claude.ai suggested the Extensions class to support data conversion between spot and derivatives on Gate.io.

We also adapt SpotAdapter to correctly process exchange data using the conversion methods suggested by Claude.

12.png

SpotAdapter was rewritten with changes in HttpClient and SocketClient, using methods from Extensions.

-----

Step 8. Futures adapter and error corrections



Claude.ai helps write methods for working with futures, but as before, careful checking of logic and fixing errors is required. For example, when building the order book data, we encountered an issue with exception handling.

13.png

Generated HttpClient for working with futures via REST API Gate.io.

Claude also sometimes makes mistakes with data handling in the futures adapter. We have to manually adjust the methods for correct order book processing.

14.png

Fixed futures adapter with correct order book processing logic.

As a result, after corrections, AI proposed an optimized method for working with order books and data recovery.

15.png

Optimized methods for data recovery and order book processing based on Claude.ai’s suggestions.

-----

GitHub Copilot — overhyped toy or real helper?



GitHub Copilot, which Microsoft and GitHub actively promote as a tool of the future, is actually more of an interesting toy than a full-fledged developer assistant at its current stage. Copilot helps write needed sections of code, but expecting groundbreaking results from it is premature — its strength lies in refining and suggesting code improvements. However, its key advantage is its tight integration with the development environment, making it convenient for quick fixes and completing template code, which it handles much better than writing complex logic modules.

copilot.gif


-----

AI: an enhancer for professionals and a barrier for beginners



If you’re a beginner developer just diving into the world of programming, you’re, to put it mildly, out of luck. Currently, AI won’t boost your efficiency in any significant way. Moreover, trusting AI with complex tasks can get you even more tangled in code and problems it generates. It’s a different story for experienced programmers.

For professionals, AI becomes a powerful tool that amplifies their capabilities: it speeds up development, offers solutions, and enriches knowledge of various technologies and libraries. In such a tandem, AI helps you focus on key project aspects, leaving the routine to it.

However, as we have already mentioned, AI hits the weakest spot - beginner developers, making the gap between them and professionals even wider. Experienced programmers can quickly identify AI errors, correct them, and continue working, while a novice will simply drown in these problems. Ironically, at a time when AI was conceived as a help for everyone, it currently only increases this gap.

But we should hope that in the future, AI will become more independent in programming, and then this gap may not only stop growing but disappear altogether, leveling the playing field for everyone, regardless of their level of expertise.

Tags:


Thanks:




Attach files by dragging & dropping, , or pasting from the clipboard.

loading
clippy