Chiến lược lưới đánh cá tăng cường

Tác giả:ChaoZhang, Ngày: 2023-09-12 10:49:00
Tags:

Chiến lược lưới đánh cá tăng cường

Chiến lược này cải thiện chiến lược Fish Net cổ điển bằng cách thêm ngưỡng tín hiệu mua / bán và dừng lỗ để tạo thành một hệ thống theo xu hướng hoàn chỉnh hơn.

Chiến lược Fish Net đánh giá xu hướng thị trường bằng cách tính toán lực trung tâm giá, phản ánh mối quan hệ giữa giá và khối lượng.

Chìa khóa trong việc tính toán lực trung tâm nằm trong mối quan hệ giữa giá và thời gian. Nói một cách đơn giản, những thay đổi giá gần đây có trọng lượng lớn hơn trong việc ảnh hưởng đến phán đoán xu hướng tổng thể, trong khi các giá cũ hơn có trọng lượng nhỏ hơn. Vì vậy, khi tính toán, trọng lượng suy giảm thời gian được nhân lên. Điều này làm cho các giao dịch xảy ra ở mức cao hơn ảnh hưởng đến tổng phán đoán nhiều hơn.

Nhưng Fish Net ban đầu chỉ đánh giá dài / ngắn dựa trên hướng của đường cong trung tâm, dễ dàng bị mắc kẹt trong các chuyển động sang bên.

Ngoài ra, phiên bản được cải tiến thực hiện một cơ chế kết hợp của việc dừng lỗ và dừng lỗ cố định cho việc thoát ra. Sau khi bước vào một xu hướng, dừng lỗ có thể tiếp tục điều chỉnh cùng với hành động giá, đạt được kiểm soát rủi ro năng động.

Tất nhiên, chỉ số lực trung tâm có khả năng hạn chế trong các thị trường phức tạp, và việc dừng lại cũng có thể bị xâm nhập nếu được đặt không đúng cách, vì vậy các nhà giao dịch cần phải cảnh giác và tối ưu hóa các thông số kịp thời.


/*backtest
start: 2023-09-04 00:00:00
end: 2023-09-11 00:00:00
period: 30m
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3
// Copyright nilux: https://www.tradingview.com/u/nilux/
// Based on the original of dasanc: https://www.tradingview.com/u/dasanc/

strategy("FSCG-TSSL", "FSCG-TSSL Mod Backtest", default_qty_type = strategy.percent_of_equity, default_qty_value = 100, initial_capital = 100000, slippage = 5)
Price = input.source(close, "Source")
Length = input(20,"Period")
transform = input("Inphase-Quadrature","Use Transform?",options=["Hilbert","Inphase-Quadrature","False"])
min = input(108,"Min. Period")
buyTreshold = input(-2.41, title = "Buy Treshold (-)", type = float, defval=-2.0, minval = -2.50, maxval = -0.01, step = 0.01)
sellTreshold = input(2.43, title = "Sell Treshold (+)", type = float, defval=2.0, minval = 0.01, maxval = 2.50, step = 0.01)

// === TSSL ===
fixedSL = input(title="SL Activation", defval=300)
trailSL = input(title="SL Trigger", defval=1)
fixedTP = input(title="TP Activation", defval=150)
trailTP = input(title="TP Trigger", defval=50)

// === BACKTEST RANGE ===
FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
FromDay   = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear  = input(defval = 2019, title = "From Year", minval = 2015)
ToMonth   = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 9999, title = "To Year", minval = 2015)
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)
window()  => time >= start and time <= finish ? true : false

getIQ(src,min,max) =>
    PI = 3.14159265359
    P = src - src[7]
    lenIQ = 0.0
    lenC = 0.0
    imult = 0.635
    qmult = 0.338
    inphase = 0.0
    quadrature = 0.0
    re = 0.0
    im = 0.0
    deltaIQ = 0.0
    instIQ = 0.0
    V = 0.0
    
    inphase := 1.25*(P[4] - imult*P[2]) + imult*nz(inphase[3])
    quadrature := P[2] - qmult*P + qmult*nz(quadrature[2])
    re := 0.2*(inphase*inphase[1] + quadrature*quadrature[1]) + 0.8*nz(re[1])
    im := 0.2*(inphase*quadrature[1] - inphase[1]*quadrature) + 0.8*nz(im[1])
    if (re!= 0.0)
        deltaIQ := atan(im/re)
    for i=0 to max
        V := V + deltaIQ[i]
        if (V > 2*PI and instIQ == 0.0)
            instIQ := i
    if (instIQ == 0.0)
        instIQ := nz(instIQ[1])
    lenIQ := 0.25*instIQ + 0.75*nz(lenIQ[1],1)
    length = lenIQ<min ? min : lenIQ


getHT(src) =>
    Price = src
    Imult = .635
    Qmult = .338
    PI = 3.14159
    InPhase = 0.0
    Quadrature = 0.0
    Phase = 0.0
    DeltaPhase = 0.0
    InstPeriod = 0.0
    Period = 0.0
    Value4 = 0.0
    
    if(n > 5)
        //Detrend Price
        Value3 = Price - Price[7]
        //Compute InPhase and Quadrature components
        InPhase := 1.25*(Value3[4] - Imult*Value3[2]) + Imult*nz(InPhase[3])
        Quadrature := Value3[2] - Qmult*Value3 + Qmult*nz(Quadrature[2])
        //Use ArcTangent to compute the current phase
        if(abs(InPhase + InPhase[1]) > 0)
            Phase := 180/PI * atan(abs((Quadrature + Quadrature[1]) / (InPhase + InPhase[1])))
        //Resolve the ArcTangent ambiguity
        if(InPhase < 0 and Quadrature > 0)
            Phase := 180 - Phase
        if(InPhase < 0 and Quadrature < 0)
            Phase := 180 + Phase
        if(InPhase > 0 and Quadrature < 0)
            Phase := 360 - Phase
        //Compute a differential phase, resolve phase wraparound, and limit delta phase errors
        DeltaPhase := Phase[1] - Phase
        if(Phase[1] < 90 and Phase > 270)
            DeltaPhase := 360 + Phase[1] - Phase
        if(DeltaPhase < 1)
            DeltaPhase := 1
        if(DeltaPhase > 60)
            DeltaPhase := 60
        //Sum DeltaPhases to reach 360 degrees. The sum is the instantaneous period.
        for i = 0 to 50
            Value4 := Value4 + DeltaPhase[i]
            if(Value4 > 360 and InstPeriod == 0)
                InstPeriod := i
        //Resolve Instantaneous Period errors and smooth
        if(InstPeriod == 0)
            InstPeriod = nz(InstPeriod[1])
        Period := .25*(InstPeriod) + .75*Period[1]
    Period
    
//Get highest val in period
getHighest(src, len)=>
    H = src[len]
    for i=0 to len
        if src[i]>H
            H := src[i]
    H
    
//Get lowest val in period
getLowest(src, len)=>
    L = src[len]
    for i=0 to len
        if src[i]<L
            L := src[i]
    L

if transform == "Hilbert"
    Length := round(getHT(Price)/2)
if transform == "Inphase-Quadrature"
    Length := round(getIQ(Price,min,50)/2)
if Length<min
    Length := min
    

Num = 0.0
Denom = 0.0
CG = 0.0
MaxCG = 0.0
MinCG = 0.0
Value1 = 0.0
Value2 = 0.0
Value3 = 0.0
for i = 0 to Length - 1
    Num := Num + (1 + i)*(Price[i])
    Denom := Denom + (Price[i])
if(Denom != 0)
    CG := -Num/Denom + (Length + 1) / 2
MaxCG := getHighest(CG, Length)
MinCG := getLowest(CG, Length)
if(MaxCG != MinCG)
    Value1 := (CG - MinCG) / (MaxCG - MinCG)
Value2 := (4*Value1 + 3*Value1[1] + 2*Value1[2] + Value1[3]) / 10
Value3 := .5*log((1+1.98*(Value2-.5))/(1-1.98*(Value2-.5)))

plot(Value3, "CG",orange, linewidth=2)
plot(Value3[1], "Trigger",green, linewidth=2)
hline(0,color=color(black,60))
hline(2,linestyle=hline.style_solid,color=color(black,70))
hline(-2,linestyle=hline.style_solid,color=color(black,70))

sell = crossover(Value3[1],Value3) and Value3 > sellTreshold
buy = crossunder(Value3[1],Value3) and Value3 < buyTreshold

strategy.entry("Long", strategy.long, when= buy and window())
strategy.exit("Exit", loss=fixedSL, trail_offset=trailTP, trail_points=fixedTP)
strategy.exit("Exit", when= sell)

strategy.entry("Short", strategy.short, when= sell and window())
strategy.exit("Exit", loss=fixedSL, trail_offset=trailTP, trail_points=fixedTP)
strategy.exit("Exit", when= buy)

Thêm nữa