Beispiel #1
0
        /// <summary>
        /// Checks and configures Days.
        /// Will remove any testing days if the mode can't have testing days.
        /// If not simulating, will limit the days to just today.
        /// </summary>
        /// <param name="days"></param>
        private void PrepDays(List <FD> days)
        {
            Days = new List <FD>();
            // prep days (forces today when not sim, removes testing days if sim GatherStats)
            if (AlgoTraderState.IsSim)
            {
                if (CannotHaveTestingDays())
                {
                    List <FD> testingDays = DBMethods.GetTestingDays();
                    int       daysCount   = days.Count;
                    // remove all testing days from days
                    days.RemoveAll(d => testingDays.Exists(t => t.Equals(d)));
                    TC.Log.AddLine("Removed " + (daysCount - days.Count) + " testing days because we cannot have testing days", Verbosity.Normal);
                }
                else
                {
                    TC.Log.AddLine("Testing days are totally fine. Will add all " + days.Count + " day(s)", Verbosity.Normal);
                }
                Days.AddRange(days);

                // make sure it's ordered chronologically for tiddyness sake
                Days = Days.OrderBy(d => d.StartNano).ToList();
            }
            else
            {
                ZonedDateTime zdt = UCDT.GetCurrentEastCoastTime();
                FD            fd  = new FD(zdt.Year, zdt.Month, zdt.Day);
                TC.Log.AddLine("Added only today because it's not a simulation: " + fd.ToString(), Verbosity.Normal);
                Days.Add(fd);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Starts ticking the timer.
        /// For simulations, current time will be set to 12am current day.
        /// For non-simulations, current time will be current east coast time.
        /// </summary>
        private void PrepareTimer()
        {
            if (AlgoTraderState.IsSim)
            {
                DateTime dt = new DateTime(AlgoTraderState.CurrentDay.DT.Year, AlgoTraderState.CurrentDay.DT.Month, AlgoTraderState.CurrentDay.DT.Day, 0, 0, 0, DateTimeKind.Unspecified);

                ZonedDateTime initial = UCDT.UTCDateTimeToZonedDateTime(UCDT.ZonedDateTimetoUTCDateTime(dt, UCDT.TimeZones.Eastern), UCDT.TimeZones.Eastern);

                AlgoTraderState.CurrentTime = new CurrentTime(AlgoTraderState.IsSim, initial);
            }
            else
            {
                AlgoTraderState.CurrentTime = new CurrentTime(AlgoTraderState.IsSim, UCDT.GetCurrentEastCoastTime());
            }

            SimulationTimer = new Timer(TimerCallback, null, TimerInterval, TimerInterval);
        }
        public void RefreshTime()
        {
            // lock is set in wrapping calling code (AlgoTraderProcess.TimerCallback)
            lock (LockObj)
            {
                if (!AlgoTraderState.PauseSim)
                {
                    if (IsSim)
                    {
                        if (AlgoTraderState.TradingManagerRunning)
                        {
                            AlgoTraderMethods.Pause();
                        }
                        Time = Time.PlusMinutes(IncrementMinutes);
                    }
                    else
                    {
                        Time = UCDT.GetCurrentEastCoastTime();
                    }

                    CurrentMinute = Time.Minute;
                }
            }
        }
Beispiel #4
0
        private string GetKey()
        {
            ZonedDateTime dt = UCDT.GetCurrentEastCoastTime();

            return(KeyPrefix + "_" + dt.Year + "-" + dt.Month + "-" + dt.Day);
        }
Beispiel #5
0
        public void Run(ThreadControl tc)
        {
            PTC = tc;

            BuildSchedule(ScheduleJSON);

            NewDay();

            ZonedDateTime dt = UCDT.GetCurrentEastCoastTime();

            int currentSecond = -1;
            int lastSecond    = -1;
            int currentDay    = dt.Day;
            int lastDay       = dt.Day;

            while (PTC.CheckNotStopped())
            {
                dt = UCDT.GetCurrentEastCoastTime();

                if (dt.Day != lastDay)
                {
                    // reset for new day
                    NewDay();
                }

                lastDay = dt.Day;

                string currentKey = GetKey();

                currentSecond = dt.Second;

                if (currentSecond != lastSecond)
                {
                    lastSecond = currentSecond;

                    // check any items that have finished and mark them complete

                    List <SchedulerItem> CompletedNotMarkedItems = SchedulerItems.FindAll(i => !i.MarkedComplete && i.TC != null && i.TC.State == ThreadControlState.Complete);
                    for (int n = 0; n < CompletedNotMarkedItems.Count; n++)
                    {
                        PTC.Log.AddLine("Marking " + CompletedNotMarkedItems[n].Id + " as completed");
                        CompletedNotMarkedItems[n].MarkedComplete = true;
                        DBMethods.MarkSchedulerItem(currentKey, CompletedNotMarkedItems[n].Id);
                    }

                    // stop any that are running and it's after their "endAt" date

                    List <SchedulerItem> ItemsNeedStopped = SchedulerItems.FindAll(i => !i.MarkedComplete && i.TC != null && i.TC.CanBeStopped() && i.EndAt != null && CurrentTimeAfterOrEqualSchedulerItemTime(dt, i.EndAt));
                    for (int n = 0; n < ItemsNeedStopped.Count; n++)
                    {
                        PTC.Log.AddLine("Stopping " + ItemsNeedStopped[n].Id + " because on or after EndAt. Will wait for stop before marking completed.");

                        PurposedlyStoppedIds.Add(ItemsNeedStopped[n].TC.Id);
                        ItemsNeedStopped[n].TC.AddSignalForChild(Signal.Stop);
                    }

                    // find all recently stopped items BY SCHEDULER

                    List <SchedulerItem> ItemsThatHaveStopped = SchedulerItems.FindAll(i => !i.MarkedComplete && i.TC != null && i.TC.State == ThreadControlState.Done && PurposedlyStoppedIds.Exists(p => p == i.TC.Id));
                    for (int n = 0; n < ItemsThatHaveStopped.Count; n++)
                    {
                        PTC.Log.AddLine("Found a Done. Marking it as complete.");
                        PurposedlyStoppedIds.Remove(ItemsThatHaveStopped[n].TC.Id);
                        ItemsThatHaveStopped[n].MarkedComplete = true;
                        DBMethods.MarkSchedulerItem(currentKey, ItemsThatHaveStopped[n].Id);
                    }

                    // start any that are within the current time and not running
                    List <SchedulerItem> ItemsThatNeedStarted = SchedulerItems.FindAll(i => !i.MarkedComplete && i.TC == null && ItemIsWithinCurrentTime(dt, i));
                    for (int n = 0; n < ItemsThatNeedStarted.Count; n++)
                    {
                        PTC.Log.AddLine("Starting " + ItemsThatNeedStarted[n].Id + " because current time is between [At, EndAt]");

                        string typeName = ItemsThatNeedStarted[n].Class + ", " + ItemsThatNeedStarted[n].Assembly;

                        string methodName = ItemsThatNeedStarted[n].Call;

                        ThreadControl childTc = new ThreadControl(ItemsThatNeedStarted[n].Id, ItemsThatNeedStarted[n].ObjectId);
                        PTC.Children.Add(childTc);
                        ItemsThatNeedStarted[n].TC = childTc;
                        Task.Factory.StartNew(() => Methods.ThreadRun(typeName, methodName, childTc, null, null), CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
                    }
                }
                else
                {
                    Thread.Sleep(250);
                }
            }
        }
Beispiel #6
0
        public void Run(AlgoTraderModes mode, List <FD> days, ThreadControl tc)
        {
            TC = tc;

            ResetData();

            AlgoTraderState.Mode = mode;

            SetIntervalBasedOnMode();

            AlgoTraderState.IsSim =
                AlgoTraderState.Mode == AlgoTraderModes.GatherStats ||
                AlgoTraderState.Mode == AlgoTraderModes.Simulating ||
                AlgoTraderState.Mode == AlgoTraderModes.Backtesting;

            AlgoTraderState.UseSimOrdering = AlgoTraderState.IsSim || AlgoTraderState.Mode == AlgoTraderModes.FakeLive;

            PrepDays(days);


            bool allowToRun = true;

            if (!AlgoTraderState.IsSim)
            {
                ZonedDateTime zdt = UCDT.GetCurrentEastCoastTime();

                if (AlgoTraderState.Mode == AlgoTraderModes.Live && zdt.Hour >= 4)
                {
                    allowToRun = false;
                }
            }


            if (allowToRun)
            {
                // live or paper only has one day
                for (int n = 0; n < Days.Count; n++)
                {
                    ResetDayData();

                    AlgoTraderState.CurrentDay = Days[n];


                    // AlgoTraderUI.SelectedSymbol = Global.State.AllSymbols[UC.GetRandomInteger(0, Global.State.AllSymbols.Count - 1)];

                    // init the list where nodes, trend lines, etc are stored
                    // AlgoTraderShared.NodesData = new Dictionary<string, NodesData>();
                    for (int m = 0; m < Global.State.AllSymbols.Count; m++)
                    {
                        AlgoTraderShared.NodesData.Add(Global.State.AllSymbols[m], null);
                    }

                    CollectGarbage();

                    // this will take a while only when simulating
                    PrepareSimDayNodes();

                    // starts incrementing CurrentTime immediatly when this is called
                    PrepareTimer();

                    // waits for 3:55am
                    AlgoTraderMethods.WaitFor(AlgoTraderConfig.TicksStreamManagerAtHour, AlgoTraderConfig.TicksStreamManagerAtMinute, TC);

                    RunStocksDataUpdater();

                    RunOrdersDataUpdater();

                    RunTradingManager();

                    CollectGarbage();

                    // waits for trading manager to finish
                    WaitToContinue();

                    TC.StopAllChildrenAndWait();

                    if (!TC.CheckNotStopped())
                    {
                        break;
                    }
                }
            }

            TC.Log.AddLine("AlgoTraderProcess Run() complete", Verbosity.Minimal);

            // it will also set Ended = true
            ResetData();
        }