The Strategy Development Kit
A quickstart guide to effortlessly design trading strategies
01
Basic Setup
Setup and create your very first basic trading strategy. Learn how to install the CryptoStruct Strategy SDK and get started writing your own strategies.
02
Placing Orders
Get to know the objects, functions and various callback methods that allow you and your strategy code to easily interact with the crypto exchanges.
03
Running the Strategy
Upload and monitor your first trading strategy and see it instantly in action. Configure and manage your strategies from the dedicated trading dashboard.
04
Strategy Backtesting
Find out how to backtest your strategies using pre-recorded data and our included backtesting tool. See what paths your strategy is taking and compare.
01 Basic Setup
Introduction
Hi! Welcome to the first part of this tutorial series on creating your own strategy using the CryptoStruct Strategy SDK.
In the course of this series you will learn how to program your own strategies, upload and configure them – and finally how to run them on real crypto exchanges. In the fourth video, you’ll also learn how to locally test-run any strategies you developed.
In this first tutorial we will start a new Java project and create a strategy class that will hold our entire trading logic.
Classes that want to use the Strategy SDK simply need to implement Strategy. Almost all included methods (that you’ll need to override in your code) describe some form of event that originates directly from the crypto exchange.
That means, you can write event-based code that reacts instantly to any new information, such as incoming public trades, or changes to your positions.
Other events include start, stop and error events that communicate status updates of your strategy, or the methods onHeartbeat() or onTimer() that get called every 500 ms or once a timer (set by you) runs out respectively.
Prerequisites
- The CryptoStruct Strategy SDK that you received from us
- Any Java IDE (we recommend IntelliJ)
- Apache Maven - a common build-management tool for Java
- A pre-configured pom.xml file containing information for Maven on how to include the Strategy SDK
Steps
- Unzip and install the Strategy SDK using the install script (available for Linux and Windows machines)
- Open your Java IDE with the provided pom.xml file
- Create a new Java file and call it e.g. 'ExampleStrategy.java'
- Let your class implement 'Strategy'
- Add the @Slf4j for easier logging
- Add the @ExportStrategy annotation and give your strategy a name (i.e. @ExportStrategy(name = "ExampleStrategy"))
- Each implemented method gets called upon receiving an event - let's add simple log messages to each method (use e.g. log.info("onStarted()"))
- Use instrument.getSnapshot().printBook(5, log::info) to log the current state of an instrument's order book (with a depth of 5)
- Use instrument.getLastTrades() in onInstrumentTrades() to get all newly recorded public trades
Download the Java file created in this video here.
Let’s move on to the next video where we’ll add the actual strategy logic to our code!
02 Placing Orders
Introduction
In the second tutorial we will pick up where we left off in the last video and finish writing the code for our first trading strategy.
Later, when we upload the strategy to the trading dashboard, we will also create a configuration file that includes basic information about the strategy (e.g. its name), but also values for all variables that we want to keep open and easy modifiable – so we don’t have to change and recompile the code all the time!
We call these variables parameters and they require the @Parameter annotation to work.
You will see how easy it is to write a simple, reactive strategy with only a few lines of code, while the SDK takes care of all the boilerplate code, leaving you with more time to perfect your strategy!
Prerequisites
- The basic code created in the previous lesson.
Steps
- Add an instance of TradingInstrument using the @Parameter annotation
- Add two instances of Order, with names bidOrder and askOrder
- In onInstrumentSnapshot() check whether an order is still null, and then place an order for that respective side (bid or ask) using tradingInstrument.placeOrder() and a fully instantiated OrderParams object that includes all needed configuration of our order
- Our orders should be limit orders, have a quantity of 100 and be placed exactly $15 away from the top of book
- You can get the current top of book using instrument.getSnapshot().getTopOfBook()
- Create a variable diffToToB of type Price to use instead of the hard-coded $15 and prepend it with @Parameter(optional = true)
- Add else-branches to the two checks to instead modify the two orders, if they are already placed and their prices have changed
- Simply use order.modifyPrice() to change an already placed order's price
Note: Remember to add the @Parameter annotation to diffToToB (not shown in video).
Download the Java file created in this video here.
Let’s move on to the next video, where we’ll upload and run our strategy!
03 Running the Strategy
Introduction
In the third tutorial we will take the finished java code of the previous two tutorials, upload it to the trading dashboard and run it.
We will also upload a configuration file (in json-format) that the server will use to execute your strategy.
The configuration file must contain at least the name of your strategy – the name you exported using @ExportStrategy(name = „…“), an instance name by which the strategy can be identified on the trading dashboard as well as values to all parameters that you have added to your code (using the @Parameter annotation).
By the way, any parameters with the annotation @Parameter(optional = true) can but need not be added to the configuration file.
Prerequisites
- The final code created in the previous lesson (Java) or already packaged as .jar-file
- An example configuration file (Note: don't forget to replace the values between < and > with your own information!)
Steps
- Place the final .jar file of your strategy into the (already existing) cryptostruct/strategies folder of your installation
- On your trading dashboard use the Settings menu Server Control to see the status of your servers and to restart the strategy server. This will register the freshly placed strategy file
- Go to BitMEX Testnet and create a free account
- Create an API key (menu Account & Preferences / API Keys), save the API key and API secret that you receive after creation
- On your dashboard, in the Exchanges menu choose "Testnet: BitMEX", click on Accounts and add a new account using the API key and secret that you received above
- Take the account_id automatically created here and paste it into the configuration file.
- Go to menu Instrument Explorer, choose again "Testnet: BitMEX" and select a random instrument. Take the instrument's id and also paste it into the configuration file
- In menu Strategies click on Strategy Instances and upload the final configuration file
- Click on the instance name of your strategy and press start 1 strategies
- You can now head over to the BitMex Testnet website and observe your strategy in action!
04 Strategy Backtesting
Introduction
In the fourth tutorial we will take our compiled code and run it locally against pre-compiled market data.
The backtesting tool quickly simulates days of trading data and lets you test the usefulness and risk involved with your strategy.
In this example we will use the data that you already received with your Strategy SDK package.
Prerequisites
- The code created in the previous tutorials, packaged as a .jar-file
Steps
- Duplicate and open the simplespreader-backtest.json file included in your SDK archive
- Modify it to fit our strategy:
- Delete the second instrument
- Delete the unused parameters
- Add our own diffToToB parameter
- Change the first instruments name to our instrument variable name (tradingInstrument)
- Change instanceName and strategyName to fit our strategy
- Change the strategyFile path to point to our code's .jar file
- In your SDK folder, run java -jar ./bin/backtest.jar -el output.json ./[path to your copied configuration file].json > output.log
- After a short time, this will have created both an output.json and an output.txt file in your current directory
- In the output.json file you can see all orders and modifications generated by your code
- In the output.log file you can see the actual logging output generated by your strategy's log.info() lines
Download the backtest configuration file created in this tutorial here!