MACD와 RSI를 기반으로 한 롱 앤 숏 양방향 트레이딩 전략


생성 날짜: 2023-10-09 15:33:17 마지막으로 수정됨: 2023-10-09 15:33:17
복사: 0 클릭수: 701
avatar of ChaoZhang ChaoZhang
1
집중하다
1617
수행원

개요

이 전략은 MACD와 RSI를 결합하여 트렌드 방향이 불분명할 때 동시에 과잉 수익을 얻기 위해 더 많은 코스피 거래를 수행합니다.

전략 원칙

  1. 계산 빠른 EMA ((12 일선) 과 느린 EMA ((26 일선)
  2. MACD 수렴 이차를 계산하기 (고속 EMA 빼고 느린 EMA)
  3. MACD의 9일 이동 평균을 신호 라인 signal로 계산한다.
  4. 14일 RSI 계산
  5. MACD <-0.1, RSI <27 그리고 빠른 EMA가 느린 EMA보다 낮을 때 더 많은 일을하십시오.
  6. MACD>0.125, RSI>81 그리고 빠른 EMA가 느린 EMA보다 높을 때, 공백
  7. 정지, 중지, 이동 중단을 설정하여 포지션을 관리합니다.

우위 분석

  1. 동시 다중 거래는 추세에서 벗어난 상황에서 초과 수익을 얻을 수 있습니다.
  2. 트렌드 방향 지표인 EMA와 반전 지표인 RSI를 결합하여 신호 품질을 향상시킬 수 있습니다.
  3. 이동적 손실을 사용하여 수익을 고정하여 손실 위험을 효과적으로 제어할 수 있습니다.

위험 분석

  1. 양방향 거래는 보증금 요구 사항을 뒷받침하기 위해 더 많은 자금이 필요합니다.
  2. 시장이 급격하게 변할 경우, 동시에 과잉 공백을 막을 수 있습니다.
  3. 잘못된 매개 변수 설정으로 인해 거래가 너무 자주 발생할 수 있습니다.

위험 해결 방법:

  1. 충분한 자금 지원, 포지션 규모 통제
  2. 정지 거리를 합리적으로 설정하여 너무 밀집된 정지를 피하십시오.
  3. 최적화 매개 변수, 거래 빈도 감소

최적화 방향

  1. 진입 시기를 최적화하기 위해 변동성 지표와 결합할 수 있습니다.
  2. 다양한 변수 조합을 테스트하여 최적의 변수를 찾을 수 있습니다.
  3. 마켓 조건에 따라 최적화된 손실을 막는 전략, 예를 들어 후속 손실을 막는 방법
  4. 기계 학습 알고리즘과 결합 가능한 자동 최적화 파라미트

요약하다

이 전략은 MACD와 RSI의 조합을 통해 양방향 거래를 실현한다. 이동적 손실을 사용하여 수익을 고정하고, 비 트렌드 상황에서 초과 수익을 얻을 수 있다. 이 전략은 더 안정적인 초과 수익을 얻기 위해 파라미터 설정을 더 최적화하고, 상쇄 전략을 추가 할 수 있다.

전략 소스 코드
/*backtest
start: 2023-09-08 00:00:00
end: 2023-10-08 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3
// Revision:        290
// Author:          @Hugo_Moriceau
//study("Moriceau_Crypto_strategies_Long_short_indicator_thesis",overlay=true)

// Pyramide 10 order size 100, every tick

strategy("Moriceau_Crypto_strategies_Long_short_indicator",overlay=true)

// === GENERAL INPUTS ===

fast = 12, slow = 26
fastMA = ema(close, fast)
slowMA = ema(close, slow)

macd = fastMA - slowMA
signal = sma(macd, 9)
rsi = rsi(close,14)

dataB = macd < -0.1  and rsi<27 and fastMA < slowMA
// data1 = macd > 0.125  and rsi>81 and fastMA> slowMA
dataS = macd > 0.125 and rsi > 81 and fastMA > slowMA

tradeInvert     = input(defval = false, title = "Invert Trade Direction?")

// === LOGIC ===

// is fast ma above slow ma?
Achat = macd < -0.1  and rsi < 27 and fastMA < slowMA ? true : false
vente = macd > 0.125 and rsi > 81 and fastMA > slowMA ? true : false

// are we inverting our trade direction?
tradeDirection = vente ? Achat ? false : true : Achat ? true : false

// === Plot Setting ===

plot(fastMA,color=red)
plot(slowMA,color=blue)
barcolor(color=iff(fastMA > slowMA, yellow, na))
barcolor(color=iff(fastMA < slowMA, black, na))
//barcolor(color=iff(macd > 0.12*close , fuchsia, na))
//barcolor(color=iff(macd < -0.1*close , lime, na))
plotchar(dataB, char='B',color=black,size = size.auto,location = location.belowbar,transp= 0)  
plotchar(dataS, char='S',color=black,size = size.auto,location = location.abovebar,transp= 0)

//fast = plot(maFast, title = "FastMA", color = yellow, linewidth = 2, style = line, transp = 50)
//slow = plot(maSlow, title = "SlowMA", color = black, linewidth = 2, style = line, transp = 50)

// === BACKTEST RANGE ===
FromMonth = input(defval = 05, title = "From Month", minval = 1)
FromDay   = input(defval = 23, title = "From Day", minval = 1)
FromYear  = input(defval = 2021, title = "From Year", minval = 2017)
ToMonth   = input(defval = 5, title = "To Month", minval = 1)
ToDay     = input(defval = 25, title = "To Day", minval = 1)
ToYear    = input(defval = 2021, title = "To Year", minval = 2017)


// === STRATEGY RELATED INPUTS ===+
// the risk management inputs
inpTakeProfit   = input(defval = 2500, title = "Take Profit", minval = 28)
inpStopLoss     = input(defval = 600, title = "Stop Loss", minval = 15)
inpTrailStop    = input(defval = 300, title = "Trailing Stop Loss", minval = 5)
inpTrailOffset  = input(defval = 50, title = "Trailing Stop Loss Offset", minval = 1)

// === RISK MANAGEMENT VALUE PREP ===

// if an input is less than 1, assuming not wanted so we assign 'na' value to disable it.

useTakeProfit   = inpTakeProfit  >= 1 ? inpTakeProfit  : na
useStopLoss     = inpStopLoss    >= 1 ? inpStopLoss    : na
useTrailStop    = inpTrailStop   >= 1 ? inpTrailStop   : na
useTrailOffset  = inpTrailOffset >= 1 ? inpTrailOffset : na


// === STRATEGY - LONG POSITION EXECUTION ===

enterLong() => not tradeDirection[1] and tradeDirection 
exitLong() => tradeDirection[1] and not tradeDirection
strategy.entry(id = "Achat", long = true, when = enterLong()) // use function or simple condition to decide when to get in
strategy.close(id = "TP 50% Sell", when = exitLong()) // ...and when to get out

// === STRATEGY - SHORT POSITION EXECUTION ===

enterShort() => tradeDirection[1] and not tradeDirection
exitShort() => not tradeDirection[1] and tradeDirection
strategy.entry(id = "Vente", long = false, when = enterShort())
strategy.close(id = "Vente", when = exitShort())

// === STRATEGY RISK MANAGEMENT EXECUTION ===

// finally, make use of all the earlier values we got prepped
strategy.exit("Vente", from_entry = "Vente", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)
strategy.exit("Short", from_entry = "Achat", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)