Size a trade from a risk basis, with fee and buying-power modelling.
const url = 'https://api.tradr.cloud/api/calculator';const options = { method: 'POST', headers: {'Content-Type': 'application/json'}, body: '{"balance":"example","direction":"long","dollarRisk":"example","entryPrice":"example","feeSchedule":{},"manualFees":"example","mode":"stock","riskPercent":"example","stopLoss":"example","targetPrice":"example"}'};
try { const response = await fetch(url, options); const data = await response.json(); console.log(data);} catch (error) { console.error(error);}curl --request POST \ --url https://api.tradr.cloud/api/calculator \ --header 'Content-Type: application/json' \ --data '{ "balance": "example", "direction": "long", "dollarRisk": "example", "entryPrice": "example", "feeSchedule": {}, "manualFees": "example", "mode": "stock", "riskPercent": "example", "stopLoss": "example", "targetPrice": "example" }'Authed and stateless — the whole calculation is a pure function of the request body (no DB read, no account id). Supply the trade shape (entryPrice, stopLoss, direction, mode) plus exactly one risk basis: a direct dollarRisk, OR a balance + riskPercent (the dollar risk is then derived as balance × riskPercent ÷ 100 at full precision). Supplying both bases, or neither, is a 400. In the percent basis the position size is also capped to what the balance can fund at entry (floor(balance ÷ (entry × multiplier))); the three percent-only response fields (derivedDollarRisk, sizingStatus, buyingPowerLimited) are absent from dollar-basis results. Non-sizing outcomes (non-positive balance, derived risk over the ceiling, insufficient risk, balance funds zero units) are valid 200s with positionSize: 0 and a sizingStatus discriminator — not 400s.
Request Bodyrequired
Section titled “Request Bodyrequired”Provide exactly one risk basis — dollarRisk, or balance + riskPercent.
object
Percent-basis balance to size against and cap against. Sign-agnostic finite decimal within the account-balance domain (a losing account’s balance can be ≤ 0). Percent basis requires both balance and riskPercent.
Dollar-basis risk (positive decimal up to 99,999,999.99). Required in dollar mode; omit (or send empty ⇒ treated as absent) in percent mode.
Positive decimal up to 9,999,999.99.
Optional brokerage fee schedule (mutually exclusive with manualFees).
object
Optional flat fee estimate (mutually exclusive with feeSchedule).
Percent of balance to risk (0 < p ≤ 100).
Positive decimal up to 9,999,999.99.
Optional positive decimal for R:R.
Responses
Section titled “Responses”A CalculatorOutput. Always carries positionSize, perUnitRisk, actualDollarRisk, totalPositionValue; perUnitReward / riskRewardRatio when a target is set, and the fee fields when fees are supplied. Percent basis additionally echoes derivedDollarRisk (2dp, when balance > 0), sizingStatus (nothing-to-size-against | exceeds-maximum | buying-power-zero) on the zero-position outcomes, and buyingPowerLimited: true when the buying-power cap set the size.
Validation error — both/neither risk basis, riskPercent ∉ (0,100], bad balance/price format, or a structural price error.
Authentication required.