
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)