This strategy adopts EMA crossover to track price trends. It goes long when the fast EMA crosses above the slow EMA, and closes position when the fast EMA crosses below the slow EMA. Mainly suitable for products with obvious trends, effectively following trends and gaining excess returns.
The core indicator of this strategy is EMA. The EMA formula is:
EMA(t)=C(t)×2/(n+1)+EMA(t-1)×(n-1)/(n+1)
Where t is the current tick, C(t) is the current closing price, and n is the N parameter value. EMA is a moving average technique with a weighted factor, assigning more weight to recent prices, thus reacting faster to the latest price changes.
The strategy constructs fast and slow EMAs and takes fast EMA crossing above slow EMA as the buy signal, and fast EMA crossing below slow EMA as the sell signal. The fast EMA crossing above indicates the start of a new round of rise, while fast EMA crossing below indicates the end of the upside trend and the start of a pullback.
The advantages of this strategy are:
The main risks are:
To reduce the above risks, the following optimizing measures can be adopted:
The strategy can be optimized from the following aspects:
In summary, this is a simple and practical trend following strategy utilizing EMA to judge price trends. The logic is clear and easy to implement. The advantages lie in the simplicity to adjust parameters and effectively follow trends. The disadvantages are prone to false signals and actual performance may underperform backtests. Next steps of optimization can focus on adding filters, dynamic parameters, model building to make the strategy more robust.
/*backtest start: 2022-12-20 00:00:00 end: 2023-12-26 00:00:00 period: 1d basePeriod: 1h exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=4 strategy("EMA交叉策略by GPT", format = format.inherit, overlay = true, default_qty_type= strategy.percent_of_equity, default_qty_value = 100, currency = currency.USD, initial_capital = 1000000) // 定義回測交易開始和結束時間的變數 start_time = input(title="開始時間", type=input.time, defval=timestamp("01 Jan 2020 00:00 +0000")) end_time = input(title="結束時間", type=input.time, defval=timestamp("31 Dec 2050 23:59 +0000")) // 判斷是否在回測交易時間範圍內 in_range = true // Define input variables fast_length = input(title="Fast EMA Length", type=input.integer, defval=5) slow_length = input(title="Slow EMA Length", type=input.integer, defval=20) // Define EMAs fast_ema = ema(close, fast_length) slow_ema = ema(close, slow_length) // Define buy and sell signals buy_signal = crossover(fast_ema, slow_ema) sell_signal = crossunder(fast_ema, slow_ema) // Buy signal if in_range and buy_signal strategy.entry("Buy", strategy.long, when=in_range) // Sell signal if in_range and sell_signal strategy.close("Buy", when=sell_signal)