PINE LIBRARY
Cập nhật

Vantage_LO1_Sizing

266
**Overview**
Position-sizing library for the LO1 breakout box day-trading strategy. Provides a unified recoup (opposite-add) sizing pipeline and dollar-risk/profit helpers. Extracting these functions into a library avoids Pine Script's function inlining, reducing compiled token count in the main strategy.

**Exported Type: RecoupSizingResult**
Holds the output of the 8-step sizing pipeline:
• Micro-level quantities (raw, DLL-capped, final)
• YM-level quantities (pre/post min-1-YM policy, proxy-capped)
• Risk gate values (projected loss, worst-case drawdown, pass/fail)
• Recoup scenario P&L (net outcome if T1 stops and recoup wins)

**Exported Functions**
`f_calcTradeRiskDollars(entry, stop, qty)` → float
Projected risk in dollars. Converts price distance to ticks via syminfo.mintick, then to dollars via syminfo.pointvalue.

`f_calcTradeProfitDollars(entry, tp, qty)` → float
Projected profit in dollars. Same tick-to-dollar conversion applied to the take-profit distance.

`f_computeRecoupSizing(...)` → RecoupSizingResult
8-step recoup sizing pipeline:
1. Raw micro qty from risk multiplier (CEILING)
2. Daily loss limit cap at micro level
3. Micro-to-YM conversion (FLOOR)
4. Minimum-1-YM policy
5. Proxy capacity cap
6. Micro-equivalent for risk gating
7. Worst-case projection (base stop + opposing flatten + recoup stop)
8. Risk gate pass/fail
Plus scenario P&L: net recoup outcome and T1-win profit.

**Usage**
import Vantage-Stack/Vantage_LO1_Sizing/1 as sz

float risk = sz.f_calcTradeRiskDollars(entry, stop, qty)
sz.RecoupSizingResult r = sz.f_computeRecoupSizing(baseQty, baseEntry, baseStop, recoupEntry, recoupStop,
recoupTP, multiplier, proxyMul, minOneYM, curLoss, maxLoss, maxProxy, t1TP, oppTP, oppFrac, oppRemoval)
======================

Library "Vantage_LO1_Sizing"
Position sizing library for LO1 breakout box strategy.
Extracts pure-computation functions to reduce compiled token count in the main script.

f_calcTradeRiskDollars(_entry, _stop, _qty)
  Calculates projected risk in dollars for a position (qty × |entry−stop| in ticks × pointvalue).
  Parameters:
    _entry (float): Entry price
    _stop (float): Stop-loss price
    _qty (float): Position quantity (contracts)
  Returns: Risk in dollars

f_calcTradeProfitDollars(_entry, _tp, _qty)
  Calculates projected profit in dollars for a position (qty × |tp−entry| in ticks × pointvalue).
  Parameters:
    _entry (float): Entry price
    _tp (float): Take-profit price
    _qty (float): Position quantity (contracts)
  Returns: Profit in dollars

f_computeRecoupSizing(baseQtyMicro, baseEntry, baseStop, recoupEntry, recoupStop, recoupTP, oppositeMultiplier, proxyQtyMul, minOneYMEnabled, currentLossDollars, maxDailyLossLimit, maxProxyCap, t1TPPrice, oppTPPrice, oppAddStopFrac, opposingRemovalEnabled)
  Computes recoup (opposite-add) position sizing through an 8-step pipeline: raw micro qty, DLL cap, micro-to-YM conversion, min-1-YM policy, proxy cap, risk gating, worst-case projection, and recoup scenario P&L.
  Parameters:
    baseQtyMicro (int): T1 micro-contract quantity
    baseEntry (float): T1 entry price
    baseStop (float): T1 stop-loss price
    recoupEntry (float): Recoup entry price
    recoupStop (float): Recoup stop-loss price
    recoupTP (float): Recoup take-profit price
    oppositeMultiplier (float): Target risk multiplier for recoup vs T1 (e.g., 4.0)
    proxyQtyMul (float): Micro-to-YM conversion factor (0 = no proxy)
    minOneYMEnabled (bool): Force minimum 1 YM contract when proxy is active
    currentLossDollars (float): Running session loss in dollars (0 for estimate mode)
    maxDailyLossLimit (float): Daily loss limit in dollars (-1 = disabled)
    maxProxyCap (int): Maximum proxy contracts cap (0 = unlimited)
    t1TPPrice (float): T1 take-profit price (for scenario P&L calculation)
    oppTPPrice (float): Opposing MYM TP price when T1 stops (for scenario profit calc)
    oppAddStopFrac (float): Fraction of base risk for opposite MYM emergency flatten
    opposingRemovalEnabled (bool): Whether opposing removal entry mode is active
  Returns: RecoupSizingResult with quantities, risk values, and scenario P&L

RecoupSizingResult
  Holds the complete output of the 8-step recoup sizing pipeline: micro/YM quantities, risk gate results, worst-case projections, and recoup-scenario P&L.
  Fields:
    microQtyRaw (series int)
    microCapByDLL (series int)
    microQtyCapped (series int)
    ymQtyPre (series int)
    ymQtyFinal (series int)
    microEqForGate (series int)
    projectedLossRecoup (series float)
    dllRemaining (series float)
    totalWorstCase (series float)
    riskOK (series bool)
    oppMYMLoss (series float)
    didMinOneOverride (series bool)
    dllForcedZero (series bool)
    recoupScenarioNet (series float)
    t1WinProfit (series float)
Phát hành các Ghi chú
v2 Updated rounding calcs
Phát hành các Ghi chú
v3 Updated Rounding
Phát hành các Ghi chú
v4 Updated Rounding
Phát hành các Ghi chú
v5 Update Signature
Phát hành các Ghi chú
v6 Updated Sizing Behavior
Phát hành các Ghi chú
v7 Updated Sizing
Phát hành các Ghi chú
v8 - Partial Exit Support
Phát hành các Ghi chú
v9 Updated support for auto risk sizing
Phát hành các Ghi chú
v10 Updating Sizing Logic

Updated:
f_computeRecoupSizing(baseQtyMicro, baseEntry, baseStop, recoupEntry, recoupStop, recoupTP, oppositeMultiplier, proxyQtyMul, minOneYMEnabled, currentLossDollars, maxDailyLossLimit, maxProxyCap, t1TPPrice, oppTPPrice, oppAddStopFrac, opposingRemovalEnabled, t1AlreadyClosed, useRoundUp)
  Computes recoup (opposite-add) position sizing through an 8-step pipeline: raw micro qty, DLL cap, micro-to-YM conversion, min-1-YM policy, proxy cap, risk gating, worst-case projection, and recoup scenario P&L.
  Parameters:
    baseQtyMicro (int): T1 micro-contract quantity
    baseEntry (float): T1 entry price
    baseStop (float): T1 stop-loss price
    recoupEntry (float): Recoup entry price
    recoupStop (float): Recoup stop-loss price
    recoupTP (float): Recoup take-profit price
    oppositeMultiplier (float): Target risk multiplier for recoup vs T1 (e.g., 4.0)
    proxyQtyMul (float): Micro-to-YM conversion factor (0 = no proxy)
    minOneYMEnabled (bool): Force minimum 1 YM contract when proxy is active
    currentLossDollars (float): Running session loss in dollars (0 for estimate mode)
    maxDailyLossLimit (float): Daily loss limit in dollars (-1 = disabled)
    maxProxyCap (int): Maximum proxy contracts cap (0 = unlimited)
    t1TPPrice (float): T1 take-profit price (for scenario P&L calculation)
    oppTPPrice (float): Opposing MYM TP price when T1 stops (for scenario profit calc)
    oppAddStopFrac (float): Fraction of base risk for opposite MYM emergency flatten
    opposingRemovalEnabled (bool): Whether opposing removal entry mode is active
    t1AlreadyClosed (bool): Reserved for future use. All current call sites use false (default). Would be true only if recoup were re-sized after T1 close, which no current code path does.
    useRoundUp (bool)
  Returns: RecoupSizingResult with quantities, risk values, and scenario P&L

f_findBestPartialExit(baseQtyMicro, baseRisk, riskPerMicroRecoup, profitPerMicroRecoup, proxyQtyMul, currentLossDollars, maxDailyLossLimit, maxProxyCap, oppMYMLoss, t1AlreadyClosed, oppositeMultiplier, useRoundUp)
  Finds the best partial exit configuration (how many opposing MYM to keep) that passes both risk and profit gates.
Scans k from 0 upward — first passing config wins (prefers full removal when it works).
  Parameters:
    baseQtyMicro (int): T1 qty (q)
    baseRisk (float): T1 total risk in dollars
    riskPerMicroRecoup (float): Recoup risk per micro contract
    profitPerMicroRecoup (float): Recoup profit per micro contract
    proxyQtyMul (float): Micro-to-YM conversion factor (0.10)
    currentLossDollars (float): Running session loss in dollars
    maxDailyLossLimit (float): Daily loss limit in dollars
    maxProxyCap (int): Maximum proxy contracts cap (0 = unlimited)
    oppMYMLoss (float): Opposing MYM emergency flatten loss per micro
    t1AlreadyClosed (bool): Reserved for future use. All current call sites use false (recoup is sized at T1 entry time, not after close).
    oppositeMultiplier (float): Target risk multiplier for recoup vs T1
    useRoundUp (bool)
  Returns: PartialExitResult with best k, adjusted YM qty, and gate results

f_scanPartialExitUpward(baseGreen, baseMatch, maxScanQty, longEntry, longStop, longTP, longTPOpp, shortEntry, shortStop, shortTP, shortTPOpp, oppositeMultiplier, proxyQtyMul, minOneYMEnabled, maxDailyLossLimit, maxProxyCap, oppAddStopFrac, opposingRemovalEnabled, matchPct, commBuffer, useRoundUp)
  Scans upward from baseline quantities to find larger T1 sizes enabled by partial exit.
Replaces inline loops in the main script to reduce compiled token count.
  Parameters:
    baseGreen (int): Baseline bestQtyGreen from normal scan
    baseMatch (int): Baseline bestQtyMatch from normal scan
    maxScanQty (int): Maximum quantity to scan up to
    longEntry (float): Long leg entry price
    longStop (float): Long leg stop price
    longTP (float): Long leg TP price
    longTPOpp (float): Long leg opposing TP price
    shortEntry (float): Short leg entry price
    shortStop (float): Short leg stop price
    shortTP (float): Short leg TP price
    shortTPOpp (float): Short leg opposing TP price
    oppositeMultiplier (float): Recoup qty multiplier
    proxyQtyMul (float): Micro-to-YM conversion factor
    minOneYMEnabled (bool): Min-1-YM toggle
    maxDailyLossLimit (float): Daily loss limit
    maxProxyCap (int): Max proxy contracts
    oppAddStopFrac (float): Opposing stop fraction
    opposingRemovalEnabled (bool): Whether opposing removal is active
    matchPct (float): Match T1 profit percentage
    commBuffer (float): Commission buffer dollars
    useRoundUp (bool)
  Returns: PartialExitScanResult with updated quantities and improvement flags

f_sizingWithPartialExit(baseQtyMicro, baseEntry, baseStop, recoupEntry, recoupStop, recoupTP, oppositeMultiplier, proxyQtyMul, minOneYMEnabled, currentLossDollars, maxDailyLossLimit, maxProxyCap, t1TPPrice, oppTPPrice, oppAddStopFrac, opposingRemovalEnabled, useRoundUp)
  Runs recoup sizing and partial exit optimization in one call.
Combines f_computeRecoupSizing + f_findBestPartialExit to reduce token count at call sites.
  Parameters:
    baseQtyMicro (int): T1 micro quantity
    baseEntry (float): T1 entry price
    baseStop (float): T1 stop price
    recoupEntry (float): Recoup entry price
    recoupStop (float): Recoup stop price
    recoupTP (float): Recoup TP price
    oppositeMultiplier (float): Recoup qty multiplier
    proxyQtyMul (float): Micro-to-YM conversion factor
    minOneYMEnabled (bool): Min-1-YM toggle
    currentLossDollars (float): Running session loss
    maxDailyLossLimit (float): Daily loss limit
    maxProxyCap (int): Max proxy contracts
    t1TPPrice (float): T1 TP price
    oppTPPrice (float): Opposing TP price
    oppAddStopFrac (float): Opposing stop fraction
    opposingRemovalEnabled (bool): Whether opposing removal is active
    useRoundUp (bool)
  Returns: [RecoupSizingResult, PartialExitResult] — PE result has found=false if not applicable
Phát hành các Ghi chú
v11 Rounding Fixes
Phát hành các Ghi chú
v12 Updated Autosizing Method

Phát hành các Ghi chú
v13 Updated Risk Sizing Method

Phát hành các Ghi chú
Add slippage to sizing calculations

Thông báo miễn trừ trách nhiệm

Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.