diff --git a/KLHZ.Trader.Core.Math/Declisions/Utils/MovingAverage.cs b/KLHZ.Trader.Core.Math/Declisions/Utils/MovingAverage.cs index 25a1d85..43fdb75 100644 --- a/KLHZ.Trader.Core.Math/Declisions/Utils/MovingAverage.cs +++ b/KLHZ.Trader.Core.Math/Declisions/Utils/MovingAverage.cs @@ -20,7 +20,7 @@ namespace KLHZ.Trader.Core.Math.Declisions.Utils return (startTime, sum / count); } - public static (TradingEvent events, decimal bigWindowAv, decimal smallWindowAv) CheckByWindowAverageMean(DateTime[] timestamps, decimal[] prices, int size, int smallWindow, int bigWindow, TimeSpan timeForUptreandStart, decimal meanfullStep = 3m) + public static (TradingEvent events, decimal bigWindowAv, decimal smallWindowAv) CheckByWindowAverageMean(DateTime[] timestamps, decimal[] prices, int size, int smallWindow, int bigWindow, TimeSpan timeForUptreandStart, decimal downtrendStartingDetectionMeanfullStep = 3m, decimal uptrendEndingDetectionMeanfullStep = 3m) { var res = TradingEvent.None; var bigWindowAv = 0m; @@ -94,7 +94,7 @@ namespace KLHZ.Trader.Core.Math.Declisions.Utils // если фильтрация окном 15 наползает на окно 120 сверху, потенциальное время закрытия лонга и возможно открытия шорта if (twavss[size - 1] <= twavbs[size - 1] && twavss[size - 2] > twavbs[size - 2]) { - if (diffTotal >= meanfullStep + if (diffTotal >= uptrendEndingDetectionMeanfullStep && times[crossings[0]] - times[crossings[1]] >= timeForUptreandStart) { res |= TradingEvent.UptrendEnd; @@ -112,7 +112,7 @@ namespace KLHZ.Trader.Core.Math.Declisions.Utils // если фильтрация окном 120 наползает на окно 15 сверху, потенциальное время открытия лонга и закрытия шорта if (twavss[size - 1] >= twavbs[size - 1] && twavss[size - 2] < twavbs[size - 2]) { - if (pricesForFinalComparison[crossings[0]] - pricesForFinalComparison[crossings[1]] <= -meanfullStep + if (pricesForFinalComparison[crossings[0]] - pricesForFinalComparison[crossings[1]] <= -downtrendStartingDetectionMeanfullStep && times[crossings[0]] - times[crossings[1]] >= timeForUptreandStart) { res |= TradingEvent.UptrendStart; diff --git a/KLHZ.Trader.Core/Exchange/Services/ExchangeDataReader.cs b/KLHZ.Trader.Core/Exchange/Services/ExchangeDataReader.cs index 086ce34..384a156 100644 --- a/KLHZ.Trader.Core/Exchange/Services/ExchangeDataReader.cs +++ b/KLHZ.Trader.Core/Exchange/Services/ExchangeDataReader.cs @@ -55,9 +55,10 @@ namespace KLHZ.Trader.Core.Exchange.Services { try { + //_ = SubscribeUpdates(); if (_exchangeDataRecievingEnabled) { - await SubscribePrices(); + _ = SubscribePrices(); } await Task.Delay(1000); } @@ -68,6 +69,23 @@ namespace KLHZ.Trader.Core.Exchange.Services } } + private async Task SubscribeUpdates() + { + var req = new TradesStreamRequest(); + foreach (var a in _tradeDataProvider.Accounts) + { + req.Accounts.Add(a.Key); + } + using var stream = _investApiClient.OrdersStream.TradesStream(req); + await foreach (var response in stream.ResponseStream.ReadAllAsync()) + { + if (response.OrderTrades != null) + { + + } + } + } + private async Task SubscribePrices() { using var stream = _investApiClient.MarketDataStream.MarketDataStream(); diff --git a/KLHZ.Trader.Core/Exchange/Services/Trader.cs b/KLHZ.Trader.Core/Exchange/Services/Trader.cs index df6008b..9410407 100644 --- a/KLHZ.Trader.Core/Exchange/Services/Trader.cs +++ b/KLHZ.Trader.Core/Exchange/Services/Trader.cs @@ -145,14 +145,9 @@ namespace KLHZ.Trader.Core.Exchange.Services INewPrice message, int windowMaxSize) { var res = TradingEvent.None; - var resultMoveAvFull = MovingAverage.CheckByWindowAverageMean(data.timestamps, data.prices, windowMaxSize, 25, 120, TimeSpan.FromSeconds(20), 1.5m); - //var resultLongClose = MovingAverage.CheckByWindowAverageMean(data.timestamps, data.prices, windowMaxSize, 15, 120, 1.5m).events; + var resultMoveAvFull = MovingAverage.CheckByWindowAverageMean(data.timestamps, data.prices, + windowMaxSize, 25, 120, TimeSpan.FromSeconds(20), 1m, 1.5m); - //ar uptrendStarts = LocalTrends.CheckByLocalTrends(data.timestamps, data.prices, TimeSpan.FromSeconds(120), TimeSpan.FromSeconds(20), 1.5m, 15); - - - //res |= (uptrendStarts & TradingEvent.UptrendStart); - //res |= resultLongClose; res |= resultMoveAvFull.events; if (resultMoveAvFull.bigWindowAv != 0) @@ -160,12 +155,6 @@ namespace KLHZ.Trader.Core.Exchange.Services await LogPrice(message, _bigWindowProcessor, resultMoveAvFull.bigWindowAv); await LogPrice(message, _smallWindowProcessor, resultMoveAvFull.smallWindowAv); } - if ((resultMoveAvFull.events & TradingEvent.StopBuy) == TradingEvent.StopBuy) - { - var stopTo = (message.IsHistoricalData ? message.Time : DateTime.UtcNow).AddMinutes(_buyStopLength / 2); - //OpeningStops.AddOrUpdate(message.Figi, stopTo, (k, v) => stopTo); - //await LogDeclision(DeclisionTradeAction.StopBuy, message); - } if ((res & TradingEvent.UptrendStart) == TradingEvent.UptrendStart && !OpeningStops.TryGetValue(message.Figi, out _) @@ -221,7 +210,7 @@ namespace KLHZ.Trader.Core.Exchange.Services { var assetType = _tradeDataProvider.GetAssetTypeByFigi(message.Figi); var loggedDeclisions = 0; - if (!message.IsHistoricalData && BotModeSwitcher.CanSell()) + if (BotModeSwitcher.CanSell()) { var assetsForClose = _tradeDataProvider.Accounts .SelectMany(a => a.Value.Assets.Values) diff --git a/KLHZ.Trader.Core/Exchange/Utils/TradingCalculator.cs b/KLHZ.Trader.Core/Exchange/Utils/TradingCalculator.cs index 81e4393..f3f26d8 100644 --- a/KLHZ.Trader.Core/Exchange/Utils/TradingCalculator.cs +++ b/KLHZ.Trader.Core/Exchange/Utils/TradingCalculator.cs @@ -4,7 +4,13 @@ { public static decimal CaclProfit(decimal openPrice, decimal closePrice, decimal comission, decimal leverage, bool isShort) { - var diff = ((isShort ? (closePrice - openPrice) : (openPrice - closePrice)) - closePrice * comission - openPrice * comission) * leverage; + var summComission = closePrice * comission + openPrice * comission; + var priceDiff = closePrice - openPrice; + if (isShort) + { + priceDiff *= -1; + } + var diff = priceDiff * leverage - summComission; return diff; } } diff --git a/KLHZ.Trader.Service/Controllers/PlayController.cs b/KLHZ.Trader.Service/Controllers/PlayController.cs index f733497..5438596 100644 --- a/KLHZ.Trader.Service/Controllers/PlayController.cs +++ b/KLHZ.Trader.Service/Controllers/PlayController.cs @@ -24,7 +24,7 @@ namespace KLHZ.Trader.Service.Controllers { try { - var time = DateTime.UtcNow.AddMinutes(-40); + var time = DateTime.UtcNow.AddMinutes(-35); using var context1 = await _dbContextFactory.CreateDbContextAsync(); context1.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; var data = await context1.PriceChanges