---
title: "US Electricity"
description: "US Electricity dataset from Energy Information Administration."
canonical: https://www.quantconnect.com/docs/v2/writing-algorithms/datasets/energy-information-administration/us-electricity
markdown: https://www.quantconnect.com/docs/v2/writing-algorithms/datasets/energy-information-administration/us-electricity.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/03%20Writing%20Algorithms/14%20Datasets/13%20Energy%20Information%20Administration/01%20US%20Electricity
modified: 2026-10-26
index: https://www.quantconnect.com/llms.txt
---

# US Electricity

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Writing Algorithms](https://www.quantconnect.com/docs/v2/writing-algorithms.md) > Datasets > [Energy Information Administration](https://www.quantconnect.com/docs/v2/writing-algorithms/datasets/energy-information-administration.md) > US Electricity

> Code examples are shown in Python. Append `?lang=csharp` to this URL for the C# version of this page.

## Introduction

The US Electricity dataset by the Energy Information Administration (EIA) tracks daily demand and generation on the US power grid, with generation broken down by fuel type. The data covers 81 balancing authorities, starts in January 2019, and is delivered on a daily frequency. This dataset is created by processing the Form EIA-930 data that balancing authorities report to the EIA. Some balancing authorities do not report the fuel mix.

For more information about the US Electricity dataset, including CLI commands and pricing, see the [dataset listing](https://www.quantconnect.com/datasets/eia-us-electricity).

## About the Provider

The U.S. Energy Information Administration (EIA) is the statistical agency of the US Department of Energy, created by Congress in 1977. It collects and publishes energy data that is independent of policy. Policymakers, markets, and the public use its data to understand energy in the United States.

## Getting Started

The following snippet demonstrates how to request data from the US Electricity dataset:

```python
self.dataset_symbol = self.add_data(EIAElectricity, EIA.BalancingAuthorities.PJM, Resolution.DAILY).symbol
```

## Data Summary

The following table describes the dataset properties:

| Property | Value |
| --- | --- |
| Start Date | January 2019 |
| Asset Coverage | 81 US Balancing Authorities |
| Data Density | Regular |
| Resolution | Daily |
| Timezone | New York |

## Requesting Data

To add US Electricity data to your algorithm, call the `add_data` method. Save a reference to the dataset `Symbol` so you can access the data later in your algorithm.

```python
class EIAElectricityDataAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2020, 6, 1)
        self.set_end_date(2020, 9, 1)
        self.set_cash(100000)

        self._spy = self.add_equity("SPY", Resolution.DAILY).symbol
        self._pjm = self.add_data(EIAElectricity, EIA.BalancingAuthorities.PJM, Resolution.DAILY).symbol
```

## Accessing Data

To get the current US Electricity data, index the current [`Slice`](https://www.quantconnect.com/docs/v2/writing-algorithms/key-concepts/time-modeling/timeslices.md) with the dataset `Symbol`. `Slice` objects deliver unique events to your algorithm as they happen, but the `Slice` may not contain data for your dataset at every time step. To avoid issues, check if the `Slice` contains the data you want before you index it.

```python
def on_data(self, slice: Slice) -> None:
    if slice.contains_key(self._pjm):
        data_point = slice[self._pjm]
        self.log(f"{self._pjm} demand at {slice.time}: {data_point.demand}")
```

To iterate through all of the dataset objects in the current `Slice`, call the `get` method.

```python
def on_data(self, slice: Slice) -> None:
    for dataset_symbol, data_point in slice.get(EIAElectricity).items():
        self.log(f"{dataset_symbol} at {slice.time}: demand {data_point.demand}, net generation {data_point.net_generation}")
```

## Historical Data

To get historical US Electricity data, call the `history` method with the dataset `Symbol`. If there is no data in the period you request, the history result is empty.

```python
# DataFrame
history_df = self.history(self._pjm, 100, Resolution.DAILY)

# Dataset objects
history_bars = self.history[EIAElectricity](self._pjm, 100, Resolution.DAILY)
```

For more information about historical data, see [History Requests](https://www.quantconnect.com/docs/v2/writing-algorithms/historical-data/history-requests.md).

## Remove Subscriptions

To remove your subscription to US Electricity data, call the `remove_security` method.

```python
self.remove_security(self._pjm)
```

## Supported Balancing Authorities

The following table shows the accessor code you need to add each major balancing authority to your algorithm:

| Constant | Balancing Authority |
| --- | --- |
| `EIA.BalancingAuthorities.PJM` | PJM Interconnection, the largest balancing authority in the country |
| `EIA.BalancingAuthorities.ERCOT` | Electric Reliability Council of Texas |
| `EIA.BalancingAuthorities.CAISO` | California Independent System Operator |
| `EIA.BalancingAuthorities.MISO` | Midcontinent Independent System Operator |
| `EIA.BalancingAuthorities.NYISO` | New York Independent System Operator |
| `EIA.BalancingAuthorities.ISONE` | ISO New England |
| `EIA.BalancingAuthorities.SPP` | Southwest Power Pool |
| `EIA.BalancingAuthorities.BPA` | Bonneville Power Administration |

## Example Applications

The US Electricity dataset enables you to trade on the daily state of the US power grid. Examples include the following strategies:

- Trading utility stocks when actual demand runs above the day-ahead forecast
- Trading natural gas based on how much power comes from gas plants
- Trading coal and gas producers as their share of generation shifts

### Classic Algorithm Example

The following example algorithm buys SPY when PJM demand runs more than 2% above the day-ahead forecast. When demand undershoots the forecast by the same margin, it liquidates the position.

```python
from AlgorithmImports import *

class EIAElectricityDataAlgorithm(QCAlgorithm):
    def initialize(self):
        self.set_start_date(2020, 6, 1)
        self.set_end_date(2020, 9, 1)
        self.set_cash(100000)

        self._spy = self.add_equity("SPY", Resolution.DAILY).symbol
        self._pjm = self.add_data(EIAElectricity, EIA.BalancingAuthorities.PJM, Resolution.DAILY).symbol
        history = self.history(EIAElectricity, self._pjm, 60, Resolution.DAILY)

        self.debug(f"We got {len(history)} items from our history request")

    def on_data(self, slice):
        if not slice.contains_key(self._pjm):
            return

        grid = slice[self._pjm]
        if grid.demand is None or not grid.demand_forecast:
            return

        # Calculate the load surprise as a fraction of the forecast.
        load_surprise = (grid.demand - grid.demand_forecast) / grid.demand_forecast

        # Buy SPY when demand runs more than 2% above the forecast.
        if load_surprise > 0.02:
            self.set_holdings(self._spy, 1)

        # Liquidate when demand runs more than 2% below the forecast.
        elif load_surprise < -0.02:
            self.liquidate(self._spy)
```

### Framework Algorithm Example

The following example algorithm emits up insights for SPY when PJM demand runs more than 2% above the day-ahead forecast. When demand runs more than 2% below the forecast, it emits down insights.

```python
from AlgorithmImports import *

class EIAElectricityFrameworkAlgorithm(QCAlgorithm):
    def initialize(self):
        self.set_start_date(2020, 6, 1)
        self.set_end_date(2020, 9, 1)
        self.set_cash(100000)

        self.universe_settings.resolution = Resolution.DAILY
        # Select SPY to trade.
        symbols = [Symbol.create("SPY", SecurityType.EQUITY, Market.USA)]
        self.set_universe_selection(ManualUniverseSelectionModel(symbols))
        # Add a custom alpha model that emits insights from PJM grid operations.
        self.add_alpha(EIAElectricityAlphaModel(self))
        # Equally invest to dissipate non-systematic capital concentration risk on individual stock
        self.set_portfolio_construction(EqualWeightingPortfolioConstructionModel())

class EIAElectricityAlphaModel(AlphaModel):
    def __init__(self, algorithm):
        # Add the PJM grid operations data.
        self._pjm = algorithm.add_data(EIAElectricity, EIA.BalancingAuthorities.PJM, Resolution.DAILY).symbol
        self._tradable_symbols = []

        history = algorithm.history(EIAElectricity, self._pjm, 60, Resolution.DAILY)
        algorithm.log(f"We got {len(history)} items from our history request")

    def update(self, algorithm, slice):
        insights = []
        if not slice.contains_key(self._pjm):
            return insights

        grid = slice[self._pjm]
        if grid.demand is None or not grid.demand_forecast:
            return insights

        # Calculate the load surprise as a fraction of the forecast.
        load_surprise = (grid.demand - grid.demand_forecast) / grid.demand_forecast

        # Emit insights in the direction of the load surprise when it exceeds 2%.
        if abs(load_surprise) > 0.02:
            direction = InsightDirection.UP if load_surprise > 0 else InsightDirection.DOWN
            insights = [Insight.price(symbol, timedelta(days=7), direction) for symbol in self._tradable_symbols]

        return insights

    def on_securities_changed(self, algorithm, changes):
        for security in changes.added_securities:
            if security.symbol != self._pjm:
                self._tradable_symbols.append(security.symbol)
        for security in changes.removed_securities:
            if security.symbol in self._tradable_symbols:
                self._tradable_symbols.remove(security.symbol)
```

## Data Point Attributes

The US Electricity dataset provides `EIAElectricity` objects, which have the following attributes:

*Members of `QuantConnect.DataSource.EIAElectricity` (JSON): https://www.quantconnect.com/services/inspector?type=T:QuantConnect.DataSource.EIAElectricity&language=python*
