
Chiến lược này là một hệ thống giao dịch kết hợp tỷ lệ phần trăm ZigZag và chỉ số ngẫu nhiên {Stochastic}. Chiến lược này sử dụng chỉ số ZigZag được điều chỉnh động để xác định các điểm biến động quan trọng trong xu hướng thị trường và kết hợp với tín hiệu mua quá mức của chỉ số ngẫu nhiên để tối ưu hóa thời gian vào thị trường. Hệ thống cũng tích hợp các cơ chế dừng lỗ và dừng để thực hiện quản lý rủi ro.
Logic cốt lõi của chiến lược này dựa trên các thành phần chính sau:
Chiến lược này kết hợp ZigZag và chỉ số ngẫu nhiên để xây dựng một hệ thống giao dịch hoàn chỉnh hơn. Các đặc điểm chính của chiến lược là tín hiệu rõ ràng, rủi ro có thể kiểm soát được, nhưng vẫn cần tối ưu hóa các tham số dựa trên tình hình thị trường thực tế.
/*backtest
start: 2025-02-18 14:00:00
end: 2025-02-20 00:00:00
period: 5m
basePeriod: 5m
exchanges: [{"eid":"Binance","currency":"SOL_USDT"}]
*/
//@version=5
strategy("[RS]ZigZag Percent Reversal with Stochastic Strategy", overlay=true)
// ||---}---------------------------------------------------------------------||
// |--------------------------------------------------------------------------||
// | ZigZag: ||
// |--------------------------------------------------------------------------||
// |{
string percent_method = input.string(
defval="MANUAL",
title="Method to use for the zigzag reversal range:",
options=[
"MANUAL",
"ATR005 * X", "ATR010 * X", "ATR020 * X", "ATR050 * X", "ATR100 * X", "ATR250 * X"
]
)
var float percent = input.float(
defval=0.25,
title="Percent of last pivot price for zigzag reversal:",
minval=0.0, maxval=99.0
) / 100
float percent_multiplier = input.float(
defval=1.0,
title="Multiplier to apply to ATR if applicable:"
)
if percent_method == "ATR005 * X"
percent := ta.atr(5) / open * percent_multiplier
if percent_method == "ATR010 * X"
percent := ta.atr(10) / open * percent_multiplier
if percent_method == "ATR020 * X"
percent := ta.atr(20) / open * percent_multiplier
if percent_method == "ATR050 * X"
percent := ta.atr(50) / open * percent_multiplier
if percent_method == "ATR100 * X"
percent := ta.atr(100) / open * percent_multiplier
if percent_method == "ATR250 * X"
percent := ta.atr(250) / open * percent_multiplier
// ||-------------------------------------------------------------------------||
// || zigzag function:
// ||-------------------------------------------------------------------------||
// |{
f_zz(_percent)=>
// direction after last pivot
var bool _is_direction_up = na
// track highest price since last lower pivot
var float _htrack = na
// track lowest price since last higher pivot
var float _ltrack = na
// zigzag variable for plotting
var float _pivot = na
// range needed for reaching reversal threshold
float _reverse_range = 0.0
// real pivot time
var int _real_pivot_time = na
var int _htime = na
var int _ltime = na
// reverse line
var float _reverse_line = na
if bar_index >= 1
if na(_is_direction_up)
_is_direction_up := true
_reverse_range := nz(_pivot[1]) * _percent
if _is_direction_up
_ltrack := na
_ltime := time
if na(_htrack)
if high > high[1]
_htrack := high
_htime := time
else
_htrack := high[1]
_htime := time[1]
else
if high > _htrack
_htrack := high
_htime := time
// Reversal line calculation based on the current candle's closing price
_reverse_line := _htrack - _reverse_range
// If close <= reversal line, mark pivot and reverse direction
if close <= _reverse_line
_pivot := _htrack
_real_pivot_time := _htime
_is_direction_up := false
if not _is_direction_up
_htrack := na
_htime := na
if na(_ltrack)
if low < low[1]
_ltrack := low
_ltime := time
else
_ltrack := low[1]
_ltime := time[1]
else
if low < _ltrack
_ltrack := low
_ltime := time
// Reversal line calculation based on the current candle's closing price
_reverse_line := _ltrack + _reverse_range
// If close >= reversal line, mark pivot and reverse direction
if close >= _reverse_line
_pivot := _ltrack
_real_pivot_time := _ltime
_is_direction_up := true
[_pivot, _is_direction_up, _reverse_line, _real_pivot_time]
[pivot, direction_up, reverse_line, pivot_time] = f_zz(percent)
// Çizim
// Sabit Reversal Line (fiyat seviyesinde sabit)
var float static_reverse_line = na
if (not na(reverse_line))
static_reverse_line := reverse_line
plot(series=static_reverse_line, color=color.gray, style=plot.style_line, title="Reversal Line", trackprice=false)
// ||-------------------------------------------------------------------------||
// || Stochastic: ||
// ||-------------------------------------------------------------------------||
// |{
K_length = 9
K_smoothing = 3
// Stochastic %K hesaplama
stochK = ta.sma(ta.stoch(close, high, low, K_length), K_smoothing)
// ||-------------------------------------------------------------------------||
// || Custom Buy/Sell Signals:
// ||-------------------------------------------------------------------------||
// |{
// Buy sinyali: Fiyat reversal line'ının üstünde ve stochastic K değeri 20'nin altında ise
buy_signal = close > static_reverse_line and stochK < 20
// Sell sinyali: Fiyat reversal line'ının altındaysa ve stochastic K değeri 80'in üstünde ise
sell_signal = close < static_reverse_line and stochK > 80
// Alım ve satım sinyali için strateji girişlerini ekle
long_condition = buy_signal
short_condition = sell_signal
// **Burada Stop Loss ve Take Profit değerleri ayarlandı**
stop_loss_pips = 100 // Stop Loss 10 pip
take_profit_pips = 300 // Take Profit 30 pip
// Stop Loss ve Take Profit seviyeleri hesaplanıyor
long_stop_loss = close - stop_loss_pips * syminfo.mintick
long_take_profit = close + take_profit_pips * syminfo.mintick
short_stop_loss = close + stop_loss_pips * syminfo.mintick
short_take_profit = close - take_profit_pips * syminfo.mintick
// Long stratejisi: Alım sinyali ve stop loss/take profit seviyeleri ile
if long_condition
strategy.entry("Buy", strategy.long, stop=long_stop_loss, limit=long_take_profit)
strategy.exit("Take Profit / Stop Loss", from_entry="Buy", stop=long_stop_loss, limit=long_take_profit)
// Webhook ile alım bildirimi gönder
alert("Buy Signal Triggered!", alert.freq_once_per_bar)
// Short stratejisi: Satım sinyali ve stop loss/take profit seviyeleri ile
if short_condition
strategy.entry("Sell", strategy.short, stop=short_stop_loss, limit=short_take_profit)
strategy.exit("Take Profit / Stop Loss", from_entry="Sell", stop=short_stop_loss, limit=short_take_profit)
// Webhook ile satım bildirimi gönder
alert("Sell Signal Triggered!", alert.freq_once_per_bar)
// Alım sinyali gösterimi
plotshape(series=buy_signal, location=location.belowbar, color=color.green, style=shape.labelup, title="Buy Signal", text="BUY", textcolor=color.white)
// Satım sinyali gösterimi
plotshape(series=sell_signal, location=location.abovebar, color=color.red, style=shape.labeldown, title="Sell Signal", text="SELL", textcolor=color.white)