Skip to main content
FAQ5 min read
Frequently asked questions about kScript, covering everything from basic syntax to advanced topics.

Getting Started

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.
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

These are built-in context variables that automatically contain the current trading pair and exchange being analyzed:
// These variables are automatically set based on your chart context
timeseries data = ohlcv(symbol=currentSymbol, exchange=currentExchange);
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:
// Check bar index before accessing historical data
if (barIndex >= 5) {
  var historicalValue = data[5];
}
Yes, but you need to explicitly specify each symbol:
timeseries btcData = ohlcv(symbol="BTCUSDT", exchange="BINANCE");
timeseries ethData = ohlcv(symbol="ETHUSDT", exchange="BINANCE");
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.
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.
// Invalid - source inside condition
if (someCondition) {
  timeseries data = ohlcv(...);  // Error!
}

// Valid - source at root level
timeseries data = ohlcv(...);

if (someCondition) {
  var value = data.close;  // OK
}
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

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
Solution: Check for NaN values:
var value = isNaN(calculation) ? 0 : calculation;

Plotting and Visualization

Use multiple plot function calls:
plotLine(value=sma20, width=2, colors=["blue"], label=["SMA 20"], desc=["20-period SMA"]);
plotLine(value=sma50, width=2, colors=["red"], label=["SMA 50"], desc=["50-period SMA"]);
Yes, use ternary operators or conditional values with na:
var signal = condition ? ohlcvData.low : na;
plotShape(value=signal, shape="triangle", colors=["green"], label=["Buy"], desc=["Buy Signal"]);
Use the colorIndex parameter with a color array:
var colorIndex = value > threshold ? 0 : 1;
plotLine(value=data, colors=["green", "red"], colorIndex=colorIndex, label=["Data"], desc=["Dynamic Color"]);
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:
// Define conditional value
var plotValue = condition ? calculatedValue : na;

// Plot at root level
plotLine(value=plotValue, colors=["blue"], label=["Conditional"], desc=["Conditional Plot"]);