Guidelines8 min read
Guidelines and recommendations for writing efficient, maintainable, and error-free kScript code.
Code Structure
Variable Declarations
Use the appropriate variable type for your data: Static variables for constants: Use for constants and values that never change across bars (e.g., periods, thresholds, multipliers).- Store data source outputs:
timeseries price_data = ohlcv(...) - Access past values at previous candle indices:
sma_values[5]
Technical Indicators
Explicit Parameters
Always specify parameters explicitly for clarity. This makes your code more readable and maintainable.Historical Data Access
Use bracket notation to access historical values safely:Plotting & Visualization
Descriptive Plots
Use meaningful colors and widths to make your plots easy to distinguish:Choose Appropriate Plot Types
Select the right plot function for your data type:| Function | Use Case |
|---|---|
plotLine() | Continuous lines (MAs, RSI) |
plotBar() | Histogram data (volume, MACD) |
plotCandle() | OHLC data |
plotShape() | Discrete signals |
Debugging
Use printTimeSeries for Timeseries Objects
Always useprintTimeSeries() for timeseries objects instead of print(). The print() function only outputs the first value, while printTimeSeries() displays the complete structure.