Chiến lược dừng thua lỗ

Tác giả:ChaoZhang, Ngày: 2024-01-12 10:47:38
Tags:

img

Tổng quan

Chiến lược này tính toán trung bình di chuyển nhanh và chậm để xác định xu hướng. Nó đi dài khi trung bình di chuyển nhanh vượt qua trung bình di chuyển chậm, và thiết lập một lệnh dừng lỗ động để khóa lợi nhuận khi giá thay đổi một tỷ lệ phần trăm nhất định.

Chiến lược logic

Chiến lược này sử dụng đường chéo vàng của các đường trung bình di chuyển nhanh và chậm để xác định sự khởi đầu của xu hướng tăng. Cụ thể, nó tính toán đường trung bình di chuyển đơn giản của giá đóng trong một khoảng thời gian nhất định, so sánh các giá trị của đường trung bình di chuyển nhanh và chậm, và đánh giá sự khởi đầu của xu hướng tăng khi đường trung bình di chuyển nhanh vượt qua đường trung bình di chuyển chậm.

Sau khi mở một vị trí dài, chiến lược không đặt một mức dừng lỗ cố định, nhưng sử dụng một mức dừng lỗ theo dõi năng động để khóa lợi nhuận. Dòng dừng lỗ được đặt dựa trên: Giá cao nhất * (1 - Tỷ lệ Stop Loss). Điều này cho phép đường dừng lỗ di chuyển lên khi giá tăng. Khi giá giảm một tỷ lệ phần trăm nhất định, mức dừng lỗ sẽ được kích hoạt để thoát khỏi vị trí.

Ưu điểm của phương pháp này là nó cho phép theo đuổi xu hướng tăng không giới hạn, trong khi khóa lợi nhuận khi chúng đạt đến một mức nhất định thông qua dừng lỗ.

Phân tích lợi thế

Những lợi thế chính của chiến lược dừng lỗ kéo theo này là:

  1. Nó cho phép theo đuổi các xu hướng không giới hạn mà không bỏ lỡ các động thái lớn.

  2. Nó khóa lợi nhuận bằng cách đặt tỷ lệ stop loss. Chỉ đơn giản là theo đuổi xu hướng mà không có stop loss có thể dẫn đến tổn thất khi xu hướng kết thúc.

  3. Nó linh hoạt hơn so với dừng lỗ cố định. Dừng cố định chỉ có thể được đặt ở một mức giá, trong khi mức dừng lỗ này di chuyển với mức giá cao nhất.

  4. Nó có rủi ro pullback thấp hơn. Các điểm dừng cố định thường ở xa mức giá cao nhất, dẫn đến việc dừng sớm trên các điểm rút bình thường.

Phân tích rủi ro

Chiến lược này cũng có một số rủi ro:

  1. Chỉ số được sử dụng cho tín hiệu nhập cảnh có thể không ổn định và tạo ra tín hiệu sai.

  2. Chỉ có một cách tiếp cận dừng lỗ duy nhất mà không xem xét các yếu tố khác.

  3. Không có mục tiêu lợi nhuận, chỉ dựa vào việc dừng lỗ.

  4. Các thông số như thời gian trung bình động cần tối ưu hóa thêm.

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

Chiến lược có thể được cải thiện trong một số lĩnh vực:

  1. Thêm thêm các chỉ số để xác nhận các mục nhập và tránh các tín hiệu sai, ví dụ như khối lượng.

  2. Thêm lợi nhuận khi lợi nhuận đạt một tỷ lệ phần trăm nhất định.

  3. Cải thiện an toàn dừng lỗ bằng cách điều chỉnh năng động khoảng cách dừng trong các sự kiện thị trường đặc biệt.

  4. Tối ưu hóa các tham số như các công cụ giao dịch và các phiên giao dịch. Các sản phẩm và phiên giao dịch khác nhau đòi hỏi điều chỉnh tham số.

  5. Thêm máy học để điều chỉnh động các thông số và tối ưu hóa các chỉ số và mức dừng lỗ tự động.

Tóm lại

Phương pháp này được sử dụng để xác định xu hướng của thị trường. Việc sử dụng trung bình chuyển động nhanh và chậm để xác định xu hướng là một cách tiếp cận cổ điển. Việc dừng lỗ cũng có hiệu quả trong việc khóa lợi nhuận và giảm rủi ro. Tuy nhiên, cần phải thử nghiệm và tối ưu hóa liên tục các chỉ số và tham số để làm cho chiến lược có lợi nhuận nhất quán. Đồng thời, cần phải phòng ngừa những thay đổi lớn trên thị trường có thể làm vô hiệu hóa chiến lược bằng cách cải thiện logic và khung tổng thể và thêm các biện pháp bảo vệ.


/*backtest
start: 2023-12-01 00:00:00
end: 2023-12-31 23:59:59
period: 4h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 
//  -----------------------------------------------------------------------------
//  Copyright 2021 Iason Nikolas | jason5480
//  Trainiling Take Profit Trailing Stop Loss script may be freely distributed under the MIT license.
//
//  Permission is hereby granted, free of charge, 
//  to any person obtaining a copy of this software and associated documentation files (the "Software"), 
//  to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, 
//  publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
//  subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
//  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//  -----------------------------------------------------------------------------
//
//  Authors:  @jason5480
//  Revision: v1.0.0
//  Date:     05-May-2021
//
//  Description
//  =============================================================================
//  This strategy will go long if fast MA crosses over slow MA.
//  The strategy will exit from long position when the price increases by a fixed percentage.
//  If the trailing take profit is checked then the strategy instead of setting a limit order in a predefined price (based on the percentage)
//  it will follow the price with small steps (percentagewise)
//  If the price drops by this percentage then the exit order will be executed
//
//  The strategy has the following parameters:
//
//  Fast SMA Length - How many candles back to calculte the fast SMA.
//  Slow SMA Length - How many candles back to calculte the slow SMA.
//  Enable Trailing - Enable or disable the trailing.
//  Stop Loss % - The percentage of the price decrease to set the stop loss price target for long positions.
//  
//  -----------------------------------------------------------------------------
//  Disclaimer:
//    1. I am not licensed financial advisors or broker dealer. I do not tell you 
//       when or what to buy or sell. I developed this software which enables you 
//       execute manual or automated trades using TradingView. The 
//       software allows you to set the criteria you want for entering and exiting 
//       trades.
//    2. Do not trade with money you cannot afford to lose.
//    3. I do not guarantee consistent profits or that anyone can make money with no 
//       effort. And I am not selling the holy grail.
//    4. Every system can have winning and losing streaks.
//    5. Money management plays a large role in the results of your trading. For 
//       example: lot size, account size, broker leverage, and broker margin call 
//       rules all have an effect on results. Also, your Take Profit and Stop Loss 
//       settings for individual pair trades and for overall account equity have a 
//       major impact on results. If you are new to trading and do not understand 
//       these items, then I recommend you seek education materials to further your
//       knowledge.
//
//    YOU NEED TO FIND AND USE THE TRADING SYSTEM THAT WORKS BEST FOR YOU AND YOUR 
//    TRADING TOLERANCE.
//
//    I HAVE PROVIDED NOTHING MORE THAN A TOOL WITH OPTIONS FOR YOU TO TRADE WITH THIS PROGRAM ON TRADINGVIEW.
//    
//    I accept suggestions to improve the script.
//    If you encounter any problems I will be happy to share with me.
//  -----------------------------------------------------------------------------
//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// SETUP ============================================================================================================
strategy(title = "Trailing Stop Loss",
         shorttitle = "TSL",
         overlay = true,
         pyramiding = 0,
         calc_on_every_tick = true,
         default_qty_type = strategy.cash,
         default_qty_value = 100000,
         initial_capital = 100000)

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// INPUTS ===========================================================================================================

// STRATEGY INPUT ===================================================================================================
fastMALen = input(defval = 21, title = "Fast SMA Length", type = input.integer, group = "Strategy", tooltip = "How many candles back to calculte the fast SMA.")
slowMALen = input(defval = 49, title = "Slow SMA Length", type = input.integer, group = "Strategy", tooltip = "How many candles back to calculte the slow SMA.")

enableStopLossTrailing = input(defval = true, title = "Enable Trailing", type = input.bool, group = "Strategy", tooltip = "Enable or disable the trailing for stop loss.")
longTrailingStopLossPerc = input(defval = 7.5, title = 'Long Stop Loss %', type = input.float, minval = 0.1, maxval = 100, step = 0.1, inline = "Trailing Stop Loss Perc", group = "Strategy") / 100

// BACKTEST PERIOD INPUT ============================================================================================
fromDate = input(defval = timestamp("01 Jan 2021 00:00 UTC"), title = "From Date", type = input.time, minval = timestamp("01 Jan 1970 00:00 UTC"), group = "Backtest Period") // backtest start date
toDate   = input(defval = timestamp("31 Dec 2121 23:59 UTC"), title = "To Date",   type = input.time, minval = timestamp("01 Jan 1970 00:00 UTC"), group = "Backtest Period") // backtest finish date

isWithinBacktestPeriod() => true

// SHOW PLOT INPUT ==================================================================================================
showDate = input(defval = true, title = "Show Backtest Range", type = input.bool, group = "Plot", tooltip = "Gray out the backround of the backtest period.")

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// STRATEGY LOGIC ===================================================================================================

fastMA = sma(close, fastMALen)
slowMA = sma(close, slowMALen)

bool startLongDeal = crossover(fastMA, slowMA)

bool longIsActive = startLongDeal or strategy.position_size > 0

// determine trailing stop loss price
float longTrailingStopLossPrice = na
longTrailingStopLossPrice := if (longIsActive)
    stopValue = high * (1 - longTrailingStopLossPerc)
    max(stopValue, nz(longTrailingStopLossPrice[1]))
else
    na

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// STRATEGY EXECUTION ===============================================================================================

if (isWithinBacktestPeriod())
    // getting into LONG position
    strategy.entry(id = "Long Entry", long = strategy.long, when = startLongDeal, alert_message = "Long(" + syminfo.ticker + "): Started")
    // submit exit orders for trailing stop loss price
    strategy.exit(id = "Long Stop Loss", from_entry = "Long Entry", stop = longTrailingStopLossPrice, when = longIsActive, alert_message = "Long(" + syminfo.ticker + "): Stop Loss activated")


//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// PLOT DATE POSITION MA AND TRAILING TAKE PROFIT STOP LOSS =========================================================

bgcolor(color = showDate and isWithinBacktestPeriod() ? color.gray : na, transp = 90)

plot(series = fastMA, title = "Fast SMA", color = #0056BD, linewidth = 2, style = plot.style_line)
plot(series = slowMA, title = "Slow SMA", color = #FF6A00, linewidth = 2, style = plot.style_line)
plotshape(series = isWithinBacktestPeriod() and startLongDeal and strategy.position_size <= 0 ? fastMA : na, title = "UpTrend Begins", style = shape.circle, location = location.absolute, color = color.green, transp = 0, size = size.tiny)
plotshape(series = isWithinBacktestPeriod() and startLongDeal and strategy.position_size <= 0 ? fastMA : na, title = "Buy", text = "Buy", style = shape.labelup, location = location.absolute, color = color.green, textcolor = color.black, transp = 0, size = size.tiny)

plot(series = strategy.position_avg_price, title = "Position", color = color.blue, linewidth = 2, style = plot.style_linebr, offset = 1)
plot(series = longTrailingStopLossPrice, title = "Long Trail Stop", color = color.fuchsia, linewidth = 2, style = plot.style_linebr, offset = 1)

// ==================================================================================================================

Thêm nữa