Chiến lược chuyển động trung bình chuyển động

Tác giả:ChaoZhang, Ngày: 2023-09-21 21:29:22
Tags:

Tổng quan

Chiến lược này sử dụng các nguyên tắc chéo giữa các đường trung bình di chuyển nhanh và chậm để xác định hướng xu hướng thị trường và tạo ra tín hiệu mua và bán.

Chiến lược logic

Chiến lược này sử dụng hai đường trung bình động, một đường nhanh và một đường chậm. Đường nhanh sử dụng EMA 3 ngày và đường chậm sử dụng EMA 15 ngày. Khi đường nhanh vượt qua trên đường chậm từ dưới, nó chỉ ra xu hướng tăng và đưa ra tín hiệu mua. Ngược lại, khi đường nhanh vượt qua dưới đường chậm từ trên, nó báo hiệu xu hướng giảm và đưa ra tín hiệu bán.

Chiến lược này cũng thiết lập EMA 3 ngày nhanh hơn như đường thoát nhanh. Khi giá phá vỡ dưới đường thoát nhanh này, nó đánh giá xu hướng đã đảo ngược và nên thoát khỏi vị trí mua hiện tại. Tương tự như vậy, khi giá phá vỡ trở lại trên đường thoát, nó chỉ ra xu hướng tăng mới và đưa ra tín hiệu để quay lại mua.

Các tín hiệu hoạt động cụ thể được thiết lập như sau:

  1. Đường nhanh vượt qua phía trên đường chậm từ phía dưới, đi dài

  2. Đường nhanh vượt qua bên dưới đường chậm từ trên, đi ngắn

  3. Giá phá vỡ dưới đường thoát nhanh, đóng vị trí dài

  4. Giá phá vỡ trở lại trên đường thoát nhanh, quay trở lại dài

Ưu điểm

  • Dễ sử dụng, chỉ cần cấu hình hai thông số trung bình động, dễ thực hiện

  • Dữ liệu backtesting đầy đủ, sử dụng các chỉ số chung để đánh giá khả năng tồn tại

  • Nhiều thông số cấu hình để tối ưu hóa

  • Sử dụng đường thoát nhanh như là dừng lỗ để kiểm soát tốt hơn rủi ro

  • Logic chiến lược rõ ràng, tín hiệu mua và bán rõ ràng

  • Tần suất hoạt động thích hợp, tránh giao dịch quá mức

Rủi ro

  • Có xu hướng phát hiện nhiều tín hiệu sai khi xu hướng không rõ ràng hơn là theo xu hướng theo chiến lược

  • Đường trung bình di chuyển có bản chất chậm, có thể bỏ lỡ các điểm chuyển đổi

  • Các thông số cố định không thể thích nghi với những thay đổi của thị trường, cần tối ưu hóa

  • Dừng lỗ có thể quá mềm, không thể dừng lỗ kịp thời

  • Các tín hiệu thường xuyên có thể dẫn đến chi phí giao dịch cao hơn

  • Các tín hiệu có thể khác nhau và cần xác nhận với các chỉ số khác

Rủi ro có thể được quản lý bằng cách tối ưu hóa tham số, thêm bộ lọc, thư giãn dừng lỗ, cập nhật tham số kịp thời v.v.

Tăng cường

  • Kiểm tra và tối ưu hóa các thông số để phù hợp hơn với điều kiện thị trường

  • Đưa ra nhiều chỉ số hơn để hình thành một hệ thống mạnh mẽ

  • Xây dựng cài đặt tham số thích nghi dựa trên thị trường thời gian thực

  • Áp dụng các mô hình học máy để tối ưu hóa thông minh hơn

  • Thiết lập stop loss động hoặc trailing để kiểm soát rủi ro tốt hơn

  • Kết hợp các chỉ số khối lượng để tránh sự khác biệt

Kết luận

Đây là một chiến lược chéo trung bình di chuyển kép tương đối đơn giản. Nó xác định xu hướng thị trường và tín hiệu giao dịch dựa trên sự tương tác giữa trung bình di chuyển nhanh và chậm. Chiến lược này dễ thực hiện và có thể được điều chỉnh thông qua tối ưu hóa. Nhưng nó cũng có một số rủi ro. Cần nhiều bộ lọc hơn để xác nhận tín hiệu và quản lý rủi ro. Khi được tối ưu hóa đúng cách và áp dụng cho giao dịch trung hạn, nó có thể trở thành một hệ thống giao dịch định lượng rất thực tế.


/*backtest
start: 2023-01-01 00:00:00
end: 2023-02-03 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/
// © ehaarjee, ECHKAY, JackBauer007

//@version=4
//study(title="Tale_indicators", overlay=true)
strategy("Tale Indicators Strategy", overlay=true, precision=8, max_bars_back=200, pyramiding=0, initial_capital=20000, commission_type="percent", commission_value=0.1)

len_fast = input(3, minval=1, title="FAST EMA")
src_fast = input(close, title="Source for Fast")
fastMA = ema(src_fast, len_fast)
plot(fastMA, title="Slow EMA", color=color.orange)

len_slow = input(15, minval=1, title="SLOW EMA")
src_slow = input(close, title="Source for Slow")
slowMA = ema(src_slow, len_slow)
plot(slowMA, title="Fast EMA", color=color.blue)

len_fast_exit = input(3, minval=1, title="FAST EMA Exit")
src_fast_exit = input(close, title="Source for Fast Exit")
fastMAE = ema(src_fast_exit, len_fast_exit)
plot(fastMAE, title="Fast EMA Ex", color=color.red)


src_slow_enter_short = input(low, title="Source for Short Entry")
slowMASEn = ema(src_slow_enter_short, len_slow)

src_slow_enter_long = input(high, title="Source for Long Entry")
slowMALEn = ema(src_slow_enter_long, len_slow)

src_slow_exit_short = input(low, title="Source for Short Exit")
slowMASEx = ema(src_slow_enter_short, len_slow)

src_slow_exit_long = input(high, title="Source for Long Exit")
slowMALEx = ema(src_slow_enter_long, len_slow)

enter_long = crossover(fastMA, slowMALEn)
enter_short = crossunder(fastMA, slowMASEn)
exit_long = crossunder(fastMAE, slowMALEx)
exit_short = crossover(fastMAE, slowMALEx)

out_enter = iff(enter_long == true, 1, iff(enter_short == true, -1, 0))
plotarrow(out_enter, "Plot Enter Points", colorup=color.green, colordown=color.red, maxheight = 30)

bull = fastMA > slowMALEn
bear = fastMA < slowMASEn

c = bull ? color.green : bear ? color.red  : color.white
bgcolor(c)

exit_tuner = input(0.005, title="Exit Tuner to touch slowEMA")

bull_exit = (bull and (low>(fastMAE*(1+exit_tuner)))) or exit_long or (not(bear) and (fastMAE>high))
bear_exit = (bear and ((fastMAE*(1-exit_tuner))>high)) or exit_short or (not(bull) and (low>fastMAE))

bull_r = (bull and ((bull_exit[1]) or (bull_exit[2] and bull_exit[1])) and (low<=fastMAE))
bear_r = (bear and ((bear_exit[1]) or (bull_exit[2] and bull_exit[1])) and (fastMAE<=high))

bull_re = (bull and (low<slowMALEn)) and not(enter_long)
bear_re = (bear and (high>slowMASEn)) and not(enter_short)

bull_ree = (bull and ((low<slowMALEn) and not(bull_re[1] or enter_long[1]))) 
bear_ree = (bear and ((high>slowMASEn) and not(bear_re[1] or enter_short[1])))

bull_reenter =  (bull_r) and not(enter_long)
bear_reenter =  (bear_r) and not(enter_short)

plotshape(bull_exit, "Plot Bull Exit", style = shape.arrowdown, color=color.green, size=size.small, text="ExL", location=location.abovebar)
plotshape(bear_exit, "Plot Bear Exit", style = shape.arrowup, color=color.red, size=size.small, text="ExS", location=location.belowbar)

plotshape(bull_reenter, "Plot Bull ReEnter", style = shape.arrowup, color=color.green, size=size.small, text="ReL", location=location.belowbar)
plotshape(bear_reenter, "Plot Bear ReEnter", style = shape.arrowdown, color=color.red, size=size.small, text="ReS", location=location.abovebar)

run_strategy = input(true, title="Run Strategy")
// === INPUT BACKTEST RANGE ===
fromMonth = input(defval = 1,    title = "From Month",      type = input.integer, minval = 1, maxval = 12)
fromDay   = input(defval = 1,    title = "From Day",        type = input.integer, minval = 1, maxval = 31)
fromYear  = input(defval = 2020, title = "From Year",       type = input.integer, minval = 2000)
thruMonth = input(defval = 1,    title = "Thru Month",      type = input.integer, minval = 1, maxval = 12)
thruDay   = input(defval = 1,    title = "Thru Day",        type = input.integer, minval = 1, maxval = 31)
thruYear  = input(defval = 2100, title = "Thru Year",       type = input.integer, minval = 2000)

// === INPUT SHOW PLOT ===
showDate  = input(defval = true, title = "Show Date Range", type = input.bool)
// === FUNCTION EXAMPLE ===
start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)        // backtest start window
finish    = timestamp(thruYear, thruMonth, thruDay, 23, 59)        // backtest finish window
window()  => true       // create function "within window of time"

var long_position_open = false
var short_position_open = false


if (enter_long and not(bull_exit) and not(long_position_open))
//    strategy.entry("LO", strategy.long, qty=4)
    long_position_open := true
    if (short_position_open)
//        strategy.close("SO")
        short_position_open := false

if (bull_reenter and not(long_position_open))  
//    strategy.entry("LO", strategy.long, qty=1)
    long_position_open := true

if (bull_exit and long_position_open)
//  strategy.close("LO")
    long_position_open := false
    
if (enter_short and not(bear_exit) and not(short_position_open))
//    strategy.entry("SO", strategy.short, qty=4)
    short_position_open := true
    if(long_position_open)
//        strategy.close("LO")
        long_position_open := false

if (bear_reenter and not(short_position_open))  
//    strategy.entry("SO", strategy.long, qty=1)
    long_position_open := true

if (bear_exit and short_position_open)
//    strategy.close("SO")
    short_position_open := false

if(run_strategy)
    strategy.entry("LO", strategy.long, when=(window() and enter_long), qty=4)
    strategy.entry("LO", strategy.long, when=(window() and bull_reenter and not(long_position_open)), qty=1)
    strategy.close("LO", when=(window() and bull_exit and long_position_open))
    strategy.entry("SO", strategy.short, when=(window() and enter_short), qty=4)
    strategy.entry("SO", strategy.short, when=(window() and bear_reenter and not(short_position_open)), qty=1)
    strategy.close("SO", when=(window() and bear_exit and short_position_open))


Thêm nữa