Chiến lược dừng lỗ và dừng lãi theo dõi xu hướng đa chỉ báo


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

Tổng quan về chiến lược

Chiến lược theo dõi xu hướng đa chỉ số Stop Loss bằng cách tích hợp nhiều chỉ số như EMA, MACD, OBV và PSAR để đánh giá hướng xu hướng hiện tại và thiết lập Stop Loss sau khi đưa ra thị trường để kiểm soát rủi ro. Chiến lược này tích hợp nhiều yếu tố để xác nhận tín hiệu giao dịch, kiểm soát chặt chẽ lợi nhuận và rủi ro của mỗi giao dịch trong khi theo dõi xu hướng.

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

  1. Xác định hướng xu hướng: xác định xu hướng khi các chỉ số như EMA, MACD, OBV và PSAR đều phát ra tín hiệu đa đầu hoặc vô đầu đồng bộ.

  2. Quy tắc nhập cảnh: làm nhiều khi có tín hiệu đa đầu, làm trống khi có tín hiệu trống.

  3. Đặt điểm dừng lỗ: Đặt điểm dừng lỗ của giao dịch này theo khoảng cách điểm của chỉ số PSAR sau khi nhập.

  4. Quy tắc xuất phát: Hạ điểm sau khi kích hoạt dừng hoặc dừng lỗ.

Ưu điểm của chiến lược này là sử dụng phán đoán đa chỉ số để phát ra tín hiệu giao dịch có xác suất cao, trong khi chiến lược dừng lỗ có thể chủ động kiểm soát rủi ro với giả định đảm bảo lợi nhuận. Các danh mục chỉ số và cài đặt tham số có thể được tối ưu hóa theo tình hình thị trường.

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

  • Kết hợp đa chỉ số phát ra tín hiệu xác suất cao

  • Chiến lược dừng lỗ chủ động kiểm soát rủi ro

  • Đặt điểm dừng lỗ dựa trên điểm PSA

  • Tính linh hoạt để tối ưu hóa các chỉ số và tham số

  • Có thể tiếp tục lợi nhuận trong xu hướng

Cảnh báo rủi ro

  • Xác định kết hợp đa chỉ số phức tạp hơn

  • Có một số rủi ro về tín hiệu chậm trễ

  • Cần cảnh giác với sự đảo ngược và biến động của thị trường

  • Cài đặt tham số cần được kiểm tra và tối ưu hóa liên tục

Tóm tắt

Chiến lược dừng lỗ theo dõi xu hướng đa chỉ số đã cải thiện toàn diện đối với giao dịch xu hướng, không chỉ giúp cải thiện độ chính xác của quyết định mà còn có thể chủ động kiểm soát rủi ro. Bằng cách thử nghiệm nhiều lần các thị trường và tham số khác nhau, chiến lược này có thể được tối ưu hóa thành một hệ thống định lượng ổn định và đáng tin cậy.

Mã nguồn chiến lược
/*backtest
start: 2023-08-15 00:00:00
end: 2023-09-14 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/
// © exlux99

//@version=4

strategy("Scalping FOrex full strategy with risk management",overlay=true,initial_capital = 10000, default_qty_type= strategy.percent_of_equity, default_qty_value = 100, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.cash_per_contract ,commission_value=0.00005)

//VOLUME o
shortlen = input(1, minval=1, title = "Short Length OBV")
longlen = input(8, minval=1, title = "Long Length OBV")
upColor = #2196F3//input(#2196F3, "Color Up")
dnColor = #6B1599//input(#6B1599, "Color Down")
f_normGradientColor(_series, _crossesZero, _colorNormLen, _dnColor, _upColor) =>
    _dnValue = _crossesZero?-100:0
    _mult = 0.0
    _lowest = lowest(_colorNormLen)
    _highest = highest(_colorNormLen)
    _diff1 = close - _lowest
    _diff2 = _highest - _lowest
    if _diff2 > 0
        _mult := _diff1 / _diff2 * 100
    color.from_gradient(sign(_series) * _mult, _dnValue, 100, _dnColor, _upColor)
shorta = ema(volume, shortlen)
longa = ema(volume, longlen)
osc = 100 * (shorta - longa) / longa

start = input(0.1, title="PSAR START")
increment = input(0.05,title="PSAR INC")
maximum = input(0.3, title="PSAR MAX")
// multitp=input(1)
// multisl=input(1)
var bool uptrend = na
var float EP = na
var float SAR = na
var float AF = start
var float nextBarSAR = na
if bar_index > 0
    firstTrendBar = false
    SAR := nextBarSAR
    if bar_index == 1
        float prevSAR = na
        float prevEP = na
        lowPrev = low[1]
        highPrev = high[1]
        closeCur = close
        closePrev = close[1]
        if closeCur > closePrev
            uptrend := true
            EP := high
            prevSAR := lowPrev
            prevEP := high
        else
            uptrend := false
            EP := low
            prevSAR := highPrev
            prevEP := low
        firstTrendBar := true
        SAR := prevSAR + start * (prevEP - prevSAR)
    if uptrend
        if SAR > low
            firstTrendBar := true
            uptrend := false
            SAR := max(EP, high)
            EP := low
            AF := start
    else
        if SAR < high
            firstTrendBar := true
            uptrend := true
            SAR := min(EP, low)
            EP := high
            AF := start
    if not firstTrendBar
        if uptrend
            if high > EP
                EP := high
                AF := min(AF + increment, maximum)
        else
            if low < EP
                EP := low
                AF := min(AF + increment, maximum)
    if uptrend
        SAR := min(SAR, low[1])
        if bar_index > 1
            SAR := min(SAR, low[2])
    else
        SAR := max(SAR, high[1])
        if bar_index > 1
            SAR := max(SAR, high[2])
    nextBarSAR := SAR + AF * (EP - SAR)
//    if barstate.isconfirmed
    
//         if uptrend
//             strategy.entry("ParSE", strategy.short, stop=nextBarSAR, comment="ParSE")
//             strategy.cancel("ParLE")
//         else
//             strategy.entry("ParLE", strategy.long, stop=nextBarSAR, comment="ParLE")
//             strategy.cancel("ParSE")
//plot(SAR, style=plot.style_cross, linewidth=3, color=color.orange)


psarshort = close- SAR
psarlong= SAR-close

 


lena = input(200, minval=1, title="Length EMA")
srca = input(close, title="Source")
out = ema(srca, lena)

fast_length = input(title="Fast Length MACD", type=input.integer, defval=12)
slow_length = input(title="Slow Length MACD", type=input.integer, defval=25)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing MACD", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title="Simple MA (Oscillator)", type=input.bool, defval=true)
sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=true)
// Calculating
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal


long = hist[1]<0 and hist > 0 and close > out and uptrend and osc < 0
short = hist[1]>0 and hist< 0 and close < out and not uptrend and osc >0

// ------------------------- Strategy Logic --------------------------------- //
var longOpeneds = false
var shortOpeneds = false
var int timeOfBuys = na
var float tpLong = na
var float slLong = na
var int entrys = na

 

longConditions = long and not longOpeneds and entrys<100

if longConditions
    longOpeneds := true
    timeOfBuys := time
    tpLong := close+ (psarshort) //* multitp)
    slLong := close- (psarshort)//*multisl)
    entrys:=entrys+1


tpLongTrigger = (longOpeneds[1] and (crossover(close, tpLong) or crossover( high,tpLong)))
slLongTrigger = (longOpeneds[1] and (crossunder(close, slLong) or crossunder( low,slLong)))

longExitSignals =  slLongTrigger or tpLongTrigger  or short
exitLongConditions = longOpeneds[1] and longExitSignals

if exitLongConditions
    longOpeneds := false
    timeOfBuys := na
    tpLong := na
    slLong := na
    
if(short)
    entrys:=0

//short
// ------------------------- Strategy Logic --------------------------------- //
var longOpenedss = false
// var shortOpeneds = false
var int timeOfBuyss = na
var float tpLongs = na
var float slLongs = na
var int entry = na

 

longConditionss = short and not longOpenedss and entry<100

if longConditionss
    longOpenedss := true
    timeOfBuyss := time
    tpLongs := close- (psarlong)//*multitp )
    slLongs := close+ (psarlong)//*multisl)
    entry:=1

 

tpLongTriggers = (longOpenedss[1] and ( crossunder(close, tpLongs) or crossunder( low,tpLongs)))
slLongTriggers = (longOpenedss[1] and (crossover(close, slLongs) or crossover( high,slLongs)))

longExitSignalss =  slLongTriggers or tpLongTriggers  or long
exitLongConditionss = longOpenedss[1] and longExitSignalss

if exitLongConditionss
    longOpenedss := false
    timeOfBuyss := na
    tpLongs := na
    slLongs := na

if(long)
    entry:=0
    
longEntry=input(true)
shortEntry=input(true)

if(longEntry)
    strategy.entry("long",1,when=longConditions)
    strategy.close('long',when=exitLongConditions)

if(shortEntry)
    strategy.entry("short",0,when=longConditionss)
    strategy.close("short",when=exitLongConditionss)