동적 패턴 트렌드 역전 전략

저자:차오장, 날짜: 2023-12-13 16:52:34
태그:

img

전반적인 설명

동적 패턴 트렌드 역전 전략은 가격을 예측하기 위해 선형 회귀와 이동 평균 라인을 사용하여 거래 신호를 생성하는 패턴을 형성합니다. 예측된 가격이 이동 평균 라인을 상향으로 넘을 때 구매 신호를 생성하고 아래로 넘을 때 판매 신호를 판매하여 트렌드 역전을 포착합니다.

전략 논리

  1. 예측 가격을 얻기 위해 거래량에 기초한 주식 가격의 선형 회귀를 계산합니다.
  2. 다른 조건 하에서 이동 평균을 계산
  3. 예측된 가격이 상향 이동 평균을 넘을 때 구매 신호를 생성합니다.
  4. 예측된 가격이 이동평균을 넘어선 경우 판매 신호를 생성합니다.
  5. 트렌드 반전 시기를 결정하기 위해 MACD 지표를 포함합니다.

위의 신호와 여러 가지 확인을 결합하면 잘못된 파열을 피하고 정확도를 향상시킵니다.

이점 분석

  • 선형 회귀를 사용하여 가격 추세를 예측하여 신호 정확도를 향상시킵니다.
  • 이동 평균 패턴을 통해 트렌드 반전을 포착합니다.
  • 거래량에 기반한 회귀는 더 나은 경제적인 의미를 가지고 있습니다.
  • MACD 등을 통해 여러 번 확인하면 잘못된 신호가 감소합니다.

위험 분석

  • 선형 회귀의 매개 변수는 결과에 중요한 영향을 미칩니다.
  • 이동 평균 설정 또한 신호 품질에 영향을
  • 확인이 있음에도 불구하고 거짓 신호는 여전히 위험합니다.
  • 코드는 거래 빈도를 줄이고 수익률을 향상시키기 위해 더 이상 최적화 될 수 있습니다.

최적화 방향

  • 선형 회귀 및 이동 평균의 매개 변수를 최적화
  • 잘못된 신호 비율을 낮추기 위해 더 많은 확인 조건을 추가합니다.
  • 트렌드 반전의 품질을 판단하기 위해 더 많은 요소를 포함합니다.
  • 개별 거래에 대한 위험을 줄이기 위해 스톱 로스 전략을 강화합니다.

결론

동적 패턴 트렌드 역전 전략은 선형 회귀 예측과 이동 평균 패턴을 통합하여 트렌드 역전을 포착합니다. 단일 지표 전략에 비해 더 높은 신뢰성을 가지고 있습니다. 매개 변수, 확인 및 기타 최적화에 대한 추가 개선은 신호 품질과 수익성을 향상시킬 수 있습니다.


/*backtest
start: 2023-12-05 00:00:00
end: 2023-12-12 00:00:00
period: 1m
basePeriod: 1m
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/
// © stocktechbot
//@version=5
strategy("Linear Cross", overlay=true, margin_long=100, margin_short=0)

//Linear Regression

vol = volume

// Function to calculate linear regression
linregs(y, x, len) =>
    ybar = math.sum(y, len)/len
    xbar = math.sum(x, len)/len
    b = math.sum((x - xbar)*(y - ybar),len)/math.sum((x - xbar)*(x - xbar),len)
    a = ybar - b*xbar
    [a, b]

// Historical stock price data
price = close

// Length of linear regression
len = input(defval = 21, title = 'Strategy Length')
linearlen=input(defval = 9, title = 'Linear Lookback')
[a, b] = linregs(price, vol, len)

// Calculate linear regression for stock price based on volume
//eps = request.earnings(syminfo.ticker, earnings.actual)
//MA For double confirmation

out = ta.sma(close, 200)
outf = ta.sma(close, 50)
outn = ta.sma(close, 90)
outt = ta.sma(close, 21)
outthree = ta.sma(close, 9)

// Predicted stock price based on volume
predicted_price = a + b*vol

// Check if predicted price is between open and close
is_between = open < predicted_price and predicted_price < close

//MACD
//[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)

// Plot predicted stock price
plot(predicted_price, color=color.rgb(65, 59, 150), linewidth=2, title="Predicted Price")
plot(ta.sma(predicted_price,linearlen), color=color.rgb(199, 43, 64), linewidth=2, title="MA Predicted Price")
//offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
plot(out, color=color.blue, title="MA200")
[macdLine, signalLine, histLine] = ta.macd(predicted_price, 12, 26, 9)

//BUY Signal

longCondition=false
mafentry =ta.sma(close, 50) > ta.sma(close, 90)
//matentry = ta.sma(close, 21) > ta.sma(close, 50)
matwohun = close > ta.sma(close, 200)
twohunraise = ta.rising(out, 2)
twentyrise = ta.rising(outt, 2)
macdrise = ta.rising(macdLine,2)
macdlong = ta.crossover(predicted_price, ta.wma(predicted_price,linearlen))  and (signalLine < macdLine)
if macdlong and macdrise
    longCondition := true

if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)
//Sell Signal
lastEntryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)
daysSinceEntry = len
daysSinceEntry := int((time - strategy.opentrades.entry_time(strategy.opentrades - 1)) / (24 * 60 * 60 * 1000))
percentageChange = (close - lastEntryPrice) / lastEntryPrice * 100
//trailChange = (ta.highest(close,daysSinceEntry) - close) / close * 100

//label.new(bar_index, high, color=color.black, textcolor=color.white,text=str.tostring(int(trailChange)))
shortCondition=false
mafexit =ta.sma(close, 50) < ta.sma(close, 90)
matexit = ta.sma(close, 21) < ta.sma(close, 50)
matwohund = close < ta.sma(close, 200)
twohunfall = ta.falling(out, 3)
twentyfall = ta.falling(outt, 2)
shortmafall = ta.falling(outthree, 1)
macdfall = ta.falling(macdLine,1)
macdsell = macdLine < signalLine
if macdfall and macdsell and (macdLine < signalLine) and ta.falling(low,2)
    shortCondition := true

if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)




더 많은