public async Task <IActionResult> StopStrategy(string strategyId) { var userId = HttpContext.User.GetUserId(); if (string.IsNullOrEmpty(userId)) { return(BadRequest("User not found")); } var strategy = _strategyRepository.GetByUserId(userId).FirstOrDefault(o => o.Id == strategyId); if (strategy == null) { return(BadRequest("Strategy not found")); } if (strategy.StrategyState != StrategyState.Running) { return(BadRequest("Strategy is not running")); } strategy.Stop = DateTime.Now; strategy.StrategyState = StrategyState.Stopped; var userAssets = _assetRepository.GetByUserId(userId); var strategyAssets = userAssets.Where(o => o.StrategyId == strategyId); foreach (var asset in strategyAssets) { var assetOrigin = userAssets.FirstOrDefault(o => !o.IsReserved && o.IsActive && o.Currency == asset.Currency && o.TradingMode == asset.TradingMode && string.IsNullOrEmpty(o.StrategyId) && o.Exchange == asset.Exchange); if (assetOrigin != null) { assetOrigin.Amount += asset.Amount; _assetRepository.EditNotSave(assetOrigin); } else { _assetRepository.AddNotSave(new Asset(asset)); } asset.IsActive = false; _assetRepository.EditNotSave(asset); } var finalEvaluation = await _marketDataService .EvaluateAssetSet(strategyAssets.Select(o => (currency: o.Currency, amount: o.Amount)), "binance"); finalEvaluation.StrategyId = strategyId; finalEvaluation.IsFinal = true; _evaluationRepository.AddNotSave(finalEvaluation); _strategyRepository.Edit(strategy); _assetRepository.Save(); _strategyRepository.Save(); //TODO piece of work? _evaluationRepository.Save(); return(Ok()); }
public StrategyToken Create(StrategyToken token) { return(_strategyRepository.Save(token.AsStrategy()).AsToken()); }