Set Quantity Dynamically

Strategies that perform well will reach a point where the trade capital is doubled over time. The user might want to scale/increase the traded quantity for the structure when that point is reached. Such behavior can be achieved by leveraging the nav variable while specifying the quantity (see rows 2 and 10):

{
  "Cash": 10000.0000,
...
  "Structure": {
 "Name": "ShortStrangle",
      ...
            "Legs": [
     {
         "Name": "upper_long",
        "Qty": "1 * floor(max(1, nav/10000))",
         "ExpirationName": "160dte",
        ...

The formula "1 * floor(max(1, nav/10000)" instructs the script engine to take the current Net Asset Value (Cash + Positions if any) and divide it by 10000, which is our initial Cash (or planned capital). At the start of the execution, the division will yield 1, and later as trades progress, it will go above or beyond 1. Since fractional shares are not supported, and the minimal tradable amount is 1, we calculate the quantity by first taking the largest value of (maximum) [1 and the result of division]. Taking the maximum ensures that we always have at least 1 contract to trade, even if our NAV decreases below the initial 10000 amount.
If the capital has grown beyond 10000, the max function will still take the largest value (e.g., NAV increased to 11000, then max(1, nav/10000) will yield 1.1). The largest value might or might not be a round number (integer). Hence we apply the floor Lua function to round it to the closest integer less than or equal to that number.

Besides setting the Qty dynamically, it is essential to scale the StopLimit and ProfitTarget (if they exist). The same method can be applied:

"ProfitTarget": "1000 * floor(max(1, nav/10000))"
"StopLoss": "2 * 1000 * floor(max(1, nav/10000))"

Comments