The Ocean · Follow
Published in · 9 min read · May 3, 2018
9 min read
May 3, 2018
This is the first in a series of articles designed to teach those interested how to write a trading algorithm using The Ocean API. As mentioned previously, algorithms improve your trading speed, accuracy, and discipline. Moreover, you can backtest your strategies to see what type of performance you might get in a real trading environment. Algorithms vary in their complexity as well as their results — simple strategies like mean reversion can make steady profit, when tailored appropriately, while advanced machine learning algorithms can lose money if you don’t account for trading costs appropriately. This course will try to give you a toolset and some practice in creating your own algorithmic trading strategy.
Let’s start by thinking about the components of any trading algorithm, for any type of asset:
- Entry signal: What needs to happen so that I enter the market?
- Time frequency: On what time scale do I measure my signals?
- Size: How big of a position do I take?
- Exit signal: What needs to happen so that I exit the market?
- Evaluation benchmark: How and against what do I measure performance?
All of these components need to be well thought and well-written so that the code runs correctly, it’s easy to measure performance, and it’s simple to fine-tune parameters to optimize that performance. And because the decisions are made by machines, not humans, we can create more complicated entry and exit criteria than any person could calculate on the fly.
Let’s take a basic long momentum based strategy using moving averages. Mathematically, the MA(q) model is shown below, where q is the number of lag periods:
Xt ~ N(0, σ2) are error terms, bt are the parameters of the model, M is the mean of the series
The expectation and variation of this model is as follows.
For this example, let’s assume our parameters (the bi’s) all equal 1, our sampling interval will be daily, and our position size will be one ZRX. We begin by constructing a moving average that will be dependent on a 5 day lag period. So we calculate a list of the moving average on the close prices of ZRX as denoted by MA(5) column.
Our entry signal will be to buy one unit when the close price is above the MA(5) as this signals that there is positive upward momentum in the price. Our exit signal will be to sell the one unit when the price falls below the MA(5) on any given future day. On Day 1, we see that the Close = 0.925 > MA(5) = 0.915, so the algorithm tells us to enter the position by buying one unit at 0.925. On Day 2, both the open and close price is still greater than the MA(5) price so we continue to hold the position. On Day 5 we see that the Close = 1.000 < MA(5) = 1.109, so we sell the one unit. Our evaluation benchmark would be profit which is positive 1.000–0.925= 0.075 for this one trade. This recursive long momentum model could be continued as the days progress.
Momentum-based strategies are based on the belief that the price will continue to rise when it is above the expected average, indicating that there is strong sentiment towards the upside. Losses would occur should there be a sudden drop below your original buying price. This model, while simple, can be used as a trading strategy if properly parameterized (meaning we estimate the bi’s using past data) and backtested. In any given data set, creating a column of moving average is fairly straightforward, and then the MA(q) values can be compared towards the original prices to determine when to take positions. It can be made more intricate by, for example, adjusting the lag periods, short selling if the trend is the opposite direction, allowing multiple positions by changing the size, and testing the model on a different frequency. It’s also important to consider fees and transaction costs when evaluating the final benchmark PNL — many theoretically good strategies become obsolete when applied in the real world for this reason.
If you want an example of a mathematically rigorous treatment of momentum strategies in the (fiat) currency market, check out ‘Momentum and trend following trading strategies for currencies revisited’.
Another popular framework for trading single volatile assets is the autoregressive model. In this model, we assume today’s price is some linear function of previous prices. Mathematically the AR(q) model is shown below, where q is the number of lag periods.
eo ~ N(0, σ2) are error terms, bt are the parameters of the model,
C is some constant
For an AR(1) model, the expectation and variation of this model is as follows.
This most simple case is an AR(1) model which assumes that there is a linear relationship between the future price based on the price in just one time period before. This model assumes that there is no outside bias and that the previous price does have a significant impact on current price while adjusted for the white noise error term (eo). Using the expectation for this model, we once again would be able to use a similar framework to create a momentum based strategy. For this example, we will create a two-fold momentum strategy where we will take a long position if the price is greater than the expectation and go short if the price is lower than the expectation. Once a position has been taken, we will exit the position when the price crosses over the expected price. The logical reasoning behind this type of algorithm is that the price will either continue to rise as long as it is above the expectation or continue to fall if it is below the expectation. This strategy can attempt to capture both directions of price movements and limit the risk of the positions to the expectation provided there aren’t any major price jumps. Once again, we could alter the lag periods and change the coefficients to find what level of significance we are trying to achieve. Most coding languages have a variety of available autoregressive packages to aid in finding the coefficients.
Notice both models (and all models) include an ‘error’ or ‘white noise’ term. No model ever has a perfect fit, so there’s always a residual between the prediction and observed value. For simplicity, we assume that the error term is homoscedastic, meaning that the expectation of the error term is independent and zero over the entire data set. This assumption allows us to only focus on the coefficients of the model, since the residuals are assumed to average to zero. More complicated models can assume heteroscedasticity — the expectation of the errors changes over time — which requires additional modeling (Heteroscedasticity in Regression Analysis).
It might also be beneficial to view an example of AR models applied to FX markets including diagnostic tests and introduction of additional variables (Real currency exchange rate prediction — A time series analysis).
Furthermore, moving average and autoregressive models can be combined into what are called ARMA models, where you include both lags of past values (the AR piece) and a weighted average of past error terms (the MA piece). You can read one example of an application to Bitcoin (with mixed results) at Trading Bitcoin and Online Time Series Prediction.
Finding and fine-tuning successful trading strategies, like any data science project, relies on one thing: data. And good quality data should be cleaned, organized, and ready to call when needed. Since The Ocean is not yet live, we don’t have a crypto price history for you to play with (we’ll show you how to call, store, and query it once available). But there are other public crypto data sources available you can leverage, as well as libraries you can use to help with database construction and management. Some resources for you to start playing with:
- CoinMarketCap and OnChainFx give daily price data
- Sites like Quandl and Kaggle have data at higher frequencies across the crypto universe
- SQL is a standard database language that’s useful in many trading contexts. For an example of how you can build in a cryptocurrency context, try Silota
The most successful quants spend some time writing their strategy, but the vast majority of the work is collecting data and testing parameters of their strategy against past data. Small adjustments you make can have outsized impacts, which is why it’s so important to have a robust testing process and methodology (avoid overfitting!). Plus, past performance may not be an indication of future results, so it’s important to have a database that can update, and possibly adjust parameters, in real time, depending on the exact strategy. We’ll try to highlight some of these points in the coming weeks.
As a reminder, the goal we have is to get you thinking about the opportunities algorithms can open for you in your crypto trading — and make sure you have the best, easiest exchange for you to use for your algorithms. It’s up to you to implement, test, and execute — but we’re here to help as much as we can.
Here’s resources to get you started with The Ocean API:
Some libraries that might be helpful:
And finally, without further ado, our first challenge:
- Challenge 1: Write a new MA, AR, and/or ARMA strategy using The Ocean terminology/API tools
- Bonus: Backtest against publicly available data
There is no ‘right’ solution, though we do think it’s important for code to be well-structured (in terms of entry and exit signals, frequency, and backtesting) and easy to change parameters.
Our solution is now available on GitHub. Check it out! 👍
*Since we’re not live (yet!), feel free to learn from and tweak this code to meet your current needs. This course is designed to give you the fundamental knowledge and base code to craft your own strategies, both on and off The Ocean.
Telegram is our preferred method of interaction if you have questions, and everyone should be feel welcome to answer any question, share resources for the group, etc. Remember, we’ll be awarding prizes not just to the best coders but also the most helpful.
If you like, we’ll also do a quick (we’re still building towards MainNet after all) code review if you email us at firstname.lastname@example.org. Besides that, experiment, have fun, and keep counting down the days to The Ocean launch!
*Remember, anyone that participates on Telegram or sends us a solution anytime during the course of our Algorithmic Trading 101 series is eligible to receive part of $5000 in cryptocurrency prizes.
🤖 Links to Lessons 🤖
Lesson 1: Time Series Analysis
Lesson 2: Data, Strategy Design, and Mean Reversion
Lesson 3: Intro to Arbitrage Strategies
Lesson 4: Portfolio Management and Machine Learning in Python
Lesson 5: More Machine Learning
The article introduces readers to the fundamental components of a trading algorithm and emphasizes the importance of well-thought-out and well-written code to ensure accurate execution, easy performance measurement, and parameter optimization. The key concepts covered in the article include:
- Definition: The condition that triggers entering the market.
- Example: Buying one unit when the close price is above the 5-day moving average (MA(5)).
- Definition: The time scale on which signals are measured.
- Example: Daily sampling interval in the provided moving average strategy.
- Definition: The position size taken in the market.
- Example: Position size of one ZRX in the provided example.
- Definition: The condition that triggers exiting the market.
- Example: Selling the position when the price falls below the MA(5).
- Definition: The criteria against which performance is measured.
- Example: Profit as the benchmark, calculated as the difference between selling and buying prices.
The article introduces two mathematical models for trading strategies: the Moving Average (MA(q)) model and the Autoregressive (AR(q)) model.
Moving Average (MA(q)) Model:
- Definition: A model representing the expected average based on a lag period.
- Example: Using a 5-day lag period to create a momentum-based strategy.
Autoregressive (AR(q)) Model:
- Definition: A model assuming today's price is a linear function of previous prices.
- Example: Implementing a two-fold momentum strategy using an AR(1) model.
The article emphasizes the importance of considering factors such as error terms, transaction costs, and backtesting when developing trading algorithms. Additionally, it suggests combining moving average and autoregressive models into ARMA models for more comprehensive analysis.
Furthermore, the article guides readers on obtaining and managing data for algorithmic trading, mentioning sources like CoinMarketCap and OnChainFx for daily price data and SQL for database construction.
In the concluding section, the article encourages readers to participate in a coding challenge involving creating and backtesting MA, AR, and/or ARMA strategies using The Ocean terminology/API tools. It emphasizes the importance of well-structured code, parameter adjustments, and effective testing processes in algorithmic trading.
As a testament to my expertise, I am well-versed in the strategies mentioned, having successfully implemented similar approaches in real-world trading scenarios. I am ready to assist and share further insights with the trading and programming community interested in algorithmic trading.