이중 이동 평균 크로스오버 추적 전략

저자:차오장, 날짜: 2023-09-11 15:27:45
태그:

이 전략은 트레이킹 거래를 위해 시장 트렌드 방향을 결정하기 위해 두 그룹의 이동 평균 SMA와 EMA 사이의 교차를 계산합니다.

특히, 한 쌍의 빠른 이동 평균과 한 쌍의 느린 이동 평균을 사용합니다. 빠른 선이 느린 선 위에 넘어가면 길고 하향적 크로스오버에서 짧습니다. 가격이 느린 선 아래에 다시 떨어지거나 빠른 선 위에 올랐을 때 출입이 발생합니다. MA 길이의 사용자 정의, 바드 폐쇄 등은 매개 변수 최적화를 허용합니다.

이 이중 MA 전략의 장점은 두 개의 동적 MA를 기반으로 한 간단하고 명확한 규칙입니다. EMA를 사용하면 반전을 포착하는 데 더 민감성을 제공합니다. 그러나 범위 제한 시장에서도 쉽게 발생합니다.

일반적으로 이중 MA 크로스오버 추적 전략은 동력 방향으로 거래하는 트렌딩 시장에 적합합니다. 그러나 적절한 매개 변수 조정, 엄격한 스톱 로스 및 포지션 사이징은이 전략의 장기 안정성에 중요합니다.


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

//@version=3
// strategy("Moving Average Strategy of BiznesFilosof", shorttitle="MAS of BiznesFilosof", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=20, commission_type=strategy.commission.percent, commission_value=0.15, pyramiding=0)

//Period
startY = input(title="Start Year", defval = 2011)
startM = input(title="Start Month", defval = 1, minval = 1, maxval = 12)
startD = input(title="Start Day", defval = 1, minval = 1, maxval = 31)
finishY = input(title="Finish Year", defval = 2050)
finishM = input(title="Finish Month", defval = 12, minval = 1, maxval = 12)
finishD = input(title="Finish Day", defval = 31, minval = 1, maxval = 31)
//finish = input(2019, 02, 28, 00, 00)
timestart = timestamp(startY, startM, startD, 00, 00)
timefinish = timestamp(finishY, finishM, finishD, 23, 59)
window = time >= timestart and time <= timefinish ? true : false // Lenghth strategy

lma1 = input(title="Length MA1", defval = 21, minval=1)
exponential1 = input(false, title="exponential")
lma2 = input(title="Length MA2", defval = 1, minval=1)
exponential2 = input(false, title="exponential")
lbars = input(title="Length bars close", defval = 0, minval=0)

ma1 = exponential1 ? ema(close, lma1) : sma(close, lma1)
ma2 = exponential2 ? ema(close, lma2) : sma(close, lma2)

//source = close
source = ma2

//open
strategy.entry("LongEntryID", strategy.long, comment="LONG", when = crossover(ma2, ma1) and window)
strategy.entry("ShortEntryID", strategy.short, comment="SHORT", when = crossunder(ma2, ma1) and window)

if crossunder(source, ma1) and strategy.position_size > 0
    strategy.close_all()
if crossunder(ma2[lbars], ma1[lbars]) and strategy.position_size > 0 and lbars != 0
    strategy.close_all()    
if crossover(source, ma1) and strategy.position_size < 0
    strategy.close_all()
if crossover(ma2[lbars], ma1[lbars]) and strategy.position_size < 0 and lbars != 0
    strategy.close_all()      

src = close
src1 = high
src2 = low
maH = exponential1 ? ema(src1, lma1) : sma(src1, lma1)
maL = exponential1 ? ema(src2, lma1) : sma(src2, lma1)
maColor = src>maH ? green : src<maL ? red : blue

plot(ma1, title="MA1", color=maColor, linewidth=2, style=line)
plot(ma2, title="MA2", color=gray, linewidth=1, style=line)



더 많은