N Bars 突破策略是一种基于价格突破的量化交易策略。该策略的主要思路是,当收盘价突破过去N个交易日的最高价时开多仓,当收盘价跌破过去N个交易日的最低价时平多仓。该策略通过比较当前价格与过去N个交易日的最高最低价,捕捉强势突破行情,达到趋势跟踪的效果。
N Bars 突破策略是一个简单实用的量化交易策略,通过捕捉价格突破行情,实现了较好的趋势跟踪效果。该策略逻辑清晰,优化空间大,适用性广,是一个值得进一步研究和优化的量化策略。通过合理的参数优化和逻辑改进,可以进一步提高该策略的稳定性和盈利能力,更好地适应不同市场环境。
/*backtest start: 2023-04-06 00:00:00 end: 2024-04-11 00:00:00 period: 1d basePeriod: 1h exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=5 strategy("Breakout", overlay=true, precision=6, pyramiding=0, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=25.0, commission_value=0.05) n = input.int(5, "N Bars", minval=1) src_input = input.string("Close", "Source", ["Close", "High/Low"]) bull_src = switch src_input "Close" => close "High/Low" => high => runtime.error("Invalid source input") na bear_src = switch src_input "Close" => close "High/Low" => low => runtime.error("Invalid source input") na highest = ta.highest(bull_src[1], n) lowest = ta.lowest(bear_src[1], n) //----------------------------------------------------------------------------------------------------------------------------------------------------------------- // Plots //----------------------------------------------------------------------------------------------------------------------------------------------------------------- bool long = ta.crossover(bull_src, highest) bool short = ta.crossunder(bear_src, lowest) //Plots lowest_plot = plot(lowest, color=color.red, title="Lowest") highest_plot = plot(highest, color=color.green, title="Highest") bull_src_plot = plot(bull_src, color=color.blue, title="Bull") bear_src_plot = plot(bear_src, color=color.orange, title="Bear") // this message is an alert that can be sent to a webhook, which allows for simple automation if you have a server that listens to alerts and trades programmatically. enter_long_alert = '{"side": "Long", "order": "Enter", "price": ' + str.tostring(open) + ', "timestamp": ' + str.tostring(timenow) + '}' exit_long_alert = '{"side": "Long", "order": "Exit", "price": ' + str.tostring(open) + ', "timestamp": ' + str.tostring(timenow) + '}' if long strategy.entry(id="Long", direction=strategy.long, limit=open, alert_message=enter_long_alert) if short strategy.close(id="Long", comment="Close Long", alert_message=exit_long_alert)