
Chiến lược này sử dụng sự giao thoa của hai chỉ số trung bình di chuyển ((EMA) vào ngày 20 và 55 để đánh giá tín hiệu giao dịch. Khi EMA ngắn hạn đi qua EMA dài hạn, tín hiệu mua được phát ra, ngược lại, tín hiệu bán được phát ra. Chiến lược cũng giới thiệu giao dịch đòn bẩy, mở rộng lợi nhuận bằng cách sử dụng đòn bẩy, đồng thời mở rộng rủi ro. Ngoài ra, chiến lược cũng thêm các điều kiện giới hạn, chỉ sau khi hai đường trung bình giao thoa, khi giá chạm vào đường trung bình ngắn hạn, để giảm nguy cơ tín hiệu giả. Cuối cùng, người dùng cũng có thể chọn sử dụng đường trung bình di chuyển đơn giản ((MA) thay vì EMA.
Chiến lược này tăng lợi nhuận bằng cách kết hợp giao dịch chéo và đòn bẩy, đồng thời nắm bắt xu hướng thị trường. Tuy nhiên, đòn bẩy cũng mang lại rủi ro cao và cần thận trọng. Ngoài ra, chiến lược này còn có không gian tối ưu hóa, có thể nâng cao hiệu suất chiến lược bằng cách giới thiệu nhiều chỉ số, tham số điều chỉnh động, v.v..
/*backtest
start: 2024-03-01 00:00:00
end: 2024-03-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("EMA Crossover Strategy with Leverage, Conditional Entry, and MA Option", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Inputs for backtesting period
startDate = input(defval=timestamp("2023-01-01"), title="Start Date")
endDate = input(defval=timestamp("2024-04-028"), title="End Date")
// Input for leverage multiplier
leverage = input.float(3.0, title="Leverage Multiplier", minval=1.0, maxval=10.0, step=0.1)
// Input for choosing between EMA and MA
useEMA = input.bool(true, title="Use EMA (true) or MA (false)?")
// Input source and lengths for MAs
src = close
ema1_length = input.int(20, title='EMA/MA-1 Length')
ema2_length = input.int(55, title='EMA/MA-2 Length')
// Calculate the MAs based on user selection
pema1 = useEMA ? ta.ema(src, ema1_length) : ta.sma(src, ema1_length)
pema2 = useEMA ? ta.ema(src, ema2_length) : ta.sma(src, ema2_length)
// Tracking the crossover condition for strategy entry
crossedAbove = ta.crossover(pema1, pema2)
// Define a variable to track if a valid entry condition has been met
var bool readyToEnter = false
// Check for MA crossover and update readyToEnter
if (crossedAbove)
readyToEnter := true
// Entry condition: Enter when price touches MA-1 after the crossover // and (low <= pema1 and high >= pema1)
entryCondition = readyToEnter
// Reset readyToEnter after entry
if (entryCondition)
readyToEnter := false
// Exit condition: Price crosses under MA-1
exitCondition = ta.crossunder(pema1, pema2)
// Check if the current bar's time is within the specified period
inBacktestPeriod = true
// Execute trade logic only within the specified date range and apply leverage to position sizing
if (inBacktestPeriod)
if (entryCondition)
strategy.entry("Long", strategy.long, qty=strategy.equity * leverage / close)
if (exitCondition)
strategy.close("Long")
// Plotting the MAs for visual reference
ema1_color = pema1 > pema2 ? color.red : color.green
ema2_color = pema1 > pema2 ? color.red : color.green
plot(pema1, color=ema1_color, style=plot.style_line, linewidth=1, title='EMA/MA-1')
plot(pema2, color=ema2_color, style=plot.style_line, linewidth=1, title='EMA/MA-2')