Exemple #1
0
            public CallState(long id, IContext context, PositionsManager.SecInfo secInfo, bool onlyAtTradingSession)
            {
                Contract.Assert(context != null, "Почему вдруг (context==null) ??");
                Contract.Assert(secInfo != null, "Почему вдруг (secInfo==null) ??");

                CallId               = id;
                CallContext          = context;
                SecInfo              = secInfo;
                OnlyAtTradingSession = onlyAtTradingSession;
            }
Exemple #2
0
            public CallState(long id, IContext context, IDataSourceSecurity dataSourceSecurity, bool onlyAtTradingSession)
            {
                Contract.Assert(context != null, "Почему вдруг (context==null) ??");
                Contract.Assert(dataSourceSecurity != null, "Почему вдруг (dataSourceSecurity==null) ??");

                CallId               = id;
                CallContext          = context;
                SecInfo              = new PositionsManager.SecInfo(dataSourceSecurity);
                OnlyAtTradingSession = onlyAtTradingSession;
                DataSourceSecurity   = dataSourceSecurity;
            }
Exemple #3
0
        public IList <double> Execute(ISecurity sec)
        {
            //Context.Log(String.Format("[Heartbeat.Execute ( ID:{0} )] I'm checking timer settings.", m_id), MessageType.Warning, false);

            // PROD-5496 - В режиме оптимизации отключаюсь
            if (Context.IsOptimization)
            {
                return(Constants.EmptyListDouble);
            }

            CallState timerState = null;
            string    cashKey    = VariableId + "_timerState";

            {
                object localObj = Context.LoadObject(cashKey, false);
                timerState = localObj as CallState;
                // PROD-3970 - 'Важный' объект
                if (timerState == null)
                {
                    var container = localObj as NotClearableContainer;
                    if ((container != null) && (container.Content != null))
                    {
                        timerState = container.Content as CallState;
                    }
                }
            }

            //m_timer = Context.LoadObject(VariableId + "m_timer") as IThreadingTimerProfiler;
            if (timerState == null)
            {
                string msg = String.Format("[Heartbeat.Execute ( ID:{0} )] Preparing new timer for agent '{1}'...",
                                           m_id, Context.Runtime.TradeName);
                Context.Log(msg, MessageType.Info, false);

                var secInfo = new PositionsManager.SecInfo(sec.SecurityDescription);
                timerState = new CallState(m_id, Context, secInfo, m_onlyAtTradingSession);
                var timer = new ThreadingTimerProfiler(Recalculate, timerState, m_delayMs, Timeout.Infinite);
                // Обязательно дозаполняем ссылку на таймер
                timerState.Timer = timer;

                var container = new NotClearableContainer(timerState);
                Context.StoreObject(cashKey, container, false);
            }
            else if (timerState.Timer == null)
            {
                // PROD-5427 - Добавляю счетчик этого аварийного события и логгирую
                int problemCounter = 0;
                if (s_problemCounters.ContainsKey(Context.Runtime.TradeName))
                {
                    problemCounter = s_problemCounters[Context.Runtime.TradeName];
                }

                s_problemCounters[Context.Runtime.TradeName] = Interlocked.Increment(ref problemCounter);

                string msg = String.Format("[Heartbeat.Execute ( ID:{0} )] Timer is null in agent '{1}'. Problem counter: {2}",
                                           m_id, Context.Runtime.TradeName, problemCounter);
                Context.Log(msg, MessageType.Warning, false);

                if (problemCounter > 3)
                {
                    // Если проблема систематически повторяется -- выбрасываю ассерт для дальнейшего анализа ситуации
                    Contract.Assert(timerState.Timer != null, msg);
                }
            }
            else
            {
                //Contract.Assert(timerState.Timer != null, "Почему вдруг (timerState.Timer==null) ??");

                // Если при изменении скрипта пересоздается агент, то контекст становится невалидным?
                if (Object.ReferenceEquals(Context, timerState.CallContext))
                {
                    // Если контекст совпадает, то обновляем режим работы...
                    timerState.OnlyAtTradingSession = m_onlyAtTradingSession;
                    // и перезапускаем таймер
                    try
                    {
                        timerState.Timer.Change(m_delayMs, Timeout.Infinite);
                        // PROD-5427 - При штатной работе блока обнуляю счетчик проблем
                        s_problemCounters[Context.Runtime.TradeName] = 0;
                    }
                    catch (ObjectDisposedException)
                    {
                        // Если таймер уже убит, то надо создать новый
                        timerState.Timer = null;
                        timerState       = null;

                        string msg = String.Format("[Heartbeat.Execute ( ID:{0} )] Replacing DISPOSED timer for agent '{1}'...",
                                                   m_id, Context.Runtime.TradeName);
                        Context.Log(msg, MessageType.Warning, false);

                        // Создаём новый таймер. При этом используем НОВЫЙ m_id
                        var secInfo = new PositionsManager.SecInfo(sec.SecurityDescription);
                        timerState = new CallState(m_id, Context, secInfo, m_onlyAtTradingSession);
                        var timer = new ThreadingTimerProfiler(Recalculate, timerState, m_delayMs, Timeout.Infinite);
                        // Обязательно дозаполняем ссылку на таймер
                        timerState.Timer = timer;

                        var container = new NotClearableContainer(timerState);
                        Context.StoreObject(cashKey, container, false);
                    }
                }
                else
                {
                    // Если по какой-то причине изменился контекст, то создаём новый таймер...
                    timerState.Timer.Dispose();
                    timerState.Timer = null;
                    timerState       = null;

                    string msg = String.Format("[Heartbeat.Execute ( ID:{0} )] Replacing timer for agent '{1}'...",
                                               m_id, Context.Runtime.TradeName);
                    Context.Log(msg, MessageType.Warning, false);

                    // Создаём новый таймер. При этом используем НОВЫЙ m_id
                    var secInfo = new PositionsManager.SecInfo(sec.SecurityDescription);
                    timerState = new CallState(m_id, Context, secInfo, m_onlyAtTradingSession);
                    var timer = new ThreadingTimerProfiler(Recalculate, timerState, m_delayMs, Timeout.Infinite);
                    // Обязательно дозаполняем ссылку на таймер
                    timerState.Timer = timer;

                    var container = new NotClearableContainer(timerState);
                    Context.StoreObject(cashKey, container, false);
                }
            }

            int len = Context.BarsCount;

            double[] res = Context.GetArray <double>(len);
            if (len > 0)
            {
                res[len - 1] = m_id;
            }

            return(res);
        }