피보나치 추세 반전 전략

ATR TSOT RMA
생성 날짜: 2024-04-28 14:05:45 마지막으로 수정됨: 2024-04-28 14:05:45
복사: 0 클릭수: 652
avatar of ChaoZhang ChaoZhang
1
집중하다
1617
수행원

피보나치 추세 반전 전략

개요

“피보나치 트렌드 반전 전략”은 피보나치 회수수준과 트렌드 강도 지표 ((TSOT) 를 사용하여 시장의 트렌드 반전점을 포착하는 거래 전략이다. 이 전략은 동적 ATR 중지 및 부분 정지를 통해 위험 관리 및 수익을 극대화한다. 이 전략은 5 분 확장 가능한 시장에 적용된다.

전략 원칙

이 전략은 피포나치 리트레이스 레벨 ((0.236, 0.5 및 0.786) 을 사용하여 잠재적인 트렌드 반전점을 식별한다. 동시에, TSOT 지표는 가격의 백분율 순위를 통해 트렌드 강도를 측정한다. 가격이 중간 피포나치 레벨 ((0.5) 을 깨고 TSOT 지표가 상승/하락 신호를 표시할 때, 전략은 포지션을 열고 더 많이/무슨하게한다.

전략적 이점

  1. 피보나치 회귀와 트렌드 강도 지표의 조합으로 트렌드 반전 지점을 더 정확하게 포착할 수 있다.
  2. 동적 ATR 상쇄는 현재 시장의 변동에 따라 조정되며, 효과적인 위험 관리를 구현한다.
  3. 부분적인 정지 설정은 목표 수익을 달성할 때 수익을 적당히 얻을 수 있고, 수익은 계속 달릴 수 있습니다.
  4. 새로운 신호에 따라 포지션을 반전할 수 있게 되어, 전략의 적응성과 유연성을 높였다.

전략적 위험

  1. 시장이 흔들리거나 추세가 불명확할 때, 빈번한 반전 신호는 과도한 거래와 손실을 초래할 수 있다.
  2. 동적 상쇄와 부분 상쇄는 위험을 조절하는 데 도움이 되지만, 극단적인 상황에서는 더 큰 회수 현상이 발생할 수 있습니다.
  3. 전략 파라미터의 선택 (예: 피보나치 레벨, TSOT 계산 등) 은 다른 시장과 시간 주기에 따라 최적화되어야 하며, 부적절한 파라미터는 전략 성능에 영향을 줄 수 있다.

전략 최적화 방향

  1. 더 많은 확인 신호 (거래량, 동력 지표 등) 를 도입하여 가짜 신호를 필터링하여 포지션 개시의 정확성을 향상시킵니다.
  2. 스톱 스톱 로직을 최적화하여, 트렌드 강도를 결합하여 스톱 목표를 동적으로 조정하거나, 추적 스톱을 도입하는 등.
  3. 자주 반전되는 경우, 반전 횟수 제한을 설정하거나 반전 필터링 조건을 추가하여 과도한 거래를 줄일 수 있습니다.
  4. 다양한 시장 특성과 거래 유형에 대한 전략 파라미터를 정밀하게 최적화하고 테스트합니다.

요약하다

“피보나치 트렌드 반전 전략”은 피보나치 회수수준과 TSOT 지표의 결합을 통해 트렌드 반전점을 효과적으로 포착하고, 동적 상쇄 및 부분 상쇄를 통해 위험 제어 및 수익 목표를 달성할 수 있습니다. 전략은 트렌드가 명확한 시장에서 우수한 성능을 발휘하지만, 불안정한 시장에서는 신중하게 대처해야합니다. 향후 신호 확인, 상쇄 상쇄 최적화, 반전 관리 등의 측면에서 전략에 대한 추가적인 개선 및 개선이 가능하여 안정성과 수익성을 향상시킬 수 있습니다.

전략 소스 코드
/*backtest
start: 2023-04-22 00:00:00
end: 2024-04-27 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © nioboi

//@version=5
strategy("Fibonacci Trend Reversals", overlay=true, process_orders_on_close = true, commission_value = 0.055, initial_capital = 1000)

// =========================================
// Input Groups
// =========================================
string rsi_group = "RSI"
string main_group = "Fib Sensitivity"
string atr_sl_finder_group = "ATR SL Finder"
string trade_execution_group = "Strategy Execution"

// =========================================
// Fibonacci Retracement Trend Reversal
// =========================================
sensitivity_input = input.float(title = 'Sensitive', step = 0.1, defval = 18, group = main_group)
var bool is_long_trend_started = false
var bool is_short_trend_started = false
var bool is_trend_change = na
var bool is_long_trend = false
var bool is_short_trend = false
var bool can_long = false
var bool can_short = false

sensitivity = sensitivity_input
sensitivity *= 10

high_line = ta.highest(high, int(sensitivity))
low_line = ta.lowest(low, int(sensitivity))
channel_range = high_line - low_line
fib_236 = high_line - channel_range * (0.236)
fib_5 = high_line - channel_range * 0.5
fib_786 = high_line - channel_range * (0.786)
imba_trend_line = fib_5

// =========================================
// TSOT | Trend Strength Over Time
// =========================================

// Calculate 75th percentile of price for each length
percentile_13H = ta.percentile_nearest_rank(high, 13, 75) 
percentile_21H = ta.percentile_nearest_rank(high, 21, 75) 
percentile_34H = ta.percentile_nearest_rank(high, 34, 75) 
percentile_55H = ta.percentile_nearest_rank(high, 55, 75) 
percentile_89H = ta.percentile_nearest_rank(high, 89, 75)

// Calculate 25th percentile of  price for each length
percentile_13L =  ta.percentile_nearest_rank(low, 13, 25) 
percentile_21L =  ta.percentile_nearest_rank(low, 21, 25) 
percentile_34L =  ta.percentile_nearest_rank(low, 34, 25) 
percentile_55L = ta.percentile_nearest_rank(low, 55, 25) 
percentile_89L = ta.percentile_nearest_rank(low, 89, 25)

// Calculate 75th and 25th for length 144 (longest length)
highest_high = ta.percentile_nearest_rank(high, 144, 75) 
lowest_low = ta.percentile_nearest_rank(low, 144, 25) 

// Calculate trend strength conditions
trendBull1 = percentile_13H > highest_high
trendBull2 = percentile_21H > highest_high
trendBull3 = percentile_34H > highest_high
trendBull4 = percentile_55H > highest_high
trendBull5 = percentile_89H > highest_high
trendBull6 = percentile_13L > highest_high
trendBull7 = percentile_21L > highest_high
trendBull8 = percentile_34L > highest_high
trendBull9 = percentile_55L > highest_high
trendBull10 = percentile_89L > highest_high

trendBear1 = percentile_13H < lowest_low
trendBear2 = percentile_21H < lowest_low
trendBear3 = percentile_34H < lowest_low
trendBear4 = percentile_55H < lowest_low
trendBear5 = percentile_89H < lowest_low
trendBear6 = percentile_13L < lowest_low
trendBear7 = percentile_21L < lowest_low
trendBear8 = percentile_34L < lowest_low
trendBear9 = percentile_55L < lowest_low
trendBear10 = percentile_89L < lowest_low

countBull =
     (trendBull1 ? 1 : 0) +
     (trendBull2 ? 1 : 0) +
     (trendBull3 ? 1 : 0) +
     (trendBull4 ? 1 : 0) +
     (trendBull5 ? 1 : 0) +
     (trendBull6 ? 1 : 0) +
     (trendBull7 ? 1 : 0) +
     (trendBull8 ? 1 : 0) +
     (trendBull9 ? 1 : 0) +
     (trendBull10 ? 1 : 0)

countBear =
     (trendBear1 ? 1 : 0) +
     (trendBear2 ? 1 : 0) +
     (trendBear3 ? 1 : 0) +
     (trendBear4 ? 1 : 0) +
     (trendBear5 ? 1 : 0) +
     (trendBear6 ? 1 : 0) +
     (trendBear7 ? 1 : 0) +
     (trendBear8 ? 1 : 0) +
     (trendBear9 ? 1 : 0) +
     (trendBear10 ? 1 : 0)

// Calculate weak bull count
weakBullCount = 
     (percentile_13L < highest_high and percentile_13L > lowest_low ? 1 : 0) +
     (percentile_21L < highest_high and percentile_21L > lowest_low ? 1 : 0) +
     (percentile_34L < highest_high and percentile_34L > lowest_low ? 1 : 0) +
     (percentile_55L < highest_high and percentile_55L > lowest_low ? 1 : 0) +
     (percentile_89L < highest_high and percentile_89L > lowest_low ? 1 : 0)

// Calculate weak bear count
weakBearCount = 
     (percentile_13H > lowest_low and percentile_13H < highest_high ? 1 : 0) +
     (percentile_21H > lowest_low and percentile_21H < highest_high ? 1 : 0) +
     (percentile_34H > lowest_low and percentile_34H < highest_high ? 1 : 0) +
     (percentile_55H > lowest_low and percentile_55H < highest_high ? 1 : 0) +
     (percentile_89H > lowest_low and percentile_89H < highest_high ? 1 : 0)

// Calculate bull strength and bear strength
bullStrength = 10 * (countBull + 0.5*weakBullCount - 0.5*weakBearCount - countBear)
bearStrength = 10 * (countBear + 0.5*weakBearCount - 0.5*weakBullCount - countBull)

// Calculate the current trend
currentTrendValue = bullStrength - bearStrength

tsot_bullish = currentTrendValue > 0
tsot_bearish = currentTrendValue < 0

// CAN LONG/SHORT
can_long := close >= imba_trend_line and close >= fib_236 and not is_long_trend and tsot_bullish
can_short := close <= imba_trend_line and close <= fib_786 and not is_short_trend and tsot_bearish

if can_long
    is_long_trend := true
    is_short_trend := false
    is_long_trend_started := is_long_trend_started ? false : true
else if can_short
    is_short_trend := true
    is_long_trend := false
    is_short_trend_started := is_short_trend_started ? false : true
else
    is_trend_change := false
    can_long := false
    can_short := false
    is_short_trend_started := false
    is_long_trend_started := false

is_trend_change := is_short_trend_started or is_long_trend_started
plotshape(is_long_trend and is_long_trend_started ? imba_trend_line : na, title="Long", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(is_short_trend and is_short_trend_started ? imba_trend_line : na, title="Short", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
plot(imba_trend_line, color = is_long_trend[1] ? color.green : color.red, linewidth = 3)

// =========================================
// ATR SL Finder
// =========================================
atrlength = input.int(title='Length', defval=14, minval=1, group = atr_sl_finder_group)
smoothing = input.string(title='Smoothing', defval='RMA', options=['RMA', 'SMA', 'EMA', 'WMA'], group = atr_sl_finder_group)
m = input(3.5, 'Multiplier', group = atr_sl_finder_group)
src1 = high
src2 = low
ma_function(source, length) =>
    if smoothing == 'RMA'
        ta.rma(source, length)
    else
        if smoothing == 'SMA'
            ta.sma(source, length)
        else
            if smoothing == 'EMA'
                ta.ema(source, length)
            else
                ta.wma(source, length)

x = ma_function(ta.tr(true), atrlength) * m + src1 // SHORT SL
x2 = src2 - ma_function(ta.tr(true), atrlength) * m // LONG SL

p1 = plot(x, title="ATR Short Stop Loss", color=color.red)
p2 = plot(x2, title="ATR Long Stop Loss", color=color.green)

// =========================================
// Strategy Execution
// =========================================

tradeDirection = input.string("Both", "Trade Direction", ["Long Only", "Short Only", "Both"], group = trade_execution_group, tooltip = "Select if you want this strategy to run only Long or Only Short positions, or Both")

risk_reward_ratio = input.float(2, "Risk Reward Ratio", group = trade_execution_group)
partialTp = input.bool(true, "Use Partial Take Profit", tooltip = "Enable this if you want to exit 50% of your position when half point of your Risk Reward is reached.", group = trade_execution_group)
allowReversePosition = input.bool(true, "Allow Reversing of Position", tooltip = "Enable this if you want to reverse position when new opposite signal occurs", group = trade_execution_group)

// Long or Short Conditions
enterLong = can_long and (tradeDirection == "Long Only" or tradeDirection == "Both")
enterShort = can_short and (tradeDirection == "Short Only" or tradeDirection == "Both")

// Long Entry Variables
var bool plotMarkers_long = false
var bool firstTPHit_long = false
var float sl_long = na
var float breakEven_long = na
var float tp1_long = na
var float tp2_long = na
var float entryPrice_long = na
var bool inLongPosition = false

// Short Entry Variables
var bool plotMarkers_short = false
var bool firstTPHit_short = false
var float sl_short = na
var float breakEven_short = na
var float tp1_short = na
var float tp2_short = na
var float entryPrice_short = na
var bool inShortPosition = false


// Reversal Logic
if inLongPosition and can_short and allowReversePosition // in a long position and signal to enter short and havent yet hit first tp
    strategy.close("Long", "Reversing Long to Short") // close Long in preparation to enter short in the next few lines
    inLongPosition := false
else if inShortPosition and can_long and allowReversePosition // in a short position and signal to enter long and havent yet hit first tp
    strategy.close("Short", "Reversing Short to Long") // close Short in preparation to enter long in the next few lines 
    inShortPosition := false

// Long Entries
if enterLong
    entryPrice_long := close 
    sl_long := x2
    risk = entryPrice_long - sl_long
    tp1_long := entryPrice_long + ((risk_reward_ratio * risk) / 2)
    tp2_long := entryPrice_long + (risk_reward_ratio * risk)
    breakEven_long := entryPrice_long + (entryPrice_long * 0.002)
    strategy.entry("Long", strategy.long)
    if not partialTp
        strategy.exit("Exit Long", "Long", limit = tp2_long, stop = sl_long)
    firstTPHit_long := false
    inLongPosition := true

// Short Entries
if enterShort
    entryPrice_short := close
    sl_short := x
    risk = sl_short - entryPrice_short
    tp1_short := entryPrice_short - ((risk_reward_ratio * risk)/2)
    tp2_short := entryPrice_short - (risk_reward_ratio * risk)
    breakEven_short := entryPrice_short - (entryPrice_short * 0.002)
    strategy.entry("Short", strategy.short)
    if not partialTp
        strategy.exit("Exit Short", "Short", limit = tp2_short, stop = sl_short)
    firstTPHit_short := false
    inShortPosition := true

// Dynamic TP and exit strategy for Longs
if inLongPosition and partialTp // in long position and partial TP for exit strategy is enabled
    if high >= tp1_long and not firstTPHit_long // high of candle hit first TP of long, and not yet hit first TP before
        strategy.close("Long", "TP-1 Long", qty_percent = 50) // close 50% of our long position
        sl_long := breakEven_long
        firstTPHit_long := true // set the first TP checker flag to true
    else if high >= tp2_long and firstTPHit_long // already hit the first TP and we hit our 2nd tp
        strategy.close("Long", "TP-2 long") // close the remaining of the long position
        inLongPosition := false // not in long position anymore
    else if low <= sl_long and not firstTPHit_long // not yet hit first TP but hit our SL
        strategy.close("Long", "SL long") // close the entire long position
        inLongPosition := false // not in long position anymore
    else if low <= breakEven_long and firstTPHit_long // already hit first TP and retraced back to breakEven
        strategy.close("Long", "BE Long")
        inLongPosition := false // not in long position anymore

// Dynamic TP and exit strategy for Shorts
if inShortPosition and partialTp // in short position and partial TP for exit strategy is enabled
    if low <= tp1_short and not firstTPHit_short // low of candle hit first TP of short, and not yet hit first TP before
        strategy.close("Short", "TP-1 Short", qty_percent = 50) // close 50% of our short position
        firstTPHit_short := true // set the first TP checker flag to true
        sl_short := breakEven_short
    else if low <= tp2_short and firstTPHit_short // already hit the first TP and we hit our 2nd tp
        strategy.close("Short", "TP-2 Short") // close the remaining of the short position
        inShortPosition := false // not in short position anymore
    else if high >= sl_short and not firstTPHit_short // not yet hit first TP but hit our SL
        strategy.close("Short", "SL Short") // close the entire long position
        inShortPosition := false // not in long position anymore
    else if high >= breakEven_short and firstTPHit_short // already hit first TP and retraced back to breakEven
        strategy.close("Short", "BE Short")
        inShortPosition := false // not in long position anymore

// =========================================
// Entry Visuals
// =========================================

// Entry Visual Flags
if inLongPosition
    plotMarkers_long := true
    plotMarkers_short := false
else if inShortPosition
    plotMarkers_long := false
    plotMarkers_short := true
    
showEntryVisuals = input.bool(true, "Show Entry Visuals", group = trade_execution_group)
plot(plotMarkers_long and showEntryVisuals?sl_long:na, "SL Marker L", color = #ff0000a4, linewidth = 1, style = plot.style_linebr)
plot(plotMarkers_long and showEntryVisuals?tp1_long:na, "TP1 Marker L", color = #00ff08a8, linewidth = 1, style = plot.style_linebr)
plot(plotMarkers_long and showEntryVisuals?tp2_long:na, "TP2 Marker L", color = #1100ffa9, linewidth = 1, style = plot.style_linebr)
plot(plotMarkers_short and showEntryVisuals?sl_short:na, "SL Marker S", color = #ff0000a4, linewidth = 1, style = plot.style_linebr)
plot(plotMarkers_short and showEntryVisuals?tp1_short:na, "TP1 Marker S", color = #00ff08a8, linewidth = 1, style = plot.style_linebr)
plot(plotMarkers_short and showEntryVisuals?tp2_short:na, "TP2 Marker S", color = #1100ffa9, linewidth = 1, style = plot.style_linebr)