The strategy named “Variance and Moving Averages Based Volatility Strategy” uses the variance of the price volatility over the past 30 candles and three moving averages (MA5, MA15, and MA30) to make trading decisions.
The main idea of the strategy is to measure market volatility by calculating the variance of price volatility and combine it with moving averages of different periods to determine the trend direction. When volatility is low and the short-term moving average is above the long-term moving average, the strategy enters a long position. At the same time, the strategy sets stop-loss and take-profit conditions to control risk and lock in profits.
The principle of the strategy can be divided into the following steps:
The advantages of this strategy include:
The risks of the strategy mainly include:
To optimize this strategy, the following directions can be considered:
In summary, the “Variance and Moving Averages Based Volatility Strategy” is a trading strategy that combines volatility and trend indicators. It measures market volatility by calculating the variance of price volatility and combines it with moving averages of different periods to determine the trend direction, entering trades in appropriate market conditions. The strategy sets clear stop-loss and take-profit conditions, which can effectively control risk and lock in profits. At the same time, the strategy has room for optimization and can improve its adaptability and robustness through parameter optimization, introducing more indicators, and implementing risk management mechanisms.
/*backtest start: 2024-02-01 00:00:00 end: 2024-02-29 23:59:59 period: 1h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=5 strategy("Variance and Moving Averages Strategy", overlay=true) // 计算MA5、MA15和MA30 ma5 = ta.sma(close, 5) ma15 = ta.sma(close, 15) ma30 = ta.sma(close, 30) // 计算过去30根K线的波动幅度(最高价和最低价)的方差 variance = ta.variance((high - low) / close, 30) * 1000000 // 定义买入条件 buy_condition = variance < 35 and ma5 > ma15 and ma15 > ma30 // 定义止损条件 close < ma30 or ma5 < ma30 stop_loss_condition = true // 定义止盈条件 take_profit_condition = variance > 500 // 执行交易逻辑 if (buy_condition) strategy.entry("Long", strategy.long) if (stop_loss_condition) strategy.close("Long") if (take_profit_condition) strategy.close("Long") // 绘制MA5、MA15和MA30 // plot(ma5, color=color.blue, title="MA5") // plot(ma15, color=color.orange, title="MA15") // plot(ma30, color=color.red, title="MA30") // 绘制方差 hline(0.0004, color=color.green, linestyle=hline.style_dashed, title="Variance < 0.0004") hline(0.0005, color=color.red, linestyle=hline.style_dashed, title="Variance > 0.0005") plot(variance, color=color.white, title="Variance")