MACD와 RSI를 기반으로 한 긴 짧은 거래 전략

저자:차오장, 날짜: 2023-10-09 15:33:17
태그:

전반적인 설명

이 전략은 MACD와 RSI 지표를 결합하여 불분명한 트렌드 상황에서 초과 수익을 얻기 위해 동시에 긴 거래와 짧은 거래를 구현합니다.

전략 논리

  1. 빠른 EMA (12일) 및 느린 EMA (26일) 를 계산합니다.
  2. MACD 컨버전스 디버전스 계산 (빠른 EMA 빼기 느린 EMA)
  3. MACD의 9일 이동평균을 신호선으로 계산합니다.
  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. 트래일링 스톱 로스 (trailing stop loss) 와 같은 시장 조건에 기반한 스톱 로스 전략을 최적화합니다.
  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)

더 많은