Heiken Ashi và Chiến lược siêu xu hướng

Tác giả:ChaoZhang, Ngày: 2023-11-02 16:15:18
Tags:

img

Tổng quan

Chiến lược Heiken Ashi và siêu xu hướng là một chiến lược theo xu hướng kết hợp các ngọn nến Heiken Ashi và chỉ số siêu xu hướng. Nó nhằm mục đích xác định hướng xu hướng, giao dịch với xu hướng và thoát nhanh chóng khi xu hướng đảo ngược, để giảm thiểu lỗ từ các giao dịch không theo xu hướng.

Chiến lược logic

Nến Heiken Ashi là một loại nến đặc biệt sử dụng mức trung bình của giá mở, đóng, cao và thấp để vẽ các thân nến, lọc ra tiếng ồn thị trường và làm cho mô hình rõ ràng hơn.

Chiến lược đầu tiên tính toán các nến Heiken Ashi, sau đó tính toán chỉ số siêu xu hướng dựa trên nến Heiken Ashi. Các tín hiệu giao dịch được tạo ra khi giá vượt qua các đường siêu xu hướng. Cụ thể, chiến lược sử dụng nến Heiken Ashi để tính toán phạm vi thực sự, sau đó dẫn ra các dải trên và dưới của siêu xu hướng bằng cách sử dụng phạm vi và giá trung bình. Các tín hiệu dài được tạo ra khi giá vượt qua dải dưới, và các tín hiệu ngắn khi giá vượt qua dải trên.

Các thông số siêu xu hướng cũng được tối ưu hóa để có kết quả tốt nhất, cải thiện độ nhạy của chỉ số. Ngoài ra, một cơ chế dừng lỗ được thực hiện để kiểm soát rủi ro trong khi khóa lợi nhuận.

Phân tích lợi thế

  • Nến Heiken Ashi lọc tiếng ồn để có tín hiệu rõ ràng hơn.
  • Super Trend nhanh chóng bắt được những thay đổi xu hướng và tạo ra các tín hiệu kịp thời.
  • Tối ưu hóa tham số cải thiện độ tin cậy của chỉ số.
  • Cơ chế dừng lỗ tích hợp có hiệu quả kiểm soát rủi ro.
  • Mức độ tự động hóa cao bằng cách kết hợp theo dõi xu hướng và giao dịch tự động.

Phân tích rủi ro

  • Tự động hóa cao đòi hỏi phải theo dõi chặt chẽ để tránh bất thường.
  • Heiken Ashi có thể bỏ lỡ các tín hiệu đảo ngược rất nhỏ khi lọc tiếng ồn.
  • Super Trend có thể tạo ra các tín hiệu sai, gây ra bước vào sớm hoặc dừng lỗ.
  • Việc đặt stop loss không đúng cũng có thể dẫn đến tổn thất không cần thiết.
  • Dữ liệu backtesting không đủ có thể gây ra quá tải.

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

  • Kiểm tra thêm các kết hợp tham số để tối ưu hóa thêm Super Trend.
  • Thêm các chỉ số khác để xác nhận tín hiệu siêu xu hướng và giảm tín hiệu sai.
  • Tối ưu hóa chiến lược dừng lỗ để giảm thiểu các điểm dừng không cần thiết trong khi khóa lợi nhuận.
  • Kết hợp các thuật toán học máy để đào tạo đánh giá các xu hướng thực tế bằng cách sử dụng dữ liệu lớn.
  • Sử dụng khung thời gian dài hơn và dữ liệu lịch sử đa dạng hơn cho kiểm tra ngược để cải thiện độ tin cậy.

Tóm lại

Chiến lược Heiken Ashi và Super Trend là một chiến lược theo xu hướng. Nó xác định hướng xu hướng và giao dịch với xu hướng chính, trong khi nhanh chóng dừng lại khi đảo ngược. Chiến lược tích hợp bộ lọc tiếng ồn Heiken Ashi và phát hiện thay đổi xu hướng nhanh chóng của Super Trend.


/*backtest
start: 2022-10-26 00:00:00
end: 2023-11-01 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5

strategy("Heiken Ashi & Super Trend_ARM", overlay=true,  pyramiding=1,initial_capital = 10000, default_qty_type= strategy.percent_of_equity, default_qty_value = 100, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.percent,commission_value=0.02)

///////////////////////////////////////////////////
////////////////////Function///////////////////////
///////////////////////////////////////////////////


heikinashi_open = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open)
heikinashi_high = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, high)
heikinashi_low  = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low)
heikinashi_close= request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close)
heikinashi_color = heikinashi_open < heikinashi_close ? #53b987 : #eb4d5c
// plotbar(heikinashi_open, heikinashi_high, heikinashi_low, heikinashi_close, color=heikinashi_color)

x_sma(x, y) =>
    sumx = 0.0
    for i = 0 to y - 1
        sumx := sumx + x[i] / y
    sumx

x_rma(src, length) =>
	alpha = 1/length
	sum = 0.0
	sum := na(sum[1]) ? x_sma(src, length) : alpha * src + (1 - alpha) * nz(sum[1])

x_atr(length) =>
    trueRange = na(heikinashi_high[1])? heikinashi_high-heikinashi_low : math.max(math.max(heikinashi_high - heikinashi_low, math.abs(heikinashi_high - heikinashi_close[1])), math.abs(heikinashi_low - heikinashi_close[1]))
    //true range can be also calculated with ta.tr(true)
    x_rma(trueRange, length)

x_supertrend(factor, atrPeriod) =>
	src = (heikinashi_high+heikinashi_low)/2
	atr = x_atr(atrPeriod)
	upperBand = src + factor * atr
	lowerBand = src - factor * atr
	prevLowerBand = nz(lowerBand[1])
	prevUpperBand = nz(upperBand[1])

	lowerBand := lowerBand > prevLowerBand or heikinashi_close[1] < prevLowerBand ? lowerBand : prevLowerBand
	upperBand := upperBand < prevUpperBand or heikinashi_close[1] > prevUpperBand ? upperBand : prevUpperBand
	int direction = na
	float superTrend = na
	prevSuperTrend = superTrend[1]
	if na(atr[1])
		direction := 1
	else if prevSuperTrend == prevUpperBand
		direction := heikinashi_close > upperBand ? -1 : 1
	else
		direction := heikinashi_close < lowerBand ? 1 : -1
	superTrend := direction == -1 ? lowerBand : upperBand
	[superTrend, direction]
	

///////////////////////////////////////////////////
////////////////////Indicators/////////////////////
///////////////////////////////////////////////////

factor = input.float(3.0, "Factor", step = 0.01)
atrPeriod = input(10, "ATR Length")


[supertrend, direction] = x_supertrend(factor, atrPeriod)

bodyMiddle = plot((heikinashi_open + heikinashi_close) / 2, display=display.none)
upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = color.green, style=plot.style_linebr)
downTrend = plot(direction < 0? na : supertrend, "Down Trend", color = color.red, style=plot.style_linebr)

fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps=false)
fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps=false)

///////////////////////////////////////////////////
////////////////////Strategy///////////////////////
///////////////////////////////////////////////////

var bool longCond                    = na, var bool shortCond                   = na, longCond := nz(longCond[1]), shortCond := nz(shortCond[1])
var int CondIni_long                 = 0, var int CondIni_short                 = 0, CondIni_long := nz(CondIni_long[1]), CondIni_short := nz(CondIni_short[1])
var float open_longCondition         = na, var float open_shortCondition   = na


long  = ta.change(direction) < 0
short = ta.change(direction) > 0


longCond        :=                                                              long
shortCond       :=                                                              short

CondIni_long    :=                                                              longCond[1] ? 1 : shortCond[1] ? -1 : nz(CondIni_long[1])
CondIni_short   :=                                                              longCond[1] ? 1 : shortCond[1] ? -1 : nz(CondIni_short[1])
longCondition   =                                                               (longCond[1] and nz(CondIni_long[1]) == -1)
shortCondition  =                                                               (shortCond[1] and nz(CondIni_short[1]) == 1)


open_longCondition             :=                                          long ? close[1] :                                                      nz(open_longCondition[1])
open_shortCondition            :=                                          short ? close[1] :                                                     nz(open_shortCondition[1])


//TP
tp                    = input.float(1.1  , "TP [%]",                      step = 0.1) 

//BACKTESTING inputs --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

testStartYear       =                   input.int(2000,                             title="start year",                                         minval = 1997, maxval = 3000,                                                   group= "BACKTEST") 
testStartMonth      =                   input.int(01,                               title="start month",                                        minval = 1, maxval = 12,                                                        group= "BACKTEST")
testStartDay        =                   input.int(01,                               title="start day",                                          minval = 1, maxval = 31,                                                        group= "BACKTEST")
testPeriodStart     =                   timestamp(testStartYear,testStartMonth,testStartDay,0,0)
testStopYear        =                   input.int(3333,                             title="stop year",                                          minval=1980, maxval = 3333,                                                     group= "BACKTEST")
testStopMonth       =                   input.int(12,                               title="stop month",                                         minval=1, maxval=12,                                                            group= "BACKTEST")
testStopDay         =                   input.int(31,                               title="stop day",                                           minval=1, maxval=31,                                                            group= "BACKTEST")
testPeriodStop      =                   timestamp(testStopYear, testStopMonth, testStopDay, 0, 0)
testPeriod          =                   time >= testPeriodStart and time <= testPeriodStop ? true : false

// Backtest  ==================================================================================================================================================================================================================================================================================================================================


if longCond
    strategy.entry("L", strategy.long, when=testPeriod)

if shortCond
    strategy.entry("S", strategy.short, when=testPeriod)
    

strategy.exit("TP_L", "L", profit =((open_longCondition   *       (1+(tp/100))) - open_longCondition)/syminfo.mintick)

strategy.exit("TP_S", "S", profit =((open_shortCondition  *       (1+(tp/100))) - open_shortCondition)/syminfo.mintick)





Thêm nữa