Chiến lược đảo ngược động lượng


Ngày tạo: 2023-10-07 16:52:05 sửa đổi lần cuối: 2023-10-07 16:52:05
sao chép: 0 Số nhấp chuột: 673
1
tập trung vào
1617
Người theo dõi

Tổng quan

Chiến lược này dựa trên triết lý giao dịch đảo ngược động lực, đánh giá xu hướng hiện tại thông qua ba chỉ số RSI, Stoch và MACD, kết hợp với ATR thiết lập điểm dừng lỗ để thực hiện chiến lược giao dịch tự động nắm bắt xu hướng đảo ngược hiệu quả.

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

Chiến lược này sử dụng ba chỉ số RSI, Stoch và MACD để đánh giá xu hướng hiện tại.

  • RSI ((7): RSI lớn hơn 50 là tăng, nhỏ hơn 50 là giảm
  • Stoch ((%K,14,3,3): Giá trị%K lớn hơn 50 là tăng giá, nhỏ hơn 50 là giảm giá
  • MACD ((12,26,9): MACD lớn hơn Signal là tăng, nhỏ hơn Signal là giảm

Bar được đặt thành màu xanh lá cây khi ba chỉ số đồng thời tăng; Bar được đặt thành màu đỏ khi ba chỉ số đồng thời giảm; Bar được đặt thành màu đen nếu tín hiệu chỉ số có sự khác biệt.

Các quy tắc giao dịch như sau:

  • Khi Bar hiện tại là màu xanh lá cây và Bar trước là màu đen hoặc màu đỏ, hãy nhập thêm, giá nhập vào là điểm cao của Bar cộng 0.01
  • Khi Bar hiện tại là màu đỏ và Bar trước là màu đen hoặc màu xanh lá cây, hãy mở cửa bằng cách giảm 0.01 giá vào điểm thấp của Bar
  • Nếu có một màu đỏ hoặc màu đen Bar xuất hiện trong quá trình giữ nhiều vị trí, vị trí bằng phẳng
  • Nếu Bar màu xanh lá cây hoặc màu đen xuất hiện trong thời gian giữ vị trí trống, vị trí bằng phẳng

Chiến lược này cũng sử dụng ATR (đường trung bình 7 ngày) để thiết lập vị trí dừng lỗ. Địa điểm dừng lỗ là 1,5 lần ATR và vị trí dừng lỗ là 3 lần ATR.

Phân tích lợi thế

Chiến lược này có những ưu điểm sau:

  1. Sử dụng nhiều chỉ số để đánh giá xu hướng, có thể lọc hiệu quả các đợt phá vỡ giả. Khi ba chỉ số RSI, Stoch và MACD cùng lúc tăng hoặc giảm, xác suất là điểm đảo ngược xu hướng.

  2. ATR có thể theo dõi hiệu quả mức độ biến động của thị trường, sử dụng nhiều lần thiết lập dừng lỗ của ATR, có thể điều chỉnh dừng lỗ theo tình hình thị trường động, ngăn chặn lỗ quá nới lỏng hoặc quá chặt chẽ.

  3. Logic giao dịch đơn giản, rõ ràng, dễ hiểu và thích hợp để thực hiện như là một chiến lược giao dịch tự động.

Phân tích rủi ro

Chiến lược này cũng có những rủi ro sau:

  1. Việc đánh giá kết hợp nhiều chỉ số có thể xảy ra trong trường hợp các chỉ số riêng lẻ phát ra tín hiệu sai, do đó ảnh hưởng đến thời gian nhập cảnh. Bạn có thể cân nhắc điều chỉnh tham số chỉ số hoặc thêm các chỉ số khác để xác minh, giảm tỷ lệ tín hiệu sai.

  2. Kích thước ATR có ảnh hưởng lớn đến trạm dừng lỗ. Nếu ATR được tính toán không chính xác, có thể dẫn đến việc dừng lỗ quá lớn hoặc trạm dừng quá nhỏ. Bạn có thể xem xét thêm các chỉ số khác để xác nhận kích thước ATR.

  3. Thiếu phán đoán xu hướng. Chiến lược này tập trung vào giao dịch đảo ngược, không có phán đoán xu hướng và dễ bị mắc kẹt trong tình huống chấn động.

  4. Có rủi ro quá phù hợp. Cần kiểm tra lại đầy đủ, xác minh độ tin cậy của tham số và quy tắc.

Hướng tối ưu hóa

Chính sách này có thể được tối ưu hóa bằng cách:

  1. Điều chỉnh hoặc tăng các chỉ số để tăng độ chính xác trong việc đánh giá thời điểm thay đổi xu hướng. Ví dụ, thêm đường Brin để đánh giá tình trạng quá mua quá bán.

  2. Tối ưu hóa phương pháp tính toán ATR để theo dõi tốt hơn sự biến động của thị trường. Ví dụ: tỷ lệ sử dụng ATR so với giá cả.

  3. Thêm các chỉ số định hướng xu hướng để tránh bị mắc kẹt trong tình huống biến động. Ví dụ: thêm đường trung bình di chuyển để định hướng xu hướng.

  4. Tối ưu hóa quản lý vốn, chẳng hạn như điều chỉnh vị trí theo trường hợp rút tiền.

  5. Tối ưu hóa chu kỳ để xác minh tính ổn định của các tham số chu kỳ thời gian khác nhau.

  6. Xét lại trong nhiều giống và khoảng thời gian, kiểm tra tính ổn định và độ tin cậy của chiến lược.

Tóm tắt

Chiến lược này dựa trên thiết kế tư duy đảo ngược động lực, sử dụng RSI, Stoch và MACD để xác định thời điểm đảo ngược xu hướng, kết hợp với thiết lập dừng lỗ động ATR, tạo thành một bộ chiến lược giao dịch đảo ngược xu hướng hoàn chỉnh hơn. Chiến lược có logic giao dịch rõ ràng, thiết lập dừng lỗ hợp lý, nhưng cũng có tín hiệu chỉ số sai, thiếu phán đoán xu hướng. Trong tương lai, có thể cải thiện từ việc tối ưu hóa các tham số chỉ số, tham gia phán đoán xu hướng, điều chỉnh quản lý quỹ, v.v.

Mã nguồn chiến lược
/*backtest
start: 2023-09-06 00:00:00
end: 2023-10-06 00:00:00
period: 2h
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/
// © jwitt98
//The PowerX strategy - based on the rules outlined in the book "The PowerX Strategy: How to Trade Stocks and Options in Only 15 Minutes a Day" by Markus Heitkoetter

//@version=5
strategy("PowerX", overlay=true)
strategy.initial_capital = 50000
longEntry = "Enter long"
shortEntry = "Enter short"
longExit = "Exit long"
shortExit = "Exit short"

//*********************************Begin inputs*********************************

//get time range inputs and set time range bool
timeRangeGroup = "Select the trading range"
startDate = input(timestamp("1 Jan 2021 00:00 +0000"), "Start date", "Select the start date for the trading range", "", timeRangeGroup)
endDate = input(timestamp("1 Jan 2050 00:00 +0000"), "End date", "Select the end date for the trading range", "", timeRangeGroup)
isInTimeRange = true

//get long/short inputs
positionsAllowedGroup = "Select the direction(s) of trades to allow"
isLongsAllowed = input.bool(true, "Allow long positions", "Check the box if you want to allow long positions", "", positionsAllowedGroup)
isShortsAllowed = input.bool(true, "Allow short positions", "Check the box if you want to allow short positions", "", positionsAllowedGroup)

//get the stop loss and profit target multiples.  Per the PowerX rules the ratio shoud be 1:2.  1.5 and 3 are defaults
adrMultuplesGroup="Select the multipliers for the stop loss and profit targets"
stopLossMultiple = input.float(1.5, "Stop loss multiple", 0.1, 10, 0.1, "The ADR is multiplied by the stop loss multiple to calculate the stop loss", group=adrMultuplesGroup)
profitTargetMultiple=input.float(3.0, "Profit target multiple", 0.1, 10, 0.1, "The ADR is multiplied by the profit target multiple to calculate the profit target", group=adrMultuplesGroup)

//get the option to use the money management stategy or not.  This is a fixed ratio type management system
moneyManagementGroup="Money management"
isUsingMoneyManagement=input.bool(false, "Use money management", "Check the box if you want to use a fixed ratio type money management system, such as the type described in PowerX", group=moneyManagementGroup)
initial_riskPercent=input.float(2.0, "Percent risk per trade", .1, 100, .1, "The percentage of capital you want to risk when starting out.  This will increase or decrease base on the money management rules.  Only applicable if money managent is used", group=moneyManagementGroup)/100
isRiskDowsideLimited=input.bool(false, "Keep risk at or above the set point", "Check the box if you don't want the risk to fall below the set \"risk per trade\" percentage, for example, when your equity is underwater. Only applicable if money management is used", "", moneyManagementGroup)
initial_riskPerTrade=initial_riskPercent * strategy.initial_capital 
riskFactor = 0.0
currentProfit = 0.0
currentRisk = 0.0

//*********************************End inputs*********************************

//*********************************Begin money management*********************************

if(isUsingMoneyManagement)
    currentProfit := strategy.equity - strategy.initial_capital
    if(currentProfit < 0)
        currentProfit:=math.abs(currentProfit)
        riskFactor := 0.5*(math.pow(1+8*currentProfit/(2*initial_riskPerTrade), 0.5)+1)
        currentRisk := 1/riskFactor * initial_riskPercent * strategy.initial_capital
        if(isRiskDowsideLimited)
            currentRisk := initial_riskPerTrade
    else
        riskFactor := 0.5*(math.pow(1+8*currentProfit/(2*initial_riskPerTrade), 0.5)+1)
        currentRisk := riskFactor * initial_riskPercent * strategy.initial_capital
        
plot(strategy.equity, "Strategy equity")
plot(currentRisk, "Current risk")
plot(riskFactor, "Risk Factor")

//*********************************End money management*********************************


//*********************************Begin indicators*********************************
//4 indicators are used in this strategy, RSI(7), Stochastics(14, 3, 3), MACD(12, 26, 9), and ADR(7)

rsiVal = ta.rsi(close, 7)//this checks out
plot(rsiVal, "RSI(7)", color.lime)

stochKVal = ta.sma(ta.sma(ta.stoch(close, high, low, 14),3),3)//this formula checks out
plot(stochKVal, "Stoch %K", color.lime)

[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)
plot(histLine, "MACD Hist", color.lime)

adr = ta.sma(high, 7) - ta.sma(low, 7)
plot(adr, "Average daily range", color.orange)


//*********************************End indicators*********************************

//*********************************Define the bar colors*********************************

greenBar = rsiVal > 50 and stochKVal > 50 and histLine > 0
redBar = rsiVal < 50 and stochKVal < 50 and histLine < 0
blackBar = not greenBar and not redBar

color currentBarColor = switch
    greenBar => color.green
    redBar => color.red
    blackBar => color.gray //because black is too hard to see in dark mmode
    => color.yellow
    
barcolor(currentBarColor)

//*********************************End defining the bar colors*********************************

//*********************************Define the entry, stop loss and profit target*********************************

longStopLimit = high + .01
longProfitTarget = high + (profitTargetMultiple * adr)
longStopLoss = high - (stopLossMultiple * adr)

shortStopLimit = low - .01
shortProfitTarget = low - (profitTargetMultiple * adr)
shortStopLoss = low + (stopLossMultiple * adr)

qtyToTrade= math.floor(currentRisk / (stopLossMultiple * adr))//only if using money management
if(qtyToTrade * high > strategy.equity)
    qtyToTrade := math.floor(strategy.equity / high)

//*********************************End defining stop loss and profit targets*********************************

//*********************************Execute trades, set rules, stop loss and profit targets*********************************

if (greenBar and not greenBar[1] and isInTimeRange and isLongsAllowed)
    if(isUsingMoneyManagement)
        strategy.order(longEntry, strategy.long, qtyToTrade, limit=longStopLimit, stop=longStopLimit)
        //strategy.order(longEntry, strategy.long, qtyToTrade, stop=longStopLimit)
    else
        strategy.order(longEntry, strategy.long, limit=longStopLimit,stop=longStopLimit)
        //strategy.order(longEntry, strategy.long, stop=longStopLimit)
    strategy.exit("Long limit/stop", from_entry=longEntry, limit=longProfitTarget, stop=longStopLoss)
    

if(blackBar or redBar)
    strategy.cancel(longEntry)
    strategy.close(longEntry, longExit)
    

if (redBar and not redBar[1] and isInTimeRange and isShortsAllowed)
    if(isUsingMoneyManagement)
        strategy.order(shortEntry, strategy.short, qtyToTrade, limit=shortStopLimit, stop=shortStopLimit)
        //strategy.order(shortEntry, strategy.short, qtyToTrade, stop=shortStopLimit)
    else
        strategy.order(shortEntry, strategy.short, limit=shortStopLimit, stop=shortStopLimit)
        //strategy.order(shortEntry, strategy.short, stop=shortStopLimit)
    strategy.exit("Short limit/stop", from_entry=shortEntry, limit=shortProfitTarget, stop=shortStopLoss)

if(blackBar or greenBar)
    strategy.cancel(shortEntry)
    strategy.close(shortEntry, shortExit)
    

//*********************************End execute trades, set rules, stop loss and profit targets*********************************