Пример #1
0
        public async Task <PostsViewModel> GetOrderedPostsBySubredditAsync(
            string subredditId,
            IRequestCookieCollection requestCookies)
        {
            var dbSubreddit = await this.redditCloneUnitOfWork.Subreddits
                              .GetByIdAsync(subredditId);

            if (dbSubreddit == null)
            {
                return(null);
            }

            var postSortType      = this.cookieService.GetPostSortTypeFromCookieOrDefault(requestCookies);
            var postTimeFrameType = this.cookieService.GetPostTimeFrameTypeFromCookieOrDefault(requestCookies);

            var timeFrame         = TimeFrameFactory.GetTimeFrame(postTimeFrameType);
            var sortPostsStrategy = SortPostsStartegyFactory
                                    .GetSortPostsStrategy(this.redditCloneUnitOfWork, timeFrame, postSortType);

            var dbPosts = await this.redditCloneUnitOfWork.Posts
                          .GetBySubredditSortedBy(subredditId, sortPostsStrategy);

            var model = this.postMapper.MapPostsViewModelForQuest(
                dbPosts, postSortType, sortPostsStrategy, postTimeFrameType);

            return(model);
        }
Пример #2
0
        public async Task <PostsViewModel> GetOrderedPostsAsync(
            ClaimsPrincipal user,
            IRequestCookieCollection requestCookies,
            IResponseCookies responseCookies)
        {
            var postSortType      = this.cookieService.GetPostSortTypeFromCookieOrDefault(requestCookies);
            var postTimeFrameType = this.cookieService.GetPostTimeFrameTypeFromCookieOrDefault(requestCookies);

            var timeFrame         = TimeFrameFactory.GetTimeFrame(postTimeFrameType);
            var sortPostsStrategy = SortPostsStartegyFactory
                                    .GetSortPostsStrategy(this.redditCloneUnitOfWork, timeFrame, postSortType);

            var dbUserId = this.userManager.GetUserId(user);
            var dbPosts  = await this.redditCloneUnitOfWork.Posts
                           .GetBySubcribedUserSortedByAsync(dbUserId, sortPostsStrategy);

            if (dbPosts.Count() == 0)
            {
                dbPosts = await this.redditCloneUnitOfWork.Posts.GetAllSortedByAsync(sortPostsStrategy);
            }

            var model = this.postMapper
                        .MapPostsViewModelForSignInUser(dbPosts, dbUserId, postSortType, sortPostsStrategy, postTimeFrameType);

            return(model);
        }
        protected override ITradeStatisticsWithKind InternalExecute(ISecurity security)
        {
            var           timeFrame = TimeFrameFactory.Create(TimeFrame, TimeFrameUnit);
            int           topTimeFrameNumber;
            TimeFrameUnit topTimeFrameUnit;
            TimeSpan      topTimeFrame;

            if (UseTopTimeFrame)
            {
                topTimeFrameNumber = TopTimeFrame;
                topTimeFrameUnit   = TopTimeFrameUnit;
                topTimeFrame       = TimeFrameFactory.Create(topTimeFrameNumber, topTimeFrameUnit);

                if (topTimeFrame.Ticks % timeFrame.Ticks != 0)
                {
                    throw new InvalidOperationException(string.Format(RM.GetString("TopTimeFrameMustBeDivisableByTimeFrame"), ToString(TopTimeFrame, topTimeFrameUnit), ToString(TimeFrame, TimeFrameUnit)));
                }
            }
            else
            {
                var maxTimeSpan = TimeSpan.FromSeconds(int.MaxValue);
                switch (TimeFrameUnit)
                {
                case TimeFrameUnit.Second:
                    topTimeFrameNumber = (int)maxTimeSpan.TotalSeconds;
                    break;

                case TimeFrameUnit.Minute:
                    topTimeFrameNumber = (int)maxTimeSpan.TotalMinutes;
                    break;

                case TimeFrameUnit.Hour:
                    topTimeFrameNumber = (int)maxTimeSpan.TotalHours;
                    break;

                case TimeFrameUnit.Day:
                    topTimeFrameNumber = (int)maxTimeSpan.TotalDays;
                    break;

                default:
                    throw new InvalidEnumArgumentException(nameof(TimeFrameUnit), (int)TimeFrameUnit, TimeFrameUnit.GetType());
                }
                topTimeFrameNumber = topTimeFrameNumber / TimeFrame * TimeFrame;
                topTimeFrameUnit   = TimeFrameUnit;
                topTimeFrame       = TimeFrameFactory.Create(topTimeFrameNumber, topTimeFrameUnit);
            }
            var runTime = Context.Runtime;
            var id      = runTime != null?string.Join(".", runTime.TradeName, runTime.IsAgentMode, VariableId) : VariableId;

            var stateId         = string.Join(".", security.Symbol, security.Interval, security.IsAligned, CombinePricesCount, TimeFrameKind, TimeFrame, TimeFrameUnit, topTimeFrameNumber, topTimeFrameUnit);
            var tradeStatistics = Context.GetTradeStatistics(stateId, () => new TradeStatistics(id, stateId, GetTradeHistogramsCache(security), TimeFrameKind, timeFrame, TimeFrameUnit, topTimeFrame));

            return(new TradeStatisticsWithKind(tradeStatistics, Kind, WidthPercent));
        }
        protected override ITradeStatisticsWithKind InternalExecute(ISecurity security)
        {
            var timeFrame      = TimeFrameFactory.Create(TimeFrame, TimeFrameUnit);
            var timeFrameShift = TimeFrameFactory.Create(TimeFrameShift, TimeFrameShiftUnit);
            var runTime        = Context.Runtime;
            var id             = runTime != null?string.Join(".", runTime.TradeName, runTime.IsAgentMode, VariableId) : VariableId;

            var stateId         = string.Join(".", security.Symbol, security.Interval, security.IsAligned, CombinePricesCount, TimeFrameKind, TimeFrame, TimeFrameUnit, TimeFrameShift, TimeFrameShiftUnit);
            var tradeStatistics = Context.GetTradeStatistics(stateId, () => new LastTradeStatistics(id, stateId, GetTradeHistogramsCache(security), TimeFrameKind, timeFrame, TimeFrameUnit, timeFrameShift, TimeFrameShiftUnit));

            return(new TradeStatisticsWithKind(tradeStatistics, Kind, WidthPercent));
        }
Пример #5
0
        public async Task <PostsViewModel> GetOrderedPostsByKeyWordsAsync(
            string[] keyWords,
            PostSortType sortType,
            TimeFrameType timeFrameType)
        {
            var timeFrame    = TimeFrameFactory.GetTimeFrame(timeFrameType);
            var sortStrategy = SortPostsStartegyFactory
                               .GetSortPostsStrategy(this.redditCloneUnitOfWork, timeFrame, sortType);

            var filteredPosts = await this.redditCloneUnitOfWork.Posts
                                .GetByKeyWordsSortedByAsync(keyWords, sortStrategy);

            var model = this.postMapper.MapPostsViewModelForQuest(filteredPosts, sortType, sortStrategy, timeFrameType);

            return(model);
        }
Пример #6
0
        public async Task <PostsViewModel> GetOrderedPostsAsync(IRequestCookieCollection requestCookies)
        {
            var postSortType      = this.cookieService.GetPostSortTypeFromCookieOrDefault(requestCookies);
            var postTimeFrameType = this.cookieService.GetPostTimeFrameTypeFromCookieOrDefault(requestCookies);

            var timeFrame         = TimeFrameFactory.GetTimeFrame(postTimeFrameType);
            var sortPostsStrategy = SortPostsStartegyFactory
                                    .GetSortPostsStrategy(this.redditCloneUnitOfWork, timeFrame, postSortType);

            var dbPosts = await this.redditCloneUnitOfWork.Posts
                          .GetAllSortedByAsync(sortPostsStrategy);

            var model =
                this.postMapper.MapPostsViewModelForQuest(dbPosts, postSortType, sortPostsStrategy, postTimeFrameType);

            return(model);
        }
Пример #7
0
        public IList <double> Execute(ISecurity security)
        {
            if (security == null)
            {
                throw new ArgumentNullException(nameof(security));
            }

            if (TimeFrame <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(TimeFrame));
            }

            var timeFrame = TimeFrameFactory.Create(TimeFrame, TimeFrameUnit);

            var bars = security.Bars;

            if (bars.Count == 0)
            {
                return(EmptyArrays.Double);
            }

            if (bars.Count == 1)
            {
                return new[] { bars[0].Volume }
            }
            ;

            DateTime firstDateTime, lastDateTime;

            TimeFrameUtils.GetFirstBounds(timeFrame, bars[0].Date, out firstDateTime, out lastDateTime);
            var results = Context?.GetArray <double>(bars.Count) ?? new double[bars.Count];

            for (int i = 0, firstIndex = 0; i <= results.Length; i++)
            {
                if (i == results.Length || bars[i].Date >= lastDateTime)
                {
                    var count = i - firstIndex;
                    if (count > 0)
                    {
                        var result = 0D;
                        switch (ValueMode)
                        {
                        case ValueForPeriodMode.Sum:
                            for (int j = 0, index = firstIndex; j < count; j++, index++)
                            {
                                result        += GetValue(security, index);
                                results[index] = result;
                            }
                            break;

                        case ValueForPeriodMode.Average:
                            for (int j = 0, index = firstIndex; j < count; j++, index++)
                            {
                                result        += GetValue(security, index);
                                results[index] = result / (j + 1);
                            }
                            break;

                        default:
                            throw new InvalidEnumArgumentException(nameof(ValueMode), (int)ValueMode, ValueMode.GetType());
                        }
                    }
                    if (i == results.Length)
                    {
                        break;
                    }

                    TimeFrameUtils.GetBounds(timeFrame, bars[i].Date, ref firstDateTime, ref lastDateTime);
                    firstIndex = i;
                }
            }
            return(results);
        }
        public ISecurity Execute(ISecurity security)
        {
            var timeFrame = TimeFrameFactory.Create(TimeFrame, TimeFrameUnit);

            return(new AlignedSecurity(security, timeFrame));
        }