
이 전략은 두 개의 지수 이동 평균 ((EMA) 의 교차를 매매 신호로 사용한다. 더 짧은 기간의 EMA가 아래에서 위쪽으로 더 긴 기간의 EMA를 통과하면 매매 신호가 발생한다. 반대로 더 짧은 기간의 EMA가 위쪽으로 더 긴 기간의 EMA를 통과하면 매매 신호가 발생한다. 이 전략은 또한 교차점이 최근 10 거래 기간의 최고 가격 또는 최저 가격인지 여부를 판단하여 트렌드의 강도를 확인한다. 교차점이 가장 높으면 가격이 배경에 녹색으로 표시되며, 최저 가격이라면 빨간색으로 표시된다.
이 전략은 지수 이동 평균을 교차하는 것을 핵심 논리로 하고, 교차점 가격의 근기간의 상대적인 위치를 결합하여 트렌드 강도를 판단한다. 전체적으로 보면, 전략 논리는 명확하고 장점은 분명하지만, 또한 일정 한계와 위험이 존재한다. 더 많은 보조 판단 지표를 도입하고, 합리적인 위험 관리 조치를 설정하고, 핵심 매개 변수를 최적화함으로써, 이 전략의 안정성과 수익성을 더욱 향상시킬 수 있다.
/*backtest
start: 2024-02-01 00:00:00
end: 2024-02-29 23:59:59
period: 1h
basePeriod: 15m
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/
// © ZenAndTheArtOfTrading
// @version=5
strategy("ema giao nhau", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Get user input
emaLength1 = input.int(title="EMA #1 Length", defval=5)
emaLength2 = input.int(title="EMA #2 Length", defval=10)
// Get MAs
ema1 = ta.ema(close, emaLength1)
ema2 = ta.ema(close, emaLength2)
// Draw MAs
plot(ema1, color=color.blue, title="EMA 1")
plot(ema2, color=color.red, title="EMA 2")
// Detect crossovers
bool crossOver = ta.crossover(ema1, ema2)
bool crossUnder = ta.crossunder(ema1, ema2)
bool cross = crossOver or crossUnder
//float crossPrice = ta.valuewhen(cross, close, 0)
float crossPrice = cross ? close : na
// Check if the crossover price is the highest price over the past 10 bars
bool highestPrice = crossOver
for i = 1 to 10
if crossPrice <= close[i]
highestPrice := false
break
// Check if the crossover price is the lowest price over the past 10 bars
bool lowestPrice = crossUnder
for i = 1 to 10
if crossPrice >= close[i]
lowestPrice := false
break
// Flag the bar if it is a high/low close
bgcolor(highestPrice ? color.new(color.green, 50) : na)
bgcolor(lowestPrice ? color.new(color.red, 50) : na)
// Display crossover price
if cross
highestEmaPrice = ema1 > ema2 ? ema1 : ema2
label myLabel = label.new(bar_index, highestEmaPrice, "CrossPrice=" + str.tostring(crossPrice), color=color.white)
if highestPrice and strategy.position_size == 0
strategy.entry(id="Buy", direction=strategy.long)
if lowestPrice and strategy.position_size == 0
strategy.entry(id="Sell", direction=strategy.short)
// Exit trades when short-term EMA is breached
if strategy.position_size > 0 and crossUnder
strategy.close("Buy")
if strategy.position_size < 0 and crossOver
strategy.close("Sell")