|
| 1 | +<!-- |
| 2 | +--- |
| 3 | +weight: 1005 |
| 4 | +title: "Extra Fields" |
| 5 | +description: "Accessing custom CSV columns beyond OHLCV data in Pyne scripts" |
| 6 | +icon: "playlist_add" |
| 7 | +date: "2025-03-31" |
| 8 | +lastmod: "2026-03-15" |
| 9 | +draft: false |
| 10 | +toc: true |
| 11 | +categories: ["Advanced", "Data Handling"] |
| 12 | +tags: ["extra-fields", "csv", "custom-data", "series", "data"] |
| 13 | +--- |
| 14 | +--> |
| 15 | + |
| 16 | +# Extra Fields |
| 17 | + |
| 18 | +PyneCore allows you to access additional columns beyond standard OHLCV data from your CSV files inside Pyne scripts. This is useful when your data includes pre-computed indicators, signals, or any other custom data that you want to use alongside price data. |
| 19 | + |
| 20 | +## How It Works |
| 21 | + |
| 22 | +When a CSV file contains columns beyond the standard OHLCV fields (`timestamp`, `open`, `high`, `low`, `close`, `volume`), PyneCore automatically makes them available through `extra_fields` — a dictionary that is updated on each bar with the current row's extra column values. |
| 23 | + |
| 24 | +The data flow depends on how you run your script: |
| 25 | + |
| 26 | +### Binary OHLCV Path (workdir) |
| 27 | + |
| 28 | +When running from a workdir with `pyne run`, PyneCore converts CSV to binary `.ohlcv` format. Since the binary format only stores OHLCV data, extra columns are saved to a **sidecar file** (`.extra.csv`) that is position-aligned with the binary data: |
| 29 | + |
| 30 | +``` |
| 31 | +workdir/data/my_data/ |
| 32 | + EURUSD_1h.csv # Source: OHLCV + extra columns |
| 33 | + EURUSD_1h.ohlcv # Binary OHLCV (auto-generated) |
| 34 | + EURUSD_1h.toml # Symbol metadata (auto-generated) |
| 35 | + EURUSD_1h.extra.csv # Extra columns only (auto-generated) |
| 36 | +``` |
| 37 | + |
| 38 | +The sidecar file is generated and regenerated automatically whenever the source CSV is converted. You never need to create or edit it manually. |
| 39 | + |
| 40 | +### Direct CSV Path (CSVReader / standalone) |
| 41 | + |
| 42 | +When reading CSV directly (e.g., via `CSVReader` or standalone execution), extra columns are parsed inline — no sidecar file is needed. |
| 43 | + |
| 44 | +## Usage in Scripts |
| 45 | + |
| 46 | +Access extra fields through `lib.extra_fields`, which is a `dict[str, Any]` updated each bar: |
| 47 | + |
| 48 | +```python |
| 49 | +""" |
| 50 | +@pyne |
| 51 | +""" |
| 52 | +from pynecore import Series |
| 53 | +from pynecore.lib import script, ta, close, extra_fields, plot |
| 54 | + |
| 55 | + |
| 56 | +@script.indicator(title="Extra Fields Example", overlay=True) |
| 57 | +def main(): |
| 58 | + # Access extra columns as Series by annotating with Series[T] |
| 59 | + rsi: Series[float] = extra_fields["rsi"] |
| 60 | + signal: Series[str] = extra_fields["signal"] |
| 61 | + |
| 62 | + # Series indexing works — access previous bars |
| 63 | + prev_rsi = rsi[1] # Previous bar's RSI value |
| 64 | + rsi_2_ago = rsi[2] # RSI from 2 bars ago |
| 65 | + |
| 66 | + # Use with built-in functions like any other Series |
| 67 | + rsi_sma = ta.sma(rsi, 14) |
| 68 | + |
| 69 | + # Use string fields for conditional logic |
| 70 | + if signal[0] == "buy": |
| 71 | + plot(close, "Buy Signal", linewidth=2) |
| 72 | +``` |
| 73 | + |
| 74 | +### Key Points |
| 75 | + |
| 76 | +- **Type annotation creates the Series**: Writing `rsi: Series[float] = extra_fields["rsi"]` makes `rsi` a proper Series with history. The `extra_fields["rsi"]` part just returns the current bar's value (a plain `float`). |
| 77 | +- **Supported types**: `float`, `int`, `str`, and `bool`. The type is detected automatically from the CSV data. |
| 78 | +- **Missing values**: Empty cells in the CSV appear as empty string (`''`) when read via CSVReader, or `NaN` when read via the binary OHLCV + sidecar path. |
| 79 | +- **No AST magic needed**: The standard Series annotation mechanism handles everything — there is no special treatment for `extra_fields` in the AST transformers. |
| 80 | + |
| 81 | +## CSV Format |
| 82 | + |
| 83 | +Your source CSV simply includes extra columns alongside the standard OHLCV columns: |
| 84 | + |
| 85 | +```csv |
| 86 | +timestamp,open,high,low,close,volume,rsi,signal,custom_price |
| 87 | +2024-01-01T00:00:00,100.0,105.0,95.0,102.0,1000,45.2,buy,99.5 |
| 88 | +2024-01-01T01:00:00,102.0,108.0,100.0,106.0,1200,52.1,,101.3 |
| 89 | +2024-01-01T02:00:00,106.0,110.0,104.0,108.0,800,38.7,sell, |
| 90 | +``` |
| 91 | + |
| 92 | +The following column names are recognized as standard OHLCV and will **not** appear in `extra_fields`: |
| 93 | + |
| 94 | +| Recognized OHLCV columns | |
| 95 | +|--------------------------------------------------------| |
| 96 | +| `timestamp`, `time`, `date`, `datetime` | |
| 97 | +| `open`, `high`, `low`, `close`, `volume` | |
| 98 | + |
| 99 | +Any other column name is treated as an extra field. |
| 100 | + |
| 101 | +## Sidecar File Format |
| 102 | + |
| 103 | +The auto-generated `.extra.csv` file contains only the extra columns, with rows aligned 1:1 to the binary `.ohlcv` file (including gap-filled rows): |
| 104 | + |
| 105 | +```csv |
| 106 | +rsi,signal,custom_price |
| 107 | +45.2,buy,99.5 |
| 108 | +52.1,,101.3 |
| 109 | +38.7,sell, |
| 110 | +,, |
| 111 | +,, |
| 112 | +42.0,hold,100.0 |
| 113 | +``` |
| 114 | + |
| 115 | +Empty rows correspond to gap-filled bars in the OHLCV binary (bars with `volume = -1`). |
| 116 | + |
| 117 | +## Limitations |
| 118 | + |
| 119 | +- **Binary format unchanged**: The `.ohlcv` binary format remains fixed at 24 bytes per record. Extra fields are stored separately in the sidecar CSV. |
| 120 | +- **JSON source files**: Extra field extraction is currently supported for CSV and TXT source formats, not JSON. |
| 121 | +- **Memory**: The sidecar is loaded entirely into memory when opening the OHLCV file. For typical datasets (up to a few hundred thousand bars with a handful of extra columns), this is negligible. |
| 122 | +- **Not available from providers**: Data download providers (e.g., CCXT, TradingView) produce standard OHLCV data only. Extra fields are for user-provided CSV data. |
| 123 | + |
| 124 | +## See Also |
| 125 | + |
| 126 | +- [OHLCV Reader/Writer](./ohlcv-reader-writer.md) — Binary OHLCV format details |
| 127 | +- [CSV Reader/Writer](./csv-reader-writer.md) — CSV processing internals |
0 commit comments