public async Task DeleteAsync(string assetPairId) { Instrument instrument = await GetByAssetPairIdAsync(assetPairId); if (instrument.Mode == InstrumentMode.Active) { throw new InvalidOperationException("Can not remove active instrument"); } IReadOnlyCollection <RemainingVolume> remainingVolumes = await _remainingVolumeService.GetAllAsync(); RemainingVolume remainingVolume = remainingVolumes.SingleOrDefault(o => o.AssetPairId == instrument.AssetPairId); if (remainingVolume != null && remainingVolume.Volume != 0) { throw new InvalidOperationException("Can not remove instrument while remaining volume exist"); } await _instrumentRepository.DeleteAsync(assetPairId); await _crossInstrumentRepository.DeleteAsync(assetPairId); await _remainingVolumeService.DeleteAsync(assetPairId); _cache.Remove(assetPairId); _log.InfoWithDetails("Instrument was deleted", instrument); }
public async Task RegisterVolumeAsync(string assetPairId, decimal volume) { IReadOnlyCollection <RemainingVolume> remainingVolumes = await GetAllAsync(); RemainingVolume remainingVolume = remainingVolumes.SingleOrDefault(o => o.AssetPairId == assetPairId); bool isNew = false; if (remainingVolume == null) { remainingVolume = new RemainingVolume { AssetPairId = assetPairId }; isNew = true; } remainingVolume.Add(volume); if (isNew) { await _remainingVolumeRepository.InsertAsync(remainingVolume); } else { await _remainingVolumeRepository.UpdateAsync(remainingVolume); } _cache.Set(remainingVolume); }
private double TryTakeCurrency(string firstCurrency, double preRequiredVolumeFirstCurrency, string secondaryCurrency, double preRequiredSecondaryCurrency, SymbolValue symbolValue, Side sideFirstCurrency) { double requiredVolumeFirstCurrency = 0.46 * preRequiredVolumeFirstCurrency; double requiredSecondaryCurrency = 0.46 * preRequiredSecondaryCurrency; if (requiredVolumeFirstCurrency < 10000 || requiredSecondaryCurrency < 10000) { return(0); } Side revertFromFirst = sideFirstCurrency == Side.Buy ? Side.Sell : Side.Buy; RemainingVolume rVolume2 = remainingVolume.Single(p => p.Currency == secondaryCurrency && p.Side == revertFromFirst); //RemainingVolume rVolume4 = remainingVolume.Single(p => p.Currency == secondaryCurrency && p.Side == sideFirstCurrency); //RemainingVolume rVolume3 = remainingVolume.Single(p => p.Currency == firstCurrency && p.Side == revertFromFirst); if (rVolume2.Volume < requiredSecondaryCurrency /*|| rVolume4.Volume < requiredSecondaryCurrency || rVolume3.Volume < requiredSecondaryCurrency*/) { //return Math.Min( Math.Min(rVolume2.Volume, rVolume4.Volume), rVolume3.Volume); return(rVolume2.Volume); } RemainingVolume rVolume = remainingVolume.Single(p => p.Currency == firstCurrency && p.Side == sideFirstCurrency); rVolume.Volume -= requiredVolumeFirstCurrency; rVolume2.Volume -= requiredSecondaryCurrency; //rVolume3.Volume -= requiredVolumeFirstCurrency; //rVolume4.Volume -= requiredSecondaryCurrency; ResultRecord rr = new ResultRecord(); if (symbolValue.Symbol.IsFirstSymbol(firstCurrency)) { rr.Side = sideFirstCurrency; rr.Symbol = symbolValue.Symbol; rr.Volume1 = requiredVolumeFirstCurrency; rr.Volume2 = requiredSecondaryCurrency; } else { rr.Side = revertFromFirst; rr.Symbol = symbolValue.Symbol; rr.Volume1 = requiredSecondaryCurrency; rr.Volume2 = requiredVolumeFirstCurrency; } rr.ProcentVolumesFromInit = ForexSuite.QuotesManager.ConvertCurrencyEx(rr.Symbol.Currency1, "USD", CalculationPointDateTime, rr.Volume1) / InitAmount; rr.Profit = (rr.ProcentVolumesFromInit / symbolValue.Price); ResultRecord preResult = ListResultRecord.SingleOrDefault(p => p.Symbol.Currency1 == rr.Symbol.Currency1 && p.Symbol.Currency2 == rr.Symbol.Currency2 && p.Side == rr.Side); if (preResult == null) { ListResultRecord.Add(rr); } else { preResult.Volume1 += rr.Volume1; preResult.Volume2 += rr.Volume2; preResult.Profit += rr.Profit; } return(requiredSecondaryCurrency); }
public async Task UpdateAsync(RemainingVolume remainingVolume) { await _storage.MergeAsync(GetPartitionKey(), GetRowKey(remainingVolume.AssetPairId), entity => { Mapper.Map(remainingVolume, entity); return(entity); }); }
public async Task InsertAsync(RemainingVolume remainingVolume) { var entity = new RemainingVolumeEntity(GetPartitionKey(), GetRowKey(remainingVolume.AssetPairId)); Mapper.Map(remainingVolume, entity); await _storage.InsertAsync(entity); }
//[TestMethod] public async Task Close_Long_Position_Create_Remaining_Volume() { // arrange Position position = Position.Open("BTCUSD", 6500, 1.12345m, TradeType.Buy, Guid.NewGuid().ToString()); var instrument = new Instrument { AssetPairId = "BTCUSD", Mode = InstrumentMode.Active, MinVolume = 0.0001m, VolumeAccuracy = 3 }; var expectedRemainingVolume = new RemainingVolume { AssetPairId = "BTCUSD", Volume = position.Volume - Math.Round(position.Volume, instrument.VolumeAccuracy) }; RemainingVolume actualRemainingVolume = null; decimal externalTradePrice = 6505; _openPositions.Add(position); _instruments.Add(instrument); _externalExchangeServiceMock .Setup(o => o.ExecuteSellLimitOrderAsync(It.IsAny <string>(), It.IsAny <decimal>())) .Returns((string assetPairId, decimal volume) => Task.FromResult(new ExternalTrade { AssetPairId = assetPairId, Price = externalTradePrice, Volume = volume, Type = TradeType.Sell })); _remainingVolumeServiceMock.Setup(o => o.RegisterVolumeAsync(It.IsAny <string>(), It.IsAny <decimal>())) .Returns(Task.CompletedTask) .Callback((string assetPairId, decimal volume) => actualRemainingVolume = new RemainingVolume { AssetPairId = assetPairId, Volume = volume }); // act await _service.ExecuteAsync(); // assert Assert.IsTrue(AreEqual(expectedRemainingVolume, actualRemainingVolume)); }
public async Task CloseVolumeAsync(string assetPairId, decimal volume) { IReadOnlyCollection <RemainingVolume> remainingVolumes = await GetAllAsync(); RemainingVolume remainingVolume = remainingVolumes.SingleOrDefault(o => o.AssetPairId == assetPairId); if (remainingVolume == null) { _log.WarningWithDetails("Remaining volume not exist", assetPairId); return; } remainingVolume.Subtract(volume); await _remainingVolumeRepository.UpdateAsync(remainingVolume); _cache.Set(remainingVolume); }
private bool AreEqual(RemainingVolume a, RemainingVolume b) { return(a.AssetPairId == b.AssetPairId && a.Volume == b.Volume); }