Chiến lược Xu hướng Rồng bay

Tác giả:ChaoZhang, Ngày: 2023-11-07 14:57:23
Tags:

img

Tổng quan

Chiến lược xu hướng Flying Dragon tạo ra các tín hiệu giao dịch bằng cách vẽ các dải xu hướng với các màu sắc khác nhau dựa trên cấu hình của các đường trung bình động về các loại, chiều dài và trục trặc. Nó có thể tìm ra các bộ tham số tối ưu trên các khung thời gian khác nhau để cân bằng độ chính xác xu hướng và rủi ro giao dịch.

Chiến lược logic

Chiến lược này sử dụng hai đường trung bình động để vẽ các dải xu hướng, được đánh dấu là MA1 và MA4. MA1 là đường trung bình động nhanh hơn và MA4 là đường trung bình chậm hơn. Trong khi đó, MA1 có 3 cài đặt offset (Offset1, Offset2, Offset3) tạo thành MA2 và MA3.

Có 5 mức rủi ro để lựa chọn. Một tín hiệu giao dịch chỉ được kích hoạt khi giá vượt qua các đường trung bình động khác nhau dưới các mức rủi ro khác nhau, từ cao đến thấp: MA1 Offset1, MA2, MA3, MA4, tất cả các dải xu hướng cùng màu. Màu sắc của dải xu hướng chỉ ra hướng xu hướng hiện tại, màu xanh lá cây cho xu hướng tăng và màu đỏ cho xu hướng giảm.

Chiến lược cũng cho phép dừng lỗ và các tùy chọn chỉ dài, chỉ ngắn hoặc cả hai hướng.

Phân tích lợi thế

  • Tìm các bộ tham số tối ưu trên các khung thời gian khác nhau, thích nghi với nhiều điều kiện thị trường hơn
  • Các loại MA khác nhau có sẵn để tối ưu hóa cho các sản phẩm khác nhau
  • Sự thay đổi, cốt lõi của chiến lược này, làm cho đánh giá xu hướng chính xác hơn
  • Mức rủi ro đạt được sự cân bằng giữa rủi ro và lợi nhuận
  • Có thể tùy chỉnh cao với các kết hợp tham số khác nhau
  • Các dải xu hướng trực quan tạo thành các tín hiệu giao dịch trực quan rõ ràng
  • Kiểm soát rủi ro dừng lỗ

Phân tích rủi ro

  • Mức độ rủi ro cao có thể tạo ra tín hiệu sai, nên giảm mức độ rủi ro hoặc điều chỉnh các thông số
  • Rủi ro thoát lỗ dừng liên tiếp khi xu hướng đảo ngược
  • Các sản phẩm khác nhau cần thử nghiệm và tối ưu hóa tham số riêng biệt
  • Đối với giao dịch tần số cao, MA nhanh nên dẫn đến MA chậm
  • Tối ưu hóa tham số không đầy đủ có thể gây quá nhạy hoặc chậm

Các rủi ro có thể được quản lý bằng cách giảm dần mức độ rủi ro, thử nghiệm nhiều kết hợp tham số hơn và tối ưu hóa các tham số riêng biệt cho các sản phẩm khác nhau.

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

  • Kiểm tra các kết hợp khác nhau của các loại trung bình động
  • Thử nhiều giá trị chiều dài hơn để tìm chiều dài tối ưu
  • Chú ý điều chỉnh độ dịch chuyển, chìa khóa để tối ưu hóa
  • Tối ưu hóa các tham số riêng biệt cho các sản phẩm khác nhau
  • Tối ưu hóa điểm dừng lỗ, xem xét việc kiếm lợi nhuận
  • Kiểm tra các kết hợp quy tắc nhập khác nhau
  • Đánh giá liệu các bộ lọc có cần thiết cho tối ưu hóa hay không
  • Xem xét thêm các chỉ số sức mạnh xu hướng cho hỗ trợ

Tóm lại

Chiến lược xu hướng Flying Dragon kết hợp thông minh các đường trung bình chuyển động thành một hệ thống giao dịch xu hướng có thể hiển thị. Tính khả năng điều chỉnh tham số cao của nó cho phép tối ưu hóa chi tiết cho các sản phẩm và chế độ thị trường khác nhau để đạt được sự cân bằng tối ưu giữa sự ổn định và độ nhạy. Sự kết hợp tham số phong phú cung cấp đủ không gian tối ưu hóa.


/*backtest
start: 2022-10-31 00:00:00
end: 2023-02-14 00:00:00
period: 1d
basePeriod: 1h
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/
// © MarkoP010 2023

//@version=5
//The basic idea of the strategy is to select the best set of MAs, types, lenghts and offsets, which draws red trend bands for downtrend (and green for uptrend).
//Strategy executes by selected risk level either when there is MA crossover with price (MA1 Offset1 on Highest risk level, MA2 on Low risk level) or three bands with the same color on at the same time (on Lowest risk level).
//Strategy plots user selectable Moving Average lines and a colored trend band between the MA lines. The trend bands can be turned off individually if required.
//The Offset option shifts the selected MA with the set number of steps to the right. That is where the Magic happens and the Dragon roars!

//Strategy version 1.0
strategy("Flying Dragon Trend Strategy", shorttitle="FD Trend Strategy", overlay=true, pyramiding=3, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=5, commission_type=strategy.commission.cash_per_order, commission_value=10, calc_on_order_fills=false, process_orders_on_close=true)

strDirection = input.string(defval="Both", title="Strategy Direction", options=["Both", "Long", "Short"], group="Strategy") //Strategy direction selector by DashTrader
strSelection = strDirection == "Long" ? strategy.direction.long : strDirection == "Short" ? strategy.direction.short : strategy.direction.all //Strategy direction selector by DashTrader
strategy.risk.allow_entry_in(strSelection)

riskLevel = input.string(defval="Medium", title="Risk Level", options=["Highest", "High", "Medium", "Low", "Lowest"], tooltip="Strategy execution criteria. When Highest then MA1 Offset1 crossover with price, when Low then MA2 Offset crossover, when Lowest then all the Bands are the same color.", group="Strategy")

useStop = input(defval=false, title="Use Stop Loss", inline="SL", group="Strategy")
stopPrct = input.int(defval=10, title=" %", minval=0, maxval=100, step=1, inline="SL", group="Strategy") / 100

//Moving Averages function
MA(source, length, type) =>
    type == "EMA" ? ta.ema(source, length) :
     type == "HMA" ? ta.hma(source, length) :
     type == "RMA" ? ta.rma(source, length) :
     type == "SMA" ? ta.sma(source, length) :
     type == "SWMA" ? ta.swma(source) :
     type == "VWMA" ? ta.vwma(source, length) :
     type == "WMA" ? ta.wma(source, length) :
     na

//Inputs
ma1Type = input.string(defval="HMA", title="", inline="MA1", options=["EMA", "HMA", "RMA", "SMA","SWMA", "VWMA", "WMA"], group="Leading Moving Average") 
ma1Length = input.int(defval=35, title="",minval=1, inline="MA1", group="Leading Moving Average")
ma1Source = input(defval=close, title="", tooltip="For short timeframes, minutes to hours, instead of Default values try Lowest risk level and HMA75 with Offsets 0,1,4 and SMA12 with Offset 6.", inline="MA1", group="Leading Moving Average")
ma1Color  = input(defval=color.purple, title="", inline="MA-1", group="Leading Moving Average")
//useMa1Offset = input(defval=false, title="Use offset to MA-1", inline="MA1", group="Leading Moving Average")
ma1Offset = input.int(defval=0, title="Offset1 Steps", minval=0, maxval=10, step=1, tooltip="The Magic happens here! The offset to move the line to the right.", inline="MA-1", group="Leading Moving Average")
ma1 = MA(ma1Source, ma1Length, ma1Type)[ma1Offset]

ma2Color  = input(defval=color.lime, title="", inline="MA-2", group="Leading Moving Average")
//useMa2Offset = input(defval=true, title="Use offset to MA2", inline="MA-2", group="Leading Moving Average")
ma2Offset = input.int(defval=4, title="Offset2 Steps", minval=0, maxval=10, step=1, tooltip="The Magic happens here! The offset to move the line to the right.", inline="MA-2", group="Leading Moving Average")
ma2 = ma1[ma2Offset]

ma3Color  = input(defval=color.aqua, title="", inline="MA-3", group="Leading Moving Average")
//useMa3Offset = input(defval=false, title="Use offset to MA3", inline="MA-3", group="Leading Moving Average")
ma3Offset = input.int(defval=6, title="Offset3 Steps", minval=0, maxval=10, step=1, tooltip="The Magic happens here! The offset to move the line to the right.", inline="MA-3", group="Leading Moving Average")
ma3 = ma1[ma3Offset]

ma4Type = input.string(defval="SMA", title="", inline="MA4", options=["EMA", "HMA", "RMA", "SMA","SWMA", "VWMA", "WMA"], group="Lagging Moving Average") 
ma4Length = input.int(defval=22, title="",minval=1, inline="MA4", group="Lagging Moving Average")
ma4Source = input(defval=close, title="", inline="MA4", group="Lagging Moving Average")
ma4Color  = input(defval=color.yellow, title="", inline="MA-4", group="Lagging Moving Average")
//useMa4Offset = input(defval=true, title="Use offset to MA4", inline="MA-4", group="Lagging Moving Average")
ma4Offset = input.int(defval=2, title="Offset Steps", minval=0, maxval=10, step=1, tooltip="The Magic happens here! The offset to move the line to the right.", inline="MA-4", group="Lagging Moving Average")
ma4 = MA(ma4Source, ma4Length, ma4Type)[ma4Offset]

bandTransp = input.int(defval=60, title="Band Transparency", minval=20, maxval=80, step=10, group="Banding")
useBand1 = input(defval=true, title="Band 1", inline="Band", group="Banding")
band1Transp = useBand1 ? bandTransp : 100
band1clr = ma1 > ma2 ? color.new(#00ff00, transp=band1Transp) : color.new(#ff0000, transp=band1Transp)
useBand2 = input(defval=true, title="Band 2", inline="Band", group="Banding")
band2Transp = useBand2 ? bandTransp : 100
band2clr = ma1 > ma3 ? color.new(#00ff00, transp=band2Transp) : color.new(#ff0000, transp=band2Transp)
useBand3 = input(defval=true, title="Band 3", tooltip="Up trend green, down trend red. Colors get reversed if MA1 lenght is greater than MA2 lenght, or they are different type and MA2 quicker. In that case, just reverse your selections for MA1 and MA2, or let it be as is.", inline="Band", group="Banding")
band3Transp = useBand3 ? bandTransp : 100
band3clr = ma1 > ma4 ? color.new(#00ff00, transp=band3Transp) : color.new(#ff0000, transp=band3Transp)

//Graphs
piirto1 = plot(ma1, color = ma1Color, title="MA1")
piirto2 = plot(ma2, color = ma2Color, title="MA2")
piirto3 = plot(ma3, color = ma3Color, title="MA3")
piirto4 = plot(ma4, color = ma4Color, title="MA4")

fill(piirto1, piirto2, color=band1clr)
fill(piirto1, piirto3, color=band2clr)
fill(piirto1, piirto4, color=band3clr)

//Strategy entry and stop conditions

longCondition = riskLevel == "Highest" ? ma1Source > ma1 : riskLevel == "High" ? ma1Source > ma2 : riskLevel == "Medium" ? ma1Source > ma3 : riskLevel == "Low" ? ma1Source > ma4 : riskLevel == "Lowest" ? ma1 > ma2 and ma1 > ma3 and ma1 > ma4 : na
shortCondition = riskLevel == "Highest" ? ma1Source < ma1 : riskLevel == "High" ? ma1Source < ma2 : riskLevel == "Medium" ? ma1Source < ma3 : riskLevel == "Low" ? ma1Source < ma4 : riskLevel == "Lowest" ? ma1 < ma2 and ma1 < ma3 and ma1 < ma4 : na

stopLprice = useStop == true ? strategy.position_avg_price * (1-stopPrct) : na
stopSprice = useStop == true ? strategy.position_avg_price * (1+stopPrct) : na

if (longCondition)
    strategy.entry("Long",strategy.long)
    strategy.exit("Long Stop", "Long", stop=stopLprice)    
if (shortCondition)
    strategy.entry("Short",strategy.short)
    strategy.exit("Short Stop", "Short", stop=stopSprice)

//End


Thêm nữa