---
title: Keyword Arguments
description: >-
  Learn how to use keyword arguments (kwargs) in kScript v2 for clear, readable,
  and maintainable function calls with named parameters.
---


<div class="flex gap-3 mb-6">
  <span class="inline-flex items-center gap-1.5 px-3 py-1 rounded-full bg-blue-50 text-blue-600 text-sm font-medium">
    Core Concept
  </span>
  <span class="inline-flex items-center gap-1.5 px-3 py-1 rounded-full bg-gray-100 text-gray-600 text-sm font-medium">
    3 min read
  </span>
</div>

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

| Feature | Benefit |
|---------|---------|
| All Function Support | Every function accepts kwargs |
| Any Parameter Order | Parameters can be in any order |
| Self Documenting | Code 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.

```javascript title="Keyword Arguments Syntax" lines wrap
// 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:

```javascript title="Technical Indicator Examples" lines wrap
// 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:

```javascript title="Data Source Examples" lines wrap
// OHLCV data
timeseries priceData = ohlcv(symbol="BTCUSDT", exchange="BINANCE")

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

### Plotting & Visualization

Create clear, customized charts:

```javascript title="Plotting Examples" lines wrap
// 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

<table data-view="cards" data-layout="stack"><tbody>
<tr><td>Always Use kwargs for Complex Functions</td><td><p>For functions with many parameters, kwargs make your code much clearer:</p><pre><code class="language-javascript">// 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"]
)</code></pre></td><td></td></tr>
<tr><td>Use kwargs When Skipping Optional Parameters</td><td><p>When you only need to specify certain parameters:</p><pre><code class="language-javascript">// Only specify what you need
input(name="period", type="number", defaultValue=14, label="Period")</code></pre></td><td></td></tr>
<tr><td>Consistent Style</td><td>Pick a style and stick with it throughout your script for better readability.</td><td></td></tr>
</tbody></table>
