Chiến lược giao dịch đảo ngược động lực

Tác giả:ChaoZhang, Ngày: 2023-10-07 16:52:05
Tags:

Tổng quan

Chiến lược này dựa trên khái niệm giao dịch đảo ngược động lực. Nó sử dụng chỉ số RSI, Stoch và MACD để xác định hướng xu hướng hiện tại, và thiết lập dừng lỗ và lấy lợi nhuận dựa trên ATR, để thực hiện một chiến lược giao dịch tự động có thể nắm bắt hiệu quả sự đảo ngược xu hướng.

Logic giao dịch

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

  • RSI(7): RSI trên 50 chỉ ra xu hướng tăng, trong khi dưới 50 là xu hướng giảm
  • Stoch ((%K, 14, 3, 3): %K trên 50 là xu hướng tăng, dưới 50 là xu hướng giảm
  • MACD ((12, 26, 9): MACD trên Đường tín hiệu là xu hướng tăng, dưới đó là xu hướng giảm

Khi cả ba chỉ số đều tăng, màu thanh được đặt thành màu xanh lá cây. Khi tất cả đều giảm, màu thanh được đặt thành màu đỏ. Nếu có sự khác biệt giữa các chỉ số, màu thanh được đặt thành màu đen.

Các quy tắc giao dịch là:

  • Khi thanh hiện tại màu xanh lá cây, và thanh trước đó màu đen hoặc đỏ, đi dài. Giá nhập là 0.01 trên mức cao của thanh.
  • Khi thanh hiện tại màu đỏ, và thanh trước màu đen hoặc xanh lá cây, hãy mua ngắn. Giá nhập là 0.01 dưới mức thấp của thanh.
  • Nếu thanh chuyển sang màu đỏ hoặc đen trong một vị trí dài, đóng giao dịch dài.
  • Nếu thanh chuyển sang màu xanh lá cây hoặc đen trong một vị trí ngắn, đóng giao dịch ngắn.

Chiến lược này cũng sử dụng ATR(7) để thiết lập stop loss và take profit.

Phân tích lợi thế

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

  1. Sử dụng nhiều chỉ số để xác định xu hướng có thể lọc hiệu quả các sự đột phá sai.

  2. Các thiết lập dừng lỗ và lấy lợi nhuận của ATR là hợp lý. ATR có thể theo dõi hiệu quả sự biến động của thị trường. Bằng cách sử dụng nhiều lần ATR, dừng và mục tiêu có thể được điều chỉnh năng động dựa trên điều kiện thị trường.

  3. Logic đơn giản và rõ ràng, dễ hiểu và tự động hóa.

Phân tích rủi ro

Những rủi ro của chiến lược này bao gồm:

  1. Lỗi trong các chỉ số cá nhân có thể ảnh hưởng đến thời gian nhập. Có thể xem xét điều chỉnh các tham số hoặc thêm nhiều chỉ số xác nhận để giảm lỗi.

  2. Kích thước của ATR ảnh hưởng đáng kể đến các điểm dừng và mục tiêu. Việc tính toán ATR không chính xác có thể dẫn đến các điểm dừng quá rộng hoặc các mục tiêu quá chật. Có thể thêm các chỉ số bổ sung để xác nhận ATR.

  3. Thiếu xác định xu hướng. Tập trung vào giao dịch đảo ngược, phân tích xu hướng không đầy đủ có thể dẫn đến sự sụt giảm trong các thị trường dao động. Có thể thêm các chỉ số xu hướng.

  4. Rủi ro quá mức: đòi hỏi phải kiểm tra kỹ lưỡng để xác minh độ bền của các thông số và quy tắc.

Hướng dẫn cải thiện

Các cải tiến có thể cho chiến lược này:

  1. Điều chỉnh hoặc thêm các chỉ số để cải thiện độ chính xác trong việc xác định các điểm đảo ngược. ví dụ: thêm Bollinger Bands để kiểm tra mức mua quá mức / bán quá mức.

  2. Tối ưu hóa tính toán ATR để theo dõi biến động tốt hơn. ví dụ: sử dụng tỷ lệ ATR / Giá.

  3. Thêm các chỉ số xu hướng để tránh các whipsaws trong các thị trường dao động. ví dụ như trung bình động.

  4. Tối ưu hóa quản lý tiền, chẳng hạn như điều chỉnh kích thước vị trí dựa trên việc rút tiền.

  5. Tối ưu hóa thời gian để kiểm tra độ bền trên các khung thời gian.

  6. Thêm kiểm tra lại các sản phẩm khác nhau và khoảng thời gian để xác minh độ tin cậy.

Tóm lại

Chiến lược này được thiết kế dựa trên các khái niệm đảo ngược động lực, sử dụng kết hợp RSI, Stoch và MACD để xác định sự đảo ngược, với các điểm dừng và mục tiêu ATR năng động. Nó tạo thành một hệ thống đảo ngược xu hướng tương đối hoàn chỉnh. Những lợi thế bao gồm logic rõ ràng và điểm dừng / mục tiêu hợp lý. Những thiếu sót bao gồm lỗi tín hiệu và thiếu bộ lọc xu hướng. Có thể cải thiện bằng cách tối ưu hóa các chỉ số, thêm xu hướng và điều chỉnh kích thước vị trí. Điều này có thể làm cho chiến lược mạnh mẽ hơn.


/*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*********************************






















Thêm nữa