
Chiến lược giao dịch cặp đường trung bình của Bollinger Bands là một chiến lược theo dõi xu hướng hoạt động theo giá thị trường. Nó sử dụng giao thoa giữa các đường trung bình và đường trung bình di chuyển như một tín hiệu giao dịch, thực hiện một chiến lược định lượng có thể tự động xác định xu hướng thị trường và giao dịch theo quy tắc dừng lỗ.
Chiến lược này được giao dịch dựa trên tín hiệu chéo của chỉ số Brin và moving average. Cụ thể, nó sử dụng đồng thời các đường trung tâm Brin, đường trên Brin và 7 đường trung bình di chuyển với độ dài từ 5 đến 200 ngày.
Ngoài ra, chiến lược này cũng giới thiệu các chỉ số phán đoán đa không gian của moveToFract. Chỉ số này phán đoán xu hướng thị trường hiện tại là lên hoặc xuống bằng cách tính toán các trường hợp sắp xếp theo thứ tự của đường trung bình di chuyển ngắn hạn và dài hạn, do đó tránh tạo ra tín hiệu sai trong tình huống biến động. Cuối cùng, kết hợp với các quy tắc dừng lỗ có thể cấu hình, tạo thành một chiến lược giao dịch theo dõi xu hướng hoàn chỉnh hơn.
Chiến lược này nói chung là một chiến lược theo dõi xu hướng rất thực tế. Nó sử dụng các chỉ số chéo để đưa ra quyết định, và thêm vào mô-đun phán đoán xu hướng, có thể loại bỏ hiệu quả các tín hiệu sai. Sau khi thiết lập dừng dừng, bạn có thể theo dõi xu hướng đầy đủ để giao dịch và thu được lợi nhuận tốt hơn. Bằng cách điều chỉnh các tham số và thêm nhiều bộ lọc, chiến lược này có thể được tối ưu hóa hơn nữa, thích nghi với nhiều môi trường thị trường, có rất nhiều cải tiến và triển vọng ứng dụng.
/*backtest
start: 2023-10-24 00:00:00
end: 2023-11-23 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/
// © HeWhoMustNotBeNamed
//@version=4
strategy("BuyTheDip", overlay=true, initial_capital = 100000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01, calc_on_order_fills = true)
MAType = input(title="Moving Average Type", defval="sma", options=["ema", "sma", "hma", "rma", "vwma", "wma"])
exitType = input(title="Exit Strategy", defval="Signal", options=["Signal", "TrailingStop", "Both"])
LookbackPeriod = input(30, minval=10,step=10)
BBStdDev = input(2, minval=1, maxval=10, step=0.5)
BBLength = input(60, minval=5, step=5)
atrLength = input(22)
atrMult = input(6)
tradeDirection = input(title="Trade Direction", defval=strategy.direction.all, options=[strategy.direction.all, strategy.direction.long, strategy.direction.short])
backtestYears = input(10, minval=1, step=1)
includePartiallyAligned = true
f_getMovingAverage(source, MAType, length)=>
ma = sma(source, length)
if(MAType == "ema")
ma := ema(source,length)
if(MAType == "hma")
ma := hma(source,length)
if(MAType == "rma")
ma := rma(source,length)
if(MAType == "vwma")
ma := vwma(source,length)
if(MAType == "wma")
ma := wma(source,length)
ma
f_getTrailingStop(atr, atrMult)=>
stop = close - atrMult*atr
stop := strategy.position_size > 0 ? max(stop, stop[1]) : stop
stop
f_getMaAlignment(MAType, includePartiallyAligned)=>
ma5 = f_getMovingAverage(close,MAType,5)
ma10 = f_getMovingAverage(close,MAType,10)
ma20 = f_getMovingAverage(close,MAType,20)
ma30 = f_getMovingAverage(close,MAType,30)
ma50 = f_getMovingAverage(close,MAType,50)
ma100 = f_getMovingAverage(close,MAType,100)
ma200 = f_getMovingAverage(close,MAType,200)
upwardScore = 0
upwardScore := close > ma5? upwardScore+1:upwardScore
upwardScore := ma5 > ma10? upwardScore+1:upwardScore
upwardScore := ma10 > ma20? upwardScore+1:upwardScore
upwardScore := ma20 > ma30? upwardScore+1:upwardScore
upwardScore := ma30 > ma50? upwardScore+1:upwardScore
upwardScore := ma50 > ma100? upwardScore+1:upwardScore
upwardScore := ma100 > ma200? upwardScore+1:upwardScore
upwards = close > ma5 and ma5 > ma10 and ma10 > ma20 and ma20 > ma30 and ma30 > ma50 and ma50 > ma100 and ma100 > ma200
downwards = close < ma5 and ma5 < ma10 and ma10 < ma20 and ma20 < ma30 and ma30 < ma50 and ma50 < ma100 and ma100 < ma200
upwards?1:downwards?-1:includePartiallyAligned ? (upwardScore > 5? 0.5: upwardScore < 2?-0.5:upwardScore>3?0.25:-0.25) : 0
inDateRange = time >= timestamp(syminfo.timezone, year(timenow) - backtestYears, 01, 01, 0, 0)
exitBySignal = exitType == "Signal" or exitType == "Both"
exitByTrailingStop = exitType == "TrailingStop" or exitType == "Both"
maAlignment = f_getMaAlignment(MAType,includePartiallyAligned)
atr = atr(atrLength)
trailingStop = f_getTrailingStop(atr, atrMult)
maAligned = highest(maAlignment,LookbackPeriod)
[middle, upper, lower] = bb(close, BBLength, BBStdDev)
buyCondition = maAligned == 1 and (crossover(close, lower) or crossover(close, middle))
buyExitCondition = crossunder(close, upper)
strategy.entry("Buy", strategy.long, when=buyCondition and inDateRange, oca_name="oca_buy")
strategy.close("Buy", when=buyExitCondition and exitBySignal)
strategy.exit("ExitBuy", "Buy", stop = trailingStop, when=exitByTrailingStop )