FennEdge Learn · Programming

DAS Trader Advanced Hotkey Script Course

Understand the code. Inspect the state. Verify the outcome. A detailed path through Advanced Script: language, data structures, order objects, event workflows and complete practice projects.

Plan about 1046 minutes including reading, worked examples, and each lesson exercise; actual time varies.

36Lessons≈1046Est. minutes—Sign in to save progress
27
Lesson 27 of 36 · About 28 min

Build an Auditable DAS Position Sizing Calculation

Separate mathematical sizing from executable quantity.

Start with declared assumptions

A basic sizing model divides a hypothetical budget by a positive per-share distance. It is a model under specified prices, not a maximum-loss guarantee. It excludes slippage, gaps, fees, rejected protection and changing market conditions.

Advanced Script · fictional inputs · calculation only. It displays an unrounded model quantity and never writes Share.

$feBudget = 30;
$feDistance = 0.25;
$feRawUnits = 0;
if ($feBudget > 0) {
    if ($feDistance > 0) {
        $feRawUnits = $feBudget / $feDistance;
        MsgBox($feRawUnits);
    } else {
        MsgBox("Distance must be positive");
        return;
    }
} else {
    MsgBox("Budget must be positive");
    return;
}

The expected model quantity is 120. Change distance to 0.40 and it becomes 75. Change it to zero and no quantity should be presented as valid.

Turn a model into a reviewable specification

Step Required decision
Validate input Positive budget and distance, correct units
Compute model Budget divided by distance
Apply constraints Account buying power, quantity cap, lot rules
Round deliberately A documented, tested rule that cannot increase the permitted exposure
Recalculate Quantity × distance, plus separately considered costs
Present Inputs, assumptions and calculated result together

Do not guess a Floor() function or reuse Round(...,100) as integer rounding. The guide gives special even-lot meanings to some Round precision values. Verify the exact rule before adapting the result to a ticket.

Lab: edge cases

Test budget 30 with distances 0.25, 0.40, 0 and -0.25; then budget 0. Add a fractional-quantity example and write the desired rounding behavior as a specification before implementing it.

Combine constraints using the tighter quantity

If the budget model gives 120 but a manual practice cap is 80, the candidate is 80. Applying a round-down-to-100-share-lots exercise rule then yields zero. Report no quantity satisfying that rule rather than quietly rounding up to 100.

This example uses fixed fictional data and global variables. The cap is practice configuration, not an observed account authorization or buying-power limit. The 100-share lot is an exercise rule illustrating the documented Round(...,100) behavior.

$feBudget = 30;
$feDistance = 0.25;
$feCap = 110;
$feSizedReady = 0;
$feSizedUnits = 0;
if ($feBudget <= 0) { return; }
if ($feDistance <= 0) { return; }
if ($feCap <= 0) { return; }
$feCandidate = $feBudget / $feDistance;
if ($feCandidate > $feCap) {
    $feCandidate = $feCap;
}
$feSizedUnits = Round($feCandidate, 100);
if ($feSizedUnits <= 0) {
    MsgBox("No quantity under this lot rule");
    return;
}
$feModelExposure = $feSizedUnits * $feDistance;
$feSizedReady = 1;
MsgBox($feSizedUnits, " / ", $feModelExposure);

Worked answers

Expect candidate 110, rounded quantity 100 and model exposure 25. A cap of 80 leaves ready at 0; last run's 100 cannot serve as the current result. Budget 30 with distance 0.40 gives a raw 75, also unable to satisfy this example's lot rule. Rounding determines whether a valid result exists, so it is specification, not formatting.

To add a notional cap, divide that amount by a positive assumed price to obtain a quantity before comparing constraints. Never compare dollars directly with shares. Fees, gaps and slippage remain outside this model. Continue with unit checks in Numeric models.