MACD 지표 브레이크아웃 거래 전략

저자:차오장, 날짜: 2023-09-12 15:32:12
태그:

이 전략은 진입 및 출구 결정을 위해 MACD 크로스오버 신호를 거래합니다. MACD는 빠르고 느린 EMA로 구성되며, 0 이상의 MACD 라인의 크로스오버는 무역 신호를 생성합니다. 이것은 전형적인 트렌드 다음 양적 전략입니다.

전략 논리:

  1. 빠른 EMA와 느린 EMA를 계산하면 그 차이는 MACD 라인을 형성합니다.

  2. 다른 EMA를 사용하여 신호선을 도출하여 MACD 라인을 매끄럽게하십시오.

  3. MACD가 신호 위를 넘어가면 롱, 신호 아래를 넘어가면 쇼트

  4. 위험 관리로 수익을 취하고 수익을 취하는 비율을 설정합니다.

장점:

  1. MACD는 단일 EMA보다 더 명확한 트렌드 식별을 위해 개선됩니다.

  2. 브레이크아웃 거래는 전환점을 적시에 파악합니다.

  3. 스톱 로스/프로프트 메커니즘은 무역 리스크를 통제하는 데 도움이 됩니다.

위험성:

  1. MACD 0선 근처에 더 많은 가짜 파업이 있습니다.

  2. 다른 거래 도구에 필요한 매개 변수 조정

  3. 트렌드 트레이딩은 이벤트 리스크에 노출되어 있어 정지해야 합니다.

요약하자면, 이 전략은 MACD와 신호 라인 크로스오버에 기반하여 거래된다. MACD의 강점은 성과를 이롭게하지만 거짓 브레이크오웃 위험은 남아있다. 장기적으로 안정적인 이익을 위해 신중한 위험 통제가 여전히 필요합니다.


/*backtest
start: 2023-01-01 00:00:00
end: 2023-09-11 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3 
strategy("uray MACD", overlay=false, pyramiding = 0, calc_on_every_tick=true, precision=2, currency="USD", default_qty_value=10, default_qty_type=strategy.cash,initial_capital=100,commission_type=strategy.commission.percent,commission_value=0.1) 

// === INPUT BACKTEST RANGE ===
FromMonth = input(defval = 6, title = "From Month", minval = 1, maxval = 12)
FromDay   = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear  = input(defval = 2018, title = "From Year", minval = 2017)
ToMonth   = input(defval = 6, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 2020, title = "To Year", minval = 2017)

// === FUNCTION EXAMPLE ===
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  // backtest start window
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)        // backtest finish window
inTimeframe()  => true


isPosLong    = strategy.position_size > 0
isPosShort   = strategy.position_size < 0
isNoMarginPos= strategy.position_size == 0

fastLength    = input(12, minval = 1, title = "MACD fast")
slowlength    = input(26, minval = 1, title = "MACD slow")
MACDLength    = input( 9, minval = 1, title = "MACD length")
stopLoss      = input( 10, minval = 1, title = "Stop Loss (price %)", type=float)
takeProfit    = input( 50, minval = 1, title = "Take Profit (price %)", type=float)
src           = close   // Source of Calculations (Close of Bar)

MACD  = ta.ema(src, fastLength) - ta.ema(src, slowlength)
aMACD = ta.ema(MACD, MACDLength)
delta = MACD - aMACD
stopLossValue      = close*(stopLoss/100)/syminfo.mintick
takeProfitValue    = close*(takeProfit/100)/syminfo.mintick
switchLongTrigger  = ta.crossover(delta, 0)
switchShortTrigger = ta.crossunder(delta, 0)
closeLongTrigger   = ta.crossunder(delta, 0)
closeShortTrigger  = ta.crossover(delta, 0)
entryLongTrigger   = ta.crossover(delta, 0)
entryShortTrigger  = ta.crossunder(delta, 0)

// if inTimeframe()
//     if isNoMarginPos
//         if entryLongTrigger
//             strategy.entry("Long", strategy.long,  comment="Entry Long")
//             strategy.exit("Stop (long SL/TP)", loss=stopLossValue, profit=takeProfitValue)   
//         if entryShortTrigger
//             strategy.entry("Short", strategy.short, comment="Entry Short")  
//             strategy.exit("Stop (short SL/TP)", loss=stopLossValue, profit=takeProfitValue)   
//     if isPosShort
//         if switchLongTrigger
//             strategy.close_all()    
//             strategy.entry("Long", strategy.long, comment="switch Long")
//             strategy.exit("Stop (long SL/TP)", loss=stopLossValue, profit=takeProfitValue)   
//         if closeLongTrigger
//             strategy.close_all()    
//     if isPosLong 
//         if switchShortTrigger
//             strategy.close_all()   
//             strategy.entry("Short", strategy.short, comment="switch Short")
//             strategy.exit("Stop (short SL/TP)", loss=stopLossValue, profit=takeProfitValue) 
//         if closeShortTrigger
//             strategy.close_all()   

if inTimeframe()
    strategy.entry("Long", strategy.long,  comment="Entry Long", when=entryLongTrigger)
    strategy.close("Long", when=entryShortTrigger)
    strategy.entry("Short", strategy.short,  comment="Entry Short", when=entryShortTrigger)
    strategy.close("Short", when=entryLongTrigger)
    strategy.exit("Stop (short SL/TP)", loss=stopLossValue, profit=takeProfitValue, when=entryShortTrigger) 
    strategy.exit("Stop (long SL/TP)", loss=stopLossValue, profit=takeProfitValue, when=entryLongTrigger) 




더 많은