
Chiến lược theo dõi xu hướng dựa trên Z-value sử dụng chỉ số thống kê Z-value để nắm bắt cơ hội xu hướng bằng cách đo mức độ giá lệch khỏi đường trung bình di chuyển của nó và sử dụng chênh lệch tiêu chuẩn như một thước đo thống nhất. Chiến lược này nổi tiếng với tính đơn giản và hiệu quả, đặc biệt phù hợp với thị trường nơi biến động giá thường quay trở lại mức trung bình.
Cốt lõi của chiến lược này là tính toán giá trị Z. Giá trị Z được tính bằng cách tính chênh lệch giữa giá hiện tại và chỉ số giá động trung bình (EMA) của chiều dài định nghĩa của người dùng, sau đó chia chênh lệch theo tiêu chuẩn giá cùng chiều dài:
z = (x - μ) / σ
Trong đó, x là giá hiện tại, μ là EMA trung bình, σ là chênh lệch tiêu chuẩn.
Tín hiệu giao dịch được tạo ra dựa trên giá trị Z vượt qua ngưỡng dự kiến:
Những rủi ro trên có thể được kiểm soát và giảm bớt thông qua phân tích thị trường liên tục, tối ưu hóa tham số và thực hiện thận trọng trên cơ sở đánh giá lại.
“Chiến lược theo dõi xu hướng dựa trên giá trị Z” với các đặc điểm đơn giản, mạnh mẽ và linh hoạt của nó, cung cấp một tầm nhìn độc đáo để nắm bắt cơ hội theo xu hướng. Bằng cách đặt các tham số hợp lý, quản lý rủi ro thận trọng và tối ưu hóa liên tục, chiến lược này có thể trở thành một trợ lý tuyệt vời cho các nhà giao dịch định lượng, tiến bộ vững chắc trong thị trường biến động.
/*backtest
start: 2023-04-23 00:00:00
end: 2024-04-28 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PresentTrading
// This strategy employs a statistical approach by using a Z-score, which measures the deviation of the price from its moving average normalized by the standard deviation.
// Very simple and effective approach
//@version=5
strategy('Price Based Z-Trend - strategy [presentTrading]',shorttitle = 'Price Based Z-Trend - strategy [presentTrading]', overlay=false, precision=3,
commission_value=0.1, commission_type=strategy.commission.percent, slippage=1,
currency=currency.USD, default_qty_type=strategy.percent_of_equity, default_qty_value=10, initial_capital=10000)
// User-definable parameters for the Z-score calculation and bar coloring
tradeDirection = input.string("Both", "Trading Direction", options=["Long", "Short", "Both"]) // User selects trading direction
priceDeviationLength = input.int(100, "Standard Deviation Length", step=1) // Length for standard deviation calculation
priceAverageLength = input.int(100, "Average Length", step=1) // Length for moving average calculation
Threshold = input.float(1, "Threshold", step=0.1) // Number of standard deviations for Z-score threshold
priceBar = input(title='Bar Color', defval=true) // Toggle for coloring price bars based on Z-score
// Z-score calculation based on user input for the price source (typically the closing price)
priceSource = input(close, title="Source")
priceZScore = (priceSource - ta.ema(priceSource, priceAverageLength)) / ta.stdev(priceSource, priceDeviationLength) // Z-score calculation
// Conditions for entering and exiting trades based on Z-score crossovers
priceLongCondition = ta.crossover(priceZScore, Threshold) // Condition to enter long positions
priceExitLongCondition = ta.crossunder(priceZScore, -Threshold) // Condition to exit long positions
longEntryCondition = ta.crossover(priceZScore, Threshold)
longExitCondition = ta.crossunder(priceZScore, -Threshold)
shortEntryCondition = ta.crossunder(priceZScore, -Threshold)
shortExitCondition = ta.crossover(priceZScore, Threshold)
// Strategy conditions and execution based on Z-score crossovers and trading direction
if (tradeDirection == "Long" or tradeDirection == "Both") and longEntryCondition
strategy.entry("Long", strategy.long) // Enter a long position
if (tradeDirection == "Long" or tradeDirection == "Both") and longExitCondition
strategy.close("Long") // Close the long position
if (tradeDirection == "Short" or tradeDirection == "Both") and shortEntryCondition
strategy.entry("Short", strategy.short) // Enter a short position
if (tradeDirection == "Short" or tradeDirection == "Both") and shortExitCondition
strategy.close("Short") // Close the short position
// Dynamic Thresholds Visualization using 'plot'
plot(Threshold, "Dynamic Entry Threshold", color=color.new(color.green, 50))
plot(-Threshold, "Dynamic Short Entry Threshold", color=color.new(color.red, 50))
// Color-coding Z-Score
priceZScoreColor = priceZScore > Threshold ? color.green :
priceZScore < -Threshold ? color.red : color.blue
plot(priceZScore, "Z-Score", color=priceZScoreColor)
// Lines
hline(0, color=color.rgb(255, 255, 255, 50), linestyle=hline.style_dotted)
// Bar Color
priceBarColor = priceZScore > Threshold ? color.green :
priceZScore > 0 ? color.lime :
priceZScore < Threshold ? color.maroon :
priceZScore < 0 ? color.red : color.black
barcolor(priceBar ? priceBarColor : na)