Chiến lược giao dịch MACD StochRSI kép

Tác giả:ChaoZhang, Ngày: 2023-09-22 16:55:55
Tags:

Tổng quan

Chiến lược này kết hợp các chỉ số MACD kép và bộ dao động StochRSI cho các tín hiệu giao dịch. MACD kép sử dụng các tham số khác nhau cho các hiệu ứng nhanh và chậm, trong khi StochRSI xác minh sự khác biệt động lực. Các bộ lọc xu hướng và dừng lỗ cũng được thêm vào để kiểm soát rủi ro.

Chiến lược logic

Các tín hiệu giao dịch dựa trên:

  • MACD kép: MACD nhanh sử dụng thời gian xem lại ngắn, MACD chậm sử dụng thời gian xem lại dài để làm mịn hiệu ứng.

  • StochRSI: Tính toán phạm vi RSI cao / thấp để xác định mức RSI mua quá mức / bán quá mức.

Quy tắc nhập cảnh:

  • Long: MACD nhanh vượt qua đường không và MACD chậm vượt qua đường không. StochRSI được bán quá mức và K vượt qua D. Trong xu hướng tăng.

  • Khẩu: MACD nhanh vượt dưới đường không và MACD chậm vượt dưới đường không. StochRSI bị mua quá mức và K vượt dưới D. Trong xu hướng giảm.

Ưu điểm

  • MACD kép tránh đột phá sai để có chất lượng tín hiệu cao hơn.

  • StochRSI xác định mức mua quá mức / bán quá mức để tránh đuổi theo.

  • Xem xét hướng xu hướng tổng thể để giảm tổn thất chống xu hướng.

  • Xác nhận khung thời gian chéo cải thiện hiệu quả tín hiệu.

  • Ngăn lỗ kiểm soát rủi ro.

Rủi ro

  • MACD dễ bị tín hiệu sai, cần xác nhận thêm.

  • Các thông số StochRSI kém có thể bỏ lỡ giao dịch.

  • Mức dừng lỗ có thể quá bảo thủ hoặc hung hăng.

  • Thiếu quản lý vị trí cho dừng động.

Cải tiến:

  1. Thêm các bộ lọc như âm lượng hoặc độ dốc MA.

  2. Tối ưu hóa hoặc thêm các dao động khác.

  3. Theo dõi dừng mất tích năng động.

  4. Thêm kích thước vị trí dựa trên hiệu suất.

Tối ưu hóa

Các lĩnh vực chính để tối ưu hóa:

  1. Tối ưu hóa các thông số chỉ số.

  2. Thêm bộ lọc để loại bỏ tín hiệu sai.

  3. Tối ưu hóa các điểm dừng để theo dõi động.

  4. Tích hợp kích thước vị trí dựa trên hiệu suất chiến lược.

  5. Thêm máy học để tối ưu hóa tự động.

Tóm lại

Chiến lược kết hợp nhiều chỉ số để có tín hiệu mạnh hơn, nhưng cần tối ưu hóa các thông số, lọc, dừng động để giảm các giao dịch không mong muốn và cải thiện lợi nhuận.


/*backtest
start: 2023-09-14 00:00:00
end: 2023-09-21 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=2



//This strategy is an ongoing work in progress. Last updated 8/6/16.
//Feel free to modify it as you see fit, if you do borrow code then send me a link so I 
//can see and maybe borrow some of your code to improve this.
//Thanks to ChrisMoody who I stole the code for setting custom resolution from.
//
//more info in comments at end of script





strategy("MACDouble & StochRSI w/ safeties v0.3", overlay=true)

source = close
useCurrentRes = input(true, title="Uncheck to use custom res./intrv. for 2nd MACD indicator")
resCustom = input(title="Resolution/interval to use for 2nd MACD:",  defval="45")
res = useCurrentRes ? timeframe.period : resCustom

useCurrentRes2 = input(true, title="Uncheck to use custom res/intrv for StochRSI")
resCustom2 = input(title="Resolution to use for StochRSI indicator:",  defval="45")
res2 = useCurrentRes2 ? timeframe.period : resCustom2


//MACD1
fastLength = input(10, title="MACD fast length")
slowlength = input(21, title="MACD slow length")
sigLength = input(9, title="MACD signal length")

MACD = ema(source, fastLength) - ema(source, slowlength)
signal = sma(MACD, sigLength)
delta = MACD - signal



//MACD2
fastLength2 = input(31, title= "2nd MACD fast length")
slowlength2 = input(63, title= "2nd MACD slow length")
sigLength2 = input(30, title= "2nd MACD signal length")

MACD2 = ema(source, fastLength2) - ema(source, slowlength2)
signal2 = sma(MACD2, sigLength2)
delta2 = MACD2 - signal2

MACDRes = security(syminfo.tickerid, res, MACD2)
signalRes = security(syminfo.tickerid,res, signal2)
deltaRes = security(syminfo.tickerid, res, delta2)


uptrend = (close + high)/(close[1] + high[2])
downtrend = (close + low)/(close[1] + low[2])

smoothK = input(3, minval=1)
smoothD = input(3, minval=1)
lengthRSI = input(11, minval=1)
lengthStoch = input(11, minval=1)
src = close

rsi1 = rsi(src, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
RSI_buyTrig = input(90)
RSI_sellTrig = input(20)

kRes = security(syminfo.tickerid, res2, k)
dRes = security(syminfo.tickerid, res2, d)


if (delta > 0) and (year>2012) and (deltaRes > 0) and (uptrend > 1) and (  kRes and dRes < RSI_buyTrig) and (kRes > dRes)
    strategy.entry("buy", strategy.long, comment="buy")
    

if (delta < 0) and (year>2012) and (deltaRes < 0) and (downtrend < 1) and ( kRes and dRes > RSI_sellTrig) and (kRes < dRes)
    strategy.entry("sell", strategy.short, comment="sell")
	strategy.exit("sell", loss = 9000)



//  RELEASE NOTES, ETC
//
// The core starting idea for this backtesting script came from the desire to have two traditional
//MACD indicators: one 'fast' and one 'slow'. The slow one is to pretty much smooth out noisy signals
//so that short term changes in price are ignored (ideally). 
//	A brief version history
//		v0.1 - Basic two MACD indicators script
//      v0.2 - Added StochRSI indicator
//      v0.21- Added primitive uptrend/downtrend safety condition 
//      v0.22- Added changable time resolution for MACDslow
//      v0.23- Added exit safeties conditional on loss threshold   
//      v0.3 - Added changeable resolution for StochRSI
//	Future changes planned for next release:
//		-Fine tuning exit safeties
//      -Major overhaul of trade logic/triggers (may be forked as a different script)
//
//I am more than happy to discuss any difficulties you are having, questions about the script, or improvement suggestions.
//I am not a coder and my background is actually in economics, so feel free to debug ;)
//Feel free to tip me on the indcluded bitcoin address on TV as well
// tradingview.com/u/RyanMartin 
// rjmarti2@millersville.edu


Thêm nữa