신경망 슈퍼 트렌드 전략

저자:차오장, 날짜: 2023-09-14 16:49:38
태그:

전략 논리

이 전략은 신경 네트워크 모델, RSI 지표 및 슈퍼 트렌드 지표를 결합하여 거래합니다.

논리는 다음과 같습니다.

  1. 부피 변화, 볼링거 밴드, RSI 등과 같은 입력값으로 신경 네트워크 모델을 구축합니다.

  2. 네트워크는 미래의 가격 변화율을 예측합니다.

  3. RSI 값을 계산하고 예측된 가격 변화와 결합

  4. RSI를 기반으로 동적 스톱 로스 라인을 생성

  5. 가격이 상위 스톱 손실을 넘으면 짧게; 가격이 하위 스톱을 넘으면 길게

  6. 필터링을 위해 슈퍼 트렌드 트렌드 판단을 사용

이 전략은 복잡한 데이터를 모델링하는 신경 네트워크의 능력을 활용하고, RSI와 슈퍼 트렌드 같은 지표로부터 추가 신호 검증을 통해 위험을 제어하는 동시에 정확도를 향상시킵니다.

장점

  • 트렌드를 결정하기 위한 다차원 데이터의 신경망 모델

  • RSI는 수익을 보호하고, 슈퍼 트렌드는 판단을 돕습니다.

  • 여러 지표가 결합되어 신호 품질을 향상시킵니다.

위험성

  • 신경 네트워크 훈련에 큰 데이터 세트가 필요합니다.

  • RSI 및 슈퍼 트렌드 매개 변수 정밀 조정

  • 성능은 모델 예측에 달려 있고 불확실성도 있습니다.

요약

이 전략은 기계 학습과 전통적인 효율성 기술과 위험 통제를 결합합니다. 그러나 매개 변수와 모델 해석성은 개선이 필요합니다.


/*backtest
start: 2023-08-14 00:00:00
end: 2023-09-13 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
//ANN taken from https://www.tradingview.com/script/Eq4zZsTI-ANN-MACD-BTC/
//it only work for BTC as the ANN is trained for this data only
//super trend https://www.tradingview.com/script/VLWVV7tH-SuperTrend/
// Strategy version created for @che_trader
strategy ("ANN RSI SUPER TREND STRATEGY BY che_trader", overlay = true)
qty = input(10000, "Buy quantity")

testStartYear = input(2019, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testStartHour = input(0, "Backtest Start Hour")
testStartMin = input(0, "Backtest Start Minute")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,testStartHour,testStartMin)
testStopYear = input(2099, "Backtest Stop Year")
testStopMonth = input(1, "Backtest Stop Month")
testStopDay = input(30, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
testPeriod() => true

max_bars_back = (21)
src = close[0]

// Essential Functions

// Highest - Lowest Functions ( All efforts goes to RicardoSantos )

f_highest(_src, _length)=>
    _adjusted_length = _length < 1 ? 1 : _length
    _value = _src
    for _i = 0 to (_adjusted_length-1)
        _value := _src[_i] >= _value ? _src[_i] : _value
    _return = _value

f_lowest(_src, _length)=>
    _adjusted_length = _length < 1 ? 1 : _length
    _value = _src
    for _i = 0 to (_adjusted_length-1)
        _value := _src[_i] <= _value ? _src[_i] : _value
    _return = _value

// Function Sum  

f_sum(_src , _length) => 

    _output  = 0.00 
    
    _length_adjusted = _length < 1 ? 1 : _length
    
    for i = 0 to _length_adjusted-1
        _output := _output + _src[i]


// Unlocked Exponential Moving Average Function

f_ema(_src, _length)=>
    _length_adjusted = _length < 1 ? 1 : _length
    _multiplier = 2 / (_length_adjusted + 1)
    _return  = 0.00
    _return := na(_return[1]) ? _src : ((_src - _return[1]) * _multiplier) + _return[1]


// Unlocked Moving Average Function 

f_sma(_src, _length)=>
    
    _output = 0.00
    _length_adjusted = _length < 0 ? 0 : _length
    w = cum(_src)

    _output:= (w - w[_length_adjusted]) / _length_adjusted
   
    _output    


// Definition : Function Bollinger Bands

Multiplier = 2 
_length_bb = 20


e_r = f_sma(src,_length_bb)


// Function Standard Deviation : 

f_stdev(_src,_length) =>

    float _output = na 
    _length_adjusted = _length < 2 ? 2 : _length
    _avg  = f_ema(_src , _length_adjusted)
    evar  = (_src - _avg) * (_src - _avg)
    evar2 = ((f_sum(evar,_length_adjusted))/_length_adjusted)
    
    _output := sqrt(evar2)


std_r = f_stdev(src , _length_bb )


upband = e_r + (Multiplier * std_r)  // Upband
dnband = e_r - (Multiplier * std_r)  // Lowband
basis  = e_r                         // Midband

// Function : RSI


length = input(14, minval=1) // 


f_rma(_src, _length) =>
    _length_adjusted = _length < 1 ? 1 : _length
    alpha = _length_adjusted
    sum = 0.0
    sum := (_src + (alpha - 1) * nz(sum[1])) / alpha



f_rsi(_src, _length) => 

    _output = 0.00 
    _length_adjusted = _length < 0 ? 0 : _length

    u = _length_adjusted < 1 ? max(_src - _src[_length_adjusted], 0) : max(_src - _src[1] , 0) // upward change
    d = _length_adjusted < 1 ? max(_src[_length_adjusted] - _src, 0) : max(_src[1] - _src , 0) // downward change
    rs = f_rma(u, _length) / f_rma(d, _length)
    res = 100 - 100 / (1 + rs)
    res


_rsi = f_rsi(src, length)


// MACD 

_fastLength   = input(12 , title = "MACD Fast Length")
_slowlength   = input(26 , title = "MACD Slow Length")
_signalLength = input(9  , title = "MACD Signal Length")


_macd   = f_ema(close, _fastLength) - f_ema(close, _slowlength)
_signal = f_ema(_macd, _signalLength)
	   
_macdhist = _macd - _signal


// Inputs on Tangent Function : 

tangentdiff(_src) => nz((_src - _src[1]) / _src[1] ) 


// Deep Learning Activation Function (Tanh) : 

ActivationFunctionTanh(v) => (1 - exp(-2 * v))/( 1 + exp(-2 * v))


// DEEP LEARNING 

// INPUTS : 

input_1 = tangentdiff(volume)
input_2 = tangentdiff(dnband)
input_3 = tangentdiff(e_r)
input_4 = tangentdiff(upband)
input_5 = tangentdiff(_rsi)
input_6 = tangentdiff(_macdhist)

// LAYERS : 

// Input Layers 

n_0 = ActivationFunctionTanh(input_1 + 0)   
n_1 = ActivationFunctionTanh(input_2 + 0) 
n_2 = ActivationFunctionTanh(input_3 + 0) 
n_3 = ActivationFunctionTanh(input_4 + 0) 
n_4 = ActivationFunctionTanh(input_5 + 0)
n_5 = ActivationFunctionTanh(input_6 + 0)


// Hidden Layers 

n_6   = ActivationFunctionTanh( -2.580743 * n_0 + -1.883627 * n_1 + -3.512462 * n_2 + -0.891063 * n_3 + -0.767728 * n_4 + -0.542699 * n_5 +  0.221093) 
n_7   = ActivationFunctionTanh( -0.131977 * n_0 + -1.543499 * n_1 +  0.019450 * n_2 +  0.041301 * n_3 + -0.926690 * n_4 + -0.797512 * n_5 + -1.804061) 
n_8   = ActivationFunctionTanh( -0.587905 * n_0 + -7.528007 * n_1 + -5.273207 * n_2 +  1.633836 * n_3 +  6.099666 * n_4 +  3.509443 * n_5 + -4.384254) 
n_9   = ActivationFunctionTanh( -1.026331 * n_0 + -1.289491 * n_1 + -1.702887 * n_2 + -1.052681 * n_3 + -1.031452 * n_4 + -0.597999 * n_5 + -1.178839) 
n_10  = ActivationFunctionTanh( -5.393730 * n_0 + -2.486204 * n_1 +  3.655614 * n_2 +  1.051512 * n_3 + -2.763198 * n_4 +  6.062295 * n_5 + -6.367982) 
n_11  = ActivationFunctionTanh(  1.246882 * n_0 + -1.993206 * n_1 +  1.599518 * n_2 +  1.871801 * n_3 +  0.294797 * n_4 + -0.607512 * n_5 + -3.092821) 
n_12  = ActivationFunctionTanh( -2.325161 * n_0 + -1.433500 * n_1 + -2.928094 * n_2 + -0.715416 * n_3 + -0.914663 * n_4 + -0.485397 * n_5 + -0.411227) 
n_13  = ActivationFunctionTanh( -0.350585 * n_0 + -0.810108 * n_1 + -1.756149 * n_2 + -0.567176 * n_3 + -0.954021 * n_4 + -1.027830 * n_5 + -1.349766) 


// Output Layer 

_output  = ActivationFunctionTanh(2.588784 * n_6  + 0.100819 * n_7  + -5.305373 * n_8  + 1.167093 * n_9  + 
                                  3.770143 * n_10 + 1.269190 * n_11 +  2.090862 * n_12 + 0.839791 * n_13 + -0.196165)

_chg_src = tangentdiff(src) * 100

_seed = (_output - _chg_src)
// BEGIN ACTUAL STRATEGY
length1 = input(title="RSI Period", type=input.integer, defval=21)
mult = input(title="RSI Multiplier", type=input.float, step=0.1, defval=4.0)
wicks = input(title="Take Wicks into Account ?", type=input.bool, defval=false)
showLabels = input(title="Show Buy/Sell Labels ?", type=input.bool, defval=true)

srsi = mult* rsi(_seed ,length1)

longStop = hl2 - srsi
longStopPrev = nz(longStop[1], longStop)
longStop := (wicks ? low[1] : close[1]) > longStopPrev ? max(longStop, longStopPrev) : longStop

shortStop = hl2 + srsi
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := (wicks ? high[1] : close[1]) < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop

dir = 1
dir := nz(dir[1], dir)
dir := dir == -1 and (wicks ? high : close) > shortStopPrev ? 1 : dir == 1 and (wicks ? low : close) < longStopPrev ? -1 : dir

longColor = color.green
shortColor = color.red

plot(dir == 1 ? longStop : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor)
buySignal = dir == 1 and dir[1] == -1
plotshape(buySignal ? longStop : na, title="Long Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=longColor, transp=0)
plotshape(buySignal and showLabels ? longStop : na, title="Buy Label", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=longColor, textcolor=color.white, transp=0)

plot(dir == 1 ? na : shortStop, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor)
sellSignal = dir == -1 and dir[1] == 1
plotshape(sellSignal ? shortStop : na, title="Short Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=shortColor, transp=0)
plotshape(sellSignal and showLabels ? shortStop : na, title="Sell Label", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=shortColor, textcolor=color.white, transp=0)





if testPeriod() and buySignal
    strategy.entry("Long",strategy.long)

if testPeriod() and sellSignal
    strategy.entry("Short",strategy.short)

더 많은