The goal of the strategy is: sell the asset when the trading price of the asset exceeds three standard deviations above the rolling average; buy the asset when the trading price is more than three standard deviations below the rolling average. Introduction The goal of the strategy is: sell the asset when the trading price of the asset exceeds three standard deviations above the rolling average; buy the asset when the trading price is more than three standard deviations below the rolling average.To make it work properly, pay attention to the following points: A rolling average is needed Need to calculate the standard deviation from the rolling average Need to calculate the upper and lower bounds Using the code Since our strategy class applies calculations in a given sequence, we can easily connect these calculations in logical order and create signals based on these calculations. Let's start by defining the basic backtesting parameters. C++ // // symbol = "HE" start_date = "2022-01-01" end_date = "2022-12-31" C++ Now, we need to fetch the data and link the operations together:data = DataHandler(symbol=symbol, start_date=start_date, end_date=end_date).load_data() strategy = Strategy( indicators={ "sma_50": lambda row: row["close"].rolling(window=50).mean(), "std_3": lambda row: row["close"].rolling(window=50).std() * 3, "std_3_upper": lambda row: row["sma_50"] + row["std_3"], "std_3_lower": lambda row: row["sma_50"] - row["std_3"], }, signal_logic=lambda row: ( 1 if row["close"] < row["std_3_lower"] else -1 if row["close"] > row["std_3_upper"] else 0 ), ) data = strategy.generate_signals(data) backtester = Backtester() backtester.backtest(data) backtester.calculate_performance() Final Portfolio Value: $10,725.54 Total Return: 7.26% Annualized Return: 7.29% Annualized Volatility: 18.32% Sharpe Ratio: 0.40 Sortino Ratio: 0.53 Maximum Drawdown: -23.37% Support by Alltick
The strategy involves selling an asset when its trading price is more than three standard deviations above the rolling average, and buying when the price is more than three standard deviations below the rolling average. To implement this, you need to calculate a rolling average and standard deviation, then determine the upper and lower bounds based on these values. In the code example, you first define the parameters and fetch historical data for the asset using the DataHandler class. Next, you set up the strategy with indicators to calculate the 50-day rolling average, the standard deviation, and the upper and lower bounds. The signal logic generates buy or sell signals based on whether the trading price is outside these bounds. After generating signals, you backtest the strategy using the Backtester class, which evaluates its performance. In this example, the final portfolio value is $10,725.54 with a total return of 7.26%. The annualized return is 7.29%, and the annualized volatility is 18.32%. The Sharpe Ratio is 0.40, the Sortino Ratio is 0.53, and the maximum drawdown is -23.37%.