Cost Management
Costs on the Pengine platform are driven by cost rules. A cost rule determines when a cost is collected, who receives the costs and what the calculated amount is. When a cost rule is triggered, PENGINE evaluates the LUA expression and creates a Cost Record with the amount as returned by the LUA expression.
The Rule field
Every cost rule has a Rule field containing a LUA expression. The expression must return a number which becomes the cost record amount.
-- A flat 5.00 fee
return 5
-- 0.2% of the traded value (order transaction rules only)
return trade.calculatePercentage(0.2)
Which globals are available depends on the rule type. The Shared API Reference documents the globals every rule can use.
The four rule types
Validation vs. execution
A cost rule’s LUA is evaluated in two situations:
- Validation when a rule is saved. PENGINE runs the LUA expression against sample data to confirm it is syntactically valid and returns a number, without touching real accounts. This is why you can author and save a rule safely.
- Execution when the rule actually fires. The expression runs against real data and creates cost records.
Error reasons
When evaluation fails, the cost record is marked as invalid with a status reason:
| Reason | Cause |
|---|---|
INVALID_LUA_EXPRESSION |
The expression is empty, has a syntax error, or throws at runtime (including calling an unsupported API method). |
INVALID_LUA_RETURN_TYPE |
The expression ran but returned something other than a number. |
LUA_EXECUTION_TIMEOUT |
The expression exceeded the 5-second execution budget, or a data-loading pause exceeded its 5-minute ceiling. |