Beispiel #1
0
        public Strategy UpdateStrategy(Strategy strategy)
        {
            return(ExecuteFaultHandledOperation(() =>
            {
                IStrategyRepository strategyRepository = _DataRepositoryFactory.GetDataRepository <IStrategyRepository>();

                Strategy updatedEntity = null;

                if (strategy.StrategyID == 0)
                {
                    updatedEntity = strategyRepository.Add(strategy);
                }
                else
                {
                    updatedEntity = strategyRepository.Update(strategy);
                }

                return updatedEntity;
            }));
        }
        public async Task <IActionResult> RegisterStrategy([FromBody] StrategyRegistrationModel model) //TODO: Move logic from controller
        {
            var userId = HttpContext.User.GetUserId();

            if (userId == null)
            {
                return(BadRequest("User not found"));
            }
            if (model.assets == null || !model.assets.Any())
            {
                return(BadRequest("No asset selected"));
            }

            var assets = _assetRepository.GetByUserId(userId).Where(o =>
                                                                    o.Exchange.Equals(model.exchange, StringComparison.InvariantCultureIgnoreCase) &&
                                                                    o.TradingMode == model.tradingMode &&
                                                                    string.IsNullOrEmpty(o.StrategyId));

            var strategyAssets = new List <Asset>();
            var currentPrices  = await _marketDataService.GetCurrentPrices(model.exchange);

            var firstEvaluation = new EvaluationTick();

            foreach (var modelAsset in model.assets)
            {
                if (model.tradingMode == TradingMode.BackTesting)
                {
                    strategyAssets.Add(new Asset
                    {
                        Amount      = modelAsset.amount,
                        Currency    = modelAsset.currency,
                        Exchange    = modelAsset.exchange,
                        TradingMode = model.tradingMode,
                        UserId      = userId,
                    });
                    if (!currentPrices.ContainsKey(modelAsset.currency))
                    {
                        return(BadRequest("Cannot estimate initial value"));
                    }
                    firstEvaluation.BtcValue += currentPrices[modelAsset.currency].BtcValue * modelAsset.amount;
                    firstEvaluation.UsdValue += currentPrices[modelAsset.currency].UsdValue * modelAsset.amount;
                }
                else
                {
                    var asset = assets.FirstOrDefault(o => o.Id.Equals(modelAsset.id));
                    if (asset == null || (asset.Amount < modelAsset.amount))
                    {
                        return(BadRequest("Insufficient funds"));
                    }
                    asset.Amount -= modelAsset.amount;
                    strategyAssets.Add(new Asset
                    {
                        Amount      = modelAsset.amount,
                        Currency    = asset.Currency,
                        Exchange    = asset.Exchange,
                        TradingMode = model.tradingMode,
                        UserId      = userId,
                    });
                    if (!currentPrices.ContainsKey(asset.Currency))
                    {
                        return(BadRequest("Cannot estimate initial value"));
                    }
                    firstEvaluation.BtcValue += currentPrices[asset.Currency].BtcValue * modelAsset.amount;
                    firstEvaluation.UsdValue += currentPrices[asset.Currency].UsdValue * modelAsset.amount;
                    if (asset.Amount == 0)
                    {
                        _assetRepository.DeleteNotSave(asset);
                    }
                }
            }

            var strategy = new Strategy
            {
                Name          = model.name,
                Description   = model.description,
                Start         = DateTime.Now,
                StrategyState = StrategyState.Running,
                TradingMode   = model.tradingMode,
                IsOverview    = false,
                Assets        = strategyAssets,
                UserId        = userId,
                Evaluations   = new List <EvaluationTick> {
                    firstEvaluation
                }
            };

            _strategyRepository.Add(strategy);
            _assetRepository.Save();
            return(Ok(strategy.Id));
        }