Ba chỉ số trung bình động biểu thức và chỉ số sức mạnh tương đối của Stochastic

Tác giả:ChaoZhang, Ngày: 2024-01-30 16:52:48
Tags:

img

Tổng quan

Đây là một chiến lược theo xu hướng kết hợp trung bình di chuyển theo hàm số ba (EMA) và chỉ số sức mạnh tương đối của Stochastic (Stoch RSI) để tạo ra tín hiệu giao dịch. Nó sẽ dài khi EMA nhanh vượt qua EMA trung bình và EMA trung bình vượt qua EMA chậm. Nó sẽ ngắn khi điều ngược lại xảy ra. Chiến lược cũng sử dụng Stoch RSI như một chỉ số phụ trợ.

Nguyên tắc

  1. Sử dụng 8, 14, 50 ngày EMA. Đi dài khi 8 ngày EMA > 14 ngày EMA > 50 ngày EMA. Đi ngắn khi ngược lại.

  2. Sử dụng Stochastic RSI như một chỉ số phụ trợ. Tính toán 14 ngày RSI đầu tiên, sau đó tính toán Stochastics trên RSI, cuối cùng tính toán 3 ngày SMA dưới dạng đường K và 3 ngày SMA trên đường K dưới dạng đường D. K vượt qua D cho tín hiệu dài.

  3. Nhập giao dịch dài khi đóng > 8 ngày EMA trên tín hiệu dài. Nhập giao dịch ngắn khi đóng < 8 ngày EMA trên tín hiệu ngắn.

  4. Stop loss được thiết lập ở khoảng cách 1 ATR dưới/trên giá nhập.

Điểm mạnh

  1. EMA là chỉ số cơ bản có thể theo dõi xu hướng hiệu quả.

  2. Thêm Stoch RSI có thể lọc các tín hiệu sai và tăng độ chính xác nhập.

  3. Dựa trên ATR dừng lỗ và lấy lợi nhuận có thể theo dõi biến động thị trường một cách năng động, tránh đặt sai.

  4. Chiến lược này có các thông số được điều chỉnh tốt và hoạt động tốt trong thời gian xu hướng.

Rủi ro

  1. Sự kết hợp của nhiều chỉ số làm tăng rủi ro. Các tín hiệu xung đột giữa EMA và Stoch RSI có thể gây ra việc nhập vào mức xấu.

  2. Các thiết lập dừng lỗ và lấy lợi nhuận bảo thủ có thể bị vi phạm bởi những biến động lớn của thị trường, gây ra việc thoát khỏi quá sớm không có xu hướng tiếp theo. Điều chỉnh các tham số ATR hoặc tăng số nhân SL / TP có thể giúp.

  3. Thiết lập EMA ba có một sự chậm trễ nhất định khi các đường nhanh và trung bình đảo ngược.

  4. Chiến lược này có lợi cho thị trường xu hướng. Các thị trường bên cạnh sẽ không hoạt động tốt. Điều chỉnh thời gian MA hoặc thêm các chỉ số phụ khác có thể giúp.

Những cải tiến

  1. Thêm các chỉ số như MACD cho các mục nhập tốt hơn.

  2. Tối ưu hóa các tham số thử nghiệm dài / ngắn trên ATR. Ví dụ như điều chỉnh stop loss từ 1 ATR đến 1,5 ATR, lấy lợi nhuận từ 4 ATR đến 3 ATR để có kết quả tốt hơn.

  3. Loại bỏ Stoch RSI và chỉ giữ MAs để lọc tiếng ồn và lợi nhuận ổn định hơn.

  4. Thêm thêm các tiêu chí đánh giá xu hướng, như khối lượng giao dịch, để hoạt động dưới mức đáng kể.

Kết luận

Chiến lược này kết hợp ba EMA và Stoch RSI để xác định xu hướng. Các tín hiệu nhập cảnh nghiêm ngặt làm giảm các giao dịch không cần thiết. SL và TP năng động dựa trên ATR làm cho các tham số thích nghi. Các thử nghiệm ngược cho thấy kết quả tuyệt vời trong các giai đoạn xu hướng với giảm nhỏ hơn và lợi nhuận nhất quán.


/*backtest
start: 2023-12-01 00:00:00
end: 2023-12-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//              3ESRA
//              v0.2a

// Coded by Vaida Bogdan

// 3ESRA consists of a 3 EMA cross + a close above (for longs) the quickest EMA
// or below (for shorts). Note that I've deactivated the RSI Cross Over/Under
// (you can modify the code and activate it). The strategy also uses a stop loss
// that's at 1 ATR distance from the entry price and a take profit that's at
// 4 times the ATR distance from the entry price.

// Feedback:
// Tested BTCUSDT Daily
// 1. Stoch-RSI makes you miss opportunities.
// 2. Changing RR to 4:1 times ATR works better.

//@version=4
strategy(title="3 EMA + Stochastic RSI + ATR", shorttitle="3ESRA", overlay=true, pyramiding=1,
     process_orders_on_close=true, calc_on_every_tick=true,
     initial_capital=1000, currency = currency.USD, default_qty_value=10, 
     default_qty_type=strategy.percent_of_equity,
     commission_type=strategy.commission.percent, commission_value=0.1, slippage=2)

startDate = input(title="Start Date", type=input.integer,
     defval=1, minval=1, maxval=31, group="Backtesting range")
startMonth = input(title="Start Month", type=input.integer,
     defval=1, minval=1, maxval=12, group="Backtesting range")
startYear = input(title="Start Year", type=input.integer,
     defval=1900, minval=1800, maxval=2100, group="Backtesting range")
endDate = input(title="End Date", type=input.integer,
     defval=1, minval=1, maxval=31, group="Backtesting range")
endMonth = input(title="End Month", type=input.integer,
     defval=1, minval=1, maxval=12, group="Backtesting range")
endYear = input(title="End Year", type=input.integer,
     defval=2040, minval=1800, maxval=2100, group="Backtesting range")

// Date range filtering
inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and
     (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 23, 59))
     
fast = input(8, minval=8, title="Fast EMA", group="EMAs")
medium = input(14, minval=8, title="Medium EMA", group="EMAs")
slow = input(50, minval=8, title="Slow EMA", group="EMAs")
src = input(close, title="Source")

smoothK = input(3, "K", minval=1, group="Stoch-RSI", inline="K&D")
smoothD = input(3, "D", minval=1, group="Stoch-RSI", inline="K&D")
lengthRSI = input(14, "RSI Length", minval=1, group="Stoch-RSI", inline="length")
lengthStoch = input(14, "Stochastic Length", minval=1, group="Stoch-RSI", inline="length")
rsiSrc = input(close, title="RSI Source", group="Stoch-RSI")

length = input(title="Length", defval=14, minval=1, group="ATR")
smoothing = input(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"], group="ATR")

// EMAs
fastema = ema(src, fast)
mediumema = ema(src, medium)
slowema = ema(src, slow)

// S-RSI
rsi1 = rsi(rsiSrc, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
sRsiCrossOver = k[1] < d[1] and k > d
sRsiCrossUnder = k[1] > d[1] and k < d

// ATR
ma_function(source, length) =>
	if smoothing == "RMA"
		rma(source, length)
	else
		if smoothing == "SMA"
			sma(source, length)
		else
			if smoothing == "EMA"
				ema(source, length)
			else
				wma(source, length)
atr = ma_function(tr(true), length)

// Trading Logic
longCond1 = (fastema > mediumema) and (mediumema > slowema)
longCond2 = true
// longCond2 = sRsiCrossOver
longCond3 = close > fastema
longCond4 = strategy.position_size <= 0
longCond = longCond1 and longCond2 and longCond3 and longCond4 and inDateRange

shortCond1 = (fastema < mediumema) and (mediumema < slowema)
shortCond2 = true 
// shortCond2 = sRsiCrossUnder
shortCond3 = close < fastema
shortCond4 = strategy.position_size >= 0
shortCond = shortCond1 and shortCond2 and shortCond3 and shortCond4 and inDateRange

var takeProfit = float(na), var stopLoss = float(na)
if longCond and strategy.position_size <= 0
    takeProfit := close + 4*atr
    stopLoss := close - 1*atr
    // takeProfit := close + 2*atr
    // stopLoss := close - 3*atr

else if shortCond and strategy.position_size >= 0
    takeProfit := close - 4*atr
    stopLoss := close + 1*atr
    // takeProfit := close - 2*atr
    // stopLoss := close + 3*atr
    
// Strategy calls
strategy.entry("3ESRA", strategy.long, comment="Long", when=longCond and strategy.position_size <= 0)
strategy.entry("3ESRA", strategy.short, comment="Short", when=shortCond and strategy.position_size >= 0)
strategy.exit(id="TP-SL", from_entry="3ESRA", limit=takeProfit, stop=stopLoss)
if (not inDateRange)
    strategy.close_all()
    
// Plot EMAs
plot(fastema, color=color.purple, linewidth=2, title="Fast EMA")
plot(mediumema, color=color.teal, linewidth=2, title="Medium EMA")
plot(slowema, color=color.yellow, linewidth=2, title="Slow EMA")
// Plot S-RSI
// plotshape((strategy.position_size > 0) ? na : sRsiCrossOver, title="StochRSI Cross Over", style=shape.triangleup, location=location.belowbar, color=color.teal, text="SRSI", size=size.small)
// Plot trade
bgcolor(strategy.position_size > 0 ? color.new(color.green, 75) : strategy.position_size < 0 ? color.new(color.red,75) : color(na))
// Plot Strategy
plot((strategy.position_size != 0) ? takeProfit : na, style=plot.style_linebr, color=color.green, title="TP")
plot((strategy.position_size != 0) ? stopLoss : na, style=plot.style_linebr, color=color.red, title="SL")



Thêm nữa