Chiến lược phân kỳ dựa trên đường trung bình động


Ngày tạo: 2024-01-24 11:43:41 sửa đổi lần cuối: 2024-01-24 11:43:41
sao chép: 1 Số nhấp chuột: 651
1
tập trung vào
1617
Người theo dõi

Chiến lược phân kỳ dựa trên đường trung bình động

Tổng quan

Chiến lược này tìm ra sự lệch giữa giá và đường trung bình di chuyển bằng cách tính toán đường trung bình di chuyển và điểm trung tâm của nó, làm tín hiệu mua và bán. Nó có thể được áp dụng cho bất kỳ chỉ số dao động nào để tìm ra sự lệch. Đây là một công cụ có giá trị, có thể được sử dụng để kiểm tra lại và giao dịch trực tiếp.

Nguyên tắc chiến lược

  1. Tính toán chiều dài là trung bình di chuyển của Len (MA)
  2. Kiểm tra điểm trục thấp của MA ((PL) và điểm trục cao ((PH)
  3. Xác định có sự sai lệch thẳng hay không: giá sáng tạo thấp mà MA không sáng tạo thấp hoặc giá sáng tạo thấp mà MA sáng tạo thấp
  4. Xác định có sự đảo ngược hay không: giá sáng tạo cao nhưng MA không sáng tạo cao hoặc giá không sáng tạo cao nhưng MA sáng tạo cao
  5. Mua và bán tùy theo hoàn cảnh.

Phân tích lợi thế

  1. Có thể tự động phát hiện sự lệch giữa giá và MA, tránh sai lầm phán đoán bằng tay
  2. Có thể áp dụng cho bất kỳ chỉ số dao động, mở rộng mạnh mẽ
  3. Có thể được sử dụng để kiểm tra lại khả năng lợi nhuận của chiến lược xác minh
  4. Các tham số có thể cấu hình điều chỉnh độ nhạy để tránh tín hiệu sai
  5. Cung cấp nhiều loại loại khác nhau để đánh giá chính xác và toàn diện

Phân tích rủi ro

  1. Nếu chỉ số dao động được thiết lập không chính xác, có thể tạo ra một số lượng lớn các tín hiệu sai
  2. Tránh xa các điểm trung tâm cần thiết trước khi xảy ra, có thể xảy ra tín hiệu kém
  3. Cần điều chỉnh các tham số để cân bằng độ nhạy và lọc các tín hiệu sai
  4. Tác dụng tốt hơn khi kết hợp với các yếu tố khác, sử dụng một mình có độ tin cậy thấp hơn

Hướng tối ưu hóa

  1. Tối ưu hóa các tham số trung bình di chuyển để tìm các tham số kết hợp tốt nhất
  2. Kết hợp với các chỉ số khác như chỉ số giá cả để tránh tín hiệu sai
  3. Thêm mô hình học máy đánh giá không đáng tin cậy
  4. Tăng cơ chế quản lý rủi ro, kiểm soát tổn thất đơn lẻ

Tóm tắt

Chiến lược này có thể được sử dụng rộng rãi cho bất kỳ chỉ số dao động nào, có khả năng mở rộng mạnh mẽ. Cần kết hợp các tham số tối ưu hóa với các chỉ số khác để tăng đáng kể độ tin cậy và sự ổn định của hệ thống.

Mã nguồn chiến lược
/*backtest
start: 2023-12-24 00:00:00
end: 2024-01-12 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tista 
//https://www.tradingview.com/u/tista/#published-scripts

//@version=4

strategy(title="MA Divergences", format=format.price)

//* Backtesting Period Selector | Component *//
//* https://www.tradingview.com/script/eCC1cvxQ-Backtesting-Period-Selector-Component *//
//* https://www.tradingview.com/u/pbergden/ *//
//* Modifications made *//
testStartYear = input(2021, "Backtest Start Year") 
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)

testStopYear = input(999999, "Backtest Stop Year")
testStopMonth = input(9, "Backtest Stop Month")
testStopDay = input(26, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)

testPeriod() => true
/////////////// END - Backtesting Period Selector | Component ///////////////
len = input(title="MA Period", minval=1, defval=14)
src = input(title="MA Source", defval=close)
lbR = input(title="Pivot Lookback Right", defval=5)
lbL = input(title="Pivot Lookback Left", defval=5)
rangeUpper = input(title="Max of Lookback Range", defval=600)
rangeLower = input(title="Min of Lookback Range", defval=2)
plotBull = input(title="Plot Bullish", defval=true)
plotHiddenBull = input(title="Plot Hidden Bullish", defval=true)
plotBear = input(title="Plot Bearish", defval=true)
plotHiddenBear = input(title="Plot Hidden Bearish", defval=true)

bearColor = color.red
bullColor = color.green
hiddenBullColor = color.green
hiddenBearColor = color.red
textColor = color.white
noneColor = color.new(color.white, 100)

osc = wma(src, len)

plot(osc, title="MA", linewidth=2, color=color.yellow)

plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
phFound = na(pivothigh(osc, lbL, lbR)) ? false : true

_inRange(cond) =>
    bars = barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper

alertcondition(osc[1] > 100.0 and osc[2] < 100.0, title="MA value crosses over 100.0", message="Check charts for a MA cross over 100.0")
alertcondition(osc[1] < 100.0 and osc[2] > 100.0, title="MA value crosses under 100.0", message="Check charts for a MA cross under 100.0")
alertcondition(osc[1] > -100. and osc[2] < -100.0, title="MA value crosses over -100.0", message="Check charts for a MA cross over -100.0")
alertcondition(osc[1] < -100.0 and osc[2] > -100.0, title="MA value crosses under -100.0", message="Check charts for a MA cross under -100.0")

//------------------------------------------------------------------------------
// Regular Bullish

// Osc: Higher Low
oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Lower Low
priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)

bullCond = plotBull and priceLL and oscHL and plFound

plot(
	 plFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bullish",
	 linewidth=2,
	 color=(bullCond ? bullColor : noneColor),
	 transp=0
	 )

plotshape(
	 bullCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bullish Label",
	 text=" Bull ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullColor,
	 textcolor=textColor,
	 transp=0
	 )

alertcondition(bullCond, title="Regular bullish divergence in MA found", message="Check charts for a regular bullish divergence found with MA")

//------------------------------------------------------------------------------
// Hidden Bullish

// Osc: Lower Low
oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Higher Low
priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)

hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound

plot(
	 plFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bullish",
	 linewidth=2,
	 color=(hiddenBullCond ? hiddenBullColor : noneColor),
	 transp=0
	 )

plotshape(
	 hiddenBullCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bullish Label",
	 text=" H Bull ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullColor,
	 textcolor=textColor,
	 transp=0
	 )

alertcondition(hiddenBullCond, title="Hidden bullish divergence in MA found", message="Check charts for a hidden bullish divergence found with MA")

//------------------------------------------------------------------------------
// Regular Bearish

// Osc: Lower High
oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Higher High
priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)

bearCond = plotBear and priceHH and oscLH and phFound

plot(
	 phFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bearish",
	 linewidth=2,
	 color=(bearCond ? bearColor : noneColor),
	 transp=0
	 )

plotshape(
	 bearCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bearish Label",
	 text=" Bear ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearColor,
	 textcolor=textColor,
	 transp=0
	 )

alertcondition(bearCond, title="Regular bearish divergence in MA found", message="Check charts for a regular bearish divergence found with MA")

//------------------------------------------------------------------------------
// Hidden Bearish

// Osc: Higher High
oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Lower High
priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)

hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound

plot(
	 phFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bearish",
	 linewidth=2,
	 color=(hiddenBearCond ? hiddenBearColor : noneColor),
	 transp=0
	 )

plotshape(
	 hiddenBearCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bearish Label",
	 text=" H Bear ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearColor,
	 textcolor=textColor,
	 transp=0
	 )

// Alerts
//alertcondition(bearCond or hiddenBearCond, title='Bear div', message='Bear div')
//alertcondition(bullCond or hiddenBullCond, title='Bull div', message='Bull div')
//alertcondition(bearCond or bullCond, title='Bull or beal div', message='Bull or bear div') 
//alertcondition(hiddenBearCond or hiddenBullCond, title='Bull or beal div', message='Hidden Bull or bear div') 
//alertcondition(hiddenBearCond or hiddenBullCond or bearCond or bullCond, title='Bull or beal div', message='Any Bull or bear div') 

if testPeriod()
    if bullCond or hiddenBullCond
        strategy.entry("Buy", strategy.long)
    if bearCond or hiddenBearCond
        strategy.entry("Sell", strategy.short)