Overall Statistics |
Total Orders 129 Average Win 0% Average Loss 0% Compounding Annual Return -4.711% Drawdown 13.700% Expectancy 0 Start Equity 100000 End Equity 95305.47 Net Profit -4.695% Sharpe Ratio -0.357 Sortino Ratio -0.458 Probabilistic Sharpe Ratio 7.465% Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0.028 Beta 0.551 Annual Standard Deviation 0.123 Annual Variance 0.015 Information Ratio 0.822 Tracking Error 0.105 Treynor Ratio -0.08 Total Fees $129.00 Estimated Strategy Capacity $60000000.00 Lowest Capacity Asset SPY R735QTJ8XC9X Portfolio Turnover 0.29% |
#region imports using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Globalization; using System.Drawing; using QuantConnect; using QuantConnect.Algorithm.Framework; using QuantConnect.Algorithm.Framework.Selection; using QuantConnect.Algorithm.Framework.Alphas; using QuantConnect.Algorithm.Framework.Portfolio; using QuantConnect.Algorithm.Framework.Execution; using QuantConnect.Algorithm.Framework.Risk; using QuantConnect.Algorithm.Selection; using QuantConnect.Parameters; using QuantConnect.Benchmarks; using QuantConnect.Brokerages; using QuantConnect.Util; using QuantConnect.Interfaces; using QuantConnect.Algorithm; using QuantConnect.Indicators; using QuantConnect.Data; using QuantConnect.Data.Consolidators; using QuantConnect.Data.Custom; using QuantConnect.DataSource; using QuantConnect.Data.Fundamental; using QuantConnect.Data.Market; using QuantConnect.Data.UniverseSelection; using QuantConnect.Notifications; using QuantConnect.Orders; using QuantConnect.Orders.Fees; using QuantConnect.Orders.Fills; using QuantConnect.Orders.Slippage; using QuantConnect.Scheduling; using QuantConnect.Securities; using QuantConnect.Securities.Equity; using QuantConnect.Securities.Future; using QuantConnect.Securities.Option; using QuantConnect.Securities.Forex; using QuantConnect.Securities.Crypto; using QuantConnect.Securities.Interfaces; using QuantConnect.Storage; using QCAlgorithmFramework = QuantConnect.Algorithm.QCAlgorithm; using QCAlgorithmFrameworkBridge = QuantConnect.Algorithm.QCAlgorithm; #endregion namespace QuantConnect.Algorithm.CSharp { public class SPYInvestmentAlgorithm : QCAlgorithm { private Symbol _spy; private decimal _investmentAmount = 1000m; public override void Initialize() { SetStartDate(2022, 1, 1); SetEndDate(2022, 12, 31); SetCash(100000); _spy = AddEquity("SPY", Resolution.Minute).Symbol; // Set a warm-up period to ensure we have sufficient historical data SetWarmUp(TimeSpan.FromDays(1)); Schedule.On(DateRules.EveryDay(), TimeRules.At(15, 55), CheckAndInvest); } private void CheckAndInvest() { if (IsWarmingUp) return; var history = History(_spy, 2, Resolution.Daily).ToList(); if (history.Count < 2) return; var closeYesterday = history[0].Close; var currentPrice = Securities[_spy].Price; // Check if prices are valid before calculating change if (closeYesterday > 0 && currentPrice > 0) { var change = (currentPrice - closeYesterday) / closeYesterday; if (change <= -0.01m) { var quantity = Math.Floor(_investmentAmount / currentPrice); if (quantity > 0) { Buy(_spy, quantity); Log($"Invested ${_investmentAmount} in SPY: {change:P2} change, bought {quantity} shares"); } } } else { Log("Invalid price data. Skipping investment check."); } } } }