
Chiến lược giao dịch này kết hợp ba chỉ số kỹ thuật: chỉ số di chuyển trung bình (EMA), chỉ số tương đối mạnh (RSI) và chỉ số di chuyển trung bình và phân tán (MACD) để tạo ra tín hiệu mua và bán khi giá đáp ứng một số điều kiện nhất định bằng cách phân tích mối quan hệ chéo và giá trị của chúng. Đồng thời, chiến lược này cũng đặt các điểm dừng động và dừng để quản lý rủi ro giao dịch.
Chiến lược này kết hợp nhiều chỉ số kỹ thuật như EMA, RSI và MACD để tạo thành một hệ thống giao dịch hoàn chỉnh. Trong các trường hợp có xu hướng, chiến lược có thể nắm bắt được xu hướng một cách hiệu quả và kiểm soát rủi ro bằng cách dừng lỗ động. Tuy nhiên, trong các trường hợp bất ổn, giao dịch thường xuyên có thể ảnh hưởng đến lợi nhuận.
/*backtest
start: 2023-06-08 00:00:00
end: 2024-06-13 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("[BUY/SELL]EMA RSI MACD with TP and SL", overlay=true)
// Input parameters
ema1Length = input.int(9, title="EMA 1 Length")
ema2Length = input.int(21, title="EMA 2 Length")
ema3Length = input.int(34, title="EMA 3 Length")
rsiLength = input.int(14, title="RSI Length")
rsiThreshold = input.int(50, title="RSI Threshold")
macdFastLength = input.int(12, title="MACD Fast Length")
macdSlowLength = input.int(26, title="MACD Slow Length")
macdSignalSmoothing = input.int(9, title="MACD Signal Smoothing")
tpPips = input.int(10, title="Take Profit (pips)")
slPips = input.int(10, title="Stop Loss (pips)")
// HLCC4 calculation
hlcc4_custom = (high + low + close + close) / 4
// Calculate EMA and RSI based on HLCC4
ema1 = ta.ema(hlcc4_custom, ema1Length)
ema2 = ta.ema(hlcc4_custom, ema2Length)
ema3 = ta.ema(hlcc4_custom, ema3Length)
rsi = ta.rsi(hlcc4_custom, rsiLength)
// Calculate MACD Histogram
[a, b, histogram] = ta.macd(hlcc4_custom, macdFastLength, macdSlowLength, macdSignalSmoothing)
// EMA1 and EMA2 crossover conditions
emaCrossUp = ta.crossover(ema1, ema2)
emaCrossDown = ta.crossunder(ema1, ema2)
// BUY signal conditions
buySignal = emaCrossUp and hlcc4_custom > ema3 and rsi > rsiThreshold and close > open and histogram > 0
// SELL signal conditions
sellSignal = emaCrossDown and hlcc4_custom < ema3 and rsi < rsiThreshold and close < open and histogram < 0
var float entryPrice = na
var float tpPrice = na
var float slPrice = na
// Check if there is an open position and a contrary signal appears, then close all old orders first
if strategy.opentrades > 0
if sellSignal and strategy.position_size > 0
strategy.close("Buy", comment="Close Buy Order")
if buySignal and strategy.position_size < 0
strategy.close("Sell", comment="Close Sell Order")
// Place a BUY order when there is a BUY signal and set TP and SL based on pips
if buySignal and strategy.position_size == 0
entryPrice := close
tpPrice := entryPrice + tpPips * syminfo.mintick
slPrice := entryPrice - slPips * syminfo.mintick
strategy.entry("Buy", strategy.long)
strategy.exit("Take Profit/Stop Loss", "Buy", limit=tpPrice, stop=slPrice)
// Place a SELL order when there is a SELL signal and set TP and SL based on pips
if sellSignal and strategy.position_size == 0
entryPrice := close
tpPrice := entryPrice - tpPips * syminfo.mintick
slPrice := entryPrice + slPips * syminfo.mintick
strategy.entry("Sell", strategy.short)
strategy.exit("Take Profit/Stop Loss", "Sell", limit=tpPrice, stop=slPrice)
// Plot the crossover points of EMA1 and EMA2
plotshape(series=emaCrossUp, location=location.belowbar, color=color.aqua, style=shape.triangleup, title="EMA Cross Up", size=size.small)
plotshape(series=emaCrossDown, location=location.abovebar, color=color.red, style=shape.triangledown, title="EMA Cross Down", size=size.small)
// Plot the EMA lines on the chart
plot(ema1, title="EMA 1", color=color.aqua)
plot(ema2, title="EMA 2", color=color.red)
plot(ema3, title="EMA 3", color=color.yellow, linewidth=2)