
이 전략은 다중 시간 프레임 동적 회귀 메커니즘을 사용하여 서로 다른 시간 주기에서 최고 가격과 최저 가격을 비교하여 가격 추세를 판단하여 낮은 위험 스캐닝을 달성합니다.
이 전략은 사용자 정의 함수 f_get_htfHighLow를 호출하여 다양한 시간 주기에서 최고값 nhigh과 최저값 nlow을 얻는다. 특히, 사용자 설정된 시간 주기 resolution, 시간 주기 곱하기 HTFMultiplier, 회귀 파라미트 lookahead과 gaps, 그리고 오차량 오프셋에 따라, security 함수를 호출하여 다양한 시간 주기에서 최고값과 최저값을 얻는다.
예를 들어, 오프셋이 0일 때, 현재 K 선의 최고 가격과 최저 가격을 얻는다. 오프셋이 1일 때, 상위 K 선의 최고 가격과 최저 가격을 얻는다. 두 K 선 사이의 가격 변화를 비교하여 트렌드 방향을 판단한다.
최고가 상승하고 최저가 상승하면 낙점 트렌드라고 판단하고, 최고가 하락하고 최저가 하락하면 하락 트렌드라고 판단한다. 트렌드 방향에 따라 롱링 또는 쇼팅을 하고, 중도 거래를 한다.
해결책:
이 전략은 전체적인 아이디어가 명확하고, 다중 시간 프레임의 동적 역으로 주가 추세를 판단하여 인위적 판단 오류를 최대한 줄여주는 전형적인 프로그램화 거래 전략이다. 매개 변수 최적화 및 기능 확장으로 전략의 안정성과 수익 공간을 더욱 강화할 수 있으며, 깊이 있는 연구와 추적할 가치가 있다.
/*backtest
start: 2022-11-14 00:00:00
end: 2023-11-20 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HeWhoMustNotBeNamed
//@version=4
strategy("HTF High/Low Repaint Strategy", overlay=true, initial_capital = 20000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01)
i_startTime = input(defval = timestamp("01 Jan 2010 00:00 +0000"), title = "Start Time", type = input.time)
i_endTime = input(defval = timestamp("01 Jan 2099 00:00 +0000"), title = "End Time", type = input.time)
inDateRange = true
resolution = input("3M", type=input.resolution)
HTFMultiplier = input(22, minval=1, step=1)
offset = input(0, minval=0, step=1)
lookahead = input(true)
gaps = false
f_secureSecurity_on_on(_symbol, _res, _src, _offset) => security(_symbol, _res, _src[_offset], lookahead = barmerge.lookahead_on, gaps=barmerge.gaps_on)
f_secureSecurity_on_off(_symbol, _res, _src, _offset) => security(_symbol, _res, _src[_offset], lookahead = barmerge.lookahead_on, gaps=barmerge.gaps_off)
f_secureSecurity_off_on(_symbol, _res, _src, _offset) => security(_symbol, _res, _src[_offset], lookahead = barmerge.lookahead_off, gaps=barmerge.gaps_on)
f_secureSecurity_off_off(_symbol, _res, _src, _offset) => security(_symbol, _res, _src[_offset], lookahead = barmerge.lookahead_off, gaps=barmerge.gaps_off)
f_multiple_resolution(HTFMultiplier) =>
target_Res_In_Min = timeframe.multiplier * HTFMultiplier * (
timeframe.isseconds ? 1. / 60. :
timeframe.isminutes ? 1. :
timeframe.isdaily ? 1440. :
timeframe.isweekly ? 7. * 24. * 60. :
timeframe.ismonthly ? 30.417 * 24. * 60. : na)
target_Res_In_Min <= 0.0417 ? "1S" :
target_Res_In_Min <= 0.167 ? "5S" :
target_Res_In_Min <= 0.376 ? "15S" :
target_Res_In_Min <= 0.751 ? "30S" :
target_Res_In_Min <= 1440 ? tostring(round(target_Res_In_Min)) :
tostring(round(min(target_Res_In_Min / 1440, 365))) + "D"
f_get_htfHighLow(resolution, HTFMultiplier, lookahead, gaps, offset)=>
derivedResolution = resolution == ""?f_multiple_resolution(HTFMultiplier):resolution
nhigh_on_on = f_secureSecurity_on_on(syminfo.tickerid, derivedResolution, high, offset)
nlow_on_on = f_secureSecurity_on_on(syminfo.tickerid, derivedResolution, low, offset)
nhigh_on_off = f_secureSecurity_on_off(syminfo.tickerid, derivedResolution, high, offset)
nlow_on_off = f_secureSecurity_on_off(syminfo.tickerid, derivedResolution, low, offset)
nhigh_off_on = f_secureSecurity_off_on(syminfo.tickerid, derivedResolution, high, offset)
nlow_off_on = f_secureSecurity_off_on(syminfo.tickerid, derivedResolution, low, offset)
nhigh_off_off = f_secureSecurity_off_off(syminfo.tickerid, derivedResolution, high, offset)
nlow_off_off = f_secureSecurity_off_off(syminfo.tickerid, derivedResolution, low, offset)
nhigh = lookahead and gaps ? nhigh_on_on :
lookahead and not gaps ? nhigh_on_off :
not lookahead and gaps ? nhigh_off_on :
not lookahead and not gaps ? nhigh_off_off : na
nlow = lookahead and gaps ? nlow_on_on :
lookahead and not gaps ? nlow_on_off :
not lookahead and gaps ? nlow_off_on :
not lookahead and not gaps ? nlow_off_off : na
[nhigh, nlow]
[nhigh, nlow] = f_get_htfHighLow(resolution, HTFMultiplier, lookahead, gaps, offset)
[nhighlast, nlowlast] = f_get_htfHighLow(resolution, HTFMultiplier, lookahead, gaps, offset+1)
plot(nhigh , title="HTF High",style=plot.style_circles, color=color.green, linewidth=1)
plot(nlow , title="HTF Low",style=plot.style_circles, color=color.red, linewidth=1)
buyCondition = nhigh > nhighlast and nlow > nlowlast
sellCondition = nhigh < nhighlast and nlow < nlowlast
strategy.entry("Buy", strategy.long, when= buyCondition and inDateRange, oca_name="oca_buy")
strategy.entry("Sell", strategy.short, when= sellCondition and inDateRange, oca_name="oca_sell")