This strategy generates trading signals based on the breakthrough and callback of multiple moving average lines. It goes long when the price breaks through the upward moving average line and goes short when the price falls below the downward moving average line.
The code uses 4 moving average lines with different periods - 21-day, 50-day, 100-day and 200-day. It enters long positions when the price breaks through these MA lines and enters short positions when the price falls below these MA lines. In addition, stop loss and take profit levels are set in the strategy. Specifically, the stop loss is set near the lowest point of the previous candle, and the take profit is set at 3 times the distance between the lowest point and the highest point of the previous candle.
The core idea of this strategy is to judge the trend using moving averages. When the price breaks through the upward MA lines, it indicates an upward trend so should go long. When the price falls below the downward MA lines, it indicates a downward trend so should go short. Using multiple MA lines with different periods can judge the trend more accurately and also verify trading signals through consistency of the trend.
The main advantages of this strategy are:
The main risks of this strategy are:
These risks can be reduced by adjusting MA parameters and optimizing stop loss and take profit.
This strategy can be optimized in the following aspects:
In general, this is a typical trend following strategy. The advantages are clear logic and easy to understand and implement. The disadvantage is prone to false signals. The strategy can be improved by parameter tuning and adding other indicators. It is suitable for medium-to-long term holding and can also be used as a component of short-term trading strategies.
/*backtest start: 2022-11-15 00:00:00 end: 2023-11-21 00:00:00 period: 1d basePeriod: 1h exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=5 strategy("DolarBasar by AlperDursun", shorttitle="DOLARBASAR", overlay=true) // Input for Moving Averages ma21 = ta.sma(close, 21) ma50 = ta.sma(close, 50) ma100 = ta.sma(close, 100) ma200 = ta.sma(close, 200) // Calculate the lowest point of the previous candle for stop loss lowestLow = ta.lowest(low, 2) // Calculate the highest point of the previous candle for stop loss highestHigh = ta.highest(high, 2) // Calculate take profit levels takeProfitLong = lowestLow - 3 * (lowestLow - highestHigh) takeProfitShort = highestHigh + 3 * (lowestLow - highestHigh) // Entry Conditions longCondition = ta.crossover(close, ma21) or ta.crossover(close, ma50) or ta.crossover(close, ma100) or ta.crossover(close, ma200) shortCondition = ta.crossunder(close, ma21) or ta.crossunder(close, ma50) or ta.crossunder(close, ma100) or ta.crossunder(close, ma200) // Stop Loss Levels stopLossLong = lowestLow * 0.995 stopLossShort = highestHigh * 1.005 // Exit Conditions longExitCondition = low < stopLossLong or high > takeProfitLong shortExitCondition = high > stopLossShort or low < takeProfitShort if (longCondition) strategy.entry("Long", strategy.long) if (shortCondition) strategy.entry("Short", strategy.short) if (longExitCondition) strategy.exit("Long Exit", from_entry="Long", stop=stopLossLong, limit=takeProfitLong) if (shortExitCondition) strategy.exit("Short Exit", from_entry="Short", stop=stopLossShort, limit=takeProfitShort)