示例#1
0
        /// <summary>
        /// Initializes this alpha handler to accept alphas from the specified algorithm
        /// </summary>
        /// <param name="job">The algorithm job</param>
        /// <param name="algorithm">The algorithm instance</param>
        /// <param name="messagingHandler">Handler used for sending alphas</param>
        /// <param name="api">Api instance</param>
        public virtual void Initialize(AlgorithmNodePacket job, IAlgorithm algorithm, IMessagingHandler messagingHandler, IApi api)
        {
            // initializing these properties just in case, doens't hurt to have them populated
            Job                      = job;
            Algorithm                = algorithm;
            _messagingHandler        = messagingHandler;
            _isNotFrameworkAlgorithm = !algorithm.IsFrameworkAlgorithm;
            if (_isNotFrameworkAlgorithm)
            {
                return;
            }


            _securityValuesProvider = new AlgorithmSecurityValuesProvider(algorithm);

            AlphaManager = CreateAlphaManager();

            var statistics = new StatisticsAlphaManagerExtension();

            RuntimeStatistics = statistics.Statistics;
            AlphaManager.AddExtension(statistics);
            _charting = new ChartingAlphaManagerExtension(algorithm, statistics);
            AlphaManager.AddExtension(_charting);

            // when alpha is generated, take snapshot of securities and place in queue for alpha manager to process on alpha thread
            algorithm.AlphasGenerated += (algo, collection) => _alphaQueue.Enqueue(new AlphaQueueItem(collection.DateTimeUtc, CreateSecurityValuesSnapshot(), collection));
        }
示例#2
0
        /// <summary>
        /// Invoked after the algorithm's Initialize method was called allowing the alpha handler to check
        /// other things, such as sampling period for backtests
        /// </summary>
        /// <param name="algorithm">The algorithm instance</param>
        public void OnAfterAlgorithmInitialized(IAlgorithm algorithm)
        {
            if (_isNotFrameworkAlgorithm)
            {
                return;
            }

            // send date ranges to extensions for initialization -- this data wasn't available when the handler was
            // initialzied, so we need to invoke it here
            AlphaManager.InitializeExtensionsForRange(algorithm.StartDate, algorithm.EndDate, algorithm.UtcTime);
        }
 public override void Execute()
 {
     if (NestSelected.Used)
     {
         return;
     }
     Cursor.Nest.AssignShape(NestSelected.Shape);
     Cursor.Nest.AssignFilling(NestSelected.Filling);
     AlphaManager.ForceHideCells(Cursor.Nest.Elements);
     AlphaManager.ForceHighlightCells(Cursor.Nest.Elements, true);
     CursorTr.position = SelectedTr.position;
     AlphaManager.ForceHideCells(NestSelected.Elements);
     Cursor.NestSelected = NestSelected;
     Executor.ExecCoroutine(ChasePointer());
 }
示例#4
0
        //[Inject]
        //public ValidateFieldSignal ValidateSignal { get; private set; }

        public override void Execute()
        {
            Retain();
            Dictionary <CellDescriptor, CellFilling> cellsToFill = new Dictionary <CellDescriptor, CellFilling>();
            List <Vector2> coords = new List <Vector2>();

            foreach (CellDescriptor cell in Cursor.Nest.Elements)
            {
                if (!cell.Enabled)
                {
                    continue;
                }
                int            x, y;
                CellDescriptor nearestCell = GetNearestCell(cell, out x, out y);
                if (cellsToFill.ContainsKey(nearestCell) || nearestCell.Enabled || !nearestCell.IsUnderCell(cell))
                {
                    Revoke();
                    return;
                }
                cellsToFill.Add(nearestCell, cell.Filling);
                coords.Add(new Vector2(x, y));
            }
            Cursor.NestSelected.Used = true;
            AlphaManager.ForceHideCells(Cursor.Nest.Elements);
            int i = 0;

            foreach (CellDescriptor cell in cellsToFill.Keys)
            {
                Model.Cells[(int)coords[i].x, (int)coords[i].y].Filling =
                    cell.Filling = cellsToFill[cell];
                Model.Cells[(int)coords[i].x, (int)coords[i].y].Filled =
                    cell.Enabled = true;
                i++;
            }
            AlphaManager.HighlightCells(cellsToFill.Keys, false);
            foreach (NestDescriptor nest in NestGrid.Elements)
            {
                if (!nest.Used)
                {
                    Release();
                    //ValidateSignal.Dispatch();
                    return;
                }
            }
            OverSignal.Dispatch();
            //ValidateSignal.Dispatch();
            Release();
        }
示例#5
0
        /// <summary>
        /// Initializes this alpha handler to accept alphas from the specified algorithm
        /// </summary>
        /// <param name="job">The algorithm job</param>
        /// <param name="algorithm">The algorithm instance</param>
        /// <param name="messagingHandler">Handler used for sending alphas</param>
        /// <param name="api">Api instance</param>
        public virtual void Initialize(AlgorithmNodePacket job, IAlgorithm algorithm, IMessagingHandler messagingHandler, IApi api)
        {
            // initializing these properties just in case, doens't hurt to have them populated
            Job                      = job;
            Algorithm                = algorithm;
            _messagingHandler        = messagingHandler;
            _isNotFrameworkAlgorithm = !algorithm.IsFrameworkAlgorithm;
            if (_isNotFrameworkAlgorithm)
            {
                return;
            }

            AlphaManager      = CreateAlphaManager();
            RuntimeStatistics = new AlphaRuntimeStatistics();

            // wire events to update runtime statistics at key moments in alpha life cycle (new/period end/analysis end)
            AlphaManager.AlphaReceived          += (sender, context) => StatisticsUpdater.OnAlphaReceived(RuntimeStatistics, context);
            AlphaManager.AlphaClosed            += (sender, context) => StatisticsUpdater.OnAlphaClosed(RuntimeStatistics, context);
            AlphaManager.AlphaAnalysisCompleted += (sender, context) => StatisticsUpdater.OnAlphaAnalysisCompleted(RuntimeStatistics, context);

            algorithm.AlphasGenerated += (algo, collection) => OnAlphasGenerated(collection);

            // chart for average scores over sample period
            var scoreChart = new Chart("Alpha");

            foreach (var scoreType in ScoreTypes)
            {
                var series = new Series($"{scoreType} Score", SeriesType.Line, "%");
                scoreChart.AddSeries(series);
                _alphaScoreSeriesByScoreType[scoreType] = series;
            }

            // chart for prediction count over sample period
            var predictionCount = new Chart("Alpha Count");

            predictionCount.AddSeries(_totalAlphaCountSeries);

            Algorithm.AddChart(scoreChart);
            Algorithm.AddChart(predictionCount);
            Algorithm.AddChart(_totalAlphaCountPerSymbolChart);
            // removing this for now, not sure best way to display this data
            //Algorithm.AddChart(_dailyAlphaCountPerSymbolChart);
        }
示例#6
0
        /// <summary>
        /// Thread entry point for asynchronous processing
        /// </summary>
        public virtual void Run()
        {
            if (_isNotFrameworkAlgorithm)
            {
                return;
            }

            IsActive = true;
            _cancellationTokenSource = new CancellationTokenSource();

            // run main loop until canceled, will clean out work queues separately
            while (!_cancellationTokenSource.IsCancellationRequested)
            {
                try
                {
                    ProcessAsynchronousEvents();
                }
                catch (Exception err)
                {
                    Log.Error(err);
                    throw;
                }

                Thread.Sleep(1);
            }

            // finish alpha scoring analysis
            _alphaQueue.ProcessUntilEmpty(item => AlphaManager.Step(item.FrontierTimeUtc, item.SecurityValues, item.GeneratedAlphas));

            // send final alpha scoring updates before we exit
            var alphas = AlphaManager.GetUpdatedContexts().Select(context => context.Alpha).ToList();

            _messages.Enqueue(new AlphaResultPacket(AlgorithmId, Job.UserId, alphas));

            // finish sending packets
            _messages.ProcessUntilEmpty(packet => _messagingHandler.Send(packet));

            // persist alphas at exit
            StoreAlphas();

            Log.Trace("DefaultAlphaHandler.Run(): Ending Thread...");
            IsActive = false;
        }
示例#7
0
        /// <summary>
        /// Performs asynchronous processing, including broadcasting of alphas to messaging handler
        /// </summary>
        protected void ProcessAsynchronousEvents()
        {
            // step the alpha manager forward in time
            AlphaQueueItem item;

            while (_alphaQueue.TryDequeue(out item))
            {
                AlphaManager.Step(item.FrontierTimeUtc, item.SecurityValues, item.GeneratedAlphas);
            }

            // send alpha upate messages
            Packet packet;

            while (_messages.TryDequeue(out packet))
            {
                _messagingHandler.Send(packet);
            }

            // persist generated alphas to storage
            if (DateTime.UtcNow > _nextPersistenceUpdate)
            {
                StoreAlphas();
                _nextPersistenceUpdate = DateTime.UtcNow + PersistenceUpdateInterval;
            }

            // push updated alphas through messaging handler
            if (DateTime.UtcNow > _nextMessagingUpdate)
            {
                var alphas = AlphaManager.GetUpdatedContexts().Select(context => context.Alpha).ToList();
                if (alphas.Count > 0)
                {
                    _messages.Enqueue(new AlphaResultPacket
                    {
                        AlgorithmId = AlgorithmId,
                        Alphas      = alphas
                    });
                }
                _nextMessagingUpdate = DateTime.UtcNow + MessagingUpdateInterval;
            }
        }
示例#8
0
 void Revoke()
 {
     AlphaManager.ForceHideCells(Cursor.Nest.Elements);
     AlphaManager.ForceHighlightCells(Cursor.NestSelected.Elements, true);
     Fail();
 }
示例#9
0
 public void Init()
 {
     _eventsManager = new GameOverMenuEventsManager();
     _alphaManager  = new AlphaManager(gameObject);
 }