평행선 교차 전략은 이동 평행선을 기반으로 한 일반적으로 사용되는 거래 전략이다. 이 전략은 빠른 이동 평행선과 느린 이동 평행선의 교차를 구매 및 판매 신호로 사용합니다. 빠른 평행선이 아래에서 느린 평행선을 통과하면 구매 신호로 간주되며, 빠른 평행선이 위에서 아래로 느린 평행선을 통과하면 판매 신호로 간주됩니다. 이 전략은 50 일평균선을 빠른 평행선으로 사용하고 200 일평균선을 느린 평행선으로 간주합니다.
이 전략의 핵심 논리는 평평선 이론에 기반한다. 이동 평평선은 가격 변동을 효과적으로 평형화하여 가격 트렌드를 반영한다. 빠른 평평선은 가격 변화에 더 민감하여 트렌드의 전환점을 포착한다. 느린 평평선은 가격 변화에 덜 민감하여 단기간의 변동을 필터링한다.
구체적으로 보면, 이 전략은 먼저 50일 평균선과 200일 평균선을 정의한다. 그리고는 다단 입문 조건을 빠른 평균선 위에 느린 평균선으로, 빈 입문 조건을 빠른 평균선 아래에 느린 평균선으로 설정한다. 중복 거래를 피하기 위해, 전략은 isEntry와 isExit 표시 지트를 사용한다. 입문 조건이 충족되면 isEntry를 True로 설정하고, 출구 조건이 충족되면 isExit를 true로 설정한다.
또한, 전략은 스톱 스톱 손실 지점을 설정한다. 사용자는 스톱 스톱 손실의 거리를 입력하여 설정할 수 있다. 스톱 스톱 및 스톱 손실 가격은 출입 가격의 퍼센트 변화에 따라 계산된다. 포지션이 0 이상일 때, 설정된 스톱 스톱 및 스톱 손실 비율에 따라 스톱 스톱 손실을 수행한다. 포지션이 0 미만일 때, 설정된 빈 헤드 스톱 손실 비율에 따라 스톱 스톱 손실을 수행한다.
이 전략은 다음과 같은 장점을 가지고 있습니다.
조작이 간단하고 구현하기 쉽다. 단지 평선 교차에 의존하여 거래를 할 수 있으며, 거래 경험이 없는 초보자에게는 매우 적합하다.
회수 제어 가능, 위험 관리 장치가 있다. 이동평균선은 단기 가격 변동을 효과적으로 필터링하여 중매를 피할 수 있다.
사용자 정의 가능한 변수, 적응력이 강하다. 사용자는 평균선 변수와 스톱 스톱 손실 기준을 스스로 설정하고, 전략을 최적화 할 수 있다.
시각적으로 명확하게 보여준다. 전략은 중요한 평균선, 입점, 스톱로스 포인트를 차트에 직접 그려서 눈에 띄게 보여준다.
확장성이 강하다. 이 전략의 프레임워크는 완전하며, 중요한 거래 신호를 수정하거나 지표를 추가하는 것만으로 전략을 개선할 수 있다.
이 전략에는 몇 가지 위험도 있습니다.
시장의 갑작스러운 사건으로 인해 엄청난 거래상황이 발생하여 손실을 막을 수 없습니다. 급속한 평균선은 가격 변화에 민감하고 갑작스러운 사건에 효과적으로 대응할 수 없습니다.
위기상황에 쉽게 휘둘릴 수 있다. 위기상황이 장기간 지속되면 손실이 반복된다.
거래비용을 고려하지 않는다. 실제 거래의 수수료와 슬라이드 손실은 수익에 심각한 영향을 미칩니다.
재검토 데이터 적합성 위험. 실전 상황은 복잡하고 변동적이며, 재검토 결과는 실전 성능을 나타내지 않는다.
대응방법:
더 느슨한 손해 기준을 설정할 수 있고, 추가적인 돌파 손해 기준을 추가할 수도 있다.
적절한 경우 평균선 거리를 넓히거나 거래 빈도를 낮추거나 다른 지표로 신호를 필터링 할 수 있습니다.
실제 거래비용을 고려하여 좀 더 여유있는 휴식 공간을 설정해야 합니다.
시장 환경의 변화를 충분히 고려하고, 매개 변수를 최적화하고, 단계적으로 적합성을 줄여야 한다.
이 전략은 다음과 같은 부분에서 최적화될 수 있습니다.
다양한 변수 조합을 테스트하여 최적의 변수를 찾습니다. 빠른 속도로 평균을 테스트 할 수 있는 날 수, 변수 조합 등이 있습니다.
다른 지표에 필터링을 추가하여 충격 상황에서 잘못된 거래를 피하십시오. MACD, KD 등 지표.
스톱 스톱 손실 전략을 최적화하여 더 효율적인 위험 관리를 구현하십시오. 예를 들어, 스톱을 추적하고, 스톱을 꽂습니다.
포지션 규모를 늘리고, 레버리지를 활용하여 확대하여 수익을 창출할 수 있다. 하지만 위험을 통제해야 한다.
실체 거래 비용을 고려하고, 재측정 파라미터를 조정하여 최적화하여 전략 파라미터를 실제 전투에 더 적합하게 만든다.
통계적 방법과 결합하여 매개 변수의 안정성을 평가하여 데이터 일치 위험을 줄이고 안정성을 향상시킵니다.
요약하자면, 이 평선 교차 전략의 전체적인 아이디어는 명확하고, 구현하기 간단하며, 양적 거래의 입문 전략에 적합하다. 그러나 이 전략은 또한 위험과 결함이 있으며, 매개 변수와 필터를 세밀하게 최적화하고, 실 디스크 거래 위험을 제어하는 데 주의를 기울여야 안정적인 수익을 얻을 수 있다. 이 전략 프레임 워크는 매우 확장성이 있으며, 사용자는 이를 기반으로 혁신과 최적화를 수행하여 자신의 스타일에 더 적합한 거래 전략을 개발할 수 있다.
/*backtest
start: 2023-10-02 00:00:00
end: 2023-10-09 00:00:00
period: 3m
basePeriod: 1m
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/
// © gjfsdrtytru
//@version=4
strategy("Backtest Engine", "Backtest", overlay=true, commission_type=strategy.commission.percent, commission_value=0.07, initial_capital = 1000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, currency = currency.USD)
// Start code here...
fastMA = sma(close,50)
slowMA = sma(close,200)
plot(fastMA, "Fast MA", color.blue)
plot(slowMA, "Slow MA", color.red)
// Long Enrty/Exit
longCondition = crossover(fastMA,slowMA)
closeLong = crossover(slowMA,fastMA)
// Short Enrty/Exit
shortCondition = crossover(slowMA,fastMA)
closeShort = crossover(fastMA,slowMA)
// Bot web-link alert - {{strategy.order.comment}}
botLONG = "ENTRY LONG ALERT"
botCLOSELONG = "CLOSE LONG ALERT"
botSHORT = "ENTRY SHORT ALERT"
botCLOSESHORT = "CLOSE SHORT ALERT"
//////////////////////////////////////////////////////////////////
//////////////////////// BACKTEST ENGINE \\\\\\\\\\\\\\\\\\\\\\\\\
/////////////////// [NO USER INPUT REQUIRED] /////////////////////
//////////////////////////////////////////////////////////////////
// Time period
testStartYear = input(2020, "Backtest Start Year")
testStartMonth = input(5, "Backtest Start Month")
testStartDay = input(11, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
periodLength = input(3650, "Backtest Period (days)", minval=0,tooltip="Days until strategy ends") * 86400000 // convert days into UNIX time
testPeriodStop = testPeriodStart + periodLength
testPeriod() =>
time >= testPeriodStart and time <= testPeriodStop ? true : false
// Convert Take profit and Stop loss to percentage
longTP = input(title="Long Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=0) * 0.01 // Set levels with input options
longSL = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=0) * 0.01 // Set levels with input options
shortTP = input(title="Short Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=0) * 0.01 // Set levels with input options
shortSL = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=0) * 0.01 // Set levels with input options
// 0% TP/SL = OFF (a value of 0 turns off TP/SL feature)
longProfitPerc = longTP == 0 ? 1000 : longTP
longStopPerc = longSL == 0 ? 1 : longSL
shortProfitPerc = shortTP == 0 ? 1 : shortTP
shortStopPerc = shortSL == 0 ? 1000 : shortSL
// Determine TP/SL price based on percentage given
longProfitPrice = strategy.position_avg_price * (1 + longProfitPerc)
longStopPrice = strategy.position_avg_price * (1 - longStopPerc)
shortProfitPrice = strategy.position_avg_price * (1 - shortProfitPerc)
shortStopPrice = strategy.position_avg_price * (1 + shortStopPerc)
// Anti-overlap
isEntry_Long = false
isEntry_Long := nz(isEntry_Long[1], false)
isExit_Long = false
isExit_Long := nz(isExit_Long[1], false)
isEntry_Short = false
isEntry_Short := nz(isEntry_Short[1], false)
isExit_Short = false
isExit_Short := nz(isExit_Short[1], false)
entryLong = not isEntry_Long and longCondition
exitLong = not isExit_Long and closeLong
entryShort = not isEntry_Short and shortCondition
exitShort = not isExit_Short and closeShort
if (entryLong)
isEntry_Long := true
isExit_Long := false
if (exitLong)
isEntry_Long := false
isExit_Long := true
if (entryShort)
isEntry_Short := true
isExit_Short := false
if (exitShort)
isEntry_Short := false
isExit_Short := true
// Order Execution
if testPeriod()
if entryLong
strategy.entry(id="Long", long=true, when = entryLong, comment=botLONG) // {{strategy.order.comment}}
if entryShort
strategy.entry(id="Short", long=false, when = entryShort, comment=botSHORT) // {{strategy.order.comment}}
// TP/SL Execution
if (strategy.position_size > 0)
strategy.exit(id="Long SL/TP", from_entry="Long", limit=longProfitPrice, stop=longStopPrice)
strategy.close(id="Long", when=exitLong, comment=botCLOSELONG) // {{strategy.order.comment}}
if (strategy.position_size < 0)
strategy.exit(id="Short TP/SL", from_entry="Short", limit=shortProfitPrice, stop=shortStopPrice)
strategy.close(id="Short", when=exitShort, comment=botCLOSESHORT) // {{strategy.order.comment}}
// Draw Entry, TP and SL Levels for Long Positions
plot(strategy.position_size > 0 ? longTP == 0 ? na : longProfitPrice : na, style=plot.style_linebr, color=color.green, title="Long TP")
plot(strategy.position_size > 0 ? strategy.position_avg_price : na, style=plot.style_linebr, color=color.blue, title="Long Entry")
plot(strategy.position_size > 0 ? longSL == 0 ? na : longStopPrice : na, style=plot.style_linebr, color=color.red, title="Long SL")
// Draw Entry, TP and SL Levels for Short Positions
plot(strategy.position_size < 0 ? shortTP == 0 ? na : shortProfitPrice : na, style=plot.style_linebr, color=color.green, title="Short TP")
plot(strategy.position_size < 0 ? strategy.position_avg_price : na, style=plot.style_linebr, color=color.blue, title="Short Entry")
plot(strategy.position_size < 0 ? shortSL == 0 ? na : shortStopPrice : na, style=plot.style_linebr, color=color.red, title="Short SL")