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","buyingPower":"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", "buyingPower": "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. The position size is also capped to what the account can fund at entry — floor(capBasis ÷ (entry × multiplier)) — where capBasis is buyingPower when supplied (either risk basis), else balance on the percent basis, else no cap at all on the dollar basis. The risk budget is always a percent of balance; only the cap consults buyingPower. derivedDollarRisk remains percent-only, but sizingStatus and buyingPowerLimited can now appear on a dollar-basis result whenever buyingPower is supplied. Non-sizing outcomes (non-positive balance, derived risk over the ceiling, insufficient risk, cap basis 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 the RISK BUDGET is derived from, and the default basis for the buying-power cap. Sign-agnostic finite decimal within the account-balance domain (a losing account’s balance can be ≤ 0). Percent basis requires both balance and riskPercent.
Optional figure the BUYING-POWER CAP is computed against, valid in EITHER risk basis. Absent ⇒ the percent basis caps against balance (the original behaviour) and the dollar basis is uncapped (likewise). Supply an account’s cash here to stop the calculator sizing a position the account cannot fund: total equity overstates fundable capital by whatever is already deployed, and a direct dollar risk overshoots just as readily as a percentage one. Sign-agnostic — a fully-deployed or margined account can present ≤ 0 cash, which yields sizingStatus: buying-power-zero. Never affects the risk budget.
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). Always a percent of balance, never of buyingPower.
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 cap set the size appear on EITHER basis — the latter two require only that a cap basis existed, which on the dollar basis means buyingPower was supplied.
Validation error — both/neither risk basis, riskPercent ∉ (0,100], bad balance/price format, or a structural price error.
Authentication required.