
Chiến lược này tạo ra tín hiệu giao dịch dựa trên nguyên tắc chéo giữa đường trung bình di chuyển nhanh và đường trung bình di chuyển chậm. Khi đường trung bình di chuyển nhanh đi qua đường trung bình di chuyển chậm từ phía dưới, tạo ra tín hiệu mua; Khi đường trung bình di chuyển nhanh đi qua đường trung bình di chuyển chậm từ phía trên xuống, tạo ra tín hiệu bán.
Chiến lược này sử dụng hàm sma để tính toán trung bình di chuyển nhanh và trung bình di chuyển chậm. Trong đó fast_SMA là trung bình di chuyển nhanh với độ dài chu kỳ là fast_SMA_input; slow_SMA là trung bình di chuyển chậm với độ dài chu kỳ là slow_SMA_input.
Chiến lược sử dụng các hàm cross và crossunder để đánh giá sự giao chéo giữa trung bình di chuyển nhanh và trung bình di chuyển chậm. Khi trung bình di chuyển nhanh đi qua trung bình di chuyển chậm, biến LONG là đúng, tạo ra tín hiệu mua; khi trung bình di chuyển nhanh đi qua trung bình di chuyển chậm, biến SHORT là đúng, tạo ra tín hiệu bán.
Chiến lược này có những ưu điểm sau:
Chiến lược này cũng có những rủi ro sau:
Phương pháp kiểm soát rủi ro:
Chiến lược này có thể được tối ưu hóa bằng cách:
Chiến lược này sử dụng lợi thế của đường trung bình di chuyển để tạo ra tín hiệu giao dịch đơn giản và hiệu quả. Mặc dù có một số rủi ro, nhưng có thể cải thiện bằng cách tối ưu hóa tham số, thêm các điều kiện lọc. Chiến lược giao chéo đường trung bình di chuyển đáng để nghiên cứu và áp dụng thêm.
/*backtest
start: 2023-10-01 00:00:00
end: 2023-10-13 00:00:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@author Jacques Grobler
//
// SIMPLE CROSS OVER BOT
// =====================
//
// This is a simple example of how to set up a strategy to go long or short
// If you make any modifications or have any suggestions, let me know
// When using this script, every section marked back testing should be
// uncommented in order to use for back testing, same goes for using the script portion
///////////////////////////////////////////////////////////////////////////////////////
//// INTRO
//// -----
// BACKTESTING
//@version=4
strategy(title="SimpleCrossOver_Bot_V1_Backtester", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, pyramiding=0, commission_type=strategy.commission.percent, commission_value=0.1)
// SIGNALS
//study(title="SimpleCrossOver_Bot_V1_Signals", overlay = true)
///////////////////////////////////////////////////////////////////////////////////////
//// INPUTS
//// ------
// BACKTESTING
dateSart_Year = input(2018, title="Start Year", minval=2000)
dateSart_Month = input(1, title="Start Month", minval=1, maxval=12)
dateSart_Day = input(1, title="Start Day", minval=1, maxval=31)
dateEnd_Year = input(2019, title="End Year", minval=2000)
dateEnd_Month = input(1, title="End Month", minval=1, maxval=12)
dateEnd_Day = input(1, title="End Day", minval=1, maxval=31)
// BACKTESTING AND SIGNALS
fast_SMA_input = input(7, title="SMA Fast")
slow_SMA_input = input(25, title="SMA Slow")
///////////////////////////////////////////////////////////////////////////////////////
//// INDICATORS
//// ----------
fast_SMA = sma(close, fast_SMA_input)
slow_SMA = sma(close, slow_SMA_input)
///////////////////////////////////////////////////////////////////////////////////////
//// STRATEGY
//// --------
LONG = cross(fast_SMA, slow_SMA) and fast_SMA > slow_SMA
stratLONG() => crossover(fast_SMA, slow_SMA)
SHORT = cross(fast_SMA, slow_SMA) and fast_SMA < slow_SMA
stratSHORT() => crossunder(fast_SMA, slow_SMA)
///////////////////////////////////////////////////////////////////////////////////////
//// TRIGGERS
//// --------
// BACKTESTING
testPeriodStart = timestamp(dateSart_Year, dateSart_Month, dateSart_Day, 0, 0)
testPeriodStop = timestamp(dateEnd_Year, dateEnd_Month, dateEnd_Day, 0, 0)
timecondition = true
strategy.entry(id="LONG", long = true, when=timecondition and stratLONG())
strategy.entry(id="SHORT", long = false, when=timecondition and stratSHORT())
// SIGNALS
//alertcondition(LONG, title="LONG")
//alertcondition(SHORT, title="SHORT")
///////////////////////////////////////////////////////////////////////////////////////
//// PLOTS
//// -----
// BACKTESTING AND SIGNALS
plot(fast_SMA, color=green, linewidth=1)
plot(slow_SMA, color=yellow, linewidth=1)
plotshape(LONG, title="LONG", style=shape.triangleup, text="LONG", location=location.belowbar, size=size.small, color=green)
plotshape(SHORT, title="SHORT", style=shape.triangledown, text="SHORT", location=location.abovebar, size=size.small, color=red)