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

Script open-source

In pieno spirito TradingView, l'autore di questo script lo ha pubblicato open-source, in modo che i trader possano comprenderlo e verificarlo. Un saluto all'autore! È possibile utilizzarlo gratuitamente, ma il riutilizzo di questo codice in una pubblicazione è regolato dal nostro Regolamento. Per aggiungerlo al grafico, mettilo tra i preferiti.

Vuoi usare questo script sui tuoi grafici?

Declinazione di responsabilità