Today we are impletmenting a very simple trading strategy based on trend: "buy the trend" they said. The strategy will first detects an increasing trend that generates a buy signal, then detects a decreasing trend where we sell and close our position(all in strategy). We will be using the following python libraries:
- Backtrader to backtest the strategy
- Matplotlib to visualize our work
- As usual numpy and pandas for data manipulation
Trend strategy for Ripple
According to the Cambridge dictionary a trend is :A general development or change in a situation or in the way that people are behaving"This definition is exactly what we need, we want to know:
- early on when people are buying, strongly enough to be sure it's a trend and that the price keeps increasing, after we bought.
- when they are selling so we can sell as soon as the price is decreasing. Early on to be sure we make a profit
The main hypothesis here is that the price reflects all available information aka the efficient-market hypothesis(EMH), we are just sheep following the trend. We don't try to predict anything... we're simply reacting to trend, which is a naive approach.
We download the data using crypto compare api, generates a key and download your data it's free. As usual the full code is available on Github. You can always use the data in the repo, but it's not necessarly the latest price.⚠️ We are all smart and grown people here, if you are planning to trade using real money based on this approach it is at your own risk so take full responsibility.
📈 Define a trend
We call a sequence of price a decreasing (increasing) trend if for k consecutives days the closing price is decreasing (increasing) from at least x %. In other words we need to define the best (k,x) that gives us the highest profit.
We will define our stategy on a simpler method based on central tendency parameters: average duration of a trend. In a future tutorial I will explain in more details how one could optimized this strategy to maximize profit with x and k as hyperparamaters.
Define buy
For 2,3,4 days the percent change is not enough considering the volatility of the crypto currency market. On top of that 2 days and 3 days trend how a too frequent we will be paying a lot of fees for small gain. Obviously the 8 days is not appealing. We are left with 5, 6 and 7 days trend. For simplicity let's use 7 days trend which correponds to the highest percentage change but also to a weekly cycle.
In conclusion we will generates a buy signal when :
- for the last 7 days the trend was decreasing at the closing price.
- the current price is above the minimal point of the sequence. We want to be sure people are starting to buy very shortly after we buy( remember it's a trend game), we do not want to be stuck in a sell panic 😅.
Define sell
We can see that after wainting more than 6 days the average profit is more than 60%, which is more than comfortable. But the counter part is that his events are less comomn: an increase during more or equal to 5 days don't happen that often.
- closing price have been up for 4 days.
- current price close below last 4 days price.
- our position is open ie our last trade was a buy
We want to maximise profit so ideally we could cashout every time we have 7 days of increasing price but this a rare event. Waiting for a 4 days pattern give us more selling opportunites which is not a small advantage.
💸 Backtest our strategy
We are backtesting our strategy by using Backtrader which is a very simple python package to backtest trading strategies.
Not much left today … Enjoy it!!! 🤓