/// <inheritdoc />
        public async Task <int> CreateStrategy(StrategyDto strategy)
        {
            if (await _strategiesRepository.StrategyExists(strategy.UserId, strategy.Name))
            {
                throw new BusinessException(nameof(strategy.Name), "Strategy with this name already exists");
            }
            if (!strategy.Indicators.Any())
            {
                throw new BusinessException(nameof(strategy.Indicators), "At least one indicator has to be chosen");
            }
            var investmentStrategy = new InvestmentStrategy
            {
                UserId           = strategy.UserId,
                Name             = strategy.Name,
                SignalDaysPeriod = strategy.SignalDaysPeriod,
                Indicators       = new List <StrategyIndicator>()
            };

            foreach (var indicator in strategy.Indicators)
            {
                AddIndicator(indicator, investmentStrategy);
            }
            _strategiesRepository.Insert(investmentStrategy);
            await _strategiesRepository.Save();

            return(investmentStrategy.Id);
        }
        private void AddIndicator(ParameterizedIndicator indicator, InvestmentStrategy investmentStrategy)
        {
            if (!indicator.IndicatorType.HasValue)
            {
                throw new BusinessException("Wrong indicator value");
            }

            var strategyIndicator = new StrategyIndicator
            {
                IndicatorType = (int)indicator.IndicatorType.Value,
                Strategy      = investmentStrategy,
                Properties    = new List <StrategyIndicatorProperty>()
            };

            foreach (var indicatorProperty in indicator.Properties)
            {
                if (
                    _indicatorsService.GetPropertiesForIndicator(indicator.IndicatorType.Value)
                    .All(item => item.Name != indicatorProperty.Name))
                {
                    continue;
                }
                strategyIndicator.Properties.Add(new StrategyIndicatorProperty
                {
                    Indicator = strategyIndicator,
                    Name      = indicatorProperty.Name,
                    Value     = indicatorProperty.Value
                });
            }
            investmentStrategy.Indicators.Add(strategyIndicator);
        }
Beispiel #3
0
 /// <summary>
 /// Determines whether [is set normal] [the specified mode].
 /// </summary>
 /// <param name="mode">The mode.</param>
 /// <returns>
 ///     <c>true</c> if [is set normal] [the specified mode]; otherwise, <c>false</c>.
 /// </returns>
 public bool IsSet(InvestmentStrategy mode)
 {
     return((Type & mode) == mode);
 }