コード例 #1
0
        private async Task CloseRemainingVolumeAsync()
        {
            IReadOnlyCollection <RemainingVolume> remainingVolumes = await _remainingVolumeService.GetAllAsync();

            foreach (RemainingVolume remainingVolume in remainingVolumes)
            {
                MarketMakerState marketMakerState = await _marketMakerStateService.GetStateAsync();

                if (marketMakerState.Status != MarketMakerStatus.Active)
                {
                    continue;
                }

                Instrument instrument = await _instrumentService.GetByAssetPairIdAsync(remainingVolume.AssetPairId);

                if (Math.Abs(remainingVolume.Volume) < instrument.MinVolume)
                {
                    continue;
                }

                decimal volume = Math.Round(Math.Abs(remainingVolume.Volume), instrument.VolumeAccuracy);

                PositionType positionType = remainingVolume.Volume > 0
                    ? PositionType.Long
                    : PositionType.Short;

                ExternalTrade externalTrade =
                    await ExecuteLimitOrderAsync(instrument.AssetPairId, volume, positionType);

                if (externalTrade != null)
                {
                    await _positionService.CloseRemainingVolumeAsync(instrument.AssetPairId, externalTrade);

                    await _remainingVolumeService.RegisterVolumeAsync(instrument.AssetPairId,
                                                                      volume *GetSign(positionType) * -1);
                }
            }
        }
コード例 #2
0
        private async Task CloseRemainingVolumeAsync()
        {
            var startedAt = DateTime.UtcNow;

            // close remaining volumes once per 1 min
            lock (_remainingSync)
            {
                if ((startedAt - _lastCloseRemaining).TotalSeconds > 60)
                {
                    _lastCloseRemaining = startedAt;
                }
                else
                {
                    return;
                }
            }

            IReadOnlyCollection <RemainingVolume> remainingVolumes = await _remainingVolumeService.GetAllAsync();

            foreach (RemainingVolume remainingVolume in remainingVolumes)
            {
                MarketMakerState marketMakerState = await _marketMakerStateService.GetStateAsync();

                if (marketMakerState.Status != MarketMakerStatus.Active)
                {
                    continue;
                }

                Instrument instrument = await _instrumentService.GetByAssetPairIdAsync(remainingVolume.AssetPairId);

                if (Math.Abs(remainingVolume.Volume) < instrument.MinVolume)
                {
                    continue;
                }

                decimal volume = Math.Round(Math.Abs(remainingVolume.Volume), instrument.VolumeAccuracy);

                PositionType positionType = remainingVolume.Volume > 0
                    ? PositionType.Long
                    : PositionType.Short;

                ExternalTrade externalTrade =
                    await ExecuteLimitOrderAsync(instrument.AssetPairId, volume, positionType);

                if (externalTrade != null)
                {
                    await _positionService.CloseRemainingVolumeAsync(instrument.AssetPairId, externalTrade);

                    await _remainingVolumeService.RegisterVolumeAsync(instrument.AssetPairId,
                                                                      externalTrade.Volume *GetSign(positionType) * -1);
                }
            }

            var finishedAt = DateTime.UtcNow;

            _log.Info("HedgeService.CloseRemainingVolumeAsync() completed.", new
            {
                StartedAt  = startedAt,
                FinishedAt = finishedAt,
                Latency    = (finishedAt - startedAt).TotalMilliseconds
            });
        }