이동 평균 및 촛불 패턴을 가진 트렌드를 따르는 전략

저자:차오장, 날짜: 2024-02-02 17:53:43
태그:

img

전반적인 설명

이 전략은 트렌드 방향을 결정하기 위해 이동 평균과 촛불 패턴을 결합하여 잠재적 인 반전 지점을 식별하여 트렌드를 수행합니다. 전략은 먼저 이동 평균을 사용하여 전체 트렌드 방향을 판단하고, 트렌드를 추적하기 위해 트렌드 방향을 따라 입력 신호로 잠재적 인 반전 촛불 패턴을 찾습니다.

전략 원칙

이 전략은 가격 추세를 결정하기 위해 10 일 간 간단한 이동 평균을 채택합니다. 가격이 이동 평균보다 높을 때 상승 추세로 간주됩니다. 가격이 이동 평균보다 낮을 때 하락 추세로 간주됩니다.

트렌드 방향을 결정한 후, 전략은 상승세와 하락세 촛불 패턴을 기반으로 잠재적 인 트렌드 역전 지점을 판단합니다. 일반적인 상승세 패턴에는 아침 별, 상승세 삼각, 세 개의 흰 군인 등이 포함됩니다. 일반적인 하락세 패턴에는 저녁 별, 세 개의 검은 까마귀 등이 포함됩니다. 상승세 신호가 확인되면 전략은 구매 명령을 내립니다. 하락세 신호가 확인되면 전략은 판매 명령을 내립니다.

또한 전략은 특정 입시 가격을 결정하기 위해 주요 지원 및 저항 수준을 포함합니다. 예를 들어 상승 추세에서 구매할 때 첫 번째 지원 수준을 뚫을 때 구매합니다.

장점

이 전략의 가장 큰 장점은 트렌드 판단과 역전 신호를 결합하여 트렌드를 추적하기 위해 트렌드 전환점을 적시에 파악 할 수 있다는 것입니다. 간단한 이동 평균 전략과 비교하면 이 전략은 수익성을 크게 향상시킬 수 있습니다.

또한 촛불 패턴 판단을 통합하면 갑작스러운 사건에 대처하는 능력도 향상됩니다. 낮은 확률 이벤트가 시장에서 잘못된 파장을 유발할 때 촛불 패턴은 잘못된 거래를 피하기 위해 필터 역할을 할 수 있습니다.

위험성

이 전략의 주요 위험은 이동 평균 매개 변수 설정의 정확성과 촛불 패턴 판단에 있습니다. 이동 평균 기간이 잘못 설정되면 잘못된 트렌드 결정으로 이어질 것입니다. 촛불 패턴의 판단에 오류가 있으면 잘못된 거래 결정으로 이어질 것입니다.

또한, 반전 촛불 패턴은 100%의 확실성을 가지고 트렌드 반전을 보장 할 수 없으므로 전략에는 여전히 약간의 위험이 있습니다. 시장이 더 큰 반전을 볼 때 전략에 더 큰 손실을 가져올 수 있습니다.

최적화 방향

이 전략에는 여전히 상당한 최적화 여지가 있습니다. 예를 들어, 우리는 움직이는 평균의 매개 변수를 동적으로 조정하고 다른 시장 단계에서 다른 움직이는 평균 기간을 채택하는 것을 고려할 수 있습니다. 또한 판단 정확성을 향상시키기 위해 역사적 데이터를 사용하여 촛불 패턴 판단 모델을 훈련하기위한 기계 학습 방법을 도입 할 수 있습니다.

또한 트렌드 및 핫 영역을 판단하는 더 많은 요소, 예를 들어 거래량 변화, 변동성 지표 등을 포함하는 것도 고려할 수 있습니다. 전략을 더 포괄적이고 견고하게 만들기 위해.

결론

일반적으로 이 전략은 주식 시장의 중장기 트렌드를 추적하는데 매우 적합하며 상대적으로 높은 안정적인 수익을 얻을 수 있다. 추가적으로 최적화되면 잘 작동하는 양적 전략이 될 가능성이 있다. 투자자들이 이 전략의 사용을 이해한다면, 더 나은 초과 수익을 얻으면서 개별 주식 위험을 통제하기 위해 장기적 지분을 구축하는 데도 사용할 수 있다.


/*backtest
start: 2024-01-01 00:00:00
end: 2024-01-31 23:59:59
period: 3h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("Trend Following Strategy with Candlestick Patterns", overlay=true)

// Moving Average
ma_period = input(10, title="Moving Average Period")
moving_average = ta.sma(close, ma_period)

// Candlestick Patterns
// Custom Function
abs(x) => x >= 0 ? x : -x

// Candlestick Patterns
isDoji() =>
    (close - open) <= (high - low) * 0.1

isMarubozuWhite() =>
    close == high and open == low and close > open

isHammer() =>
    (high - low) >= 3 * abs(open - close) and (close - low) / (0.001 + high - low) > 0.6 and (open - low) / (0.001 + high - low) > 0.6

isInvertedHammer() =>
    (high - low) >= 3 * abs(open - close) and (high - close) / (0.001 + high - low) > 0.6 and (high - open) / (0.001 + high - low) > 0.6

isLongLowerShadow() =>
    (low - close) > 2 * abs(open - close) and (low - open) / (0.001 + high - low) > 0.6

isUpsideTasukiGap() =>
    close[1] < open[1] and open > close and open > close[1] and close < open[1]

isRisingWindow() =>
    high[1] < low and close > open

isPiercing() =>
    close[1] < open[1] and close > open and close > ((open + low) / 2) and close < open[1] and open < close[1]

isBullishEngulfing() =>
    close[1] < open[1] and close > open and high > high[1] and low < low[1]

isTweezerBottom() =>
    low == ta.valuewhen(low == ta.lowest(low, 10), low, 0) and low == ta.valuewhen(low == ta.lowest(low, 20), low, 0)

isBullishAbandonedBaby() =>
    close[2] < open[2] and close[1] > open[1] and low[1] > ta.valuewhen(high == ta.highest(high, 2), high, 0) and open > close and close > ta.valuewhen(high == ta.highest(high, 2), high, 0)

isMorningStar() =>
    close[2] < open[2] and close[1] < open[1] and close > open[1] and open < close[2] and open > close[1]

isMorningDojiStar() =>
    close[2] < open[2] and close[1] < open[1] and isDoji() and close > open[1] and open < close[2] and open > close[1]

isDragonflyDoji() =>
    isDoji() and (high - close) / (0.001 + high - low) < 0.1 and (open - low) / (0.001 + high - low) > 0.6

isThreeWhiteSoldiers() =>
    close[2] < open[2] and close[1] < open[1] and close > open and open < close[2] and open < close[1] and close > open[1]

isRisingThreeMethods() =>
    close[4] < open[4] and close[1] < open[1] and close > open and open < close[4] and open < close[1] and close > open[1]

isMarubozuBlack() =>
    close == low and open == high and open > close

isGravestoneDoji() =>
    isDoji() and (close - low) / (0.001 + high - low) < 0.1 and (high - open) / (0.001 + high - low) > 0.6

isHangingMan() =>
    (high - low) >= 4 * abs(open - close) and (open - close) / (0.001 + high - low) > 0.6 and (high - open) / (0.001 + high - low) > 0.6

isLongUpperShadow() =>
    (high - open) > 2 * abs(open - close) and (high - close) / (0.001 + high - low) > 0.6

isDownsideTasukiGap() =>
    close[1] > open[1] and open < close and open < close[1] and close > open[1]

isFallingWindow() =>
    low[1] > high and close < open

isDarkCloudCover() =>
    close[1] > open[1] and close < open and close < ((open + high) / 2) and close > open[1] and open > close[1]

isBearishEngulfing() =>
    close[1] > open[1] and close < open and high > high[1] and low < low[1]

isTweezerTop() =>
    high == ta.valuewhen(high == ta.highest(high, 10), high, 0) and high == ta.valuewhen(high == ta.highest(high, 20), high, 0)

isAbandonedBaby() =>
    close[2] > open[2] and close[1] < open[1] and high[1] < ta.valuewhen(low == ta.lowest(low, 2), low, 0) and open < close and close < ta.valuewhen(low == ta.lowest(low, 2), low, 0)

isEveningDojiStar() =>
    close[2] > open[2] and close[1] > open[1] and isDoji() and close < open[1] and open > close[2] and open < close[1]

isEveningStar() =>
    close[2] > open[2] and close[1] > open[1] and close < open[1] and open > close[2] and open < close[1]

isThreeBlackCrows() =>
    close[2] > open[2] and close[1] > open[1] and close < open and open > close[2] and open > close[1] and close < open[1]

isFallingThreeMethods() =>
    close[4] > open[4] and close[1] > open

isShootingStar() =>
    (high - low) >= 3 * abs(open - close) and (high - close) / (0.001 + high - low) > 0.6 and (high - open) / (0.001 + high - low) > 0.6

doji = isDoji()
marubozuWhite = isMarubozuWhite()
hammer = isHammer()
invertedHammer = isInvertedHammer()
longLowerShadow = isLongLowerShadow()
upsideTasukiGap = isUpsideTasukiGap()
risingWindow = isRisingWindow()
piercing = isPiercing()
bullishEngulfing = isBullishEngulfing()
tweezerBottom = isTweezerBottom()
bullishAbandonedBaby = isBullishAbandonedBaby()
morningStar = isMorningStar()
morningDojiStar = isMorningDojiStar()
dragonflyDoji = isDragonflyDoji()
threeWhiteSoldiers = isThreeWhiteSoldiers()
risingThreeMethods = isRisingThreeMethods()
marubozuBlack = isMarubozuBlack()
gravestoneDoji = isGravestoneDoji()
hangingMan = isHangingMan()
longUpperShadow = isLongUpperShadow()
downsideTasukiGap = isDownsideTasukiGap()
fallingWindow = isFallingWindow()
darkCloudCover = isDarkCloudCover()
bearishEngulfing = isBearishEngulfing()
tweezerTop = isTweezerTop()
abandonedBaby = isAbandonedBaby()
eveningDojiStar = isEveningDojiStar()
eveningStar = isEveningStar()
threeBlackCrows = isThreeBlackCrows()
fallingThreeMethods = isFallingThreeMethods()
shootingStar = isShootingStar()
isBullishPattern() =>
    (isMarubozuWhite() or isHammer() or isInvertedHammer() or isDoji() or isMorningStar() or isBullishEngulfing() or isThreeWhiteSoldiers() or isMarubozuBlack() or isHangingMan() or isDownsideTasukiGap() or isDarkCloudCover())

isBearishPattern() =>
    (isMarubozuBlack() or isInvertedHammer() or isLongUpperShadow() or isTweezerTop() or isGravestoneDoji() or isEveningStar() or isBearishEngulfing() or isThreeBlackCrows() or isShootingStar())

isBullishCandle = isBullishPattern()
isBearishCandle = isBearishPattern()

// Calculate Pivot Points
pivotPoint(high, low, close) =>
    (high + low + close) / 3

r1 = pivotPoint(high[1], low[1], close[1]) * 2 - low[1]
s1 = pivotPoint(high[1], low[1], close[1]) * 2 - high[1]

r2 = pivotPoint(high[1], low[1], close[1]) + (high[1] - low[1])
s2 = pivotPoint(high[1], low[1], close[1]) - (high[1] - low[1])

r3 = high[1] + 2 * (pivotPoint(high[1], low[1], close[1]) - low[1])
s3 = low[1] - 2 * (high[1] - pivotPoint(high[1], low[1], close[1]))
// Trend Identification
is_uptrend = close > moving_average
is_downtrend = close < moving_average
// Entry and Exit Conditions with Trend Identification
enterLong = is_uptrend and isBullishCandle and close > r1
exitLong = is_uptrend and (bearishEngulfing or doji or close < s1)

enterShort = is_downtrend and isBearishCandle and close < s1
exitShort = is_downtrend and (bullishEngulfing or doji or close > r1)



// Strategy Execution
if enterLong and strategy.position_size == 0 and strategy.position_size[1] == 0 and close > r1
    strategy.entry("Buy", strategy.long, qty=1)

if exitLong and strategy.position_size > 0
    strategy.close("Buy")

if enterShort and strategy.position_size == 0 and close < s1
    strategy.entry("Sell", strategy.short, qty=1)

if exitShort and strategy.position_size < 0
    strategy.close("Sell")


// Stop-Loss and Trailing Stop-Loss
stop_loss_pct = input(2.0, title="Stop Loss Percentage")
trailing_stop_loss_pct = input(1.0, title="Trailing Stop Loss Percentage")
trailing_stop_loss_active = input(true, title="Trailing Stop Loss Active")

// Stop-Loss
stop_loss_level = strategy.position_avg_price * (1 - stop_loss_pct / 100)
strategy.exit("Stop Loss", "Buy", loss=stop_loss_level)

// Trailing Stop-Loss


// Plotting Moving Average
plot(moving_average, color=color.blue, title="Moving Average", linewidth=2)

// Plotting Candlestick Patterns
plotshape(isBullishCandle, title="Bullish Candle", location=location.belowbar, color=color.green, style=shape.labelup)
plotshape(isBearishCandle, title="Bearish Candle", location=location.abovebar, color=color.red, style=shape.labeldown)

// Plotting Support and Resistance Levels
//hline(r1, "Resistance Level 1", color=color.red, linestyle=hline.style_dotted)
//hline(s1, "Support Level 1", color=color.green, linestyle=hline.style_dotted)


더 많은