FAQ5 min read
Frequently asked questions about kScript, covering everything from basic syntax to advanced topics.
Getting Started
What is kScript?
What is kScript?
kScript is a domain-specific language designed for financial data analysis and visualization. It allows you to write scripts for technical analysis, trading indicators, and market data processing with intuitive syntax.
Do I need to know TypeScript to use kScript?
Do I need to know TypeScript to use kScript?
No, you don’t need to know TypeScript. kScript has its own simplified syntax. However, familiarity with programming concepts will help you write more complex scripts.
Data Sources and Context
What are currentSymbol and currentExchange?
What are currentSymbol and currentExchange?
These are built-in context variables that automatically contain the current trading pair and exchange being analyzed:
Why do I get NaN when accessing historical data in early bars?
Why do I get NaN when accessing historical data in early bars?
kScript executes per-bar, meaning on each bar, only data up to that point is available. When you access historical data like
data[5], the first 4 bars will return NaN because there aren’t 5 bars of history yet.Solution: Always check for NaN or ensure enough historical data exists before using it in calculations:Can I analyze multiple symbols in one script?
Can I analyze multiple symbols in one script?
Yes, but you need to explicitly specify each symbol:
Can I write a script without source data?
Can I write a script without source data?
No, you cannot run a script without source data. kScript is designed for time-series analysis and requires data to create the timeline for bar-by-bar execution.
Can I subscribe to sources inside control structures (if/while/for)?
Can I subscribe to sources inside control structures (if/while/for)?
No, you cannot call source functions inside control structures. Source subscriptions must be declared at the root level of your script in
timeseries declarations.Why: Sources need to be fetched and prepared before the script can execute. The runtime extracts source calls during the initialization phase, before the bar-by-bar loop begins.How does kScript handle data gaps and interpolation?
How does kScript handle data gaps and interpolation?
kScript fills data gaps with
NaN values. For line plots, kScript will interpolate to connect points across gaps. For other use cases, if you need interpolation, you must implement it manually.Technical Indicators
Why am I getting NaN values in my calculations?
Why am I getting NaN values in my calculations?
NaN (Not a Number) usually occurs when:
- There’s insufficient historical data for the calculation
- You’re dividing by zero
- The data source has gaps
Plotting and Visualization
How do I plot multiple indicators on the same chart?
How do I plot multiple indicators on the same chart?
Use multiple plot function calls:
Can I plot conditional signals?
Can I plot conditional signals?
Yes, use ternary operators or conditional values with
na:How do I change plot colors dynamically?
How do I change plot colors dynamically?
Use the
colorIndex parameter with a color array:Can I call plot functions inside conditionals or loops?
Can I call plot functions inside conditionals or loops?
No, plot functions must be called at the root level of your script. Define your plot values conditionally, then call the plot function at the root: