Example #1
0
            public IntervalTimeRule(IConnector connector, TimeSpan interval /*, bool firstTimeRun*/)
                : base(connector)
            {
                Name = LocalizedStrings.Str175 + " " + interval;

                _timer = new MarketTimer(connector, () => Activate(connector))
                         .Interval(interval)
                         .Start();
            }
            public TimeFrameCandlesChangedSeriesRule(ICandleManager candleManager, CandleSeries series, IConnector connector, decimal percent)
                : base(series)
            {
                if (candleManager == null)
                {
                    throw new ArgumentNullException(nameof(candleManager));
                }

                _timer = CreateAndActivateTimeFrameTimer(series.Security, (TimeSpan)series.Arg, connector, () => Activate(candleManager.GetCurrentCandle <Candle>(Series)), percent, true);
            }
Example #3
0
            public TimeComeRule(IConnector connector, IEnumerable <DateTimeOffset> times)
                : base(connector)
            {
                if (times == null)
                {
                    throw new ArgumentNullException(nameof(times));
                }

                var currentTime = connector.CurrentTime;

                var intervals = new SynchronizedQueue <TimeSpan>();
                var timesList = new SynchronizedList <DateTimeOffset>();

                foreach (var time in times)
                {
                    var interval = time - currentTime;

                    if (interval <= TimeSpan.Zero)
                    {
                        continue;
                    }

                    intervals.Enqueue(interval);
                    currentTime = time;
                    timesList.Add(time);
                }

                // все даты устарели
                if (timesList.IsEmpty())
                {
                    return;
                }

                Name = LocalizedStrings.Str1055;

                var index = 0;

                _timer = new MarketTimer(connector, () =>
                {
                    var activateTime = timesList[index++];

                    Activate(activateTime);

                    if (index == timesList.Count)
                    {
                        _timer.Stop();
                    }
                    else
                    {
                        _timer.Interval(intervals.Dequeue());
                    }
                })
                         .Interval(intervals.Dequeue())
                         .Start();
            }
        private static MarketTimer CreateAndActivateTimeFrameTimer(Security security, TimeSpan timeFrame, IConnector connector, Action callback, decimal percent, bool periodical)
        {
            if (security == null)
            {
                throw new ArgumentNullException(nameof(security));
            }

            if (connector == null)
            {
                throw new ArgumentNullException(nameof(connector));
            }

            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }

            if (percent <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(percent), LocalizedStrings.Str1077);
            }

            MarketTimer timer = null;

            timer = new MarketTimer(connector, () =>
            {
                if (periodical)
                {
                    timer.Interval(timeFrame);
                }
                else
                {
                    timer.Stop();
                }

                callback();
            });

            var time         = connector.CurrentTime;
            var candleBounds = timeFrame.GetCandleBounds(time, security.Board);

            percent = percent / 100;

            var startTime = candleBounds.Min + TimeSpan.FromMilliseconds(timeFrame.TotalMilliseconds * (double)percent);

            var diff = startTime - time;

            if (diff == TimeSpan.Zero)
            {
                timer.Interval(timeFrame);
            }
            else if (diff > TimeSpan.Zero)
            {
                timer.Interval(diff);
            }
            else
            {
                timer.Interval(timeFrame + diff);
            }

            return(timer.Start());
        }
 public TimeFrameCandleChangedRule(Candle candle, IConnector connector, decimal percent)
     : base(candle)
 {
     _timer = CreateAndActivateTimeFrameTimer(candle.Security, (TimeSpan)candle.Arg, connector, () => Activate(candle), percent, false);
 }