Overall Statistics |
Total Trades 1 Average Win 0% Average Loss 0% Compounding Annual Return 7146.263% Drawdown 0.000% Expectancy 0 Net Profit 0.785% Sharpe Ratio 0 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio 0 Tracking Error 0 Treynor Ratio 0 Total Fees $1.00 |
using System.Threading; namespace QuantConnect.Algorithm.CSharp { /// <summary> /// Basic template algorithm simply initializes the date range and cash. This is a skeleton /// framework you can use for designing an algorithm. /// </summary> /* Things to do: 1. Add two or more symbols to be looked at. 2. Schedule events: -- Have a good idea a. Do something before Market Opens b. Do something when Market Opens c. Do something every 2 hours after market opens upto 3:30 PM d. Do something at 3:31 PM d. Do something at Market Close e. Do something after Market Close f. Do something every 5 minutes - create the list based on a function. 3. Get the last 10 prices for each 2 hour period. This could be over multiple days, ignore weekend and holidays. 4. Figure out how to make this parametrized. 5. Set the following: a. Benchmark b. Slippage c. Comission d. Half-Day no trade */ /* Errors: 1. Removing AddEquity(_spy, Resolution.Hour); causes an error */ public class BasicTemplateAlgorithm : QCAlgorithm { private const int MINUTES_IN_HOUR = 60; private const int CHECK_EVERY_N_MINUTES = 120; private static readonly DateTime TODAY = DateTime.Now; private static readonly DateTime TRADING_START_TIME = new DateTime(TODAY.Year, TODAY.Month, TODAY.Day, 9, 30, 0); private static readonly DateTime TRADING_END_TIME = new DateTime(TODAY.Year, TODAY.Month, TODAY.Day, 16, 0, 0); private static readonly TimeSpan TRADING_TIME_SPAN = TRADING_END_TIME.Subtract ( TRADING_START_TIME ); private static readonly int TRADING_MINUTES = (int)TRADING_TIME_SPAN.TotalMinutes; private const string SPY = "SPY"; private static readonly IReadOnlyList<int> HoursToCheck = new List<int>{ 0, 2, 4, 6 }; private static readonly IReadOnlyList<int> MinutesToCheck = Enumerable.Range(CHECK_EVERY_N_MINUTES, TRADING_MINUTES).Where(n => (n % CHECK_EVERY_N_MINUTES) == 0).ToList(); private Symbol _spy = QuantConnect.Symbol.Create(SPY, SecurityType.Equity, Market.USA); /// <summary> /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized. /// </summary> public override void Initialize() { SetStartDate(2013, 10, 10); //Set Start Date SetEndDate(2013, 10, 10); //Set End Date SetCash(10000); //Set Strategy Cash AddEquity(_spy, Resolution.Hour); #region Schedule Functions Schedule.On(DateRules.EveryDay(SPY), TimeRules.At(9, 0), BeforeMarketOpen); foreach (var hour in HoursToCheck) { Schedule.On(DateRules.EveryDay(SPY), TimeRules.AfterMarketOpen(SPY, (MINUTES_IN_HOUR * hour) + 1), TimelyCheck); } Schedule.On(DateRules.EveryDay(SPY), TimeRules.BeforeMarketClose(SPY, 25), BeforeMarketClose); Schedule.On(DateRules.EveryDay(SPY), TimeRules.At(17, 0), AfterMarketClose); #endregion Schedule Functions } /// <summary> /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here. /// </summary> /// <param name="data">Slice object keyed by symbol containing the stock data</param> public override void OnData(Slice data) { if (!Portfolio.Invested) { SetHoldings(_spy, 1); Debug("Purchased Stock"); } } /// <summary> /// End of a trading day event handler. This method is called at the end of the algorithm day (or multiple times if trading multiple assets). /// </summary> /// <remarks>Method is called 10 minutes before closing to allow user to close out position.</remarks> public override void OnEndOfDay() { LogMethodStart("OnEndOfDay"); } #region Scheduled Functions private void TimelyCheck() { LogMethodStart("TimelyCheck"); } private void BeforeMarketOpen() { LogMethodStart("BeforeMarketOpen"); // Do Something Thread.Sleep(10000); LogMethodEnd("BeforeMarketOpen"); } private void BeforeMarketClose() { LogMethodStart("BeforeMarketClose"); } private void AfterMarketClose() { LogMethodStart("AfterMarketClose"); } #endregion Scheduled Functions #region Log Method Start & End private void LogMethodStart(string pMethodName) { LogMethod(pMethodName); } private void LogMethodEnd(string pMethodName) { LogMethod(pMethodName, false); } private void LogMethod(string pMethodName, bool pIsStart = true) { var vState = pIsStart ? "Start" : "End"; var vMessage = String.Format("Method: {0} {1} - {2:MM/dd/yy H:mm:ss:fff}", pMethodName, vState, Time); LogIt(vMessage); } #endregion Log Method Start & End private void LogIt(string pMessage) { Debug(pMessage); Log(pMessage); } } }