Chiến lược dao động dải Fibonacci

Tác giả:ChaoZhang, Ngày: 2023-11-21 13:47:12
Tags:

img

Tổng quan

Chiến lược dao động dải Fibonacci là một chiến lược định lượng được thiết kế dựa trên lý thuyết Fibonacci. Nó chủ yếu sử dụng tỷ lệ Fibonacci để tính toán nhiều dải giá để tạo thành các dải trên và dưới. Khi giá vượt qua các dải, các tín hiệu giao dịch được tạo ra để nắm bắt các đặc điểm dao động giữa các dải để kiếm lợi nhuận.

Chiến lược logic

Lý thuyết cốt lõi của mã là tính toán các dải giá Fibonacci như các điểm chính.

  1. Tính toán EMA 14 giai đoạn như đường trung gian
  2. Tính toán các đường 4 dải trên và dưới theo tỷ lệ ATR và Fibonacci
  3. Tạo tín hiệu giao dịch khi giá vượt qua các dải giảm hoặc dải tăng
  4. Thiết lập dừng lỗ và lấy lợi nhuận để theo dõi dao động giá cho lợi nhuận

Với phương pháp dựa trên đột phá này, nó có thể nắm bắt hiệu quả biến động ngắn hạn trên thị trường và thực hiện giao dịch đi lại giữa các dải để kiếm lợi nhuận.

Ưu điểm

Lợi thế lớn nhất của chiến lược này là nó sử dụng chỉ số lý thuyết quan trọng của tỷ lệ Fibonacci để xác định các điểm giá chính, do đó làm tăng xác suất lợi nhuận.

  1. Dải Fibonacci rõ ràng, dễ đánh giá các điểm đột phá
  2. Phạm vi băng thông hợp lý, không quá phân mảnh hoặc quá lỏng lẻo
  3. Nhiều dải có thể được chọn cho cả giao dịch tích cực và bảo thủ
  4. Đặc điểm dao động dải quan trọng, hiệu quả tốt cho các chiến lược giao dịch ngắn hạn

Rủi ro

Vì chiến lược theo đuổi lợi nhuận ngắn hạn, cũng có một số rủi ro cần lưu ý:

  1. Không thể kiếm lợi nhuận trong xu hướng chu kỳ lớn
  2. Rủi ro dừng lỗ cao trong tình trạng biến động giá mạnh
  3. Nhiều tín hiệu đột phá đòi hỏi sự lựa chọn cẩn thận
  4. Không hợp lệ khi các đặc điểm dao động băng tần biến mất

Những rủi ro này có thể được kiểm soát bằng cách điều chỉnh các tham số thích hợp, chọn các dải phù hợp và các phương pháp quản lý vốn.

Tối ưu hóa

Vẫn còn chỗ để tối ưu hóa thêm chiến lược:

  1. Kết hợp với các chỉ số xu hướng để tạo ra tín hiệu chỉ trong một số hướng xu hướng nhất định
  2. Khép lại chiến lược trước và sau thời gian cụ thể hoặc các sự kiện quan trọng
  3. Điều chỉnh năng động phạm vi dừng lỗ theo tần suất biến động thị trường
  4. Tối ưu hóa các thông số bằng cách chọn EMA của các chu kỳ khác nhau làm đường chuẩn

Kết luận

Nói chung, Chiến lược dao động dải Fibonacci là một chiến lược ngắn hạn rất thực tế. Nó sử dụng lý thuyết Fibonacci để thiết lập các điểm chính của giá. Khi giá dao động xung quanh các điểm này, lợi nhuận hào phóng có thể được thu được. Phương pháp dựa trên đột phá này phù hợp với các thị trường có một mức độ biến động và đặc điểm nhất định. Nó có thể được sử dụng một mình hoặc kết hợp với các chiến lược khác. Với điều chỉnh tham số và quản lý vốn thích hợp, chiến lược có thể hoạt động ổn định trong dài hạn.


/*backtest
start: 2022-11-14 00:00:00
end: 2023-11-20 00:00:00
period: 1d
basePeriod: 1h
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/
// © drhakankilic

//@version=5
strategy("FIBONACCI BANDS Strategy", shorttitle="FBANDS Strategy", overlay=true)
// === Date === { 
//Backtest dates
fromDay = input.int(defval=1, title='From Day',minval=1,maxval=31)
fromMonth = input.int(defval=2, title='From Month',minval=1,maxval=12)
fromYear = input.int(defval=2022, title='From Year')
thruDay = input.int(defval=1, title='Thru Day',minval=1,maxval=31)
thruMonth = input.int(defval=1, title='Thru Month',minval=1,maxval=12)
thruYear = input.int(defval=2112, title='Thru Year')
showDate = true  // input(defval=true, title="Show Date Range")
start = timestamp(fromYear, fromMonth, fromDay, 00, 00)  // backtest start window
finish = timestamp(thruYear, thruMonth, thruDay, 23, 59)  // backtest finish window
window() =>  // create function "within window of time"
    time >= start and time <= finish ? true : false
// }

// === Long or Short ===  
tradeDirection = input.string(title="Long veya Short", options=["Long", "Short", "Both"], defval="Both",                                       group="Bot")
// Translate input into trading conditions
longOK  = (tradeDirection == "Long") or (tradeDirection == "Both")
shortOK = (tradeDirection == "Short") or (tradeDirection == "Both")
copypaste   = input('{{strategy.order.alert_message}}',         title='alert message to copy/paste',                                    group="Bot")
// }

// === FIBONACCI BANDS === {
EMAperiod = input.int(14, title='EMAperiod', minval=1, maxval=500, group="Fibonacci")
ATRperiod = input.int(14, title='ATRperiod', minval=1, maxval=500, group="Fibonacci")
EMA = ta.ema(close, EMAperiod)
TR1 = math.max(high - low, math.abs(high - close[1]))
TR = math.max(TR1, math.abs(low - close[1]))
ATR = ta.sma(TR, ATRperiod)
F2 = input(defval=1.618, title='Fibonacci Ratio 2', group="Fibonacci")
F3 = input(defval=2.618, title='Fibonacci Ratio 3', group="Fibonacci")
F4 = input(defval=4.236, title='Fibonacci Ratio 4', group="Fibonacci")
R1 = ATR
R2 = ATR * F2
R3 = ATR * F3
R4 = ATR * F4
FIBOTOP4 = EMA + R4
FIBOTOP3 = EMA + R3
FIBOTOP2 = EMA + R2
FIBOTOP1 = EMA + R1
FIBOBOT1 = EMA - R1
FIBOBOT2 = EMA - R2
FIBOBOT3 = EMA - R3
FIBOBOT4 = EMA - R4
plot(FIBOTOP4[1], title='FIBOTOP4', linewidth=1, color=color.new(color.orange, 0))
plot(FIBOTOP3[1], title='FIBOTOP3', linewidth=1, color=color.new(color.aqua, 20))
plot(FIBOTOP2[1], title='FIBOTOP2', linewidth=1, color=color.new(color.gray, 40))
plot(FIBOTOP1[1], title='FIBOTOP1', linewidth=1, color=color.new(color.purple, 40))

plot(FIBOBOT1[1], title='FIBOBOT1', linewidth=1, color=color.new(color.green, 40))
plot(FIBOBOT2[1], title='FIBOBOT2', linewidth=1, color=color.new(color.yellow, 40))
plot(FIBOBOT3[1], title='FIBOBOT3', linewidth=1, color=color.new(color.blue, 20))
plot(FIBOBOT4[1], title='FIBOBOT4', linewidth=1, color=color.new(color.aqua, 0))
// plot(EMA[1], style=plot.style_cross, title='EMA', color=color.new(color.red, 0))

prefm = input.string(title="Fibo", options=["close>FIBOTOP4(orange)", "close>FIBOTOP3(aqua)","close>FIBOTOP2(gray)","close>FIBOTOP1(purple)", "Disable"] , defval="close>FIBOTOP1(purple)", group="Long")
_prefm = false 
if (prefm == "close>FIBOTOP4(orange)" )
    _prefm := close>FIBOTOP4[1]
    
if (prefm == "close>FIBOTOP3(aqua)" )
    _prefm := close>FIBOTOP3[1]

if (prefm == "close>FIBOTOP2(gray)" )
    _prefm := close>FIBOTOP2[1]
    
if (prefm == "close>FIBOTOP1(purple)" )
    _prefm := close>FIBOTOP2[1]
 
 
if (prefm == "Disable" )
    _prefm := low<low[1] or low>low[1]  
    
prefmS = input.string(title="Fibo", options=["close<FIBOBOT1(green)", "close<FIBOBOT2(yellow)", "close<FIBOBOT3(blue)", "close<FIBOBOT4(aqua)", "Disable"] , defval="close<FIBOBOT1(green)", group="Short")
_prefmS = false 
if (prefmS == "close<FIBOBOT1(green)" )
    _prefmS := close<FIBOBOT1[1]
  
if (prefmS == "close<FIBOBOT2(yellow)" )
    _prefmS := close<FIBOBOT2[1]

if (prefmS == "close<FIBOBOT3(blue)" )
    _prefmS := close<FIBOBOT3[1]
  
if (prefmS == "close<FIBOBOT4(aqua)" )
    _prefmS := close<FIBOBOT4[1]

if (prefmS == "Disable" )
    _prefmS := low<low[1] or low>low[1]  

// }

long2= _prefm 

short2= _prefmS
//

// === Bot Codes === { 
enterlong = input("Long Code", title='Long İlk Alım', group="Long Code")
entershort= input("Short Code", title='Short İlk Alım', group="Short Code")
exitlong = input("Long Exit Code", title='Long Exit', group="Long Code")
exitshort= input("Short Exit Code", title='Short Exit', group="Short Code")
// }

////////////////////////////////////////////////////////////////////////////////////////////TPSL
// Inputs
sl_inp = input.float(4, title='Stop %', step=0.1, group="Long") / 100
tp_inp = input.float(1.5, title='TP %', step=0.1, group="Long") / 100

sl_inp2 = input.float(4, title='Stop %', step=0.1, group="Short") / 100
tp_inp2 = input.float(1.5, title='TP %', step=0.1, group="Short") / 100

longtp = strategy.position_avg_price * (1 + tp_inp) 
longstop=  strategy.position_avg_price * (1 - sl_inp)

shortstop=  strategy.position_avg_price * (1 + sl_inp2)
shorttp = strategy.position_avg_price * (1 - tp_inp2) 
////////////////////////////////////////////////////////////////////////////////////////////
if window() and strategy.position_size==0 and longOK
    strategy.entry("Long", strategy.long, when= long2, alert_message=enterlong, comment="Long")
    
if strategy.position_size>0
    strategy.exit("Long", stop= longstop, limit=longtp, alert_message=exitlong, comment="TPSL")
////////////////////////////////////////////////////////////////////////////////////////////SHORT
if window() and strategy.position_size==0 and shortOK 
    strategy.entry("Short", strategy.short, when= short2, alert_message=entershort, comment="Short")
    
if strategy.position_size<0
    strategy.exit("Short", stop= shortstop, limit= shorttp, alert_message=exitshort, comment="TPSL")
 


Thêm nữa