private void UpdateTrendState(TrendInfo trendInfo, TradeSide side, ILocalSnapshotService localSnapshotService) { var pricesOpt = CalculatePrices(trendInfo.TargetEis, trendInfo.ReferenceEis, side, localSnapshotService); if (pricesOpt == null) { return; } var prices = pricesOpt.Value; var referenceDeltaPrice = PriceTrendCalculator.CalcPriceOffset(prices.ReferencePrice, prices.TargetPrice, 0); lock (trendInfo.Sync) { var mutTrendInfoBySide = trendInfo.GetMutable(side); var now = _dateTimeService.UtcNow; mutTrendInfoBySide.UpdateStateIncrementally(referenceDeltaPrice, now); var isReset = prices.TargetSpread > TargetSpreadThreshold || Math.Abs(referenceDeltaPrice) > ReferenceDeltaPriceThreshold; var exceededDeltaPriceStartTime = trendInfo.ExceededDeltaPriceStartTime; if (exceededDeltaPriceStartTime != null) { if (isReset) { trendInfo.TryResetProhibitionPeriod(now, ResetPeriod); } else { trendInfo.AbortReset(); if (exceededDeltaPriceStartTime + WindowPeriod <= now) { // prohibition period is ended trendInfo.ExceededDeltaPriceStartTime = null; var buyTrendInfo = trendInfo.GetMutable(TradeSide.Buy); buyTrendInfo.Equilibrium = buyTrendInfo.CalcMeanDeltaPrice(); var sellTrendInfo = trendInfo.GetMutable(TradeSide.Sell); sellTrendInfo.Equilibrium = sellTrendInfo.CalcMeanDeltaPrice(); } } } else { if (isReset) { trendInfo.TryResetProhibitionPeriod(now, ResetPeriod); } else { trendInfo.AbortReset(); mutTrendInfoBySide.Equilibrium = mutTrendInfoBySide.CalcMeanDeltaPrice(); } } } }
private void UpdateTrends(ExchangeIdSymbol eventEis, TradeSide side, ILocalSnapshotService localSnapshotService) { if (!_relatedTrends.TryGetValue(eventEis.ToTradePlace(), out var trendInfoList)) { return; } foreach (var trendInfo in trendInfoList) { UpdateTrendState(trendInfo, side, localSnapshotService); } }
private (decimal TargetPrice, decimal TargetSpread, decimal ReferencePrice)? CalculatePrices( ExchangeIdSymbol targetEis, ExchangeIdSymbol referenceEis, TradeSide side, ILocalSnapshotService localSnapshotService) { if (!localSnapshotService.TryGetSnapshot(targetEis.ExchangeNameSymbol, out var targetSnapshot)) { _log.Warning($"Can't get target snapshot {{eis}} in {nameof(TrendService)}", targetEis); return(null); } var filteredTargetTopPrices = PriceTrendCalculator.CalculateFilteredTopPrices(targetEis, _exchangesById, targetSnapshot); var targetPriceOpt = filteredTargetTopPrices.GetPrice(side); var targetPriceReverseOpt = filteredTargetTopPrices.GetPrice(side.ChangeSide()); if (targetPriceOpt == null) { return(null); } if (targetPriceReverseOpt == null) { return(null); } var referencePriceBySide = PriceTrendCalculator.CalculateReferencePrice( referenceEis, side, localSnapshotService, _log, out _); if (referencePriceBySide == null) { return(null); } return(targetPriceOpt.Value, Math.Abs(targetPriceReverseOpt.Value - targetPriceOpt.Value), referencePriceBySide.Value); }
public void UpdateTrends(ExchangeIdSymbol eis, ILocalSnapshotService localSnapshotService) { UpdateTrends(eis, TradeSide.Buy, localSnapshotService); UpdateTrends(eis, TradeSide.Sell, localSnapshotService); }