
Chiến lược này được xây dựng trên chiến lược giao dịch hai đường đồng nhất ban đầu, thêm mô-đun giới hạn thời gian để kiểm soát thời gian khởi động chiến lược. Mô-đun này có thể quản lý hiệu quả thời gian hoạt động của chiến lược và giảm rủi ro giao dịch trong điều kiện thị trường không lý tưởng.
Chiến lược sử dụng MA nhanh và MA chậm để xây dựng tín hiệu giao dịch. Địa chỉ MA nhanh là 14 ngày và MA chậm là 21 ngày. Khi MA nhanh vượt qua MA chậm, nó tạo ra tín hiệu mua; Khi MA nhanh vượt qua MA chậm, nó tạo ra tín hiệu bán.
Chiến lược cũng giới thiệu tùy chọn đảo ngược giao dịch, có thể đảo ngược hướng của tín hiệu giao dịch ban đầu.
Mô-đun giới hạn thời gian sử dụng cột thời gian để so sánh thời gian hiện tại với thời gian khởi động đã đặt, trả về giá trị thực, để kiểm soát việc bắt đầu chiến lược. Mô-đun này cần thiết lập năm, tháng, ngày, giờ, phút khởi động, chỉ khi thời gian hiện tại vượt quá thời gian đặt, chiến lược sẽ bắt đầu.
Các tham số chu kỳ MA có thể được tối ưu hóa thích hợp, giảm tần số giao dịch. Đồng thời, thiết lập thời gian giới hạn thời gian khởi động mô-đun một cách hợp lý, tránh bỏ lỡ cơ hội. Cuối cùng, tùy thuộc vào các điều kiện thị trường khác nhau, hãy thận trọng chọn xem liệu cần phải đảo ngược hướng tín hiệu giao dịch.
Chiến lược này tạo ra tín hiệu giao dịch bằng cách tạo ra hai MA, và thêm thời gian hạn chế mô-đun kiểm soát chiến lược thời gian hoạt động, có thể nắm bắt xu hướng hiệu quả, đồng thời tránh rủi ro trong điều kiện thị trường không lý tưởng. Chiến lược này cũng có thể được nâng cao hơn nữa bằng các phương tiện như thiết lập tham số tối ưu hóa, mô-đun dừng lỗ và hoạt động xuyên tiêu chuẩn, tăng sự ổn định và lợi nhuận của mỗi giao dịch trong khi giảm tần suất giao dịch.
/*backtest
start: 2023-11-06 00:00:00
end: 2023-11-13 00:00:00
period: 45m
basePeriod: 5m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=2
strategy(title = "Strategy Code Example", shorttitle = "Strategy Code Example", overlay = true)
// Revision: 1
// Author: @JayRogers
//
// *** THIS IS JUST AN EXAMPLE OF STRATEGY TIME LIMITING ***
//
// This is a follow up to my previous strategy example for risk management, extended to include a time limiting factor.
// === GENERAL INPUTS ===
// short ma
maFastSource = input(defval = open, title = "Fast MA Source")
maFastLength = input(defval = 14, title = "Fast MA Period", minval = 1)
// long ma
maSlowSource = input(defval = open, title = "Slow MA Source")
maSlowLength = input(defval = 21, title = "Slow MA Period", minval = 1)
// === STRATEGY RELATED INPUTS ===
tradeInvert = input(defval = false, title = "Invert Trade Direction?")
// Risk management
inpTakeProfit = input(defval = 1000, title = "Take Profit", minval = 0)
inpStopLoss = input(defval = 200, title = "Stop Loss", minval = 0)
inpTrailStop = input(defval = 200, title = "Trailing Stop Loss", minval = 0)
inpTrailOffset = input(defval = 0, title = "Trailing Stop Loss Offset", minval = 0)
// *** FOCUS OF EXAMPLE ***
// Time limiting
// a toggle for enabling/disabling
useTimeLimit = input(defval = true, title = "Use Start Time Limiter?")
// set up where we want to run from
startYear = input(defval = 2016, title = "Start From Year", minval = 0, step = 1)
startMonth = input(defval = 05, title = "Start From Month", minval = 0,step = 1)
startDay = input(defval = 01, title = "Start From Day", minval = 0,step = 1)
startHour = input(defval = 00, title = "Start From Hour", minval = 0,step = 1)
startMinute = input(defval = 00, title = "Start From Minute", minval = 0,step = 1)
// === RISK MANAGEMENT VALUE PREP ===
// if an input is less than 1, assuming not wanted so we assign 'na' value to disable it.
useTakeProfit = inpTakeProfit >= 1 ? inpTakeProfit : na
useStopLoss = inpStopLoss >= 1 ? inpStopLoss : na
useTrailStop = inpTrailStop >= 1 ? inpTrailStop : na
useTrailOffset = inpTrailOffset >= 1 ? inpTrailOffset : na
// *** FOCUS OF EXAMPLE ***
// === TIME LIMITER CHECKING FUNCTION ===
// using a multi line function to return true or false depending on our input selection
// multi line function logic must be indented.
startTimeOk() =>
// get our input time together
inputTime = timestamp(syminfo.timezone, startYear, startMonth, startDay, startHour, startMinute)
// check the current time is greater than the input time and assign true or false
timeOk = time > inputTime ? true : false
// last line is the return value, we want the strategy to execute if..
// ..we are using the limiter, and the time is ok -OR- we are not using the limiter
r = (useTimeLimit and timeOk) or not useTimeLimit
// === SERIES SETUP ===
/// a couple of ma's..
maFast = ema(maFastSource, maFastLength)
maSlow = ema(maSlowSource, maSlowLength)
// === PLOTTING ===
fast = plot(maFast, title = "Fast MA", color = green, linewidth = 2, style = line, transp = 50)
slow = plot(maSlow, title = "Slow MA", color = red, linewidth = 2, style = line, transp = 50)
// === LOGIC ===
// is fast ma above slow ma?
aboveBelow = maFast >= maSlow ? true : false
// are we inverting our trade direction?
tradeDirection = tradeInvert ? aboveBelow ? false : true : aboveBelow ? true : false
// *** FOCUS OF EXAMPLE ***
// wrap our strategy execution in an if statement which calls the time checking function to validate entry
// like the function logic, content to be included in the if statement must be indented.
if( startTimeOk() )
// === STRATEGY - LONG POSITION EXECUTION ===
enterLong = not tradeDirection[1] and tradeDirection
exitLong = tradeDirection[1] and not tradeDirection
strategy.entry( id = "Long", long = true, when = enterLong )
strategy.close( id = "Long", when = exitLong )
// === STRATEGY - SHORT POSITION EXECUTION ===
enterShort = tradeDirection[1] and not tradeDirection
exitShort = not tradeDirection[1] and tradeDirection
strategy.entry( id = "Short", long = false, when = enterShort )
strategy.close( id = "Short", when = exitShort )
// === STRATEGY RISK MANAGEMENT EXECUTION ===
strategy.exit("Exit Long", from_entry = "Long", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)
strategy.exit("Exit Short", from_entry = "Short", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)