Chiến lược giao dịch Pinbar cơ bản

Tác giả:ChaoZhang, Ngày: 2023-11-15 15:25:57
Tags:

img

Tổng quan

Chiến lược này sử dụng mô hình pinbar với xác định xu hướng bằng cách di chuyển trung bình để giao dịch đột phá theo hướng của xu hướng. Nó tạo ra tín hiệu giao dịch khi giá vượt ra khỏi mức cao / thấp được hình thành bởi nến pinbar. Ngoài ra, nó sử dụng trung bình di chuyển nhanh và chậm để xác định hướng xu hướng tổng thể, tránh các tín hiệu sai trong hành động giá giới hạn phạm vi.

Chiến lược logic

  1. Tính toán các đường trung bình động nhanh (20 giai đoạn) và chậm (50 giai đoạn).

  2. Xác định các pinbar tăng (khép> mở) và giảm (khép< mở) dựa trên nến.

  3. Kiểm tra xem pinbar high/low có phá vỡ high/low của nến trước không. Một pinbar tăng phá vỡ mức cao trước cho tín hiệu dài. Một pinbar giảm phá vỡ mức thấp trước cho tín hiệu ngắn.

  4. Cũng kiểm tra xem MA nhanh có cao hơn MA chậm để xác định xu hướng tăng, và ngược lại đối với xu hướng giảm.

  5. Các tín hiệu dài chỉ có giá trị khi MA nhanh / chậm chỉ ra xu hướng tăng. Các tín hiệu ngắn chỉ có giá trị khi MA nhanh / chậm chỉ ra xu hướng giảm. Điều này tránh các tín hiệu sai trong hành động giá giới hạn trong phạm vi.

  6. Trên tín hiệu dài hợp lệ, đi dài với stoploss và takeprofit được xác định trước. Trên tín hiệu ngắn hợp lệ, đi ngắn với stoploss và takeprofit được xác định trước.

  7. Nếu MA nhanh vượt qua dưới MA chậm, đóng tất cả các vị trí hiện có.

Ưu điểm

  • Sử dụng pinbar cao / thấp như mức độ đột phá đại diện cho động lực mạnh mẽ.

  • Xem xét hướng xu hướng để tránh các tín hiệu sai trong hành động giá giới hạn trong phạm vi, cải thiện độ chính xác.

  • Nhận được xu hướng và đột phá, hoạt động tốt trên các thị trường xu hướng.

  • Các tham số có thể được tối ưu hóa cho các sản phẩm và khung thời gian khác nhau.

Rủi ro và giảm thiểu

  • Rủi ro không thoát được có thể giảm thiểu bằng cách sử dụng mức thoát rộng hơn và động lực mạnh hơn.

  • Rủi ro xác định xu hướng không chính xác. Có thể giảm thiểu bằng cách điều chỉnh các thông số MA hoặc thêm các chỉ số xu hướng khác.

  • Stoploss quá chặt chẽ dẫn đến thoát sớm. Có thể điều chỉnh động stoploss dựa trên sản phẩm và khung thời gian.

  • Có thể thiết lập mục tiêu lợi nhuận và tỷ lệ rủi ro-lợi nhuận.

Cơ hội gia tăng

  • Nhìn chung, các tham số MA, breakout, stoploss và takeprofit có thể được tối ưu hóa trên các sản phẩm và khung thời gian cho một chiến lược phù hợp.

  • Các MAs khác nhau như EMA, SMA v.v. có thể được thử nghiệm để tìm ra chỉ số tối ưu.

  • Các chỉ số bổ sung như Momentum có thể cải thiện độ chính xác xu hướng.

  • Các tham số có thể được tối ưu hóa năng động bằng cách sử dụng các kỹ thuật học máy.

  • Tỷ lệ thành công của sự đột phá có thể được cải thiện thông qua học tập thống kê.

Tóm lại

Chiến lược này kết hợp xu hướng và động lực cho các tín hiệu được lọc theo lý thuyết. Chìa khóa là tối ưu hóa tham số mạnh mẽ trên các sản phẩm và khung thời gian để có hiệu suất tốt. Ngoài ra, các chỉ số phụ trợ và kỹ thuật học máy có thể cải thiện thêm chiến lược. Với các cải tiến liên tục, đây có thể trở thành một hệ thống giao dịch đột phá xu hướng mạnh mẽ.


/*backtest
start: 2023-10-15 00:00:00
end: 2023-11-14 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
//Backtested Time Frame: H1
//Default Settings: Are meant to run successfully on all currency pairs to reduce over-fitting.
//Risk Warning: This is a forex trading robot, backtest performance will not equal future performance, USE AT YOUR OWN RISK.
//Code Warning: Although every effort has been made for robustness, this code has not been vetted by independent 3rd parties.
strategy("Pin Bar Strategy v1", overlay=true)

// User Input
usr_risk = input(title="Equity Risk (%)",type=input.integer,minval=1,maxval=100,step=1,defval=3,confirm=false)
atr_mult = input(title="Stop Loss (x*ATR, Float)",type=input.float,minval=0.1,maxval=100,step=0.1,defval=1.9,confirm=false)
trd_rewd = input(title="Risk : Reward (1 : x*SL, Float)",type=input.float,minval=0.1,maxval=100,step=0.1,defval=3.1,confirm=false)
sma_fast = input(title="Fast MA (Period)",type=input.integer,minval=1,maxval=500,step=1,defval=20,confirm=false)
sma_slow = input(title="Slow MA (Period)",type=input.integer,minval=1,maxval=500,step=1,defval=50,confirm=false)
atr_valu = input(title="ATR (Period)",type=input.integer,minval=1,maxval=500,step=1,defval=14,confirm=false)
use_slpe = input(title="Use MA Slope (Boolean)",type=input.bool,defval=true,confirm=false)
slp_long = input(title="Bull Slope Angle (Deg)",type=input.integer,minval=-90,maxval=90,step=1,defval=1,confirm=false)
slp_shrt = input(title="Bear Slope Angle (Deg)",type=input.integer,minval=-90,maxval=90,step=1,defval=-1,confirm=false)
emg_exit = input(title="Exit When MA Re-Cross (Boolean)",type=input.bool,defval=true,confirm=false)
ent_canc = input(title="Cancel Entry After X Bars (Period)",type=input.integer,minval=1,maxval=500,step=1,defval=3,confirm=false)

// Create Indicators
fastSMA = sma(close, sma_fast)
slowSMA = sma(close, sma_slow)
bullishPinBar = ((close > open) and ((open - low) > 0.66 * (high - low))) or ((close < open) and ((close - low) > 0.66 * (high - low)))
bearishPinBar = ((close > open) and ((high - close) > 0.66 * (high - low))) or ((close < open) and ((high - open) > 0.66 * (high - low)))
atr = atr(atr_valu)

// Specify Trend Conditions
smaUpTrend = (fastSMA > slowSMA) and (fastSMA[1] > slowSMA[1]) and (fastSMA[2] > slowSMA[2]) and (fastSMA[3] > slowSMA[3]) and (fastSMA[4] > slowSMA[4])
smaDnTrend = (fastSMA < slowSMA) and (fastSMA[1] < slowSMA[1]) and (fastSMA[2] < slowSMA[2]) and (fastSMA[3] < slowSMA[3]) and (fastSMA[4] < slowSMA[4])
candleUpTrend = (close[5] > fastSMA[5]) and (open[5] > fastSMA[5]) and (close[6] > fastSMA[6]) and (open[6] > fastSMA[6]) and (close[7] > fastSMA[7]) and (open[7] > fastSMA[7]) and (close[8] > fastSMA[8]) and (open[8] > fastSMA[8]) and (close[9] > fastSMA[9]) and (open[9] > fastSMA[9]) and (close[10] > fastSMA[10]) and (open[10] > fastSMA[10])
candleDnTrend = (close[5] < fastSMA[5]) and (open[5] < fastSMA[5]) and (close[6] < fastSMA[6]) and (open[6] < fastSMA[6]) and (close[7] < fastSMA[7]) and (open[7] < fastSMA[7]) and (close[8] < fastSMA[8]) and (open[8] < fastSMA[8]) and (close[9] < fastSMA[9]) and (open[9] < fastSMA[9]) and (close[10] < fastSMA[10]) and (open[10] < fastSMA[10])

// Specify Piercing Conditions
bullPierce = ((low < fastSMA) and (open > fastSMA) and (close > fastSMA)) or ((low < slowSMA) and (open > slowSMA) and (close > slowSMA))
bearPierce = ((high > fastSMA) and (open < fastSMA) and (close < fastSMA)) or ((high > slowSMA) and (open < slowSMA) and (close < slowSMA))

// MA Slope Function
angle(_source) =>
    rad2degree=180/3.14159265359
    ang=rad2degree*atan((_source[0] - _source[1])/atr(atr_valu)) 

// Calculate MA Slope
fastSlope=angle(fastSMA)
slowSlope=angle(slowSMA)
slopingUp = fastSlope > slp_long
slopingDn = fastSlope < slp_shrt
    
// Specify Entry Conditions
longEntry = smaUpTrend and bullishPinBar and bullPierce
shortEntry = smaDnTrend and bearishPinBar and bearPierce
longEntryWithSlope = smaUpTrend and bullishPinBar and bullPierce and slopingUp
shortEntryWithSlope = smaDnTrend and bearishPinBar and bearPierce and slopingDn

// Specify Secondary Exit Conditions
longExit = crossunder(fastSMA, slowSMA)
shortExit = crossover(fastSMA, slowSMA)

// Long Entry Function
enterlong() =>
    risk = usr_risk * 0.01 * strategy.equity
    stopLoss = low[1] - atr[1] * atr_mult
    entryPrice = high[1]
    units = risk / (entryPrice - stopLoss)
    takeProfit = entryPrice + trd_rewd * (entryPrice - stopLoss)
    strategy.entry("long", strategy.long, units, stop=entryPrice)
    strategy.exit("exit long", "long", stop=stopLoss, limit=takeProfit)
    
// Short Entry Function
entershort() =>
    risk = usr_risk * 0.01 * strategy.equity
    stopLoss = high[1] + atr[1] * atr_mult
    entryPrice = low[1]
    units = risk / (stopLoss - entryPrice)
    takeProfit = entryPrice - trd_rewd * (stopLoss - entryPrice)
    strategy.entry("short", strategy.short, units, stop=entryPrice)
    strategy.exit("exit short", "short", stop=stopLoss, limit=takeProfit)
    
// Execute Long Entry w/o Slope
if (longEntry and use_slpe == false)
    enterlong()
    
// Execute Long Entry w/ Slope
if (longEntryWithSlope and use_slpe == true)
    enterlong()

// Exit Long Due to Re-Cross
if(longExit and strategy.position_size > 0 and emg_exit)    
    strategy.order("exit long, re-cross", strategy.short, abs(strategy.position_size))

// Cancel the Long Entry
strategy.cancel("long", barssince(longEntry) > ent_canc)

// Execute Short Entry w/o Slope
if (shortEntry and use_slpe == false)
    entershort() 
    
// Execute Short Entry w/ Slope
if (shortEntryWithSlope and use_slpe == true)
    entershort() 

// Exit Short Due to Re-Cross
if(shortExit and strategy.position_size < 0 and emg_exit)    
    strategy.order("exit short, re-cross", strategy.long, abs(strategy.position_size))

// Cancel the Short Entry
strategy.cancel("short", barssince(shortEntry) > ent_canc)

// Plot Moving Averages to Chart
plot(fastSMA, color=color.red)
plot(slowSMA, color=color.blue)

// Plot Pin Bars to Chart
plotshape(bullishPinBar, style=shape.arrowup, location=location.abovebar, color=#FF0000, text='')
plotshape(bearishPinBar, style=shape.arrowdown, location=location.belowbar, color=#0000FF, text='')

Thêm nữa