Chiến lược quản lý tiền năng động đa yếu tố

Tác giả:ChaoZhang, Ngày: 2023-10-17 15:09:59
Tags:

img

Tổng quan

Chiến lược này tích hợp MACD, RSI, PSAR và các chỉ số kỹ thuật khác cùng với phương pháp quản lý tiền năng để theo dõi xu hướng và thực hiện các giao dịch đảo ngược trên nhiều khung thời gian.

Nguyên tắc

Chiến lược này sử dụng chỉ số PSAR để xác định hướng xu hướng. Sự chéo chéo giữa đường trung EMA và BB đóng vai trò là điểm xác nhận đầu tiên. Hướng của biểu đồ MACD đóng vai trò là điểm xác nhận thứ hai. Các khu vực mua quá mức và bán quá mức RSI đóng vai trò là điểm xác nhận thứ ba. Các tín hiệu giao dịch được tạo ra khi tất cả các điều kiện trên được đáp ứng.

Sau khi nhập vào vị trí, điểm lấy lợi nhuận và điểm dừng lỗ được thiết lập. Điểm dừng lỗ được xác định bằng cách nhân giá trị ATR bằng một số cố định. Điểm lấy lợi nhuận được tính theo cùng một cách. Trong khi đó, tỷ lệ lỗ lưu động được thiết lập. Khi lỗ đạt một tỷ lệ phần trăm nhất định của tổng vốn tài khoản, điểm dừng lỗ sẽ được kích hoạt.

Ngoài ra còn có tỷ lệ phần trăm cho lợi nhuận biến động. Khi lợi nhuận đạt một tỷ lệ phần trăm nhất định của tổng vốn tài khoản, lợi nhuận sẽ được kích hoạt.

Quản lý tiền động tính toán kích thước vị trí dựa trên tổng vốn hóa tài khoản, giá trị ATR và nhân được sử dụng để dừng lỗ.

Ưu điểm

  1. Xác nhận nhiều yếu tố tránh các sự đột phá sai và cải thiện độ chính xác nhập.

  2. Quản lý tiền năng kiểm soát rủi ro giao dịch duy nhất và bảo vệ tài khoản hiệu quả.

  3. Các điểm dừng lỗ và lấy lợi nhuận được thiết lập theo ATR, có thể được điều chỉnh dựa trên biến động thị trường.

  4. Các thiết lập tỷ lệ thua lỗ và lợi nhuận thay đổi khóa lợi nhuận và ngăn chặn rút tiền.

Rủi ro

  1. Kết hợp nhiều yếu tố có thể bỏ lỡ một số cơ hội giao dịch.

  2. Các tỷ lệ phần trăm cao có thể dẫn đến tổn thất lớn hơn.

  3. Các thiết lập giá trị ATR không chính xác có thể dẫn đến dừng lỗ và lấy điểm lợi nhuận quá rộng hoặc quá mạnh.

  4. Cài đặt quản lý tiền không đúng có thể dẫn đến kích thước vị trí quá lớn.

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

  1. Điều chỉnh trọng lượng yếu tố để cải thiện độ chính xác tín hiệu.

  2. Kiểm tra các cài đặt tham số phần trăm khác nhau để tìm kết hợp tối ưu.

  3. Chọn các nhân ATR hợp lý dựa trên các đặc điểm khác nhau của sản phẩm.

  4. Điều chỉnh năng động các thông số quản lý tiền dựa trên kết quả backtest.

  5. Tối ưu hóa cài đặt khung thời gian và thử nghiệm các phiên giao dịch.

Tóm lại

Chiến lược này tích hợp nhiều chỉ số kỹ thuật để xác định xu hướng và thêm quản lý tiền năng để kiểm soát rủi ro, nhận ra lợi nhuận ổn định trong nhiều khung thời gian. Nó có thể được tối ưu hóa hơn nữa bằng cách điều chỉnh cân nặng yếu tố, tham số kiểm soát rủi ro và cài đặt quản lý tiền dựa trên kết quả backtest.


/*backtest
start: 2023-09-16 00:00:00
end: 2023-10-16 00:00:00
period: 10m
basePeriod: 1m
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/
// © SoftKill21

//@version=4
strategy("EURUSD 1min strat RISK %% ", overlay=false, initial_capital = 1000)

// BACKTESTING RANGE
 
// From Date Inputs
fromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
fromMonth = input(defval = 6, title = "From Month", minval = 1, maxval = 12)
fromYear = input(defval = 2020, title = "From Year", minval = 1970)
 
// To Date Inputs
toDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
toMonth = input(defval = 12, title = "To Month", minval = 1, maxval = 12)
toYear = input(defval = 2020, title = "To Year", minval = 1970)
 
// Calculate start/end date and time condition
DST = 1 //day light saving for usa
//--- Europe
London = iff(DST==0,"0000-0900","0100-1000")
//--- America
NewYork = iff(DST==0,"0400-1500","0500-1600")
//--- Pacific
Sydney = iff(DST==0,"1300-2200","1400-2300")
//--- Asia
Tokyo = iff(DST==0,"1500-2400","1600-0100")

//-- Time In Range
timeinrange(res, sess) => time(res, sess) != 0

london = timeinrange(timeframe.period, London)
newyork = timeinrange(timeframe.period, NewYork)

startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finishDate = timestamp(toYear, toMonth, toDay, 00, 00)
time_cond = true
// 
// 


// rsi
length = input( 5 )
overSold = input( 23 )
overBought = input( 72 )
price = close

vrsi = rsi(price, length)
co = crossover(vrsi, overSold)
cu = crossunder(vrsi, overBought)

// macd
fast_length_macd = input(title="Fast Length", type=input.integer, defval=12)
slow_length_macd = input(title="Slow Length", type=input.integer, defval=26)
src_macd = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", 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)

// Plot colors
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350
col_macd = #0094ff
col_signal = #ff6a00

// Calculating
fast_ma = sma_source ? sma(src_macd, fast_length_macd) : ema(src_macd, fast_length_macd)
slow_ma = sma_source ? sma(src_macd, slow_length_macd) : ema(src_macd, slow_length_macd)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal

//plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )


// sar
start = input(0.02)
increment = input(0.02)
maximum = input(0.2)

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)
    


//plot(SAR, style=plot.style_cross, linewidth=3, color=color.orange)
//plot(nextBarSAR, style=plot.style_cross, linewidth=3, color=color.aqua)
//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)

//bb
length_bb = input(17, minval=1)
src_bb = input(close, title="Source")
mult_bb = input(2.0, minval=0.001, maxval=50, title="StdDev")
basis_bb = sma(src_bb, length_bb)
dev_bb = mult_bb * stdev(src_bb, length_bb)
upper_bb = basis_bb + dev_bb
lower_bb = basis_bb - dev_bb
offset = input(0, "Offset", type = input.integer, minval = -500, maxval = 500)
//plot(basis_bb, "Basis", color=#872323, offset = offset)
//p1_bb = plot(upper_bb, "Upper", color=color.teal, offset = offset)
//p2_bb = plot(lower_bb, "Lower", color=color.teal, offset = offset)

//fill(p1_bb, p2_bb, title = "Background", color=#198787, transp=95)

//ema

len_ema = input(10, minval=1, title="Length")
src_ema = input(close, title="Source")
offset_ema = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)
out_ema = ema(src_ema, len_ema)
//plot(out_ema, title="EMA", color=color.blue, offset=offset_ema) 
//out_ema e emaul
//basis_bb e middle de la bb
//hist e histograma
// rsi cu band0 cross pt rsi

// confirmarea

shortCondition = (uptrend==false and crossunder(ema(src_ema, len_ema),sma(src_bb, length_bb)) and  hist < 0  and vrsi <   overSold) //and time_cond
longCondition = (uptrend==true and crossover(ema(src_ema, len_ema),sma(src_bb, length_bb))  and hist > 0 and vrsi >  overBought ) //and time_cond

//tp=input(0.0025,type=input.float, title="tp")
//sl=input(0.001,type=input.float, title="sl")

//INDICATOR---------------------------------------------------------------------    
    //Average True Range (1. RISK)
atr_period = input(14, "Average True Range Period")
atr = atr(atr_period)

strategy.initial_capital = 50000

//MONEY MANAGEMENT--------------------------------------------------------------
balance = strategy.netprofit + strategy.initial_capital //current balance
floating = strategy.openprofit          //floating profit/loss
risk = input(2,type=input.float,title="Risk %")/100           //risk % per trade
isTwoDigit = input(false,"Is this a 2 digit pair? (JPY, XAU, XPD...")

equity_protector = input(1 ,type=input.float, title="Equity Protection %")/100  //equity protection %
equity_protectorTP = input(2 ,type=input.float, title="Equity TP %")/100  //equity protection %
multtp = input(5,type=input.float, title="multi atr tp")
multsl = input(5,type=input.float, title="multi atr sl")
stop = atr*100000*input(1,"SL X")* multsl    //Stop level
if(isTwoDigit)
    stop := stop/100
target = atr*100000*input(1,"TP X")*multtp    //Stop level
    //Calculate current DD and determine if stopout is necessary
equity_stopout = false

if(floating<0 and abs(floating/balance)>equity_protector)
    equity_stopout := true
    
equity_stopout2 = false
if(floating>0 and abs(floating/balance)>equity_protectorTP)
    equity_stopout2 := true
    
    //Calculate the size of the next trade
temp01 = balance * risk     //Risk in USD
temp02 = temp01/stop        //Risk in lots
temp03 = temp02*100000      //Convert to contracts
size = temp03 - temp03%1000 //Normalize to 1000s (Trade size)
if(size < 10000)
    size := 10000           //Set min. lot size

//TRADE EXECUTION---------------------------------------------------------------
strategy.close_all(equity_stopout, comment="equity sl", alert_message = "equity_sl")      //Close all trades w/equity protector
//strategy.close_all(equity_stopout2, comment="equity tp", alert_message = "equity_tp")      //Close all trades w/equity protector
is_open = strategy.opentrades > 0


strategy.entry("long",true,oca_name="a",when=longCondition and not is_open)  //Long entry
strategy.entry("short",false,oca_name="a",when=shortCondition and not is_open) //Short entry
    
strategy.exit("exit_long","long",loss=stop, profit=target)      //Long exit (stop loss)
strategy.close("long",when=shortCondition)            //Long exit (exit condition)
strategy.exit("exit_short","short",loss=stop, profit=target)      //Short exit (stop loss)
strategy.close("short",when=longCondition)            //Short exit (exit condition)


//strategy.entry("long", strategy.long,size,when=longCondition , comment="long" , alert_message = "long")
//strategy.entry("short", strategy.short, size,when=shortCondition , comment="short" , alert_message = "short")
 
//strategy.exit("closelong", "long" , profit = close * tp / syminfo.mintick,  alert_message = "closelong")
//strategy.exit("closeshort", "short" , profit = close * tp / syminfo.mintick, alert_message = "closeshort")
 
//strategy.exit("closelong", "long" ,size, profit = close * tp / syminfo.mintick, loss = close * sl / syminfo.mintick, alert_message = "closelong")
//strategy.exit("closeshort", "short" , size, profit = close * tp / syminfo.mintick, loss = close * sl / syminfo.mintick, alert_message = "closeshort")
 
//strategy.close("long" , when=not (time_cond), comment="time", alert_message = "closelong" )
//strategy.close("short" , when=not (time_cond), comment="time", alert_message = "closeshort")
//strategy.close_all(when=not (time_cond), comment ='time')



Thêm nữa