OPEN-SOURCE SCRIPT

Smart RR Lot (Forex) — RR + Lot auto (Final v6 Stable)

106
//version=6
indicator("Smart RR Lot (Forex) — RR + Lot auto (Final v6 Stable)", overlay=true, max_lines_count=12, max_labels_count=12)

// ===== Paramètres du compte =====
acc_currency = input.string("EUR", "Devise du compte", options=["EUR","USD","GBP","JPY","CHF","AUD","CAD","NZD"])
account_balance = input.float(6037.0, "Solde du compte", step=1.0)
risk_pct = input.float(1.0, "Risque par trade (%)", step=0.1, minval=0.01)

// ===== Niveaux à placer sur le graphique =====
entry_price = input.price(1.1000, "Entry (cliquer la pipette)")
sl_price = input.price(1.0990, "Stop Loss (cliquer la pipette)")
tp_price = input.price(1.1010, "Take Profit (cliquer la pipette)")

// ===== Taille du pip (Forex) =====
isJPYpair = str.contains(syminfo.ticker, "JPY")
pip_size = isJPYpair ? 0.01 : 0.0001

// ===== Valeur du pip (1 lot = 100 000 unités) =====
pip_value_quote = 100000.0 * pip_size
quote_ccy = syminfo.currency

// ===== Conversion QUOTE → devise du compte =====
f_rate(sym) =>
request.security(sym, "D", close, ignore_invalid_symbol=true)

f_conv_to_account(quote, acc) =>
acc_equals = quote == acc
if acc_equals
1.0
else
r1 = f_rate(acc + quote)
r2 = f_rate(quote + acc)
float res = na
if not na(r1)
res := 1.0 / r1
else if not na(r2)
res := r2
else
res := 1.0
res

quote_to_account = f_conv_to_account(quote_ccy, acc_currency)
pip_value_account = pip_value_quote * quote_to_account

// ===== Calcul RR & taille de lot =====
stop_dist_points = math.abs(entry_price - sl_price)
tp_dist_points = math.abs(tp_price - entry_price)
distance_pips = stop_dist_points / pip_size
rr = tp_dist_points / stop_dist_points

risk_amount = account_balance * (risk_pct * 0.01)
lot_size = distance_pips > 0 ? (risk_amount / (distance_pips * pip_value_account)) : na
lot_size_clamped = na(lot_size) ? na : math.max(lot_size, 0)

// ====== Lignes horizontales ======
var line lEntry = na
var line lSL = na
var line lTP = na

f_hline(line_id, float y, color colr) =>
var line newLine = na
if na(line_id)
newLine := line.new(bar_index - 1, y, bar_index, y, xloc=xloc.bar_index, extend=extend.right, color=colr, width=2)
else
line.set_xy1(line_id, bar_index - 1, y)
line.set_xy2(line_id, bar_index, y)
line.set_color(line_id, colr)
line.set_extend(line_id, extend.right)
newLine := line_id
newLine

colEntry = color.new(color.gray, 0)
colSL = color.new(color.red, 0)
colTP = color.new(color.teal, 0)

lEntry := f_hline(lEntry, entry_price, colEntry)
lSL := f_hline(lSL, sl_price, colSL)
lTP := f_hline(lTP, tp_price, colTP)

// ===== Labels d’informations =====
var label infoLbl = na
var label lblEntry = na
var label lblSL = na
var label lblTP = na

txtInfo = "RR = " + (na(rr) ? "—" : str.tostring(rr, "#.##")) +
" | Lot = " + (na(lot_size_clamped) ? "—" : str.tostring(lot_size_clamped, "#.##")) +
" (" + acc_currency + ")\n" +
"Risque " + str.tostring(risk_pct, "#.##") + "% = " + str.tostring(risk_amount, "#.##") + " " + acc_currency

midY = (entry_price + tp_price) * 0.5

if na(infoLbl)
infoLbl := label.new(bar_index, midY, txtInfo, xloc=xloc.bar_index, style=label.style_label_right, textcolor=color.white, color=color.new(color.black, 0))
else
label.set_x(infoLbl, bar_index)
label.set_y(infoLbl, midY)
label.set_text(infoLbl, txtInfo)

entryTxt = "ENTRY\n" + str.tostring(entry_price, format.price)
slTxt = "SL\n" + str.tostring(sl_price, format.price)
tpTxt = "TP\n" + str.tostring(tp_price, format.price)

if na(lblEntry)
lblEntry := label.new(bar_index, entry_price, entryTxt, xloc=xloc.bar_index, style=label.style_label_down, textcolor=color.white, color=color.new(colEntry, 0))
else
label.set_x(lblEntry, bar_index)
label.set_y(lblEntry, entry_price)
label.set_text(lblEntry, entryTxt)

if na(lblSL)
lblSL := label.new(bar_index, sl_price, slTxt, xloc=xloc.bar_index, style=label.style_label_down, textcolor=color.white, color=color.new(colSL, 0))
else
label.set_x(lblSL, bar_index)
label.set_y(lblSL, sl_price)
label.set_text(lblSL, slTxt)

if na(lblTP)
lblTP := label.new(bar_index, tp_price, tpTxt, xloc=xloc.bar_index, style=label.style_label_down, textcolor=color.white, color=color.new(colTP, 0))
else
label.set_x(lblTP, bar_index)
label.set_y(lblTP, tp_price)
label.set_text(lblTP, tpTxt)

Отказ от ответственности

Все виды контента, которые вы можете увидеть на TradingView, не являются финансовыми, инвестиционными, торговыми или любыми другими рекомендациями. Мы не предоставляем советы по покупке и продаже активов. Подробнее — в Условиях использования TradingView.