Universal Propfirm Lot Size Calculator

V-i-c-k-y · study · 195 行 · 点赞 52 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Vicky
//@version=6
indicator('Universal Propfirm Lot Size Calculator', shorttitle = 'Fx Calc', overlay = true)
 
// TP Mode selector
mode = input.string('Custom', 'TP Mode', options = ['Fixed', 'Custom'], group = 'TP Mode')
 
// Account 1 Details
var float accountSize1 = input.float(10000, 'Account 1 Size', group = 'Account 1 Risk Management')
var float riskPercent1 = input.float(0.5, 'Account 1 Risk %', minval = 0.1, maxval = 100, group = 'Account 1 Risk Management')
var float riskAmountUsd1 = accountSize1 * (riskPercent1 / 100)
 
// Account 2 Details
var float accountSize2 = input.float(25000, 'Account 2 Size', group = 'Account 2 Risk Management')
var float riskPercent2 = input.float(0.5, 'Account 2 Risk %', minval = 0.1, maxval = 100, group = 'Account 2 Risk Management')
var float riskAmountUsd2 = accountSize2 * (riskPercent2 / 100)
 
// --- Lot Display ---
splitLotsInput = input.bool(false, 'Split Lot Size (50/50)', tooltip = 'Check this box if you plan to open two separate trades (e.g., to close half at TP1 and let the rest run). It divides the total calculated lot size into two equal halves on the display table.', group = 'Lot Display')
 
// --- Universal Contract Sizes ---
cs_forex = input.float(100000, 'Forex (All Currency Pairs)', group = 'Universal Contract Sizes')
cs_gold = input.float(100, 'Gold (XAUUSD / GOLD)', group = 'Universal Contract Sizes')
cs_silver = input.float(5000, 'Silver (XAGUSD / SILVER)', group = 'Universal Contract Sizes')
cs_btc = input.float(1, 'Bitcoin (BTC)', group = 'Universal Contract Sizes')
cs_eth = input.float(1, 'Ethereum (ETH)', group = 'Universal Contract Sizes')
cs_nas100 = input.float(20, 'Nasdaq (NAS100 / US100 / USTech)', group = 'Universal Contract Sizes')
cs_spx500 = input.float(50, 'S&P 500 (SPX500 / US500)', group = 'Universal Contract Sizes')
cs_us30 = input.float(10, 'Dow Jones (US30 / DJ30)', group = 'Universal Contract Sizes')
cs_ger40 = input.float(25, 'DAX (GER40 / DE40)', group = 'Universal Contract Sizes')
cs_oil = input.float(1000, 'Oil (USOIL / WTI / CRUDE)', group = 'Universal Contract Sizes')
cs_default = input.float(1, 'Other / Default Size', group = 'Universal Contract Sizes')
 
// Position Details
var entryPrice = input.price(0, 'Entry', group = 'Position', confirm = true, display = display.none)
var slPrice = input.price(0, 'SL', group = 'Position', confirm = true, display = display.none)
rr = input.float(5, 'Risk Reward (Fixed)', minval = 0.1, group = 'TP Mode')
takeProfitPrice = input.price(0, 'Take Profit (Custom)', group = 'TP Mode', confirm = true)
 
// Table Customization Settings
tablePosInput = input.string('Top Right', 'Table Position', options = ['Top Right', 'Top Center', 'Top Left', 'Middle Right', 'Middle Center', 'Middle Left', 'Bottom Right', 'Bottom Center', 'Bottom Left'], group = 'Table Settings')
tableSizeInput = input.string('Small', 'Table Size', options = ['Tiny', 'Small', 'Normal', 'Large'], group = 'Table Settings')
 
// Map Table Position
tblPos = tablePosInput == 'Top Left' ? position.top_left : tablePosInput == 'Top Center' ? position.top_center : tablePosInput == 'Top Right' ? position.top_right : tablePosInput == 'Middle Left' ? position.middle_left : tablePosInput == 'Middle Center' ? position.middle_center : tablePosInput == 'Middle Right' ? position.middle_right : tablePosInput == 'Bottom Left' ? position.bottom_left : tablePosInput == 'Bottom Center' ? position.bottom_center : position.bottom_right
 
// Map Table Text Sizes
txtSizeBody = tableSizeInput == 'Tiny' ? size.tiny : tableSizeInput == 'Small' ? size.small : tableSizeInput == 'Normal' ? size.normal : size.large
 
txtSizeHighlight = tableSizeInput == 'Tiny' ? size.small : tableSizeInput == 'Small' ? size.normal : tableSizeInput == 'Normal' ? size.large : size.huge
 
// TP Calculation based on mode
isLong = entryPrice > slPrice
tpPrice = mode == 'Fixed' ? entryPrice + (entryPrice - slPrice) * rr : isLong and takeProfitPrice > entryPrice or not isLong and takeProfitPrice < entryPrice ? takeProfitPrice : na
 
if mode == 'Custom' and na(tpPrice) and entryPrice != 0
    label.new(bar_index, entryPrice, '⚠️ Invalid TP for trade direction', style = label.style_label_left, color = color.red, textcolor = color.white)
 
// ---------------------------------------------------------
// SMART BROKER MAPPING LOGIC (Supports .p, .m suffixes etc)
// ---------------------------------------------------------
sym = str.upper(syminfo.ticker) // Convert to uppercase for accurate matching
 
getContractSize() =>
    float size = cs_default
 
    // Check for Metals & Crypto First (Because some brokers classify Gold as Forex)
    if str.contains(sym, 'XAU') or str.contains(sym, 'GOLD')
        size := cs_gold
        size
    else if str.contains(sym, 'XAG') or str.contains(sym, 'SILVER')
        size := cs_silver
        size
    else if str.contains(sym, 'BTC') or str.contains(sym, 'BITCOIN')
        size := cs_btc
        size
    else if str.contains(sym, 'ETH') or str.contains(sym, 'ETHEREUM')
        size := cs_eth
        size
 
    // Check for Indices
    else if str.contains(sym, 'NAS') or str.contains(sym, 'US100') or str.contains(sym, 'TECH') or str.contains(sym, 'NDX')
        size := cs_nas100
        size
    else if str.contains(sym, 'SPX') or str.contains(sym, 'US500') or str.contains(sym, 'SP500')
        size := cs_spx500
        size
    else if str.contains(sym, 'US30') or str.contains(sym, 'DJ') or str.contains(sym, 'DOW')
        size := cs_us30
        size
    else if str.contains(sym, 'GER') or str.contains(sym, 'DE40') or str.contains(sym, 'DAX')
        size := cs_ger40
        size
 
    // Check for Commodities
    else if str.contains(sym, 'USOIL') or str.contains(sym, 'WTI') or str.contains(sym, 'CRUDE')
        size := cs_oil
        size
 
    // Fallback to General Forex
    else if syminfo.type == 'forex'
        size := cs_forex
        size
 
    size
 
// Assigning the unit size dynamically based on the current chart
unitsPerLot = getContractSize()
 
// USD Conversion
usdConversion = request.security(syminfo.currency == 'USD' ? syminfo.tickerid : 'USD' + syminfo.currency, timeframe.period, close, ignore_invalid_symbol = true)
usdConversion := na(usdConversion) ? 1 : usdConversion
 
// Safe calculation to prevent Divide by Zero error
calculateContracts(float entry, float sl, float riskAmount, float upl) =>
    diff = math.abs(entry - sl)
    diff == 0 ? 0 : riskAmount / diff * (syminfo.currency == 'USD' ? 1 : usdConversion) / upl
 
// Plotting (Only plots if Entry and SL are set)
validTrade = entryPrice != 0 and slPrice != 0
 
plot(validTrade ? entryPrice : na, 'Entry', color.rgb(120, 123, 134), show_last = 1, display = display.price_scale)
plot(validTrade ? slPrice : na, 'SL', color.rgb(242, 76, 54, 30), show_last = 1, display = display.price_scale)
plot(validTrade ? tpPrice : na, 'TP', color.rgb(8, 153, 129, 79), show_last = 1, display = display.price_scale)
 
if barstate.islastconfirmedhistory and validTrade
    tradeLength = 20
 
    line.new(bar_index + 1, entryPrice, bar_index + tradeLength, entryPrice, color = color.rgb(120, 123, 134))
    label.new(bar_index + int(tradeLength / 2), slPrice, str.tostring(math.abs(entryPrice - slPrice) / syminfo.mintick, format = '# pips') + ' | -$' + str.tostring(riskAmountUsd1, '#.##') + ' | -$' + str.tostring(riskAmountUsd2, '#.##'), color = color.rgb(242, 76, 54, 30), textcolor = #020202, style = label.style_label_center, size = size.small)
 
    // TP label
    label.new(bar_index + int(tradeLength / 2), tpPrice, mode == 'Custom' ? 'TP | +$' + str.tostring(riskAmountUsd1 * math.abs(tpPrice - entryPrice) / math.abs(entryPrice - slPrice), '#.##') + ' | +$' + str.tostring(riskAmountUsd2 * math.abs(tpPrice - entryPrice) / math.abs(entryPrice - slPrice), '#.##') + ' | R:R ' + str.tostring(math.abs(tpPrice - entryPrice) / math.abs(entryPrice - slPrice), '#.##') : 'TP | +$' + str.tostring(riskAmountUsd1 * rr, '#.##') + ' | +$' + str.tostring(riskAmountUsd2 * rr, '#.##') + ' | R:R ' + str.tostring(rr, '#.##'), color = color.rgb(8, 153, 129, 21), textcolor = #0a0a0a, style = label.style_label_center, size = size.small)
 
    // Boxes
    box.new(bar_index + 1, entryPrice, bar_index + tradeLength, slPrice, bgcolor = #ee2a232b, border_color = na)
    box.new(bar_index + 1, entryPrice, bar_index + tradeLength, tpPrice, bgcolor = #10910b28, border_color = na)
 
    // Optional R lines for fixed mode
    if mode == 'Fixed'
        for i = 2 to int(rr) - 1 by 1
            levelPrice = entryPrice + (entryPrice - slPrice) * i
            line.new(bar_index + 1, levelPrice, bar_index + tradeLength, levelPrice, color = color.gray)
            label.new(bar_index + tradeLength - 1, levelPrice, text = str.tostring(i) + 'R', color = #787b8600, textcolor = #3b3b3b, style = label.style_label_left, size = size.small)
 
// Modern Aesthetic Info Table
if validTrade
    color c_header_bg = color.rgb(19, 23, 34)
    color c_row_bg1 = color.rgb(30, 34, 45)
    color c_row_bg2 = color.rgb(36, 42, 54)
    color c_accent_bg = color.rgb(41, 98, 255, 30)
    color c_border = color.rgb(54, 60, 78)
    color c_text_main = color.rgb(209, 212, 220)
    color c_text_dim = color.rgb(120, 123, 134)
    color c_highlight = color.rgb(0, 230, 118)
 
    var table infoTable = table.new(tblPos, 3, 6, bgcolor = color.rgb(19, 23, 34), border_width = 1, border_color = c_border, frame_width = 1, frame_color = c_border)
 
    // Headers
    table.cell(infoTable, 0, 0, 'Parameter', text_color = c_text_dim, text_size = txtSizeBody, text_halign = text.align_left, bgcolor = c_header_bg)
    table.cell(infoTable, 1, 0, 'Account 1', text_color = color.white, text_size = txtSizeBody, text_halign = text.align_right, bgcolor = c_header_bg)
    table.cell(infoTable, 2, 0, 'Account 2', text_color = color.white, text_size = txtSizeBody, text_halign = text.align_right, bgcolor = c_header_bg)
 
    // Row 1: Account Size
    table.cell(infoTable, 0, 1, 'Account Size', text_color = c_text_dim, text_size = txtSizeBody, text_halign = text.align_left, bgcolor = c_row_bg1)
    table.cell(infoTable, 1, 1, '$' + str.tostring(accountSize1, '#,###'), text_color = c_text_main, text_size = txtSizeBody, text_halign = text.align_right, bgcolor = c_row_bg1)
    table.cell(infoTable, 2, 1, '$' + str.tostring(accountSize2, '#,###'), text_color = c_text_main, text_size = txtSizeBody, text_halign = text.align_right, bgcolor = c_row_bg1)
 
    // Row 2: Risk %
    table.cell(infoTable, 0, 2, 'Risk %', text_color = c_text_dim, text_size = txtSizeBody, text_halign = text.align_left, bgcolor = c_row_bg2)
    table.cell(infoTable, 1, 2, str.tostring(riskPercent1, '#.##') + '%', text_color = c_text_main, text_size = txtSizeBody, text_halign = text.align_right, bgcolor = c_row_bg2)
    table.cell(infoTable, 2, 2, str.tostring(riskPercent2, '#.##') + '%', text_color = c_text_main, text_size = txtSizeBody, text_halign = text.align_right, bgcolor = c_row_bg2)
 
    // Row 3: Contract Size
    table.cell(infoTable, 0, 3, 'Contract Size', text_color = c_text_dim, text_size = txtSizeBody, text_halign = text.align_left, bgcolor = c_row_bg1)
    table.cell(infoTable, 1, 3, str.tostring(unitsPerLot), text_color = c_text_main, text_size = txtSizeBody, text_halign = text.align_right, bgcolor = c_row_bg1)
    table.cell(infoTable, 2, 3, str.tostring(unitsPerLot), text_color = c_text_main, text_size = txtSizeBody, text_halign = text.align_right, bgcolor = c_row_bg1)
 
    // Row 4: Cash at Risk
    table.cell(infoTable, 0, 4, 'Cash at Risk', text_color = c_text_dim, text_size = txtSizeBody, text_halign = text.align_left, bgcolor = c_row_bg2)
    table.cell(infoTable, 1, 4, '$' + str.tostring(riskAmountUsd1, '#.##'), text_color = color.rgb(255, 82, 82), text_size = txtSizeBody, text_halign = text.align_right, bgcolor = c_row_bg2)
    table.cell(infoTable, 2, 4, '$' + str.tostring(riskAmountUsd2, '#.##'), text_color = color.rgb(255, 82, 82), text_size = txtSizeBody, text_halign = text.align_right, bgcolor = c_row_bg2)
 
    // Row 5: Lot Size (Highlighted Result with Min 0.01 and Split Logic)
    lots1 = math.max(calculateContracts(entryPrice, slPrice, riskAmountUsd1, unitsPerLot), 0.01)
    lots2 = math.max(calculateContracts(entryPrice, slPrice, riskAmountUsd2, unitsPerLot), 0.01)
 
    // Formatting the string: splits only if box is checked AND lot size is > 0.01
    string lots1Str = splitLotsInput and lots1 > 0.01 ? str.tostring(lots1 / 2, '#.##') + '\n' + str.tostring(lots1 / 2, '#.##') : str.tostring(lots1, '#.##')
    string lots2Str = splitLotsInput and lots2 > 0.01 ? str.tostring(lots2 / 2, '#.##') + '\n' + str.tostring(lots2 / 2, '#.##') : str.tostring(lots2, '#.##')
 
    table.cell(infoTable, 0, 5, 'Lot Size', text_color = color.white, text_size = txtSizeHighlight, text_halign = text.align_left, bgcolor = c_accent_bg)
    table.cell(infoTable, 1, 5, lots1Str, text_color = c_highlight, text_size = txtSizeHighlight, text_halign = text.align_right, bgcolor = c_accent_bg)
    table.cell(infoTable, 2, 5, lots2Str, text_color = c_highlight, text_size = txtSizeHighlight, text_halign = text.align_right, bgcolor = c_accent_bg)

← 返回列表