OPEN-SOURCE SCRIPT

Buy/Sell TP&SL [doibol]

//version=5
indicator("Buy/Sell Pro", overlay=true)

// Lấy giá của cặp coin hiện hành
coinSymbol = syminfo.tickerid
closePrice = close

// Tính toán MA10 và EMA99
ma10 = ta.sma(closePrice, 10)
ema99 = ta.ema(closePrice, 99)

// Điều kiện Buy: MA10 cắt lên EMA99 và giá hiện tại lớn hơn giá đóng cửa trước đó
buySignal = ta.crossover(ma10, ema99) and closePrice > close[1]

// Điều kiện Sell: MA10 cắt xuống EMA99 và giá hiện tại nhỏ hơn giá đóng cửa trước đó
sellSignal = ta.crossunder(ma10, ema99) and closePrice < close[1]

// Các giá trị chi tiết cho thông báo
takeProfitPercent = input.float(1.05, title="Take Profit (%)") // 5% tăng giá
stopLossPercent = input.float(0.95, title="Stop Loss (%)") // 5% giảm giá

// Khai báo các biến toàn cục cho giá TP và SL
var float entryPrice = na
var float takeProfitPrice = na
var float stopLossPrice = na

// Kiểm tra điều kiện để không nhận tín hiệu liên tục
var int lastSignalBarIndex = na

// Lưu giá vào lệnh khi có tín hiệu Buy hoặc Sell
if (buySignal and na(entryPrice) and (na(lastSignalBarIndex) or (bar_index - lastSignalBarIndex >= 1)))
entryPrice := closePrice
takeProfitPrice := entryPrice * takeProfitPercent
stopLossPrice := entryPrice * stopLossPercent
lastSignalBarIndex := bar_index

alert("Buy Signal: Tên coin hiện hành: " + coinSymbol +
"\nEntry: $" + str.tostring(entryPrice) +
"\nTP: $" + str.tostring(takeProfitPrice) +
"\nSL: $" + str.tostring(stopLossPrice), alert.freq_once_per_bar_close)

if (sellSignal and na(entryPrice) and (na(lastSignalBarIndex) or (bar_index - lastSignalBarIndex >= 1)))
entryPrice := closePrice
takeProfitPrice := entryPrice * stopLossPercent // Sell: TP là mức giảm
stopLossPrice := entryPrice * takeProfitPercent // SL là mức tăng
lastSignalBarIndex := bar_index

alert("Sell Signal: Tên coin hiện hành: " + coinSymbol +
"\nEntry: $" + str.tostring(entryPrice) +
"\nTP: $" + str.tostring(takeProfitPrice) +
"\nSL: $" + str.tostring(stopLossPrice), alert.freq_once_per_bar_close)

// Kiểm tra nếu đạt Take Profit (TP) hoặc Stop Loss (SL)
if (not na(entryPrice))
if closePrice >= takeProfitPrice
alert("TP: " + str.tostring(takeProfitPrice) + " đã đạt cho " + coinSymbol, alert.freq_once_per_bar_close)
entryPrice := na // Reset giá vào lệnh

if closePrice <= stopLossPrice
alert("SL: " + str.tostring(stopLossPrice) + " đã chạm cho " + coinSymbol, alert.freq_once_per_bar_close)
entryPrice := na // Reset giá vào lệnh

// Hiển thị tín hiệu trên biểu đồ
plot(ma10, title="MA10", color=color.blue, linewidth=2)
plot(ema99, title="EMA99", color=color.orange, linewidth=2)

plotshape(series=buySignal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=sellSignal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")
Chart patternsCycles

Açık kaynak kodlu komut dosyası

Gerçek TradingView ruhuna uygun olarak, bu komut dosyasının yazarı komut dosyasını açık kaynak olarak yayınlamıştır, böylece yatırımcılar betiği anlayabilir ve doğrulayabilir. Yazar çok yaşa! Ücretsiz olarak kullanabilirsiniz, ancak bu kodun yayında yeniden kullanımı Ev kurallarına tabidir. Bir grafikte kullanmak için favorilere ekleyebilirsiniz.

Bu komut dosyasını bir grafikte kullanmak ister misiniz?

Feragatname