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
- Configure Lean (either locally or CLI-based) to use the Alpaca brokerage. I used the cloud version.
- Initialize any basic algorithm (e.g.,
BasicTemplateAlgorithm.py or BasicTemplateAlgorithm.cs).
- 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
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
JsonSerializationExceptionis thrown: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_traderproperty (along with other PDT-specific fields likedaytrade_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.MarketsSDK integrations within Lean) to treat thepattern_day_traderfield 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
BasicTemplateAlgorithm.pyorBasicTemplateAlgorithm.cs).System Information
Checklist
masterbranch