Chiến lược theo dõi trung bình động kép MACD

Tác giả:ChaoZhang, Ngày: 2023-12-18 12:25:13
Tags:

img

Tổng quan

Chiến lược này được gọi làChiến lược theo dõi trung bình động kép MACDNó sử dụng chỉ số MACD Golden Cross và death cross của hai đường trung bình động như là tín hiệu giao dịch, kết hợp với giá thấp nhất của ngày trước như là điểm dừng lỗ để theo dõi chuyển động giá ngắn hạn.

Chiến lược logic

  1. Tính toán EMA nhanh ((close,5), EMA chậm ((close,8) và SMA tín hiệu ((MACD,3)
  2. Định nghĩa tín hiệu dài: khi MA nhanh vượt qua MA chậm
  3. Định nghĩa tín hiệu ngắn: khi MA nhanh vượt qua dưới MA chậm hoặc giá đóng cửa thấp hơn giá thấp nhất ngày trước
  4. Kích thước vị trí là vốn ban đầu 2000 USD chia cho giá đóng
  5. Sử dụng tín hiệu ngắn để đóng vị trí dài như dừng lỗ

Phân tích lợi thế

  1. Sử dụng chỉ số MACD để xác định các vùng mua quá mức và bán quá mức, với hai MAs để tạo ra tín hiệu giao dịch, tránh phá vỡ sai
  2. Theo dõi xu hướng ngắn hạn, dừng lỗ kịp thời
  3. Điều chỉnh động kích thước vị trí tránh mất mát đơn quá lớn

Phân tích rủi ro

  1. Chỉ số MACD có hiệu ứng chậm, có thể bỏ lỡ cơ hội ngắn hạn
  2. Các tín hiệu giao dịch MA đôi có thể tạo ra các tín hiệu sai
  3. Điểm dừng mất mát là quá hung hăng, với tần suất cao của bị dừng lại

Hướng dẫn tối ưu hóa

  1. Tối ưu hóa sự kết hợp các tham số MACD để cải thiện độ nhạy của chỉ số
  2. Thêm đánh giá xu hướng để tránh các tín hiệu sai từ việc củng cố thị trường
  3. Kết hợp với Chỉ số biến động để đánh giá biến động thị trường, điều chỉnh điểm dừng lỗ

Tóm lại

Chiến lược này sử dụng chỉ số kết hợp trung bình động MACD để xác định các vùng mua quá mức và bán quá mức, tạo ra các tín hiệu giao dịch, trong khi giới thiệu kích thước vị trí năng động và giá thấp nhất ngày trước như thiết kế điểm dừng lỗ để nắm bắt biến động giá ngắn hạn.


/*backtest
start: 2023-12-10 00:00:00
end: 2023-12-13 02:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=2
// macd/cam v1 strategizing Chris Moody Macd indicator https://www.tradingview.com/script/OQx7vju0-MacD-Custom-Indicator-Multiple-Time-Frame-All-Available-Options/
// macd/cam v2 changing to macd 5,8,3
// macd/cam v2.1 
//      Sell when lower than previous day low. 
//      Initial capital of $2k. Buy/sell quantity of initial capital / close price
//      Quitar short action
//      Note: custom 1-week resolution seems to put AMD at 80% profitable

strategy(title="MACD/CAM 2.1", shorttitle="MACD/CAM 2.1") //
source = close
//get inputs from options
useCurrentRes = input(true, title="Use Current Chart Resolution?")
resCustom = input(title="Use Different Timeframe? Uncheck Box Above", defval="60")
smd = input(true, title="Show MacD & Signal Line? Also Turn Off Dots Below")
sd = input(true, title="Show Dots When MacD Crosses Signal Line?")
sh = input(true, title="Show Histogram?")
macd_colorChange = input(true,title="Change MacD Line Color-Signal Line Cross?")
hist_colorChange = input(true,title="MacD Histogram 4 Colors?")
venderLowerPrev = input(true,title="Vender cuando closing price < previous day low?")

res = useCurrentRes ? timeframe.period : resCustom

fastLength = input(5, minval=1), slowLength=input(8,minval=1)
signalLength=input(3,minval=1)

// find exponential moving average of price as x and fastLength var as y
fastMA = ema(source, fastLength)
slowMA = ema(source, slowLength)

macd = fastMA - slowMA
// simple moving average
signal = sma(macd, signalLength)
hist = macd - signal

outMacD = request.security(syminfo.tickerid, res, macd)
outSignal = request.security(syminfo.tickerid, res, signal)
outHist = request.security(syminfo.tickerid, res, hist)

histA_IsUp = outHist > outHist[1] and outHist > 0
histA_IsDown = outHist < outHist[1] and outHist > 0
histB_IsDown = outHist < outHist[1] and outHist <= 0
histB_IsUp = outHist > outHist[1] and outHist <= 0

//MacD Color Definitions
macd_IsAbove = outMacD >= outSignal
macd_IsBelow = outMacD < outSignal

plot_color = hist_colorChange ? histA_IsUp ? aqua : histA_IsDown ? blue : histB_IsDown ? red : histB_IsUp ? maroon :yellow :gray
macd_color = macd_colorChange ? macd_IsAbove ? lime : red : red
signal_color = macd_colorChange ? macd_IsAbove ? yellow : yellow : lime

circleYPosition = outSignal
 
plot(smd and outMacD ? outMacD : na, title="MACD", color=macd_color, linewidth=4)
plot(smd and outSignal ? outSignal : na, title="Signal Line", color=signal_color, style=line ,linewidth=2)
plot(sh and outHist ? outHist : na, title="Histogram", color=plot_color, style=histogram, linewidth=4)

circleCondition = sd and cross(outMacD, outSignal)

// Determine long and short conditions
longCondition  = circleCondition and macd_color == lime

redCircle = circleCondition and macd_color == red
redCirclePrevLow = redCircle or low<low[1]
shortCondition = redCircle
if (venderLowerPrev)
    shortCondition = redCirclePrevLow

strategy.initial_capital = 20000
// Set quantity to initial capital / closing price
cantidad = strategy.initial_capital/close

// Submit orders
strategy.entry(id="long", long=true, qty=cantidad, when=longCondition)
strategy.close(id="long", when=shortCondition)
plot(circleCondition ? circleYPosition : na, title="Cross", style=cross, linewidth=10, color=macd_color)
// hline(0, '0 Line', linestyle=solid, linewidth=2, color=white)

Thêm nữa