Lean icon indicating copy to clipboard operation
Lean copied to clipboard

Free margin and insufficient buying power errors on clean install with cash account

Open tommedema opened this issue 10 months ago • 0 comments

Expected Behavior

  • Since a cash account is defined in the basic algorithm below, there should be no trades when there isn't any buying power
  • There should be no free margin

Actual Behavior

  • Error is returned showing both trades even though there is no buying power AND free margin:
(venv) anonymous@anonymous-MacBook-Air world_code_framework % python -m unittest backtest.tests.test_backtest.TestFinanceWorld.test_basic_algorithm
2025-03-19 18:07:47,028 - backtest.backtest - DEBUG - Running backtest for agent basic_test
2025-03-19 18:07:47,028 - backtest.backtest - DEBUG - Wrote agent code to /var/folders/p5/dn689sh14rz52w3q5c1pm_800000gn/T/tmpkpb1_7e8/agents/agent_basic_test.py
2025-03-19 18:07:47,028 - backtest.backtest - DEBUG - Wrote config to /var/folders/p5/dn689sh14rz52w3q5c1pm_800000gn/T/tmpkpb1_7e8/agents/agent_basic_test.json
2025-03-19 18:07:47,028 - backtest.backtest - INFO - Running Lean in /Users/anonymous/Code/world_code_framework/backtest/../Lean/Launcher/bin/Debug with config /var/folders/p5/dn689sh14rz52w3q5c1pm_800000gn/T/tmpkpb1_7e8/agents/agent_basic_test.json
2025-03-19 18:07:52,490 - backtest.backtest - DEBUG - Subprocess stdout: 20250320 01:07:47.218 TRACE:: Using /var/folders/p5/dn689sh14rz52w3q5c1pm_800000gn/T/tmpkpb1_7e8/agents/agent_basic_test.json as configuration file
20250320 01:07:47.297 TRACE:: Config.Get(): Configuration key not found. Key: version-id - Using default value: 
20250320 01:07:47.297 TRACE:: Config.Get(): Configuration key not found. Key: cache-location - Using default value: 
20250320 01:07:47.297 TRACE:: Config.GetValue(): live-mode - Using default value: False
20250320 01:07:47.297 TRACE:: Config.GetValue(): job-user-id - Using default value: 0
20250320 01:07:47.297 TRACE:: Config.GetValue(): project-id - Using default value: 0
20250320 01:07:47.297 TRACE:: Config.Get(): Configuration key not found. Key: api-access-token - Using default value: 
20250320 01:07:47.297 TRACE:: Config.Get(): Configuration key not found. Key: job-organization-id - Using default value: 
20250320 01:07:47.297 TRACE:: Config.Get(): Configuration key not found. Key: api-url - Using default value: https://www.quantconnect.com/api/v2/
20250320 01:07:47.299 TRACE:: Config.Get(): Configuration key not found. Key: plugin-directory - Using default value: 
20250320 01:07:47.302 TRACE:: Config.Get(): Configuration key not found. Key: composer-dll-directory - Using default value: 
20250320 01:07:47.302 TRACE:: Composer(): Loading Assemblies from /Users/anonymous/Code/world_code_framework/Lean/Launcher/bin/Debug/
20250320 01:07:47.326 TRACE:: Python for .NET Assembly: Python.Runtime, Version=2.0.42.0, Culture=neutral, PublicKeyToken=5000fea6cba702dd
20250320 01:07:47.346 TRACE:: Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v2.5.0.0 Mode: DEBUG (64bit) Host: anonymous-MacBook-Air
20250320 01:07:47.353 TRACE:: Engine.Main(): Started 6:07 PM
20250320 01:07:47.353 TRACE:: Config.Get(): Configuration key not found. Key: algorithm-type-name - Using default value: 
20250320 01:07:47.361 TRACE:: Config.Get(): Configuration key not found. Key: lean-manager-type - Using default value: LocalLeanManager
20250320 01:07:47.375 TRACE:: JobQueue.NextJob(): Selected /var/folders/p5/dn689sh14rz52w3q5c1pm_800000gn/T/tmpkpb1_7e8/agents/agent_basic_test.py
20250320 01:07:47.375 TRACE:: Config.Get(): Configuration key not found. Key: parameters - Using default value: 
20250320 01:07:47.376 TRACE:: Config.GetValue(): scheduled-event-leaky-bucket-capacity - Using default value: 120
20250320 01:07:47.376 TRACE:: Config.GetValue(): scheduled-event-leaky-bucket-time-interval-minutes - Using default value: 1440
20250320 01:07:47.376 TRACE:: Config.GetValue(): scheduled-event-leaky-bucket-refill-amount - Using default value: 18
20250320 01:07:47.376 TRACE:: Config.GetValue(): symbol-minute-limit - Using default value: 10000
20250320 01:07:47.376 TRACE:: Config.GetValue(): symbol-second-limit - Using default value: 10000
20250320 01:07:47.376 TRACE:: Config.GetValue(): symbol-tick-limit - Using default value: 10000
20250320 01:07:47.376 TRACE:: Config.GetValue(): maximum-data-points-per-chart-series - Using default value: 1000000
20250320 01:07:47.376 TRACE:: Config.GetValue(): maximum-chart-series - Using default value: 30
20250320 01:07:47.376 TRACE:: Config.GetValue(): storage-limit - Using default value: 10737418240
20250320 01:07:47.376 TRACE:: Config.GetValue(): storage-file-count - Using default value: 10000
20250320 01:07:47.376 TRACE:: Config.GetValue(): storage-permissions - Using default value: 3
20250320 01:07:47.376 TRACE:: Config.Get(): Configuration key not found. Key: algorithm-id - Using default value: 
20250320 01:07:47.376 TRACE:: Config.Get(): Configuration key not found. Key: optimization-id - Using default value: 
20250320 01:07:47.376 TRACE:: Config.Get(): Configuration key not found. Key: backtest-name - Using default value: local
20250320 01:07:47.378 TRACE:: Config.Get(): Configuration key not found. Key: data-channel - Using default value: 
20250320 01:07:47.378 TRACE:: Config.Get(): Configuration key not found. Key: out-of-sample-max-end-date - Using default value: 
20250320 01:07:47.378 TRACE:: Config.GetValue(): out-of-sample-days - Using default value: 0
20250320 01:07:47.378 TRACE:: Config.Get(): Configuration key not found. Key: map-file-provider - Using default value: LocalDiskMapFileProvider
20250320 01:07:47.378 TRACE:: Config.Get(): Configuration key not found. Key: factor-file-provider - Using default value: LocalDiskFactorFileProvider
20250320 01:07:47.378 TRACE:: Config.Get(): Configuration key not found. Key: data-provider - Using default value: DefaultDataProvider
20250320 01:07:47.378 TRACE:: Config.Get(): Configuration key not found. Key: object-store - Using default value: LocalObjectStore
20250320 01:07:47.378 TRACE:: Config.Get(): Configuration key not found. Key: data-permission-manager - Using default value: DataPermissionManager
20250320 01:07:47.389 TRACE:: Config.GetValue(): zip-data-cache-provider - Using default value: 10
20250320 01:07:47.391 TRACE:: Config.Get(): Configuration key not found. Key: fundamental-data-provider - Using default value: CoarseFundamentalDataProvider
20250320 01:07:47.392 TRACE:: PythonIntializer.ActivatePythonVirtualEnvironment(): virtual env '/Users/anonymous/Code/world_code_framework/backtest/../venv'. Will use system packages: False
20250320 01:07:47.405 TRACE:: AlgorithmManager.CreateTokenBucket(): Initializing LeakyBucket: Capacity: 120 RefillAmount: 18 TimeInterval: 1440
20250320 01:07:47.405 TRACE:: Config.GetValue(): algorithm-manager-time-loop-maximum - Using default value: 20
20250320 01:07:47.409 TRACE:: Engine.Run(): Resource limits '0' CPUs. 2147483647 MB RAM.
20250320 01:07:47.410 TRACE:: TextSubscriptionDataSourceReader.SetCacheSize(): Setting cache size to 71582788 items
20250320 01:07:47.436 TRACE:: Config.GetValue(): downloader-data-update-period - Using default value: 7
20250320 01:07:47.754 TRACE:: Config.GetValue(): force-exchange-always-open - Using default value: False
20250320 01:07:47.755 TRACE:: Config.GetValue(): algorithm-creation-timeout - Using default value: 90
20250320 01:07:47.756 TRACE:: PythonInitializer.Initialize(): start...
PythonEngine.Initialize(): clr GetManifestResourceStream...
20250320 01:07:48.330 TRACE:: Config.GetValue(): python-additional-paths - Using default value: System.String[]
20250320 01:07:48.366 TRACE:: PythonInitializer.Initialize(): ended
20250320 01:07:48.367 TRACE:: AlgorithmPythonWrapper(): Python version 3.11.7 (main, Dec  4 2023, 18:10:11) [Clang 15.0.0 (clang-1500.1.0.2.5)]: Importing python module agent_basic_test
20250320 01:07:49.753 TRACE:: AlgorithmPythonWrapper(): agent_basic_test successfully imported.
20250320 01:07:49.758 TRACE:: AlgorithmPythonWrapper(): Creating IAlgorithm instance.
20250320 01:07:49.763 TRACE:: Config.Get(): Configuration key not found. Key: databases-refresh-period - Using default value: 1.00:00:00
20250320 01:07:49.834 TRACE:: Config.GetValue(): mute-python-library-logging - Using default value: True
20250320 01:07:49.835 TRACE:: Config.Get(): Configuration key not found. Key: object-store-root - Using default value: ./storage
20250320 01:07:49.837 TRACE:: LocalObjectStore.Initialize(): Storage Root: /Users/anonymous/Code/world_code_framework/Lean/Launcher/bin/Debug/storage. StorageFileCount 10000. StorageLimit 10240MB
20250320 01:07:49.840 TRACE:: Config.GetValue(): zip-data-cache-provider - Using default value: 10
20250320 01:07:49.842 TRACE:: HistoryProviderManager.Initialize(): history providers [SubscriptionDataReaderHistoryProvider]
20250320 01:07:49.844 TRACE:: BaseSetupHandler.Setup(LocalPlatform): UID: 0, PID: 0, Version: 2.5.0.0, Source: WebIDE
20250320 01:07:49.845 TRACE:: Config.Get(): Configuration key not found. Key: security-data-feeds - Using default value: 
20250320 01:07:49.853 TRACE:: Config.Get(): Configuration key not found. Key: reserved-words-prefix - Using default value: @
20250320 01:07:49.890 TRACE:: BaseSetupHandler.SetupCurrencyConversions():
Account Type: Cash

Symbol      Quantity    Conversion = Value in USD
USD: $      100000.00 @       1.00 = $100000
-------------------------------------------------
CashBook Total Value:                $100000

20250320 01:07:49.891 TRACE:: Total margin information: TotalMarginUsed: 0.00, MarginRemaining: 100000.00
20250320 01:07:49.891 TRACE:: SetUp Backtesting: User: 0 ProjectId: 0 AlgoId: 
20250320 01:07:49.892 TRACE:: Dates: Start: 01/01/2020 End: 12/31/2020 Cash: ¤100,000.00 MaximumRuntime: 100.00:00:00 MaxOrders: 2147483647
20250320 01:07:49.893 TRACE:: BacktestingResultHandler(): Sample Period Set: 131.76
20250320 01:07:49.893 TRACE:: Config.GetValue(): forward-console-messages - Using default value: True
20250320 01:07:49.893 TRACE:: JOB HANDLERS:
         DataFeed:             QuantConnect.Lean.Engine.DataFeeds.FileSystemDataFeed
         Setup:                QuantConnect.Lean.Engine.Setup.BacktestingSetupHandler
         RealTime:             QuantConnect.Lean.Engine.RealTime.BacktestingRealTimeHandler
         Results:              QuantConnect.Lean.Engine.Results.BacktestingResultHandler
         Transactions:         QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler
         Object Store:         QuantConnect.Lean.Engine.Storage.LocalObjectStore
         History Provider:     QuantConnect.Lean.Engine.HistoricalData.HistoryProviderManager
         Brokerage:            QuantConnect.Brokerages.Backtesting.BacktestingBrokerage
         Data Provider:        QuantConnect.Lean.Engine.DataFeeds.DefaultDataProvider

20250320 01:07:49.900 TRACE:: Event Name "Daily Sampling", scheduled to run.
20250320 01:07:49.900 TRACE:: AlgorithmManager.Run(): Begin DataStream - Start: 1/1/2020 12:00:00 AM Stop: 12/31/2020 11:59:59 PM Time: 1/1/2020 12:00:00 AM Warmup: False
20250320 01:07:49.925 TRACE:: Config.GetValue(): data-feed-workers-count - Using default value: 8
20250320 01:07:49.925 TRACE:: Config.GetValue(): data-feed-max-work-weight - Using default value: 400
20250320 01:07:49.925 TRACE:: WeightedWorkScheduler(): will use 8 workers and MaxWorkWeight is 400
20250320 01:07:49.931 TRACE:: Debug: Launching analysis for  with LEAN Engine v2.5.0.0
20250320 01:07:50.084 TRACE:: Debug: Warning: when performing history requests, the start date will be adjusted if it is before the first known date for the symbol.
20250320 01:07:50.129 TRACE:: Config.GetValue(): portfolio-margin-series-count - Using default value: 5
20250320 01:07:50.135 TRACE:: Debug: Accurate daily end-times now enabled by default. See more at https://qnt.co/3YHaWHL. To disable it and use legacy daily bars set self.settings.daily_precise_end_time = False.
20250320 01:07:50.135 TRACE:: Debug: Warning: all market orders sent using daily data, or market orders sent after hours are automatically converted into MarketOnOpen orders.
20250320 01:07:50.135 TRACE:: Debug: Warning: Portfolio rebalance result ignored as it resulted in a single share trade recommendation which can generate high fees. To disable minimum order size checks please set Settings.MinimumOrderMarginPortfolioPercentage = 0.
20250320 01:07:50.251 TRACE:: Synchronizer.GetEnumerator(): Exited thread.
20250320 01:07:50.251 TRACE:: AlgorithmManager.Run(): Firing On End Of Algorithm...
20250320 01:07:50.251 TRACE:: Engine.Run(): Exiting Algorithm Manager
20250320 01:07:50.251 TRACE:: Config.Get(): Configuration key not found. Key: transaction-log - Using default value: 
20250320 01:07:50.253 TRACE:: StopSafely(): Waiting for 'Isolator Thread' thread to stop...
20250320 01:07:50.253 TRACE:: FileSystemDataFeed.Exit(): Start. Setting cancellation token...
20250320 01:07:50.254 TRACE:: FileSystemDataFeed.Exit(): Exit Finished.
20250320 01:07:50.254 TRACE:: BacktestingResultHandler.Exit(): starting...
20250320 01:07:50.254 TRACE:: BacktestingResultHandler.Exit(): Saving logs...
20250320 01:07:50.256 TRACE:: StopSafely(): Waiting for 'Result Thread' thread to stop...
20250320 01:07:50.256 TRACE:: Debug: Algorithm Id:() completed in 0.36 seconds at 11k data points per second. Processing total of 3,861 data points.
20250320 01:07:50.256 TRACE:: Debug: The starting dates for the following symbols have been adjusted to match their map files first date: [AAA, 2020-09-09]
20250320 01:07:50.256 TRACE:: Debug: Your log was successfully created and can be retrieved from: /var/folders/p5/dn689sh14rz52w3q5c1pm_800000gn/T/tmpkpb1_7e8/results/-log.txt
20250320 01:07:50.256 TRACE:: BacktestingResultHandler.Run(): Ending Thread...
20250320 01:07:50.457 TRACE:: 
STATISTICS:: Total Orders 1002
STATISTICS:: Average Win 0.48%
STATISTICS:: Average Loss -0.39%
STATISTICS:: Compounding Annual Return -15.355%
STATISTICS:: Drawdown 40.500%
STATISTICS:: Expectancy -0.130
STATISTICS:: Start Equity 100000
STATISTICS:: End Equity 84619.26
STATISTICS:: Net Profit -15.381%
STATISTICS:: Sharpe Ratio -0.225
STATISTICS:: Sortino Ratio -0.208
STATISTICS:: Probabilistic Sharpe Ratio 7.133%
STATISTICS:: Loss Rate 61%
STATISTICS:: Win Rate 39%
STATISTICS:: Profit-Loss Ratio 1.23
STATISTICS:: Alpha 0
STATISTICS:: Beta 0
STATISTICS:: Annual Standard Deviation 0.312
STATISTICS:: Annual Variance 0.097
STATISTICS:: Information Ratio -0.205
STATISTICS:: Tracking Error 0.312
STATISTICS:: Treynor Ratio 0
STATISTICS:: Total Fees $669.72
STATISTICS:: Estimated Strategy Capacity $1700000.00
STATISTICS:: Lowest Capacity Asset AAA XHPVXIJUWS4L
STATISTICS:: Portfolio Turnover 10.19%
STATISTICS:: OrderListHash 66338e71b3820d3fc99cdbd140bf0ea3
20250320 01:07:50.457 TRACE:: BacktestingResultHandler.SendAnalysisResult(): Processed final packet
20250320 01:07:50.457 TRACE:: Engine.Run(): Disconnecting from brokerage...
20250320 01:07:50.457 TRACE:: Engine.Run(): Disposing of setup handler...
20250320 01:07:50.457 TRACE:: Engine.Main(): Analysis Completed and Results Posted.
20250320 01:07:50.457 TRACE:: StopSafely(): Waiting for '' thread to stop...
20250320 01:07:50.458 TRACE:: DataMonitor.GenerateReport():
DATA USAGE:: Total data requests 16
DATA USAGE:: Succeeded data requests 16
DATA USAGE:: Failed data requests 0
DATA USAGE:: Failed data requests percentage 0%
DATA USAGE:: Total universe data requests 0
DATA USAGE:: Succeeded universe data requests 0
DATA USAGE:: Failed universe data requests 0
DATA USAGE:: Failed universe data requests percentage 0%
Engine.Main(): Analysis Complete.
20250320 01:07:50.460 TRACE:: Engine.Main(): Packet removed from queue: 
20250320 01:07:50.460 TRACE:: LeanEngineSystemHandlers.Dispose(): start...
20250320 01:07:50.460 TRACE:: LeanEngineSystemHandlers.Dispose(): Disposed of system handlers.
20250320 01:07:50.460 TRACE:: LeanEngineAlgorithmHandlers.Dispose(): start...
20250320 01:07:50.460 TRACE:: LeanEngineAlgorithmHandlers.Dispose(): Disposed of algorithm handlers.
20250320 01:07:50.461 TRACE:: StopSafely(): Waiting for 'CpuPerformance' thread to stop...
20250320 01:07:50.461 TRACE:: PythonInitializer.Shutdown(): start
20250320 01:07:52.475 TRACE:: PythonInitializer.Shutdown(): ended
20250320 01:07:52.475 TRACE:: Program.Main(): Exiting Lean...

2025-03-19 18:07:52,491 - backtest.backtest - DEBUG - Subprocess stderr: 20250320 01:07:50.135 ERROR:: Order Error: ids: [2], Insufficient buying power to complete orders (Value:[-50013.6]), Reason: Id: 2, Initial Margin: -50014.6, Free Margin: 0.
20250320 01:07:50.135 ERROR:: Order Error: ids: [4], Insufficient buying power to complete orders (Value:[-33198.59]), Reason: Id: 4, Initial Margin: -33202.2855715, Free Margin: 0.
20250320 01:07:50.135 ERROR:: Order Error: ids: [5], Insufficient buying power to complete orders (Value:[-32866.08]), Reason: Id: 5, Initial Margin: -32867.08, Free Margin: 0.
20250320 01:07:50.135 ERROR:: Order Error: ids: [7], Insufficient buying power to complete orders (Value:[-24831.09]), Reason: Id: 7, Initial Margin: -24833.81375875, Free Margin: 16559.86.
20250320 01:07:50.135 ERROR:: Order Error: ids: [8], Insufficient buying power to complete orders (Value:[-24632.01]), Reason: Id: 8, Initial Margin: -24633.01, Free Margin: 16559.86.
20250320 01:07:50.135 ERROR:: Order Error: ids: [9], Insufficient buying power to complete orders (Value:[-24480.51]), Reason: Id: 9, Initial Margin: -24481.51, Free Margin: 16559.86.
20250320 01:07:50.135 ERROR:: Order Error: ids: [12], Insufficient buying power to complete orders (Value:[-24466.4]), Reason: Id: 12, Initial Margin: -24467.4, Free Margin: 0.
20250320 01:07:50.135 ERROR:: Order Error: ids: [16], Insufficient buying power to complete orders (Value:[-19764.87]), Reason: Id: 16, Initial Margin: -19765.87125508, Free Margin: 0.
20250320 01:07:50.135 ERROR:: Order Error: ids: [17], Insufficient buying power to complete orders (Value:[-18709.6]), Reason: Id: 17, Initial Margin: -18710.6, Free Margin: 0.
20250320 01:07:50.135 ERROR:: Order Error: ids: [19], Insufficient buying power to complete orders (Value:[-19686.28]), Reason: Id: 19, Initial Margin: -19687.281771778, Free Margin: 0.
20250320 01:07:50.135 ERROR:: Order Error: ids: [20], Insufficient buying power to complete orders (Value:[-18852.08]), Reason: Id: 20, Initial Margin: -18853.08, Free Margin: 0.
20250320 01:07:50.135 ERROR:: Order Error: ids: [23], Insufficient buying power to complete orders (Value:[-19269.25]), Reason: Id: 23, Initial Margin: -19270.25, Free Margin: 2107.156633841.
20250320 01:07:50.135 ERROR:: Order Error: ids: [24], Insufficient buying power to complete orders (Value:[-19269.25]), Reason: Id: 24, Initial Margin: -19270.25, Free Margin: 2107.156633841.
20250320 01:07:50.186 ERROR:: Order Error: ids: [31], Insufficient buying power to complete orders (Value:[-16459.87]), Reason: Id: 31, Initial Margin: -16460.874635742, Free Margin: 0.
20250320 01:07:50.186 ERROR:: Order Error: ids: [32], Insufficient buying power to complete orders (Value:[-16325.87]), Reason: Id: 32, Initial Margin: -16326.87, Free Margin: 0.
20250320 01:07:50.186 ERROR:: Order Error: ids: [34], Insufficient buying power to complete orders (Value:[-16127.87]), Reason: Id: 34, Initial Margin: -16128.87, Free Margin: 3228.314711329.
20250320 01:07:50.186 ERROR:: Order Error: ids: [43], Insufficient buying power to complete orders (Value:[14264.64]), Reason: Id: 43, Initial Margin: 14265.64, Free Margin: 2025.029774583.
20250320 01:07:50.186 ERROR:: Order Error: ids: [44], Insufficient buying power to complete orders (Value:[-13195.53]), Reason: Id: 44, Initial Margin: -13196.53, Free Margin: 2025.029774583.
20250320 01:07:50.186 ERROR:: Order Error: ids: [67], Insufficient buying power to complete orders (Value:[-11157.64]), Reason: Id: 67, Initial Margin: -11158.640238259, Free Margin: 0.
20250320 01:07:50.186 ERROR:: Order Error: ids: [69], Insufficient buying power to complete orders (Value:[-807.64]), Reason: Id: 69, Initial Margin: -808.64, Free Margin: 0.
20250320 01:07:50.186 ERROR:: Order Error: ids: [70], Insufficient buying power to complete orders (Value:[-269.4777]), Reason: Id: 70, Initial Margin: -270.47770726, Free Margin: 0.
20250320 01:07:50.186 ERROR:: Order Error: ids: [71], Insufficient buying power to complete orders (Value:[-248.6307]), Reason: Id: 71, Initial Margin: -249.63070465, Free Margin: 0.
20250320 01:07:50.186 ERROR:: Order Error: ids: [72], Insufficient buying power to complete orders (Value:[218.28]), Reason: Id: 72, Initial Margin: 219.28, Free Margin: 0.
... truncated ...
20250320 01:07:50.256 ERROR:: Order Error: ids: [921], Insufficient buying power to complete orders (Value:[-2867.4]), Reason: Id: 921, Initial Margin: -2868.58, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [925], Insufficient buying power to complete orders (Value:[-2555.7]), Reason: Id: 925, Initial Margin: -2556.75, Free Margin: 764.997287661.
20250320 01:07:50.256 ERROR:: Order Error: ids: [926], Insufficient buying power to complete orders (Value:[-2436.732]), Reason: Id: 926, Initial Margin: -2437.7320341, Free Margin: 764.997287661.
20250320 01:07:50.256 ERROR:: Order Error: ids: [927], Insufficient buying power to complete orders (Value:[-2738.25]), Reason: Id: 927, Initial Margin: -2739.375, Free Margin: 764.997287661.
20250320 01:07:50.256 ERROR:: Order Error: ids: [928], Insufficient buying power to complete orders (Value:[-2624.173]), Reason: Id: 928, Initial Margin: -2625.1729598, Free Margin: 764.997287661.
20250320 01:07:50.256 ERROR:: Order Error: ids: [930], Insufficient buying power to complete orders (Value:[-100.6991]), Reason: Id: 930, Initial Margin: -101.699138756, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [933], Insufficient buying power to complete orders (Value:[-21821.42]), Reason: Id: 933, Initial Margin: -21822.424861585, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [934], Insufficient buying power to complete orders (Value:[-7032.96]), Reason: Id: 934, Initial Margin: -7035.84, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [935], Insufficient buying power to complete orders (Value:[-4430.762]), Reason: Id: 935, Initial Margin: -4431.762105264, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [936], Insufficient buying power to complete orders (Value:[-4408.8]), Reason: Id: 936, Initial Margin: -4409.8, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [938], Insufficient buying power to complete orders (Value:[-7222]), Reason: Id: 938, Initial Margin: -7224.875, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [939], Insufficient buying power to complete orders (Value:[-4600.725]), Reason: Id: 939, Initial Margin: -4601.725306632, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [940], Insufficient buying power to complete orders (Value:[-4385.5]), Reason: Id: 940, Initial Margin: -4386.5, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [942], Insufficient buying power to complete orders (Value:[-6355.36]), Reason: Id: 942, Initial Margin: -6357.89, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [943], Insufficient buying power to complete orders (Value:[-4159.96]), Reason: Id: 943, Initial Margin: -4160.96, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [944], Insufficient buying power to complete orders (Value:[-4095.151]), Reason: Id: 944, Initial Margin: -4096.151097112, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [945], Insufficient buying power to complete orders (Value:[-6355.36]), Reason: Id: 945, Initial Margin: -6357.89, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [946], Insufficient buying power to complete orders (Value:[-4159.96]), Reason: Id: 946, Initial Margin: -4160.96, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [947], Insufficient buying power to complete orders (Value:[-4095.151]), Reason: Id: 947, Initial Margin: -4096.151097112, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [948], Insufficient buying power to complete orders (Value:[-6514.56]), Reason: Id: 948, Initial Margin: -6517.17, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [949], Insufficient buying power to complete orders (Value:[-4317.295]), Reason: Id: 949, Initial Margin: -4318.294941248, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [950], Insufficient buying power to complete orders (Value:[-4235.14]), Reason: Id: 950, Initial Margin: -4236.14, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [951], Insufficient buying power to complete orders (Value:[-6514.56]), Reason: Id: 951, Initial Margin: -6517.17, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [952], Insufficient buying power to complete orders (Value:[-4317.295]), Reason: Id: 952, Initial Margin: -4318.294941248, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [953], Insufficient buying power to complete orders (Value:[-4235.14]), Reason: Id: 953, Initial Margin: -4236.14, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [955], Insufficient buying power to complete orders (Value:[-4341.28]), Reason: Id: 955, Initial Margin: -4342.28, Free Margin: 1017.97783718.
20250320 01:07:50.256 ERROR:: Order Error: ids: [956], Insufficient buying power to complete orders (Value:[-4235.14]), Reason: Id: 956, Initial Margin: -4236.14, Free Margin: 1017.97783718.
20250320 01:07:50.256 ERROR:: Order Error: ids: [961], Insufficient buying power to complete orders (Value:[-17148.58]), Reason: Id: 961, Initial Margin: -17151.215, Free Margin: 1017.97783718.
20250320 01:07:50.256 ERROR:: Order Error: ids: [963], Insufficient buying power to complete orders (Value:[-21379.22]), Reason: Id: 963, Initial Margin: -21382.485, Free Margin: 10259.191025035.
20250320 01:07:50.256 ERROR:: Order Error: ids: [970], Insufficient buying power to complete orders (Value:[-21288.72]), Reason: Id: 970, Initial Margin: -21291.93, Free Margin: 8692.932133475.
20250320 01:07:50.256 ERROR:: Order Error: ids: [971], Insufficient buying power to complete orders (Value:[-21288.72]), Reason: Id: 971, Initial Margin: -21291.93, Free Margin: 8692.932133475.
20250320 01:07:50.256 ERROR:: Order Error: ids: [973], Insufficient buying power to complete orders (Value:[-21226.32]), Reason: Id: 973, Initial Margin: -21229.49, Free Margin: 8928.467394035.
20250320 01:07:50.256 ERROR:: Order Error: ids: [974], Insufficient buying power to complete orders (Value:[-21226.32]), Reason: Id: 974, Initial Margin: -21229.49, Free Margin: 8928.467394035.
20250320 01:07:50.256 ERROR:: Order Error: ids: [976], Insufficient buying power to complete orders (Value:[-1135.68]), Reason: Id: 976, Initial Margin: -1136.68, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [977], Insufficient buying power to complete orders (Value:[-1123.15]), Reason: Id: 977, Initial Margin: -1124.15, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [978], Insufficient buying power to complete orders (Value:[-309.3696]), Reason: Id: 978, Initial Margin: -310.369578496, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [979], Insufficient buying power to complete orders (Value:[-175.56]), Reason: Id: 979, Initial Margin: -176.56, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [981], Insufficient buying power to complete orders (Value:[-524]), Reason: Id: 981, Initial Margin: -525, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [982], Insufficient buying power to complete orders (Value:[-497.25]), Reason: Id: 982, Initial Margin: -498.25, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [983], Insufficient buying power to complete orders (Value:[-307.8115]), Reason: Id: 983, Initial Margin: -308.81154472, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [985], Insufficient buying power to complete orders (Value:[-494.91]), Reason: Id: 985, Initial Margin: -495.91, Free Margin: 375.128271317.
20250320 01:07:50.256 ERROR:: Order Error: ids: [991], Insufficient buying power to complete orders (Value:[-16924.27]), Reason: Id: 991, Initial Margin: -16925.269561524, Free Margin: 0.
20250320 01:07:50.256 ERROR:: Order Error: ids: [994], Insufficient buying power to complete orders (Value:[-16833.6]), Reason: Id: 994, Initial Margin: -16834.595059125, Free Margin: 16086.874718937.
20250320 01:07:50.256 ERROR:: Order Error: ids: [996], Insufficient buying power to complete orders (Value:[-21096.24]), Reason: Id: 996, Initial Margin: -21097.235269304, Free Margin: 16873.702444645.
20250320 01:07:50.256 ERROR:: Order Error: ids: [1002], Insufficient buying power to complete orders (Value:[-21066.23]), Reason: Id: 1002, Initial Margin: -21067.230106909, Free Margin: 8771.542802613.

Potential Solution

Reproducing the Problem

from AlgorithmImports import *

class StudentStrategy(QCAlgorithm):
    def Initialize(self) -> None:
        self.set_start_date(2020, 1, 1)
        self.set_end_date(2020, 12, 31)
        self.set_cash(100000)

        # Use all symbols we have data for from the lean sample data
        self.symbols = [
            Symbol.Create("SPY", SecurityType.EQUITY, Market.USA),
            Symbol.Create("AAPL", SecurityType.EQUITY, Market.USA),
            Symbol.Create("IBM", SecurityType.EQUITY, Market.USA),
            Symbol.Create("WMI", SecurityType.EQUITY, Market.USA),
            Symbol.Create("IWM", SecurityType.EQUITY, Market.USA),
            Symbol.Create("NWSA", SecurityType.EQUITY, Market.USA),
            Symbol.Create("QQQ", SecurityType.EQUITY, Market.USA),
            Symbol.Create("USO", SecurityType.EQUITY, Market.USA),
            Symbol.Create("UW", SecurityType.EQUITY, Market.USA),
            Symbol.Create("WM", SecurityType.EQUITY, Market.USA),
            Symbol.Create("BNO", SecurityType.EQUITY, Market.USA),
            Symbol.Create("EEM", SecurityType.EQUITY, Market.USA),
            Symbol.Create("FB", SecurityType.EQUITY, Market.USA),
            Symbol.Create("FOXA", SecurityType.EQUITY, Market.USA),
            Symbol.Create("GOOAV", SecurityType.EQUITY, Market.USA),
            Symbol.Create("GOOCV", SecurityType.EQUITY, Market.USA),
            Symbol.Create("GOOG", SecurityType.EQUITY, Market.USA),
            Symbol.Create("GOOGL", SecurityType.EQUITY, Market.USA),
            Symbol.Create("AAA", SecurityType.EQUITY, Market.USA),
            Symbol.Create("AIG", SecurityType.EQUITY, Market.USA),
            Symbol.Create("BAC", SecurityType.EQUITY, Market.USA)
        ]

        # Use ManualUniverseSelectionModel for our available symbols
        self.set_universe_selection(ManualUniverseSelectionModel(self.symbols))
        self.universe_settings.resolution = Resolution.DAILY
        
        # Use the standard framework components
        self.add_alpha(RsiAlphaModel())
        self.set_portfolio_construction(EqualWeightingPortfolioConstructionModel())
        
        self.set_brokerage_model(BrokerageName.INTERACTIVE_BROKERS_BROKERAGE, AccountType.CASH)

System Information

  • MacOS 14.6.1 (23G93)
  • lean 1.0.217

Checklist

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

tommedema avatar Mar 20 '25 01:03 tommedema