Chiến lược giao dịch đảo ngược Gradient


Ngày tạo: 2023-10-09 15:10:39 sửa đổi lần cuối: 2023-10-09 15:10:39
sao chép: 0 Số nhấp chuột: 782
1
tập trung vào
1617
Người theo dõi

Tổng quan

Chiến lược giao dịch đảo ngược bậc thang là một chiến lược theo dõi xu hướng sử dụng hệ thống đường thẳng để phát ra tín hiệu mua bán. Nó đánh giá xu hướng giá hiện tại bằng cách tính toán đường trung bình di chuyển trong các chu kỳ khác nhau, mua hoặc bán tại điểm biến đổi xu hướng. Chiến lược này nhằm mục đích nắm bắt xu hướng đường dài và trung bình, giao dịch khi xu hướng biến đổi.

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

Chiến lược này tạo ra tín hiệu giao dịch bằng cách tính hai đường trung bình di chuyển, một đường trung bình có chu kỳ dài hơn làm đường chuẩn và một đường trung bình có chu kỳ ngắn hơn.

  1. Tính một đường trung bình chuẩn với tham số chu kỳ là len1, đại diện cho xu hướng chu kỳ dài hơn.

  2. Tính một đường trung bình tín hiệu với tham số chu kỳ là len2, đại diện cho xu hướng chu kỳ ngắn hơn, len2

  3. Khi đường trung bình ngắn đi từ phía trên xuống đường trung bình dài, giao dịch ngoại hối cho thấy xu hướng đảo ngược và giá cổ phiếu có thể đi xuống.

  4. Khi đường trung bình ngắn phá vỡ đường trung bình dài từ phía dưới, hãy giao dịch nhiều hơn, cho thấy xu hướng đảo ngược và giá cổ phiếu có thể tăng lên.

  5. Khi giá quay trở lại gần đường trung bình dài, đặt lỗ hổng.

  6. Do đó, các giao dịch xu hướng được thực hiện bằng cách sử dụng các đường trung bình di chuyển để nắm bắt các điểm biến của xu hướng đường dài và trung bình.

Lợi thế chiến lược

  1. Sử dụng hệ thống chéo đường trung bình, có thể nắm bắt hiệu quả xu hướng đảo ngược của chu kỳ trung gian.

  2. Các tín hiệu giao dịch đơn giản, rõ ràng và dễ nắm bắt.

  3. Các tham số chu kỳ có thể tùy chỉnh để phù hợp với các giống và thương nhân khác nhau.

  4. Có thể thiết lập Stop Loss Stop để kiểm soát mọi rủi ro.

  5. Không cần dự đoán giá cổ phiếu, chỉ quan tâm đến xu hướng.

Rủi ro chiến lược

  1. Trong trường hợp động đất, đường trung bình giao nhau thường xuyên, tạo ra nhiều tín hiệu giả.

  2. Không có lợi nhuận từ biến động giá ngắn hạn, chỉ phù hợp với giao dịch xu hướng đường dài.

  3. Hệ thống đường trung bình bị tụt lại sau sự thay đổi giá và không thể bắt kịp sự biến đổi xu hướng.

  4. Có thể không giao dịch thường xuyên và không kiếm được nhiều tiền.

  5. Cần điều chỉnh các tham số theo thời gian để điều chỉnh tần suất giao dịch cho thị trường.

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

  1. Kết hợp với các chỉ số kỹ thuật khác như MACD, KD, v.v. để xác nhận, lọc các tín hiệu giả.

  2. Thêm bộ lọc xu hướng, chỉ phát tín hiệu khi xu hướng rõ ràng.

  3. Giao dịch theo nhiều khung thời gian, các chu kỳ khác nhau hoạt động đồng thời, kết hợp nhiều cơ hội giao dịch hơn.

  4. Các tham số tối ưu hóa động để tham số chu kỳ theo dõi sự thay đổi của thị trường.

  5. Một mô hình học máy được sử dụng để giúp xác định xu hướng đảo ngược.

Tóm tắt

Chiến lược giao dịch đảo chiều bậc thang nói chung là một chiến lược theo dõi xu hướng đơn giản và thực tế. Nó đánh giá điểm đảo chiều xu hướng trung bình bằng cách giao ngang ngang để nắm bắt xu hướng giá trong một chu kỳ thời gian dài. Chiến lược này dễ thực hiện, tín hiệu giao dịch rõ ràng, nhưng cũng có một số hạn chế. Bằng cách liên tục tối ưu hóa các tham số, kết hợp các chỉ số kỹ thuật khác và giới thiệu học máy, hiệu quả của chiến lược có thể được nâng cao và nắm bắt cơ hội thị trường tốt hơn.

Mã nguồn chiến lược
/*backtest
start: 2022-10-02 00:00:00
end: 2023-10-08 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3
//Created by 100kiwi
strategy(title = "TrapTrading", overlay = true)

/////////////////////////////////////////////////////////////////////
// COMPONENT CODE START
//*******************************************************************
// Backtesting Period Selector | Component by pbergden
//*******************************************************************
testStartYear = input(2015, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)

testStopYear = input(2018, "Backtest Stop Year")
testStopMonth = input(12, "Backtest Stop Month")
testStopDay = input(31, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)

// A switch to control background coloring of the test period
testPeriodBackground = input(title="Color Background?", type=bool, defval=true)
testPeriodBackgroundColor = testPeriodBackground and (time >= testPeriodStart) and (time <= testPeriodStop) ? #00FF00 : na
bgcolor(testPeriodBackgroundColor, transp=97)

testPeriod() => true
// COMPONENT CODE STOP
/////////////////////////////////////////////////////////////////////

// input
buySide = input(defval = true, title = "Trade Direction (ON: Buy Side OFF: Sell Side)", type = bool)
counterTrend  = input(defval = true, title = "Trade Mode (ON: Counter Trend OFF: Trend Following)", type = bool)
len1 = input(defval = 14, title = "Period")
multiple = input(defval = 1.4, title = "Multiple")

m1 = close - close[len1]
controlPoint = counterTrend ? lowest(abs(m1), len1) == abs(m1) : highest(abs(m1), len1) == abs(m1)
baseLine = valuewhen(controlPoint, avg(close, close[len1]), 0)

// trap line
atr = atr(len1)
line1Up = baseLine + (atr * multiple)
line2Up = baseLine + (atr * 2 * multiple)
line3Up = baseLine + (atr * 3 * multiple)
line4Up = baseLine + (atr * 4 * multiple)
line5Up = baseLine + (atr * 5 * multiple)
line6Up = baseLine + (atr * 6 * multiple)
line7Up = baseLine + (atr * 7 * multiple)
line8Up = baseLine + (atr * 8 * multiple)
line9Up = baseLine + (atr * 9 * multiple)
line10Up = baseLine + (atr * 10 * multiple)
line1Down = baseLine - (atr * multiple)
line2Down = baseLine - (atr * 2 * multiple)
line3Down = baseLine - (atr * 3 * multiple)
line4Down = baseLine - (atr * 4 * multiple)
line5Down = baseLine - (atr * 5 * multiple)
line6Down = baseLine - (atr * 6 * multiple)
line7Down = baseLine - (atr * 7 * multiple)
line8Down = baseLine - (atr * 8 * multiple)
line9Down = baseLine - (atr * 9 * multiple)
line10Down = baseLine - (atr * 9 * multiple)

// draw
color = close >= baseLine ? teal : red
barcolor(controlPoint ? yellow : na, title = "Candle Color")

plot(baseLine, title = "Base Line", color = white, linewidth = 4, style = stepline, transp = 0)
plot(line1Up, title = "1Up Line", color = green, linewidth = 1, style = stepline, transp = 0)
plot(line2Up, title = "2Up Line", color = green, linewidth = 1, style = stepline, transp = 0)
plot(line3Up, title = "3Up Line", color = green, linewidth = 1, style = stepline, transp = 0)
plot(line4Up, title = "4Up Line", color = green, linewidth = 1, style = stepline, transp = 0)
plot(line5Up, title = "5Up Line", color = green, linewidth = 1, style = stepline, transp = 0)
plot(line6Up, title = "6Up Line", color = green, linewidth = 1, style = stepline, transp = 0)
plot(line7Up, title = "7Up Line", color = green, linewidth = 1, style = stepline, transp = 0)
plot(line8Up, title = "8Up Line", color = green, linewidth = 1, style = stepline, transp = 0)
plot(line9Up, title = "9Up Line", color = green, linewidth = 1, style = stepline, transp = 0)
plot(line10Up, title = "10Up Line", color = green, linewidth = 1, style = stepline, transp = 0)
plot(line1Down, title = "1Down Line", color = red, linewidth = 1, style = stepline, transp = 0)
plot(line2Down, title = "2Down Line", color = red, linewidth = 1, style = stepline, transp = 0)
plot(line3Down, title = "2Down Line", color = red, linewidth = 1, style = stepline, transp = 0)
plot(line4Down, title = "4Down Line", color = red, linewidth = 1, style = stepline, transp = 0)
plot(line5Down, title = "5Down Line", color = red, linewidth = 1, style = stepline, transp = 0)
plot(line6Down, title = "6Down Line", color = red, linewidth = 1, style = stepline, transp = 0)
plot(line7Down, title = "7Down Line", color = red, linewidth = 1, style = stepline, transp = 0)
plot(line8Down, title = "8Down Line", color = red, linewidth = 1, style = stepline, transp = 0)
plot(line9Down, title = "9Down Line", color = red, linewidth = 1, style = stepline, transp = 0)
plot(line10Down, title = "10Down Line", color = red, linewidth = 1, style = stepline, transp = 0)

// strategy code
if testPeriod() and buySide
    strategy.exit("Exit Long0", from_entry = "Long0", qty = 1, limit = line2Up)
    strategy.exit("Exit Long1", from_entry = "Long1", qty = 1, limit = line1Up)
    strategy.exit("Exit Long2", from_entry = "Long2", qty = 1, limit = baseLine)
    strategy.exit("Exit Long3", from_entry = "Long3", qty = 1, limit = line1Down)
    strategy.exit("Exit Long4", from_entry = "Long4", qty = 1, limit = line2Down)
    strategy.exit("Exit Long5", from_entry = "Long5", qty = 1, limit = line3Down)
    strategy.exit("Exit Long6", from_entry = "Long6", qty = 1, limit = line4Down)
    strategy.exit("Exit Long7", from_entry = "Long7", qty = 1, limit = line5Down)
    strategy.exit("Exit Long8", from_entry = "Long8", qty = 1, limit = line6Down)
    strategy.exit("Exit Long9", from_entry = "Long9", qty = 1, limit = line7Down)
    strategy.exit("Exit Long10", from_entry = "Long10", qty = 1, limit = line8Down)
    strategy.order("Long0", strategy.long, qty = 1, limit = baseLine, when = strategy.position_size <= 0)
    strategy.order("Long1", strategy.long, qty = 1, limit = line1Down, when = strategy.position_size <= 1)
    strategy.order("Long2", strategy.long, qty = 1, limit = line2Down, when = strategy.position_size <= 2)
    strategy.order("Long3", strategy.long, qty = 1, limit = line3Down, when = strategy.position_size <= 3)
    strategy.order("Long4", strategy.long, qty = 1, limit = line4Down, when = strategy.position_size <= 4)
    strategy.order("Long5", strategy.long, qty = 1, limit = line5Down, when = strategy.position_size <= 5)
    strategy.order("Long6", strategy.long, qty = 1, limit = line6Down, when = strategy.position_size <= 6)
    strategy.order("Long7", strategy.long, qty = 1, limit = line7Down, when = strategy.position_size <= 7)
    strategy.order("Long8", strategy.long, qty = 1, limit = line8Down, when = strategy.position_size <= 8)
    strategy.order("Long9", strategy.long, qty = 1, limit = line9Down, when = strategy.position_size <= 9)
    strategy.order("Long10", strategy.long, qty = 1, limit = line10Down, when = strategy.position_size <= 10)
else
    if testPeriod() and not buySide
        strategy.exit("Exit Short0", from_entry = "Short0", qty = 1, limit = line2Down)
        strategy.exit("Exit Short1", from_entry = "Short1", qty = 1, limit = line1Down)
        strategy.exit("Exit Short2", from_entry = "Short2", qty = 1, limit = baseLine)
        strategy.exit("Exit Short3", from_entry = "Short3", qty = 1, limit = line1Up)
        strategy.exit("Exit Short4", from_entry = "Short4", qty = 1, limit = line2Up)
        strategy.exit("Exit Short5", from_entry = "Short5", qty = 1, limit = line3Up)
        strategy.exit("Exit Short6", from_entry = "Short6", qty = 1, limit = line4Up)
        strategy.exit("Exit Short7", from_entry = "Short7", qty = 1, limit = line5Up)
        strategy.exit("Exit Short8", from_entry = "Short8", qty = 1, limit = line6Up)
        strategy.exit("Exit Short9", from_entry = "Short9", qty = 1, limit = line7Up)
        strategy.exit("Exit Short10", from_entry = "Short10", qty = 1, limit = line8Up)
        strategy.order("Short0", strategy.short, qty = 1, limit = baseLine, when = strategy.position_size >= 0)
        strategy.order("Short1", strategy.short, qty = 1, limit = line1Up, when = strategy.position_size >= -1)
        strategy.order("Short2", strategy.short, qty = 1, limit = line2Up, when = strategy.position_size >= -2)
        strategy.order("Short3", strategy.short, qty = 1, limit = line3Up, when = strategy.position_size >= -3)
        strategy.order("Short4", strategy.short, qty = 1, limit = line4Up, when = strategy.position_size >= -4)
        strategy.order("Short5", strategy.short, qty = 1, limit = line5Up, when = strategy.position_size >= -5)
        strategy.order("Short6", strategy.short, qty = 1, limit = line6Up, when = strategy.position_size >= -6)
        strategy.order("Short7", strategy.short, qty = 1, limit = line7Up, when = strategy.position_size >= -7)
        strategy.order("Short8", strategy.short, qty = 1, limit = line8Up, when = strategy.position_size >= -8)
        strategy.order("Short9", strategy.short, qty = 1, limit = line9Up, when = strategy.position_size >= -9)
        strategy.order("Short10", strategy.short, qty = 1, limit = line10Up, when = strategy.position_size >= -10)