Request Security Architecture
request.security is the most complex function in PineTS as it involves creating and managing secondary execution contexts.
Context Hierarchy
PineTS creates a strictly Two-Level Hierarchy to prevent infinite recursion.
- Primary Context: The main script execution.
- Secondary Context: Created by
request.securityto run the same script on a different timeframe/symbol.
Guard: Secondary contexts have isSecondaryContext = true. If request.security is called inside a secondary context, it returns the expression directly instead of creating a tertiary context.
Caching Strategy
Since creating a PineTS instance and running a script is expensive, secondary contexts are aggressively cached.
Cache Key
The cache key is composite:
key = `${symbol}_${timeframe}_${expressionID}`;
expressionID: The unique ID (p0,p1…) of the expression parameter passed tosecurity. This is whyrequest.param()returns[val, name].
Data Flow
- User calls
request.security(..., expression). - Primary Context:
- Checks cache.
- If miss: Creates
new PineTS(...)for target timeframe. - Sets
isSecondaryContext = true. - Runs the script.
- Stores result in cache.
- Extracts result aligned to primary timeframe.
- Secondary Context:
- Runs the same script.
- When it encounters
request.security: - Checks
isSecondaryContext. - Returns
expressiondirectly. - This effectively “unwraps” the security call inside the secondary context, allowing the script to calculate the expression natively in that timeframe.