本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Crypto Position Size | USDT", shorttitle = "Crypto Size", overlay = true)
float balance = input.float(10000.0, "Account balance (USDT equivalent)", minval = 0.0, step = 100.0, group = "Account")
string riskChoice = input.string("0.50%", "Risk per trade", options = ["0.25%", "0.50%", "1.00%"], group = "Account")
float riskRate = switch riskChoice
"0.25%" => 0.0025
"1.00%" => 0.01
=> 0.005
float entry = input.float(0.0, "Entry price", minval = 0.0, group = "Trade")
float stop = input.float(0.0, "Stop-loss price", minval = 0.0, group = "Trade")
float target = input.float(0.0, "Target price", minval = 0.0, group = "Trade")
float stepOverride = input.float(0.0, "Order quantity step override", minval = 0.0, group = "Exchange",
tooltip = "Leave at 0 to use the chart symbol's minimum contract size. If your venue specifies a different quantity increment for the order, enter it here in the venue's order units.")
bool perpetual = str.endswith(syminfo.ticker, ".P")
string pair = perpetual ? str.replace(syminfo.ticker, ".P", "") : syminfo.ticker
bool supported = syminfo.currency == "USDT" and (pair == "BTCUSDT" or pair == "ETHUSDT" or pair == "SOLUSDT" or pair == "BNBUSDT")
float pointValue = syminfo.pointvalue
float minQuantity = syminfo.mincontract
float quantityStep = stepOverride > 0.0 ? stepOverride : minQuantity
bool isLong = stop < entry and entry < target
bool isShort = target < entry and entry < stop
bool hasPrices = entry > 0.0 and stop > 0.0 and target > 0.0
bool pricesOnTick = entry == math.round_to_mintick(entry) and stop == math.round_to_mintick(stop) and target == math.round_to_mintick(target)
string issue = switch
not supported => "Open BTCUSDT, ETHUSDT, SOLUSDT or BNBUSDT."
balance <= 0.0 => "Enter a positive account balance."
not hasPrices => "Enter your entry, stop and target in Settings."
not pricesOnTick => "Use prices on this symbol's tick grid."
not isLong and not isShort => "Place stop and target on opposite sides of entry."
na(pointValue) or pointValue <= 0.0 => "Contract multiplier unavailable."
na(quantityStep) or quantityStep <= 0.0 => "Enter the venue's order quantity step."
=> ""
bool valid = issue == ""
float riskBudget = balance * riskRate
// Use whole price ticks to avoid subtraction artifacts at valid price levels.
float stopDistance = math.abs(math.round(entry / syminfo.mintick) - math.round(stop / syminfo.mintick)) * syminfo.mintick
float targetDistance = math.abs(math.round(entry / syminfo.mintick) - math.round(target / syminfo.mintick)) * syminfo.mintick
float riskPerUnit = stopDistance * pointValue
float rawSteps = valid ? riskBudget / (riskPerUnit * quantityStep) : na
// Recover whole steps represented a tiny fraction below the integer.
float quantity = valid ? math.floor(rawSteps + 1e-9) * quantityStep : na
bool canSize = valid and quantity > 0.0 and (na(minQuantity) or quantity >= minQuantity)
float notional = canSize ? quantity * pointValue * entry : na
float stopLoss = canSize ? quantity * riskPerUnit : na
float targetProfit = canSize ? quantity * pointValue * targetDistance : na
string unit = perpetual ? "contracts" : syminfo.basecurrency
string status = switch
not valid => issue
not canSize => "Risk budget is below the minimum order size."
not perpetual and isShort => "Spot short requires a venue that supports borrowing."
not perpetual and notional > balance => "Position value exceeds the account balance."
perpetual and notional > balance => "Check margin and liquidation price."
=> "Sized at the planned stop."
formatQuantity(float value) =>
str.tostring(value, "0.##########")
formatUsdt(float value) =>
str.tostring(value, "0.00##") + " USDT"
color background = #142034
color muted = #BECDE0
color accent = #7DE3C0
color warning = #F9CF78
var table panel = table.new(position.bottom_right, 2, 12, bgcolor = background, frame_color = #385170, frame_width = 1)
showRow(int row, string label, string value, color valueColor) =>
table.cell(panel, 0, row, label, text_color = muted, text_halign = text.align_left, text_size = size.small)
table.cell(panel, 1, row, value, text_color = valueColor, text_halign = text.align_right, text_size = size.small)
if barstate.isfirst
table.cell(panel, 0, 10, "")
table.cell(panel, 0, 11, "")
table.merge_cells(panel, 0, 10, 1, 10)
table.merge_cells(panel, 0, 11, 1, 11)
if barstate.islast
showRow(0, "Crypto Position Size", syminfo.prefix + ":" + syminfo.ticker, accent)
showRow(1, "Trade direction", valid ? (isLong ? "Long" : "Short") : "—", color.white)
showRow(2, "Account balance", formatUsdt(balance), color.white)
showRow(3, "Risk budget · " + riskChoice, formatUsdt(riskBudget), color.white)
showRow(4, "Stop distance", valid ? str.tostring(stopDistance, format.mintick) + " USDT" : "—", color.white)
showRow(5, "Order quantity", canSize ? formatQuantity(quantity) + " " + unit : "—", accent)
showRow(6, "Position value at entry", canSize ? formatUsdt(notional) : "—", color.white)
showRow(7, "Estimated loss at stop", canSize ? formatUsdt(stopLoss) : "—", color.white)
showRow(8, "Estimated profit at target", canSize ? formatUsdt(targetProfit) : "—", color.white)
showRow(9, "Reward / risk", valid ? str.tostring(targetDistance / stopDistance, "0.00") + " : 1" : "—", color.white)
bool needsAttention = not canSize or (not perpetual and isShort) or notional > balance
table.cell(panel, 0, 10, status, text_color = needsAttention ? warning : accent, text_size = size.small)
table.cell(panel, 0, 11, "Estimates exclude fees, slippage and funding.", text_color = muted, text_size = size.small)
plot(valid ? entry : na, "Entry", color = #5AA9FF, linewidth = 2)
plot(valid ? stop : na, "Stop loss", color = #FF6577, linewidth = 2)
plot(valid ? target : na, "Target", color = #4CD6A6, linewidth = 2)