Skip to main content
Core Concept3 min read
Learn how to use keyword arguments (kwargs) in kScript v2 for clear, readable, and maintainable function calls with named parameters.

Overview

Keyword arguments (kwargs) allow you to pass parameters to functions by name rather than position. This makes function calls more readable, reduces errors, and allows for flexible parameter ordering.
FeatureBenefit
All Function SupportEvery function accepts kwargs
Any Parameter OrderParameters can be in any order
Self DocumentingCode explains itself

Basic Syntax

Use the parameterName=value syntax to pass arguments by name. You can mix positional and keyword arguments, but positional arguments must come first.
// Basic kwargs syntax - all parameters named
rsi(source=closeTs, period=14)
sma(source=closeTs, period=20)

// Mixing positional and keyword arguments
// Correct: positional arguments first, then keyword arguments
rsi(closeTs, period=14)
macd(closeTs, 12, slowPeriod=26, signalPeriod=9)

// Invalid: cannot use positional arguments after keyword arguments
// rsi(source=closeTs, 14)  // Error! Positional after keyword
// macd(closeTs, fastPeriod=12, 26)  // Error! Positional after keyword

// Parameters can be in any order when using all kwargs
macd(signalPeriod=9, source=closeTs, fastPeriod=12, slowPeriod=26)

Benefits

  • Self-Documenting - Parameter names make function calls self-explanatory
  • Flexible Order - Parameters can be specified in any order
  • Selective Parameters - Easily specify only the parameters you need

Practical Examples

Technical Indicators

Using kwargs makes indicator parameters crystal clear:
// RSI with custom period and source
rsiValue = rsi(source=closeTs, period=21)

// Moving averages with different periods
fastMA = sma(source=closeTs, period=10)
slowMA = sma(source=closeTs, period=50)

Data Sources

Specify exactly which data you need:
// OHLCV data
timeseries priceData = ohlcv(symbol="BTCUSDT", exchange="BINANCE")

// Orderbook data
timeseries orderbookData = orderbook(symbol="ETHUSDT", exchange="BINANCE")

Plotting & Visualization

Create clear, customized charts:
// Plot RSI with custom styling
plotLine(
  value=rsiValue,
  width=2,
  colors=["purple"],
  label=["RSI"],
  desc=["Relative Strength Index"]
)

// Plot MACD histogram
plotBar(
  value=histogram,
  width=1,
  colors=["green", "red"],
  label=["MACD Histogram"],
  desc=["MACD Histogram"]
)

Best Practices

Always Use kwargs for Complex Functions

For functions with many parameters, kwargs make your code much clearer:
// Hard to understand
plotLine(rsiValue, 2, ["blue"], 0, ["RSI"], ["Relative Strength Index"])

// Easy to understand
plotLine(
  value=rsiValue,
  width=2,
  colors=["blue"],
  colorIndex=0,
  label=["RSI"],
  desc=["Relative Strength Index"]
)

Use kwargs When Skipping Optional Parameters

When you only need to specify certain parameters:
// Only specify what you need
input(name="period", type="number", defaultValue=14, label="Period")

Consistent Style

Pick a style and stick with it throughout your script for better readability.