Chiến lược giao dịch mô hình nến

Tác giả:ChaoZhang, Ngày: 2024-02-19 15:07:33
Tags:

img

Tổng quan

Đây là một chiến lược giao dịch tự động dựa trên các mô hình nến. Chiến lược xác định các tín hiệu mô hình nến khác nhau và nhập vào các vị trí khi các điều kiện mô hình được đáp ứng, với lệnh dừng lỗ, lấy lợi nhuận và lệnh dừng theo dõi được cấu hình để kiểm soát rủi ro.

Chiến lược logic

Chiến lược chủ yếu xác định các mô hình nến sau đây như là các tín hiệu đầu vào: mô hình nuốt chửng, mô hình harami, đường xuyên thủng / mây tối, ngôi sao buổi sáng / ngôi sao buổi tối, mô hình giữ thắt lưng, ba binh sĩ trắng / ba con quạ đen, ba ngôi sao trong mô hình phía nam, vv. Nó đi dài khi các tín hiệu tăng được phát hiện, và đi ngắn khi các tín hiệu giảm được phát hiện.

Ngoài ra, stop loss, take profit và trailing stop được cấu hình để kiểm soát rủi ro. Cụ thể, stop loss được thiết lập ở một tỷ lệ phần trăm nhất định dưới giá nhập cảnh, lấy lợi nhuận mục tiêu một số pips trên giá nhập cảnh và trailing stop trails ở một mức năng động nhất định trên mức nhập cảnh. Điều này có hiệu quả ngăn ngừa tổn thất vượt quá số tiền chấp nhận được.

Điều đáng chú ý là tất cả các vị trí được đóng cửa bên ngoài phiên giao dịch được xác định trong chiến lược, loại bỏ rủi ro qua đêm.

Phân tích lợi thế

Lợi thế lớn nhất của chiến lược này nằm trong việc sử dụng các mẫu nến, một chỉ số kỹ thuật hiệu quả, để xác định các mục nhập. Dữ liệu lịch sử rất lớn đã chỉ ra rằng một số hình thành nến đặc biệt thường biểu thị sự thay đổi về động lực cung/tham gia và tâm lý thị trường, do đó cung cấp thời gian tốt cho các mục nhập.

Một lợi thế khác là các cơ chế kiểm soát rủi ro toàn diện hiện có.

Cuối cùng, chiến lược linh hoạt để chạy bằng cách điều chỉnh các tham số mô hình và các tham số kiểm soát rủi ro để phù hợp với các công cụ và ưu tiên giao dịch khác nhau.

Phân tích rủi ro

Rủi ro lớn nhất của chiến lược này đến từ sự bất ổn vốn có của các mẫu nến chính nó như các chỉ số kỹ thuật. Mặc dù các mẫu nến có thể phản ánh rõ ràng những thay đổi xu hướng thị trường, chúng cũng dễ bị biến động ngẫu nhiên của thị trường, dẫn đến các tín hiệu sai tiềm năng.

Hơn nữa, không có mối quan hệ nhân quả giữa sự hình thành nến và hành động giá tiếp theo. Giá có thể đi ngược lại kết quả mô hình dự kiến ngay cả khi các mô hình điển hình được phát hiện.

Để giảm thiểu các rủi ro trên, tuân thủ nghiêm ngặt các quy tắc dừng lỗ, lấy lợi nhuận và dừng lại là điều cần thiết.

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

Với những hạn chế của các mô hình nến, kết hợp chúng với các chỉ số mạnh mẽ hơn như Bollinger Bands, trung bình động cho xu hướng hoặc dao động như RSI và MACD có thể có giá trị để cải thiện thời gian nhập cảnh và chất lượng tín hiệu.

Một hướng tối ưu hóa tương lai khác là sử dụng các mô hình học máy được đào tạo trên dữ liệu lịch sử lớn để khám phá mối quan hệ thống kê giữa các mẫu và các biến động giá thực tế, tăng độ chính xác tín hiệu mẫu.

Cuối cùng, chiến lược này có thể phục vụ như một khuôn khổ để được nâng cấp với các thuật toán phức tạp hơn cho giao dịch tần số cao, ví dụ như các phương pháp dừng lỗ tinh tế hơn, mô hình hóa phức tạp với nhiều giao diện dữ liệu hơn thông qua các ngôn ngữ tiên tiến.

Kết luận

Kết luận, đây là một chiến lược đáng thử nghiệm trực tiếp, sử dụng các mẫu nến hiệu quả cho các mục nhập tín hiệu, với logic dừng lỗ / lấy lợi nhuận / dừng theo dõi toàn diện kiểm soát rủi ro.


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

//@version=4

//DanyChe
//The script allows you to test popular candlestick patterns on various instruments and timeframes. In addition, you can configure risk management (if the value is zero, it means the function is disabled), and you can also specify the time of the trading session (for example, so that the positions are not transferred to the next day). 
//The author is grateful to JayRogers and Phi35, their code examples helped a lot in writing the strategy.
strategy("Candle Patterns Strategy", shorttitle="CPS", overlay=true)

//--- Patterns Input ---

OnEngulfing = input(defval=true, title="Engulfing")
OnHarami = input(defval=true, title="Harami")
OnPiercingLine = input(defval=true, title="Piercing Line / Dark Cloud Cover")
OnMorningStar = input(defval=true, title="Morning Star / Evening Star ")
OnBeltHold = input(defval=true, title="Belt Hold")
OnThreeWhiteSoldiers = input(defval=true, title="Three White Soldiers / Three Black Crows")
OnThreeStarsInTheSouth = input(defval=true, title="Three Stars in the South")
OnStickSandwich = input(defval=true, title="Stick Sandwich")
OnMeetingLine = input(defval=true, title="Meeting Line")
OnKicking = input(defval=true, title="Kicking")
OnLadderBottom = input(defval=true, title="Ladder Bottom")

//--- Risk Management Input ---

inpsl = input(defval = 100, title="Stop Loss", minval = 0)
inptp = input(defval = 1000, title="Take Profit", minval = 0)
inptrail = input(defval = 40, title="Trailing Stop", minval = 0)
// If the zero value is set for stop loss, take profit or trailing stop, then the function is disabled
sl = inpsl >= 1 ? inpsl : na
tp = inptp >= 1 ? inptp : na
trail = inptrail >= 1 ? inptrail : na

//--- Session Input ---

sess = input(defval = "0000-0000", title="Trading session")
t = time('240', sess)
session_open = na(t) ? false : true

// --- Candlestick Patterns ---

//Engulfing 
bullish_engulfing = high[0]>high[1] and low[0]<low[1] and open[0]<open[1] and close[0]>close[1] and close[0]>open[0] and close[1]<close[2] and close[0]>open[1] ? OnEngulfing : na
bearish_engulfing = high[0]>high[1] and low[0]<low[1] and open[0]>open[1] and close[0]<close[1] and close[0]<open[0] and close[1]>close[2] and close[0]<open[1] ? OnEngulfing : na

//Harami
bullish_harami =  open[1]>close[1] and close[1]<close[2] and open[0]>close[1] and open[0]<open[1] and close[0]>close[1] and close[0]<open[1] and high[0]<high[1] and low[0]>low[1] and close[0]>=open[0] ? OnHarami : na
bearish_harami =   open[1]<close[1] and close[1]>close[2] and open[0]<close[1] and open[0]>open[1] and close[0]<close[1] and close[0]>open[1] and high[0]<high[1] and low[0]>low[1] and close[0]<=open[0] ? OnHarami : na

//Piercing Line/Dark Cloud Cover 
piercing_line = close[2]>close[1] and open[0]<low[1] and close[0]>avg(open[1],close[1]) and close[0]<open[1] ? OnPiercingLine : na
dark_cloud_cover = close[2]<close[1] and open[0]>high[1] and close[0]<avg(open[1],close[1]) and close[0]>open[1] ? OnPiercingLine : na

//Morning Star/Evening Star
morning_star = close[3]>close[2] and close[2]<open[2] and open[1]<close[2] and close[1]<close[2] and open[0]>open[1] and open[0]>close[1] and close[0]>close[2] and open[2]-close[2]>close[0]-open[0] ? OnMorningStar : na
evening_star = close[3]<close[2] and close[2]>open[2] and open[1]>close[2] and close[1]>close[2] and open[0]<open[1] and open[0]<close[1] and close[0]<close[2] and close[2]-open[2]>open[0]-close[0] ? OnMorningStar : na

//Belt Hold
bullish_belt_hold = close[1]<open[1] and low[1]>open[0] and close[1]>open[0] and open[0]==low[0] and close[0]>avg(close[0],open[0]) ? OnBeltHold :na
bearish_belt_hold =  close[1]>open[1] and high[1]<open[0] and close[1]<open[0] and open[0]==high[0] and close[0]<avg(close[0],open[0]) ? OnBeltHold :na

//Three White Soldiers/Three Black Crows 
three_white_soldiers = close[3]<open[3] and open[2]<close[3] and close[2]>avg(close[2],open[2]) and open[1]>open[2] and open[1]<close[2] and close[1]>avg(close[1],open[1]) and open[0]>open[1] and open[0]<close[1] and close[0]>avg(close[0],open[0]) and high[1]>high[2] and high[0]>high[1] ? OnThreeWhiteSoldiers : na
three_black_crows =  close[3]>open[3] and open[2]>close[3] and close[2]<avg(close[2],open[2]) and open[1]<open[2] and open[1]>close[2] and close[1]<avg(close[1],open[1]) and open[0]<open[1] and open[0]>close[1] and close[0]<avg(close[0],open[0]) and low[1]<low[2] and low[0]<low[1] ? OnThreeWhiteSoldiers : na

//Three Stars in the South
three_stars_in_the_south = open[3]>close[3] and open[2]>close[2] and open[2]==high[2] and open[1]>close[1] and open[1]<open[2] and open[1]>close[2] and low[1]>low[2] and open[1]==high[1] and open[0]>close[0] and open[0]<open[1] and open[0]>close[1] and open[0]==high[0] and close[0]==low[0] and close[0]>=low[1] ? OnThreeStarsInTheSouth : na

//Stick Sandwich
stick_sandwich = open[2]>close[2] and open[1]>close[2] and open[1]<close[1] and open[0]>close[1] and open[0]>close[0] and close[0]==close[2] ? OnStickSandwich : na

//Meeting Line 
bullish_ml = open[2]>close[2] and open[1]>close[1] and close[1]==close[0] and open[0]<close[0] and open[1]>=high[0] ? OnMeetingLine : na
bearish_ml = open[2]<close[2] and open[1]<close[1] and close[1]==close[0] and open[0]>close[0] and open[1]<=low[0] ? OnMeetingLine : na

//Kicking 
bullish_kicking =  open[1]>close[1] and open[1]==high[1] and close[1]==low[1] and open[0]>open[1] and open[0]==low[0] and close[0]==high[0] and close[0]-open[0]>open[1]-close[1] ? OnKicking : na
bearish_kicking = open[1]<close[1] and open[1]==low[1] and close[1]==high[1] and open[0]<open[1] and open[0]==high[0] and close[0]==low[0] and open[0]-close[0]>close[1]-open[1] ? OnKicking : na

//Ladder Bottom
ladder_bottom = open[4]>close[4] and open[3]>close[3] and open[3]<open[4] and open[2]>close[2] and open[2]<open[3] and open[1]>close[1] and open[1]<open[2] and open[0]<close[0] and open[0]>open[1] and low[4]>low[3] and low[3]>low[2] and low[2]>low[1] ? OnLadderBottom : na

// ---Plotting ---

plotshape(bullish_engulfing, text='Engulfing', style=shape.triangleup, color=#1FADA2, editable=true, title="Bullish Engulfing Text")
plotshape(bearish_engulfing,text='Engulfing', style=shape.triangledown, color=#F35A54, editable=true, title="Bearish Engulfing Text")
plotshape(bullish_harami,text='Harami', style=shape.triangleup, color=#1FADA2, editable=true, title="Bullish Harami Text")
plotshape(bearish_harami,text='Harami', style=shape.triangledown, color=#F35A54, editable=true, title="BEarish Harami Text")
plotshape(piercing_line,text='Piercing Line', style=shape.triangleup, color=#1FADA2, editable=false)
plotshape(dark_cloud_cover,text='Dark Cloud Cover', style=shape.triangledown, color=#F35A54, editable=false)
plotshape(morning_star,text='Morning Star', style=shape.triangleup, color=#1FADA2, editable=false)
plotshape(evening_star,text='Evening Star', style=shape.triangledown, color=#F35A54, editable=false)
plotshape(bullish_belt_hold,text='Belt Hold', style=shape.triangleup, color=#1FADA2, editable=false)    
plotshape(bearish_belt_hold,text='Belt Hold', style=shape.triangledown, color=#F35A54, editable=false)
plotshape(three_white_soldiers,text='Three White Soldiers', style=shape.triangleup, color=#1FADA2, editable=false)
plotshape(three_black_crows,text='Three Black Crows', style=shape.triangledown, color=#F35A54, editable=false)
plotshape(three_stars_in_the_south,text='3 Stars South', style=shape.triangleup, color=#1FADA2, editable=false)
plotshape(stick_sandwich,text='Stick Sandwich', style=shape.triangleup, color=#1FADA2, editable=false)
plotshape(bullish_ml,text='Meeting Line', style=shape.triangleup, color=#1FADA2, editable=false)
plotshape(bearish_ml,text='Meeting Line', style=shape.triangledown, color=#F35A54, editable=false)
plotshape(bullish_kicking,text='Kicking', style=shape.triangleup, color=#1FADA2, editable=false)
plotshape(bearish_kicking,text='Kicking', style=shape.triangledown, color=#F35A54, editable=false)
plotshape(ladder_bottom,text='Ladder Bottom', style=shape.triangleup, color=#1FADA2, editable=false)

// --- STRATEGY ---

SignalUp = bullish_engulfing or bullish_harami or piercing_line or morning_star or bullish_belt_hold or three_white_soldiers or three_stars_in_the_south or stick_sandwich or bullish_ml or bullish_kicking or ladder_bottom
SignalDown = bearish_engulfing or bearish_harami or dark_cloud_cover or evening_star or bearish_belt_hold or three_black_crows or bearish_ml or bearish_kicking

strategy.entry("long", true, when = SignalUp and session_open)
strategy.entry("short", false, when = SignalDown and session_open)
strategy.close("long", when = not session_open)
strategy.close("short", when = not session_open)
strategy.exit("Risk Exit long", from_entry = "long", profit = tp, trail_points = trail, loss = sl)
strategy.exit("Risk Exit short", from_entry = "short", profit = tp, trail_points = trail, loss = sl )

Thêm nữa