Strategy 3: Diversified Crypto Portfolio Investing with Stochastics & Bollinger Bands
This strategy is similar to Strategy 2, however strategy 2 is only made to handle 1 Crypto asset. This strategy will create and trade a portfolio of Crypto assets based on parameters that we set.
The Algorithim:
Initialization: In this section, you're setting up the parameters for the algorithm.
self.SetStartDate(2016, 6, 18)
and self.SetEndDate(2023, 7, 23)
set the time frame for the backtest from mid-June 2016 to mid-June 2023.
self.SetCash(self._cash)
sets the initial cash amount to $100,000.
AddUniverse piece of code allows us to add all the Cryptocurrencies from Binance that will meet the parameters that we set later on.
In the code below, we have defined the universe to only consist of Cryptocurrencies that have more than $60m of daily trading volume. This ensures that the portfolio only consists of high quality coins.
Setting the technical Indicators:
This sets up both our Stochastics and Bollinger Bands strategy.
Risk Management: This section adds risk management to the algorithm.
TrailingStopRiskManagementModel(0.03)
will sell the asset if its price drops by 3%. This is effectively a stop-loss.
OnData: This is the core logic of the algorithm. It runs every time new data comes in (in this case, daily).
self.Bolband[symbol] = self.BB(symbol, 10, 0.45, MovingAverageType.Simple, Resolution.Daily)
sets up a Bollinger Bands indicator with a period of 10 days and a standard deviation of 0.45.
self.sto[symbol] = self.STO(symbol, 8, 5, 5, Resolution.Daily)
sets up a Stochastic Oscillator with a period of 8 days, smoothing period K of 5, and smoothing period D of 5.
The code follows this logic:
1) The Algo will buy and hold crypto unless the conditions for a short position are met. Therefore this a bullish Investing strategy more suited for those that want to hold their crypto assets for the long-term and do not mind short-term price fluctuations. The algo Invests equally in all of the assets that meet the $60m trading volume criteria and rebalances daily.
2) If the price of the crypto asset is above the upper Bollinger band & the Stochastics value is greater than 80 then it will trigger a short position using 100% of the portfolio.The algo Invests equally in all of the assets that meet the $60m trading volume criteria and rebalances daily.
If the unrealized profit from the current holdings is above 10%, the algorithm will take profit from the trade and close it (self.Liquidate(self.ticker)
).
Plotting: The algorithm plots three charts: "Strategy Equity", "Bollinger", and "Stochastic".
The "Strategy Equity" chart shows the growth of the portfolio value.
The "Bollinger" chart shows the three Bollinger Bands (upper, middle, lower) and the closing price .
The "Stochastic" chart shows the three lines of the Stochastic Oscillator: fast stochastic, stochastic %K, and stochastic %D.
Backtesting the performance of the Algorithm:
Here is the portfolio of crypto assets that the algorithim selected based of the criteria we set earlier:
Please refer to the Algorithmic Trading Strategies main page to see how to start using this algorithm.
Last updated