本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
indicator("Gold Lot Size Calculator (MT5)", shorttitle="Gold Lot Calc", overlay=true, max_boxes_count=10, max_labels_count=10, max_lines_count=10)
// ============================================================
// Position size calculator for XAUUSD / Gold (MetaTrader lots)
// Click a candle, then your entry and your stop loss on the chart.
// The zones are anchored to the candle you clicked.
// lot = risk in $ / (stop distance in $ x ounces per lot)
// ============================================================
// --- Inputs ---
account = input.float(10000, "Account size ($)", minval=1, group="Risk", tooltip="Your account balance in USD.")
riskPct = input.float(2.0, "Max risk per trade (%)", minval=0.1, step=0.1, group="Risk", tooltip="Percentage of the account you accept to lose if the stop loss is hit.")
rr = input.float(2.0, "Risk/Reward ratio", minval=0.1, step=0.5, group="Risk", tooltip="Used to place the take profit. 2 means the target is twice the stop distance.")
contract = input.float(100, "Ounces per 1.00 lot", minval=1, group="Instrument", tooltip="Contract size. Gold is 100 oz per lot at most brokers. Check the symbol specification in MetaTrader.")
widthBars = input.int(30, "Zone width (bars)", minval=5, group="Display")
startTime = input.time(timestamp("01 Oct 2026 00:00"), "Start candle (click on chart)", confirm=true, group="Trade", tooltip="Click the candle where the trade starts.")
entry = input.price(4150.0, "Entry price (click on chart)", confirm=true, group="Trade", tooltip="Click on the chart or type the price.")
sl = input.price(4140.0, "Stop loss price (click on chart)", confirm=true, group="Trade", tooltip="Stop below entry = long. Stop above entry = short.")
// --- Calculations ---
isLong = sl < entry
slDist = math.max(math.abs(entry - sl), 0.01)
tp = isLong ? entry + slDist * rr : entry - slDist * rr
riskUsd = account * riskPct / 100
lot = math.floor(riskUsd / (slDist * contract) * 100) / 100 // rounded down, never above the risk limit
realRisk = lot * slDist * contract
gainUsd = lot * slDist * rr * contract
// --- Find the bar index of the clicked candle ---
var int startIdx = na
if na(startIdx) and time >= startTime
startIdx := bar_index
// --- Zones: green = profit, red = stop loss ---
var box tpBox = na
var box slBox = na
var line enLine = na
var label tpLbl = na
var label slLbl = na
var label enLbl = na
if barstate.islast
box.delete(tpBox)
box.delete(slBox)
line.delete(enLine)
label.delete(tpLbl)
label.delete(slLbl)
label.delete(enLbl)
left = na(startIdx) ? bar_index - 5 : startIdx
right = left + widthBars
tpBox := box.new(left, math.max(entry, tp), right, math.min(entry, tp), border_color=color.new(color.green, 30), bgcolor=color.new(color.green, 75))
slBox := box.new(left, math.max(entry, sl), right, math.min(entry, sl), border_color=color.new(color.red, 30), bgcolor=color.new(color.red, 75))
enLine := line.new(left, entry, right, entry, color=color.gray, width=2)
tpLbl := label.new(right, tp, "TP " + str.tostring(tp, "0.00") + " (+" + str.tostring(gainUsd, "0") + " $)", style=label.style_label_left, color=color.green, textcolor=color.white, size=size.small)
slLbl := label.new(right, sl, "SL " + str.tostring(sl, "0.00") + " (-" + str.tostring(realRisk, "0") + " $)", style=label.style_label_left, color=color.red, textcolor=color.white, size=size.small)
enLbl := label.new(right, entry, (isLong ? "BUY " : "SELL ") + str.tostring(entry, "0.00") + " | Lot " + str.tostring(lot, "0.00"), style=label.style_label_left, color=color.gray, textcolor=color.white, size=size.small)
// --- Result table ---
var table t = table.new(position.bottom_right, 2, 5, bgcolor=color.new(color.black, 15), border_width=1, border_color=color.gray)
if barstate.islast
table.cell(t, 0, 0, isLong ? "BUY" : "SELL", text_color=isLong ? color.lime : color.red, text_size=size.large)
table.cell(t, 1, 0, "Lot: " + str.tostring(lot, "0.00"), text_color=color.white, text_size=size.large)
table.cell(t, 0, 1, "Stop distance", text_color=color.white)
table.cell(t, 1, 1, str.tostring(slDist, "0.00") + " $", text_color=color.white)
table.cell(t, 0, 2, "Actual risk", text_color=color.white)
table.cell(t, 1, 2, str.tostring(realRisk, "0.00") + " $", text_color=color.orange)
table.cell(t, 0, 3, "Target gain", text_color=color.white)
table.cell(t, 1, 3, str.tostring(gainUsd, "0.00") + " $", text_color=color.lime)
table.cell(t, 0, 4, "Risk/Reward", text_color=color.white)
table.cell(t, 1, 4, "1 : " + str.tostring(rr, "0.0"), text_color=color.white)