Chiến lược mua sau

Tác giả:ChaoZhang, Ngày: 2023-10-09 14:53:02
Tags:

Tổng quan

Chiến lược mua sau là một chiến lược theo xu hướng. Khi trung bình di chuyển nhanh vượt qua trung bình di chuyển chậm, nó kích hoạt tín hiệu vị trí mở. Không giống như vị trí mở trực tiếp, chiến lược này sẽ không nhập thị trường ngay sau khi tín hiệu vị trí mở được kích hoạt, nhưng sẽ thực hiện lệnh mua chỉ khi giá đạt đến một số điều kiện nhất định. Điều này có thể làm tăng lợi nhuận của chiến lược đến một mức độ nào đó.

Nguyên tắc

Chiến lược này dựa trên hệ thống chéo trung bình động với hai trung bình động. Trung bình động nhanh và trung bình động chậm được tính theo thứ tự. Khi trung bình động nhanh vượt qua trung bình động chậm, một tín hiệu dài được tạo ra.

Logic thực hiện sẽ khác nhau khi tùy chọn mua sau được bật:

  1. Khi tín hiệu dài được kích hoạt, thay vì mua trực tiếp, ghi lại mức giá thấp nhất tại thời điểm đó.

  2. Sau đó tính ngưỡng giá mua theo tỷ lệ phần trăm mua cuối cùng, tức là giá thấp nhất * (1 + phần trăm).

  3. Trong các thanh tiếp theo, tiếp tục so sánh giá thấp nhất của thanh hiện tại với ngưỡng giá mua.

  4. Khi giá thấp nhất vượt qua ngưỡng giá mua, thực hiện lệnh mua.

  5. Bằng cách này, chúng tôi có thể vào thị trường với mức giá tốt hơn sau khi xác nhận xu hướng.

Phân tích lợi thế

Những lợi thế của chiến lược này là:

  1. Sử dụng mua theo dõi có thể tránh rủi ro phá vỡ sai bằng cách vào thị trường sau khi xu hướng trở nên rõ ràng hơn.

  2. Thông qua mua theo dõi, giá tốt hơn có thể đạt được, cải thiện lợi nhuận ở một mức độ nào đó.

  3. Chiến lược này rất đơn giản và dễ thực hiện.

  4. Tỷ lệ phần trăm mua cuối cùng có thể tùy chỉnh, làm cho chiến lược linh hoạt hơn.

  5. Các khoảng thời gian trung bình động có thể được tùy chỉnh để thích nghi với môi trường thị trường khác nhau.

Phân tích rủi ro

Có một số rủi ro trong chiến lược này:

  1. Mua sau có thể gây ra một sự chậm trễ nhất định và bỏ lỡ cơ hội để tham gia.

  2. Thiết lập không chính xác tỷ lệ phần trăm mua cuối có thể dẫn đến không thể mua.

  3. Thời gian trung bình động không chính xác có thể tạo ra nhiều tín hiệu sai hơn.

  4. Chiến lược có thể chịu tổn thất nghiêm trọng trên các thị trường khác nhau.

  5. Đây là một chiến lược đơn giản với chỗ cho tối ưu hóa tham số.

Các biện pháp tương ứng:

  1. Giảm tỷ lệ mua cuối để giảm chậm lại.

  2. Kiểm tra các thiết lập tỷ lệ phần trăm khác nhau để tìm ra tối ưu.

  3. Tối ưu hóa thời gian trung bình động để thích nghi với thị trường.

  4. Thêm các bộ lọc khác để tránh các thị trường khác nhau.

  5. Xem xét thêm một mức dừng lỗ để giảm lỗ.

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

Chiến lược có thể được tối ưu hóa theo các hướng sau:

  1. Thêm các chỉ số giá khối lượng như Klinger để tránh sự không phù hợp giá khối lượng.

  2. Thêm các phán đoán về điều kiện khối lượng, chỉ mua khi khối lượng mở rộng.

  3. Tối ưu hóa thời gian trung bình động cho các sản phẩm khác nhau.

  4. Thêm các chỉ số biến động để tránh các vùng dao động.

  5. Thêm ATR stop loss.

  6. Hãy xem xét làm cho tỷ lệ phần trăm tăng động, tăng nhanh hơn khi xu hướng rõ ràng hơn.

Kết luận

Tóm lại, chiến lược mua theo sau cải thiện chiến lược bằng cách theo sau giá đến các điểm nhập tốt hơn trong khi giữ cho nó đơn giản. Nhưng vẫn có một số rủi ro trong chiến lược này đòi hỏi phải tối ưu hóa hơn nữa để thích nghi với nhiều tình huống thị trường hơn. Nhìn chung, chiến lược này cung cấp một ý tưởng tham khảo đáng cho giao dịch định lượng.


/*backtest
start: 2023-10-01 00:00:00
end: 2023-10-08 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 
//  -----------------------------------------------------------------------------
//  Copyright 2022 Iason Nikolas | jason5480
//  Trailing Buy 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:     15-Feb-2022
//
//  Description
//  =============================================================================
//  This strategy will go long if fast MA crosses over slow MA.
//  If the trailing buy is checked then the strategy instead of entering into the position
//  directly it will follow the price downwards (percentagewise) with small steps
//  If the price raise by this percentage then the entry 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
//  Training Buy Deviation % - The step to follow the price when the open position condition is met.
//  Source Buy - The price to compare the current buyPrice in order to trigger the buy order when trailing
//  
//  -----------------------------------------------------------------------------
//  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 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 Buy',
         shorttitle = 'TB',
         overlay = true,
         pyramiding = 0,
         default_qty_type = strategy.percent_of_equity,
         default_qty_value = 100,
         initial_capital = 100000)

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// FILTERS ==========================================================================================================

// INPUT ============================================================================================================
usefromDate = input.bool(defval = true, title = 'From', inline = "From Date", group = "Filters")
fromDate = input(defval = timestamp('01 Jan 2021 00:00 UTC'), title = '', inline = "From Date", group = 'Filters')
usetoDate = input.bool(defval = false, title = 'To ', inline = "To Date", group = "Filters")
toDate = input(defval = timestamp('31 Dec 2121 23:59 UTC'), title = '', inline = "To Date", group = 'Filters')

// LOGIC ============================================================================================================
isWithinPeriod() => true

// PLOT =============================================================================================================
bgcolor(color = isWithinPeriod() ? color.new(color.gray, 90) : na, title = 'Period')

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

// INPUT ============================================================================================================
fastMALen = input.int(defval = 21, title = 'Fast/Slow SMA Length', inline = 'MA Length', group = 'Strategy')
slowMALen = input.int(defval = 49, title = '', tooltip = 'How many candles back to calculte the fast/slow SMA.', inline = 'MA Length', group = 'Strategy')

// LOGIC ============================================================================================================
fastMA = ta.sma(close, fastMALen)
slowMA = ta.sma(close, slowMALen)

bool openLongPosition = isWithinPeriod() and ta.crossover(fastMA, slowMA)
bool closeLongPosition = ta.crossunder(fastMA, slowMA)

bool longIsActive = openLongPosition or strategy.position_size > 0

// PLOT =============================================================================================================
var fastColor = color.new(#0056BD, 0)
plot(series = fastMA, title = 'Fast SMA', color = fastColor, linewidth = 1, style = plot.style_line)
var slowColor = color.new(#FF6A00, 0)
plot(series = slowMA, title = 'Slow SMA', color = slowColor, linewidth = 1, style = plot.style_line)

plotshape(series = openLongPosition and strategy.position_size <= 0 ? fastMA : na, title = 'Buy', text = 'Buy', style = shape.labelup, location = location.absolute, color = color.new(color.green, 0), textcolor = color.new(color.white, 0), size = size.tiny)

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// BUY ==============================================================================================================

// INPUT ============================================================================================================
enableTrailing = input.bool(defval = true, title = 'Enable Trailing', tooltip = 'Enable or disable the trailing for buy.', group = 'Buy')
trailingBuyDeviationPerc = input.float(defval = 4.0, title = 'Trailing Buy Deviation %', minval = 0.01, maxval = 100, step = 0.05, tooltip = 'The step to follow the price when the open position condition is met.', group = 'Buy') / 100
srcBuy = input.source(defval = high, title = 'Source Buy', tooltip = 'The price to check to trigger the buy order', group = 'Buy')

// LOGIC ============================================================================================================
int barsSinceOpenLong = nz(ta.barssince(openLongPosition), 999999)
int barsSinceCloseLong = nz(ta.barssince(closeLongPosition), 999999)
bool tryOpenLongPosition = isWithinPeriod() and barsSinceCloseLong >= barsSinceOpenLong and not (strategy.position_size > 0)

float longBuyPrice = na
longBuyPrice := if openLongPosition and not (strategy.position_size > 0)
    low * (1 + trailingBuyDeviationPerc)
else if tryOpenLongPosition
    math.min(low * (1 + trailingBuyDeviationPerc), nz(longBuyPrice[1], 999999))
else
    na

bool executeLongPosition = enableTrailing ? isWithinPeriod() and srcBuy > longBuyPrice : openLongPosition

// PLOT =============================================================================================================
var buyColor = color.new(#419388, 0)
plot(series = enableTrailing ? longBuyPrice : na, title = 'Long Buy Price', color = buyColor, linewidth = 1, style = plot.style_linebr, offset = 1)

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// POSITION ORDERS ==================================================================================================

// LOGIC ============================================================================================================
// getting into LONG position
strategy.entry(id = 'Long Entry', direction = strategy.long, when = executeLongPosition, alert_message = 'Long(' + syminfo.ticker + '): Started')
// submit close order on trend reversal
strategy.close(id = 'Long Entry', when = closeLongPosition, comment = 'Close Long', alert_message = 'Long(' + syminfo.ticker + '): Closed at market price')

// PLOT =============================================================================================================
var posColor = color.new(color.white, 0)
plot(series = strategy.position_avg_price, title = 'Position', color = posColor, linewidth = 1, style = plot.style_linebr)

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

Thêm nữa