멀티 타임프레임 MACD 거래 전략

저자:차오장, 날짜: 2024-01-30 17:44:36
태그:

img

전반적인 설명

이 전략은 클래식 MACD 지표, 트렌드 판단 지표, 스톱 로스 방법 및 수익 방법과 결합하여 비교적 완전한 트렌드 추적 거래 전략을 형성합니다. 암호화폐, 외화 및 주식 거래 모두에 사용할 수 있습니다.

거래 논리

  1. MACD 지표

    • FASTLENGTH 기간 EMA와 SLOWLENGTH 기간 EMA 사이의 차이는 MACD 히스토그램을 형성합니다.
    • MACDLENGTH 기간 EMA는 MACD 라인을 형성하기 위해 MACD 히스토그램을 평평하게합니다.
    • MACD 히스토그램 교차 0 축 형태 거래 신호
  2. 트렌드 필터

    • ADX: 트렌드가 있는지 판단하기 위한 평균 방향 지표
    • MA: 이동평균, MA보다 높고 낮은 가격으로 트렌드를 형성합니다.
    • SAR: 파라볼리 SAR, SAR가 가격 위와 아래로 이동하면 트렌드를 나타냅니다.
  3. 손실 중지

    • ATR 트레이일링 스톱: ATR 비율에 기초한 스톱 손실을 설정합니다.
    • SAR Stop Loss: SAR를 후속 스톱 로스로 사용한다.
  4. 이윤 을 취하라

    • ATR 고정 취득 거리: ATR에 기초한 고정 취득 거리를 설정합니다.
    • 수익을 취하는 비율: 수익을 취하는 비율을 설정
  5. 시간 정해진 출구

    • 정해진 바의 수 후에 출구를 설정할 수 있습니다

이점 분석

  1. 복수 보조판결

    • 트렌드, 지원 및 저항 판단은 잘못된 신호를 피합니다.
    • ATR/SAR 정지 손실 더 나은 위험 통제
  2. 유연 한 구성

    • 트렌드 필터를 사용 여부를 선택
    • ATR 또는 SAR 중지 손실을 선택
    • ATR 또는 표준 수익을 선택
    • 매개 변수는 설정할 수 있습니다
  3. 분산 분석 제공

    • 역사적인 정규/숨겨진 오차를 표시합니다
    • 문자 알림
  4. 최적화 하기 쉬운 방법

    • 많은 내장 설정 가능한 매개 변수
    • 다른 매개 변수 조합을 테스트하기 쉽습니다.

위험 분석

  1. 부적절 한 매개 변수 는 손실 을 증가 시킬 수 있다

    • 부적절한 ATR, SAR 매개 변수 때문에 조기 중지 손실이 발생할 수 있습니다.
    • 과도한 취득률은 영업이익을 조기 취득할 수 있습니다.
  2. 트렌드 실패 위험

    • 부적절한 경향 지표 매개 변수는 잘못된 판단을 일으킬 수 있습니다.
    • 블랙 스완 이벤트는 트렌드 실패를 일으킬 수 있습니다.
  3. 타이밍 된 탈퇴 위험

    • 일정한 시간적 출구 위험 손실

최적화 방향

  1. 더 부드러운 정류를 위해 ATR, SAR 매개 변수를 조정
  2. 트렌드 판단을 최적화하기 위해 다른 MA 기간을 테스트합니다.
  3. 수익률을 높이기 위해 테스트 조정된 취득 수익률
  4. 구성 최적화를 위해 변동성 지표를 포함

결론

이 전략은 상대적으로 완전한 암호화폐 거래 전략을 형성하기 위해 트렌드, 스톱 로스, 리프트 테이크, 풀백 식별을 포괄적으로 고려합니다. MACD 지표의 장점을 결합하고 잘못된 거래를 피하기 위해 트렌드 필터링을 추가합니다. 더 나은 위험 통제를 위해 ATR / SAR 스톱 로스를 추가합니다. 풀백 식별은 추가 참조를 제공합니다. 여러 구성 가능한 매개 변수는 쉽게 테스트 및 최적화 될 수 있습니다. 전반적으로이 전략은 암호화폐 전략 연구에 훌륭한 예로 작용 할 수 있습니다.


/*backtest
start: 2023-12-01 00:00:00
end: 2023-12-31 23:59:59
period: 1h
basePeriod: 15m
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/
// © systemalphatrader

//@version=4

strategy(title="MACD+ Strategy [SystemAlpha]", shorttitle="MACD+ Strategy [SA]", overlay=true, initial_capital=10000, currency='USD', 
   default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.04)

// == MAIN INPUT ==
// TREND FILTER
// Make input option to configure trend filter
trendFilter  = input(title="Trend Filter", defval='MA', options=['ADX', 'MA', 'SAR', 'All', 'None'])

// Translate input into trend filters
useADXFilter    = (trendFilter == 'ADX') or (trendFilter == 'All')
useSARFilter    = (trendFilter == 'SAR') or (trendFilter == 'All')
useMAFilter     = (trendFilter == 'MA') or (trendFilter == 'All')

// TRAIL STOP
// Make input option to configure trail stop
trailStop       = input(title="Trailing Stop Loss", defval='SAR', options=['ATR', 'SAR', 'None']) 

// Translate input
useATRStop      = (trailStop == 'ATR')
useSARStop      = (trailStop == 'SAR')

// TAKE PROFIT TARGET
// Make input option to configure trail stop
TPtarget       = input(title="Take Profit Type", defval='ATR', options=['ATR', 'Percent', 'None']) 

// Translate input
useATRTP        = (TPtarget == 'ATR')
usePercentTP    = (TPtarget == 'Percent')

// ALERTS and BAR COLOR
// Make input option to configure alerts
alerts          = input(title="Select Alerts to Display", defval='None', options=['Buy/Sell', 'Exit', 'Both', 'None']) 

// Translate input
showsignals     = (alerts == 'Buy/Sell') or (alerts == 'Both')
showexitsignals = (alerts == 'Exit') or (alerts == 'Both')
showBarColor    = input(true, title="Show Bar Color")

// == /MAIN INPUT  ==

/////////////////////////////////////
//*STRATEGY LOGIC                 *//
/////////////////////////////////////

// == MACD ==
fastLength = input(12, title="Fast Length")
slowlength = input(26, title="Slow Length")
MACDLength = input(9, title="MACD Length")

MACD = ema(close, fastLength) - ema(close, slowlength)
aMACD = ema(MACD, MACDLength)
delta = MACD - aMACD

// Calc breakouts
break_up        = crossover(delta, 0)
break_down      = crossunder(delta,0)
// == /MACD ==

// == DIVERGENCE ==
method      = input(title="Divergence Method", defval='Hist', options=['Hist', 'MACD'])
divtype     = input(title="Divergence Type", defval='None', options=['Regular', 'Hidden', 'Both', 'None'])
uReg        = divtype == 'Regular' or divtype == 'Both'
uHid        = divtype == 'Hidden' or divtype == 'Both'
showlabel   = input(true, title="Show Divergence Label")
v_show_last = input(500, "Plotting Lookback Bars Length")

high_src = high
low_src  = low

//  ||  Functions:
f_top_fractal(_src) =>
    _src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and 
       _src[2] > _src[0]
f_bot_fractal(_src) =>
    _src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and 
       _src[2] < _src[0]
f_fractalize(_src) =>
    f_bot_fractal__1 = f_bot_fractal(_src)
    f_top_fractal(_src) ? 1 : f_bot_fractal__1 ? -1 : 0
// === End of Functions

//  ||  Method selection
oscilator_high = float(na)
oscilator_low = float(na)
if method == 'MACD'
    oscilator_high := MACD
    oscilator_low := MACD
if method == 'Hist'
    oscilator_high := delta
    oscilator_low := delta
//
fractal_top = f_fractalize(oscilator_high) > 0 ? oscilator_high[2] : na
fractal_bot = f_fractalize(oscilator_low) < 0 ? oscilator_low[2] : na

high_prev   = valuewhen(fractal_top, oscilator_high[2], 0)[2]
high_price  = valuewhen(fractal_top, high[2], 0)[2]
low_prev    = valuewhen(fractal_bot, oscilator_low[2], 0)[2]
low_price   = valuewhen(fractal_bot, low[2], 0)[2]

regular_bearish_div = fractal_top and high[2] > high_price and oscilator_high[2] < high_prev
hidden_bearish_div  = fractal_top and high[2] < high_price and oscilator_high[2] > high_prev
regular_bullish_div = fractal_bot and low[2] < low_price and oscilator_low[2] > low_prev
hidden_bullish_div  = fractal_bot and low[2] > low_price and oscilator_low[2] < low_prev

// Plotting
plot(title='Bullish', series=fractal_top ? high[2] : na, color = regular_bearish_div and uReg or hidden_bearish_div and uHid ? color.red : na, linewidth = 2, transp=50, offset=-2, show_last = v_show_last)
plot(title='Bearish', series=fractal_bot ? low[2] : na, color = regular_bullish_div and uReg or hidden_bullish_div and uHid ? color.green : na, linewidth = 2, transp=50, offset=-2, show_last = v_show_last)

plotshape(title='Regular Bearish', series=not showlabel or not uReg ? na : regular_bearish_div ? high[2] : na, text='R', style=shape.labeldown, location=location.absolute, color=color.red, textcolor=color.white, transp=50, offset=-2, show_last = v_show_last)
plotshape(title='Hidden Bearish', series=not showlabel or not uHid ? na : hidden_bearish_div ? high[2] : na, text='H', style=shape.labeldown, location=location.absolute, color=color.red, textcolor=color.white, transp=80, offset=-2, show_last = v_show_last)
plotshape(title='Regular Bullish', series=not showlabel or not uReg ? na : regular_bullish_div ? low[2] : na, text='R', style=shape.labelup, location=location.absolute, color=color.green, textcolor=color.white, transp=50, offset=-2, show_last = v_show_last)
plotshape(title='Hidden Bullish', series=not showlabel or not uHid ? na : hidden_bullish_div ? low[2] : na, text='H', style=shape.labelup, location=location.absolute, color=color.green, textcolor=color.white, transp=80, offset=-2, show_last = v_show_last)

// == /DIVERGENCE ==

// == ENTRY ==
// == ADX FILTERING ==
// Look at ADX when it is above a user-defined key level (23 default) and this is to identify when it is trending.
// It then looks at the DMI levels.  If D+ is above D- and the ADX is sloping upwards and above the key level, it triggers a buy condition.  Opposite for short.
adxlen          = 13 //input(13, title="TF ADX Smoothing")
dilen           = 13 //input(13, title="TF DI Period")
keyLevel        = 23 //input(23, title="TF Keylevel for ADX")

// == USE BUILT-IN DMI FUNCTION TO DETERMINE ADX AND BULL/BEAR STRENGTH
[diplus, diminus, adx] = dmi(dilen, adxlen)

//Buy and Sell Filter
ADXBuyCheck     = useADXFilter? diplus > diminus : true
ADXSellCheck    = useADXFilter? diplus < diminus : true
// == /ADX FILTERING ==

// == MOVING AVERAGE FILTERING ==
// MA FILTER: Use a user-defined moving average to filter long/short if desried.
maType          = input(defval="EMA", options=["EMA", "SMA"], title = "TF MA Type")
maLength        = input(defval = 50, title = "TF MA Period", minval = 1)

// Declare function to be able to swap out EMA/SMA
ma(maType, src, length) =>
    maType == "EMA" ? ema(src, length) : sma(src, length)
maFilter        = ma(maType, close, maLength)

// Check to see if the useMaFilter check box is checked, this then inputs this conditional "maFilterCheck" variable into the strategy entry 
maFilterCheck   = if useMAFilter == true
    maFilter
else
    close
// == /MOVING AVERAGE FILTERING ==

// == PARABOLIC SAR AS AN ENTRY and EXIT OPTION ==
// SAR FILTER: The Parabolic SAR (Stop and Reverse) is a time and price technical analysis tool primarily used to identify points of potential stops and reverses.
// Go long when is located below price (BULLISH) and go short when is located above price (BEARISH).
sarStart        = 0.04 //input(0.04, "SAR Start")
sarIncrement    = 0.02 //input(0.02, "SAR Increment")
sarMaximum      = 0.5 //input(0.5, "SAR Max Value")

htclose         = close
sarOut          = useSARFilter or useSARStop ? sar(sarStart, sarIncrement, sarMaximum) : na
Trend           = sarOut < htclose ? 1 : -1

//Buy and Sell Filter
sarBuyCheck     = useSARFilter? Trend == 1: true
sarSellCheck    = useSARFilter? Trend == -1: true

// Trigger stop based on High/Low
trigResistance  = high
trigSupport     = low

// Determine if price is above resistance or below support
sarResistanceHit    = close >= sarOut and useSARStop == true
sarSupportHit       = close <= sarOut and useSARStop == true
// == /PARABOLIC SAR AS AN ENTRY and EXIT OPTION ==

// == EXITS ==
// == ATR TRAILING STOPS ==
// The ATR % stop method can be used by any type of trader because the width of the stop is determined by the percentage of average true range (ATR). 
// ATR is a measure of volatility over a specified period of time.
// Exit with ATR support/resistance is hit.
atrLookback     = 14 //input(defval=14,title="ATR Lookback Period",type=input.integer)
multiplier      = input(defval=2,title="ATR Trailing Stop Multiplier",type=input.float, step=0.1, minval=0.5, maxval=5)

// Calculate the trailing ATR
atrValue        = atr(atrLookback)
atrMultiplied   = atrValue * multiplier

// Plot the price plus or minus the ATR
atrLow          = low - atrMultiplied
atrHigh         = high + atrMultiplied

// Calculate both the low and high trailing ATRs every time. The low one never goes down, and the high one never goes up
// Set them to something to start with
trailAtrLow     = atrLow
trailAtrHigh    = atrHigh

// If the ATR Low has gone up AND it has gone above the trail, the low trailing ATR should also go up. If the ATR Low has gone up or down, but not below the trail, the ATR trail stays where it is
trailAtrLow     := na(trailAtrLow[1]) ? trailAtrLow : atrLow >= trailAtrLow[1] ? atrLow : trailAtrLow[1]
// Same for the High
trailAtrHigh    := na(trailAtrHigh[1]) ? trailAtrHigh : atrHigh <= trailAtrHigh[1] ? atrHigh : trailAtrHigh[1]

// Determine if price is above resistance or below support
atrResistanceHit    = trigResistance >= trailAtrHigh and useATRStop == true
atrSupportHit       = trigSupport <= trailAtrLow and useATRStop == true

// If price is above resistance or below support, reset the trailing ATR
trailAtrLow     := atrSupportHit ? atrLow : trailAtrLow
trailAtrHigh    := atrResistanceHit ? atrHigh : trailAtrHigh
// == /ATR TRAILING STOPS ==

// == INITIAL TP==
// ATR
tpmultiplier   = input(defval=3,title="ATR Take Profit Multiplier",type=input.float, step=0.1, minval=0.5, maxval=5)

ema = ema(close, atrLookback)
atr = rma(tr(true), atrLookback)

longATRTP     = ema + (atr * tpmultiplier)
shortATRTP    = ema - (atr * tpmultiplier)

// Determine if price is above resistance or below support
longATRTPHit    = high >= longATRTP and useATRTP == true
shortATRTPHit   = low <= shortATRTP and useATRTP == true

// STANDARD TAKE PROFIT %
// User Options to Change Inputs (%)
takePer = input(10, title='Take Profit %', type=input.float, step=0.1, minval=0.5, maxval=100) / 100

//fixed TP prices
longPercentTP   = ema * (1 + takePer)
shortPercentTP  = ema * (1 - takePer)

// Determine if price is above resistance or below support
longPercentTPHit    = high >= longPercentTP and usePercentTP == true
shortPercentTPHit   = low <= shortPercentTP and usePercentTP == true

// Set Stop and TP Values
buyStop     = trailStop == 'ATR'? trailAtrLow : trailStop == 'SAR'? sarOut : na
sellStop    = trailStop == 'ATR'? trailAtrHigh : trailStop == 'SAR'? sarOut : na
longTP      = TPtarget == 'ATR'? longATRTP : TPtarget == 'Percent'? longPercentTP : na
shortTP     = TPtarget == 'ATR'? shortATRTP : TPtarget == 'Percent'? shortPercentTP : na

// == /EXITS ==

// == PLOTTING ==
buySignal       = break_up and ADXBuyCheck and close >= maFilterCheck and sarBuyCheck 
shortSignal     = break_down and ADXSellCheck and close <= maFilterCheck and sarSellCheck
sellSignal      = atrSupportHit or sarSupportHit or longPercentTPHit or longATRTPHit
coverSignal     = atrResistanceHit or sarResistanceHit or shortPercentTPHit or shortATRTPHit

//Set BarSince counter
barsSinceLong   = 0
barsSinceShort  = 0
barsSinceLong   := nz(barsSinceLong[1])
barsSinceShort  := nz(barsSinceShort[1])

//Plot Sell and Cover Signal
plotshape(not showexitsignals and sellSignal and barsSinceLong == 1? sellSignal : na, title="Sell Long", location=location.abovebar, style=shape.diamond, size=size.auto, color=color.aqua, transp=0, show_last = v_show_last, editable = false)
plotshape(not showexitsignals and coverSignal and barsSinceShort == 1 ? coverSignal : na, title="Cover Short", location=location.belowbar, style=shape.diamond, size=size.auto, color=color.orange, transp=0, show_last = v_show_last, editable = false)
plotshape(showexitsignals and sellSignal and barsSinceLong == 1? sellSignal : na, title="Close Long Text", location=location.abovebar, style=shape.diamond, size=size.auto, color=color.aqua, transp=0, show_last = v_show_last, textcolor=color.aqua, text = "Close\nLong", editable = false)
plotshape(showexitsignals and coverSignal and barsSinceShort == 1 ? coverSignal : na, title="Cover Short Text", location=location.belowbar, style=shape.diamond, size=size.auto, color=color.orange, transp=0, show_last = v_show_last, textcolor=color.orange, text = "Cover\nShort", editable = false)

// Reset BarSince counter
if buySignal 
    barsSinceLong := 1
    barsSinceShort := 0
if shortSignal
    barsSinceShort := 1
    barsSinceLong := 0
if sellSignal
    barsSinceLong := 0
if coverSignal
    barsSinceShort := 0

// Show bar color
barcolor(showBarColor? (buySignal or shortSignal ? (shortSignal ? color.fuchsia : color.yellow) : na) : na)

// Plot Buy Alerts
plotshape(buySignal ? buySignal : na, title="Breakout Up", location=location.belowbar, style=shape.circle, size=size.auto, color=color.yellow, transp=0, editable = false)
plotshape(showsignals and buySignal, title= "Long", color=color.green, style=shape.labelup, location=location.belowbar, size=size.auto, show_last = v_show_last, text = "BUY", textcolor=color.white, editable = false)

// Plot Sell Alerts
plotshape(shortSignal ? shortSignal : na, title="Breakout Down", location=location.abovebar, style=shape.circle, size=size.auto, color=color.fuchsia, transp=0, editable = false)
plotshape(showsignals and shortSignal, title= "Short", color=color.red, style=shape.labeldown, location=location.abovebar, size=size.auto, show_last = v_show_last, text = "SELL", textcolor=color.white, editable = false)

// Plot MA Trend Filter
plot(useMAFilter? maFilter : na, title = "Trend Filter MA", color = color.green, linewidth = 2, style = plot.style_line, transp = 20, editable = true)

// Plot Initial Stop
plot(trailStop != 'None' and barsSinceLong >= 1? buyStop : na, color=#3A6CA8, title="Trail Stop Long", style = plot.style_cross, linewidth = 1, transp = 20, editable = true)
plot(trailStop != 'None' and barsSinceShort >= 1? sellStop : na, color=#3A6CA8, title="Trail Stop Short", style = plot.style_cross, linewidth = 1, transp = 20, editable = true)

// Plot Initial Target Profit
plot(TPtarget != 'None' and buySignal? longTP : na, color=color.green, title="LongTP", style = plot.style_linebr, linewidth = 1, editable = false)
plot(TPtarget != 'None' and shortSignal? shortTP : na, color=color.green, title="ShortTP", style = plot.style_linebr, linewidth = 1, editable = false)

// == /PLOTTING ==

// == ALERTS == 
// Buy and Sell Signal
alertcondition(buySignal, title='MACD Long', message='MACD {{exchange}}:{{ticker}} TF: {{interval}}. LONG (TRADE). ENTRY: {{close}}, Target Profit: {{plot("LongTP")}}, STOP LOSS: {{plot("Trail Stop Long")}}')
alertcondition(shortSignal, title='MACD Short', message='MACD {{exchange}}:{{ticker}} TF: {{interval}}. SHORT (TRADE). ENTRY: {{close}}, Target Profit: {{plot("ShortTP")}}, STOP LOSS: {{plot("Trail Stop Short")}}')
alertcondition(sellSignal, title='MACD Close Long', message='MACD {{exchange}}:{{ticker}} Close Long, Price = {{close}}')
alertcondition(coverSignal, title='MACD Cover Short', message='MACD {{exchange}}:{{ticker}} Cover Short, Price = {{close}}')
alertcondition(sellSignal or coverSignal, title="MACD Close Orders", message="{{ticker}} Close Orders")

// Initial SL and TP Hit
stophit = low <= buyStop or high >= sellStop
tphit   = low <= shortTP or high >= longTP
alertcondition(stophit, title='MACD SL Hit', message='MACD {{exchange}}:{{ticker}} Stop Loss Hit, Price = {{close}}')
alertcondition(tphit, title='MACD TP Hit', message='MACD {{exchange}}:{{ticker}} Target Profit Hit, Price = {{close}}')

//Divergence Alerts
alertcondition(regular_bullish_div, title='MACD Bullish Divergence', message='MACD Regular Bullish Divergence')
alertcondition(regular_bearish_div, title='MACD Bearish Divergence', message='MACD Regular Bearish Divergence')
alertcondition(hidden_bullish_div, title='MACD Hidden Bullish Divergence', message='MACD Hidden Bullish Divergence')
alertcondition(hidden_bearish_div, title='MACD Hidden Bearish Divergence', message='MACD Hidden Bearish Divergence')
// == /ALERTS == 

//////////////////////////
//* STRATEGY COMPONENT *//
//////////////////////////

// === BACKTEST RANGE ===
From_Year   = input(defval = 2017, title = "BACKTEST: From Year")
From_Month  = input(defval = 1, title = "BACKTEST: From Month", minval = 1, maxval = 12)
From_Day    = input(defval = 1, title = "BACKTEST: From Day", minval = 1, maxval = 31)
To_Year     = input(defval = 9999, title = "BACKTEST: To Year")
To_Month    = input(defval = 1, title = "BACKTEST: To Month", minval = 1, maxval = 12)
To_Day      = input(defval = 1, title = "BACKTEST: To Day", minval = 1, maxval = 31)
Start       = timestamp(From_Year, From_Month, From_Day, 00, 00)  // backtest start window
Finish      = timestamp(To_Year, To_Month, To_Day, 23, 59)        // backtest finish window

testPeriod() => true
// === /BACKTEST RANGE ===

// === STRATEGY ===
// Make input option to configure trade direction
tradeDirection = input(title="Trade Direction", defval="Both", options=["Long", "Short", "Both"]) 

// Translate input into trading conditions
longOK  = (tradeDirection == "Long") or (tradeDirection == "Both")
shortOK = (tradeDirection == "Short") or (tradeDirection == "Both")

// == STRATEGY ENTRIES/EXITS ==
timedExit   = input(title = "Use Timed Exit", type = input.bool, defval = true)
exitmethod  = input(title="Timed Exit Method", defval='3', options=['1', '2', '3']) 
bse         = input(10, "Bar Since Entry") // Bars to exit after entry

barsSinceEntry  = 0
barsSinceEntry  := nz(barsSinceEntry[1]) + 1

longCondition = longOK and buySignal and testPeriod()
shortcondition = shortOK and shortSignal and testPeriod()

if strategy.position_size == 0
    barsSinceEntry := 0

// === STRATEGY - LONG POSITION EXECUTION ===
if longCondition
    strategy.entry("Long", strategy.long)
    barsSinceEntry := 0

// === STRATEGY - SHORT POSITION EXECUTION ===
if shortcondition
    strategy.entry("Short", strategy.short)
    barsSinceEntry := 0

// == STRATEGY EXITS ==
if sellSignal
    strategy.close("Long")
    barsSinceEntry := 0
if coverSignal
    strategy.close("Short")
    barsSinceEntry := 0
    
// Enable Timed Exit
// Exit after specified number of bars.
if timedExit and exitmethod == '1' and barsSinceEntry > bse
    strategy.close_all()
    barsSinceEntry := 0
// Exit after specified number of bars, ONLY if position is currently profitable.
if timedExit and exitmethod == '2' and strategy.openprofit > 0 and barsSinceEntry > bse
    strategy.close_all()
    barsSinceEntry := 0
// Exit after specified number of bars, ONLY if position is currently losing.
if timedExit and exitmethod == '3' and strategy.openprofit < 0 and barsSinceEntry > bse
    strategy.close_all()
    barsSinceEntry := 0
// === /STRATEGY ===
//EOF

더 많은