public async Task <HedgeSettings> GetAsync()
        {
            HedgeSettings hedgeSettings = _cache.Get(CacheKey);

            if (hedgeSettings == null)
            {
                hedgeSettings = await _hedgeSettingsRepository.GetAsync();

                if (hedgeSettings == null)
                {
                    hedgeSettings = new HedgeSettings
                    {
                        MarketOrderMarkup = .02m,
                        ThresholdDown     = 1000,
                        ThresholdUp       = 5000,
                        ThresholdDownBuy  = 1000,
                        ThresholdDownSell = 1000,
                        ThresholdUpBuy    = 5000,
                        ThresholdUpSell   = 5000,
                        ThresholdCritical = 10000
                    };
                }

                _cache.Initialize(new[] { hedgeSettings });

                bool isDirty = false;

                if (hedgeSettings.ThresholdDownBuy == default(decimal))
                {
                    hedgeSettings.ThresholdDownBuy = hedgeSettings.ThresholdDown;
                    isDirty = true;
                }

                if (hedgeSettings.ThresholdDownSell == default(decimal))
                {
                    hedgeSettings.ThresholdDownSell = hedgeSettings.ThresholdDown;
                    isDirty = true;
                }

                if (hedgeSettings.ThresholdUpBuy == default(decimal))
                {
                    hedgeSettings.ThresholdUpBuy = hedgeSettings.ThresholdUp;
                    isDirty = true;
                }

                if (hedgeSettings.ThresholdUpSell == default(decimal))
                {
                    hedgeSettings.ThresholdUpSell = hedgeSettings.ThresholdUp;
                    isDirty = true;
                }

                if (isDirty)
                {
                    await UpdateAsync(hedgeSettings);
                }
            }

            return(hedgeSettings);
        }
        public async Task InsertOrReplaceAsync(HedgeSettings hedgeSettings)
        {
            var entity = new HedgeSettingsEntity(GetPartitionKey(), GetRowKey());

            Mapper.Map(hedgeSettings, entity);

            await _storage.InsertOrReplaceAsync(entity);
        }
 public static SettingsHedgeGlobalNoSql Create(HedgeSettings settings)
 {
     return(new SettingsHedgeGlobalNoSql()
     {
         PartitionKey = GeneratePartitionKey(),
         RowKey = GenerateRowKey(),
         Settings = settings
     });
 }
        private async Task <IReadOnlyCollection <HedgeLimitOrder> > CreateLimitOrdersAsync(
            IEnumerable <AssetInvestment> assetInvestments)
        {
            HedgeSettings hedgeSettings = await _hedgeSettingsService.GetAsync();

            var hedgeLimitOrders = new List <HedgeLimitOrder>();

            foreach (AssetInvestment assetInvestment in assetInvestments)
            {
                AssetHedgeSettings assetHedgeSettings =
                    await _assetHedgeSettingsService.EnsureAsync(assetInvestment.AssetId);

                LimitOrderType limitOrderType = assetInvestment.RemainingAmount > 0
                    ? LimitOrderType.Sell
                    : LimitOrderType.Buy;

                if (!CanCreateHedgeLimitOrder(assetInvestment, assetHedgeSettings, hedgeSettings, limitOrderType))
                {
                    continue;
                }

                decimal commonThresholdUp = limitOrderType == LimitOrderType.Buy
                    ? hedgeSettings.ThresholdUpBuy
                    : hedgeSettings.ThresholdUpSell;

                LimitOrderPrice limitOrderPrice = LimitOrderPriceCalculator.Calculate(assetInvestment.Quote,
                                                                                      Math.Abs(assetInvestment.RemainingAmount), limitOrderType,
                                                                                      assetHedgeSettings.ThresholdUp ?? commonThresholdUp, hedgeSettings.MarketOrderMarkup);

                decimal price = limitOrderPrice.Price;

                decimal volume = Math.Abs(assetInvestment.RemainingAmount / assetInvestment.Quote.Mid);

                HedgeLimitOrder hedgeLimitOrder = HedgeLimitOrder.Create(assetHedgeSettings.Exchange,
                                                                         assetHedgeSettings.AssetId, assetHedgeSettings.AssetPairId, limitOrderType, limitOrderPrice.Type,
                                                                         price, volume);

                hedgeLimitOrder.Context = assetInvestment.ToJson();

                hedgeLimitOrders.Add(hedgeLimitOrder);
            }

            return(hedgeLimitOrders);
        }
Beispiel #5
0
        public async Task <HedgeSettings> GetAsync()
        {
            HedgeSettings hedgeSettings = _cache.Get(CacheKey);

            if (hedgeSettings == null)
            {
                hedgeSettings = await _hedgeSettingsRepository.GetAsync();

                if (hedgeSettings == null)
                {
                    hedgeSettings = new HedgeSettings
                    {
                        MarketOrderMarkup = .02m,
                        ThresholdDown     = 1000,
                        ThresholdUp       = 5000,
                        ThresholdCritical = 10000
                    };
                }

                _cache.Initialize(new[] { hedgeSettings });
            }

            return(hedgeSettings);
        }
        private static bool CanCreateHedgeLimitOrder(AssetInvestment assetInvestment,
                                                     AssetHedgeSettings assetHedgeSettings, HedgeSettings hedgeSettings, LimitOrderType limitOrderType)
        {
            if (assetInvestment.IsDisabled)
            {
                return(false);
            }

            decimal absoluteRemainingAmount = Math.Abs(assetInvestment.RemainingAmount);

            if (absoluteRemainingAmount <= 0)
            {
                return(false);
            }

            decimal thresholdCritical = assetHedgeSettings.ThresholdCritical ?? hedgeSettings.ThresholdCritical;

            if (0 < thresholdCritical && thresholdCritical <= absoluteRemainingAmount)
            {
                return(false);
            }

            decimal commonThresholdDown = limitOrderType == LimitOrderType.Buy ? hedgeSettings.ThresholdDownBuy : hedgeSettings.ThresholdDownSell;

            decimal thresholdDown = assetHedgeSettings.ThresholdDown ?? commonThresholdDown;

            if (assetHedgeSettings.Exchange != ExchangeNames.Virtual && absoluteRemainingAmount < thresholdDown)
            {
                return(false);
            }

            if (assetHedgeSettings.Mode != AssetHedgeMode.Auto && assetHedgeSettings.Mode != AssetHedgeMode.Idle)
            {
                return(false);
            }

            if (assetInvestment.Quote == null)
            {
                return(false);
            }

            return(true);
        }
 public Task UpdateSettingsAsync(HedgeSettings settings)
 {
     throw new System.NotImplementedException();
 }
Beispiel #8
0
        public async Task UpdateAsync(HedgeSettings hedgeSettings)
        {
            await _hedgeSettingsRepository.InsertOrReplaceAsync(hedgeSettings);

            _cache.Set(hedgeSettings);
        }
        public async Task <HedgeSettingsModel> GetHedgeSettingsAsync()
        {
            HedgeSettings hedgeSettings = await _hedgeSettingsService.GetAsync();

            return(Mapper.Map <HedgeSettingsModel>(hedgeSettings));
        }
Beispiel #10
0
 public Task UpdateSettingsAsync(HedgeSettings request)
 {
     return(_settingsManager.UpdateSettingsAsync(request));
 }
        private async Task <IReadOnlyCollection <PositionReport> > CreateReports()
        {
            IReadOnlyCollection <Position> positions = await _positionService.GetAllAsync();

            IReadOnlyCollection <AssetInvestment> assetInvestments = _investmentService.GetAll();

            IReadOnlyCollection <HedgeLimitOrder> hedgeLimitOrders = _hedgeLimitOrderService.GetAll();

            HedgeSettings hedgeSettings = await _hedgeSettingsService.GetAsync();

            IReadOnlyCollection <AssetHedgeSettings>
            assetsHedgeSettings = await _assetHedgeSettingsService.GetAllAsync();

            string[] assets = positions.Select(o => o.AssetId)
                              .Union(assetInvestments.Select(o => o.AssetId))
                              .Union(assetsHedgeSettings.Select(o => o.AssetId))
                              .ToArray();

            var positionReports = new List <PositionReport>();

            foreach (string assetId in assets)
            {
                AssetHedgeSettings assetHedgeSettings = await _assetHedgeSettingsService.EnsureAsync(assetId);

                Position currentPosition = positions
                                           .SingleOrDefault(o => o.AssetId == assetId && o.Exchange == assetHedgeSettings.Exchange);

                HedgeLimitOrder hedgeLimitOrder = hedgeLimitOrders.SingleOrDefault(o => o.AssetId == assetId);

                AssetInvestment assetInvestment = assetInvestments.SingleOrDefault(o => o.AssetId == assetId);

                decimal?volumeInUsd = null;

                if (currentPosition != null)
                {
                    volumeInUsd = GetVolumeInUsd(currentPosition.AssetId, currentPosition.Exchange,
                                                 currentPosition.Volume);
                }

                Quote assetQuote;

                if (assetInvestment == null)
                {
                    assetQuote = _rateService.GetQuoteUsd(assetHedgeSettings.AssetId, assetHedgeSettings.Exchange);
                }
                else
                {
                    assetQuote = assetInvestment.Quote;
                }

                positionReports.Add(new PositionReport
                {
                    AssetId         = assetId,
                    Exchange        = assetHedgeSettings.Exchange,
                    Quote           = assetQuote,
                    Volume          = currentPosition?.Volume,
                    VolumeInUsd     = volumeInUsd,
                    OppositeVolume  = currentPosition?.OppositeVolume,
                    PnL             = volumeInUsd.HasValue ? currentPosition.OppositeVolume + volumeInUsd : null,
                    HedgeLimitOrder = hedgeLimitOrder,
                    AssetInvestment = assetInvestment,
                    Error           = ValidateAssetHedgeSettings(assetHedgeSettings)
                                      ?? ValidateInvestments(assetInvestment)
                                      ?? ValidateThresholdCritical(assetInvestment, hedgeSettings, assetHedgeSettings)
                                      ?? ValidateQuote(assetQuote)
                });

                IEnumerable <Position> otherPositions = positions
                                                        .Where(o => o.AssetId == assetId && o.Exchange != assetHedgeSettings.Exchange);

                foreach (Position position in otherPositions)
                {
                    Quote otherPositionQuote = _rateService.GetQuoteUsd(position.AssetId, position.Exchange);

                    volumeInUsd = GetVolumeInUsd(position.AssetId, position.Exchange, position.Volume);

                    positionReports.Add(new PositionReport
                    {
                        AssetId         = assetId,
                        Exchange        = position.Exchange,
                        Quote           = otherPositionQuote,
                        Volume          = position.Volume,
                        VolumeInUsd     = volumeInUsd,
                        OppositeVolume  = position.OppositeVolume,
                        PnL             = volumeInUsd.HasValue ? position.OppositeVolume + volumeInUsd : null,
                        HedgeLimitOrder = null,
                        AssetInvestment = null,
                        Error           = ValidateAssetHedgeSettings(assetHedgeSettings)
                                          ?? ValidateQuote(otherPositionQuote)
                    });
                }
            }

            foreach (PositionReport positionReport in positionReports)
            {
                if (positionReport.Exchange == ExchangeNames.Virtual)
                {
                    positionReport.ActualPnL = -1 * positionReport.PnL;
                }
                else
                {
                    positionReport.ActualPnL = positionReport.PnL;
                }
            }

            return(positionReports
                   .OrderBy(o => o.AssetId)
                   .ToArray());
        }
        private static string ValidateThresholdCritical(AssetInvestment assetInvestment, HedgeSettings hedgeSettings,
                                                        AssetHedgeSettings assetHedgeSettings)
        {
            decimal thresholdCritical = assetHedgeSettings.ThresholdCritical ?? hedgeSettings.ThresholdCritical;

            if (assetInvestment != null && thresholdCritical <= Math.Abs(assetInvestment.RemainingAmount))
            {
                return("Critical delta threshold exceeded");
            }

            return(null);
        }