Skip to content

Pattern Day Trader (Alpaca Support) #9588

Description

@tolusophy

Expected Behavior

When initializing a live or paper trading algorithm using the Alpaca brokerage integration, the LEAN engine should successfully connect to the Alpaca API, fetch the account's cash balance and details, and start the algorithm.

Actual Behavior

During algorithm initialization, a JsonSerializationException is thrown:

During the algorithm initialization, the following exception has occurred: Error getting cash balance from brokerage: Required property 'pattern_day_trader' not found in JSON. Path '', line 1, position 944.Required property 'pattern_day_trader' not found in JSON. Path '', line 1, position 944. in HttpResponseMethodExtensions.cs:line 22 Required property 'pattern_day_trader' not found in JSON. Path '', line 1, position 944.

Potential Solution

Alpaca has recently updated its API to align with FINRA retiring the legacy Pattern Day Trader (PDT) rules in favor of a new intraday margin framework. As part of this update, the pattern_day_trader property (along with other PDT-specific fields like daytrade_count, etc.) has been deprecated and completely removed from the JSON payload returned by Alpaca's account API endpoint.

The solution is to update the Alpaca brokerage deserialization model (or internal HTTP response handlers in Alpaca.Markets SDK integrations within Lean) to treat the pattern_day_trader field as optional or nullable (or remove the requirement/property altogether) so it does not throw an exception when the field is missing.

Reproducing the Problem

  1. Configure Lean (either locally or CLI-based) to use the Alpaca brokerage. I used the cloud version.
  2. Initialize any basic algorithm (e.g., BasicTemplateAlgorithm.py or BasicTemplateAlgorithm.cs).
  3. Attempt to run or deploy the algorithm. During initialization/connection to the brokerage, the JSON deserialization error will trigger.

System Information

  • OS: macOS
  • Brokerage: Alpaca (Live/Paper API v2)
  • Language: Python / C#

Checklist

  • I have completely filled out this template
  • I have confirmed that this issue exists on the current master branch
  • I have confirmed that this is not a duplicate issue by searching issues
  • I have provided detailed steps to reproduce the issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions