Compare commits

..

No commits in common. "7d2618dd54dcfa033a70d272e2fb961e64eeeec2" and "13ecf126ed3c2b1a37ca7cad27f2c9bba7e68c1b" have entirely different histories.

9 changed files with 64 additions and 43 deletions

View File

@ -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 downtrendStartingDetectionMeanfullStep = 3m, decimal uptrendEndingDetectionMeanfullStep = 3m)
public static (TradingEvent events, decimal bigWindowAv, decimal smallWindowAv) CheckByWindowAverageMean(DateTime[] timestamps, decimal[] prices, int size, int smallWindow, int bigWindow, TimeSpan timeForUptreandStart, decimal meanfullStep = 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 >= uptrendEndingDetectionMeanfullStep
if (diffTotal >= meanfullStep
&& 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]] <= -downtrendStartingDetectionMeanfullStep
if (pricesForFinalComparison[crossings[0]] - pricesForFinalComparison[crossings[1]] <= - meanfullStep
&& times[crossings[0]] - times[crossings[1]] >= timeForUptreandStart)
{
res |= TradingEvent.UptrendStart;

View File

@ -1,4 +1,6 @@
namespace KLHZ.Trader.Core.Math.Declisions.Utils
using System;
namespace KLHZ.Trader.Core.Math.Declisions.Utils
{
public static class ShapeAreaCalculator
{

View File

@ -55,10 +55,9 @@ namespace KLHZ.Trader.Core.Exchange.Services
{
try
{
//_ = SubscribeUpdates();
if (_exchangeDataRecievingEnabled)
{
_ = SubscribePrices();
await SubscribePrices();
}
await Task.Delay(1000);
}
@ -69,27 +68,15 @@ 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();
//var request = new SubscribeLastPriceRequest
//{
// SubscriptionAction = SubscriptionAction.Subscribe
//};
var tradesRequest = new SubscribeTradesRequest
{
SubscriptionAction = SubscriptionAction.Subscribe
@ -102,6 +89,12 @@ namespace KLHZ.Trader.Core.Exchange.Services
foreach (var f in _instrumentsFigis)
{
//request.Instruments.Add(
// new LastPriceInstrument()
// {
// InstrumentId = f
// });
tradesRequest.Instruments.Add(
new TradeInstrument()
{
@ -116,6 +109,11 @@ namespace KLHZ.Trader.Core.Exchange.Services
});
}
//await stream.RequestStream.WriteAsync(new MarketDataRequest
//{
// SubscribeLastPriceRequest = request,
//});
await stream.RequestStream.WriteAsync(new MarketDataRequest
{
SubscribeTradesRequest = tradesRequest,
@ -131,6 +129,20 @@ namespace KLHZ.Trader.Core.Exchange.Services
var lastWrite = DateTime.UtcNow;
await foreach (var response in stream.ResponseStream.ReadAllAsync())
{
//if (response.LastPrice != null)
//{
// var message = new PriceChange()
// {
// Figi = response.LastPrice.Figi,
// Ticker = _tradeDataProvider.GetTickerByFigi(response.LastPrice.Figi),
// Time = response.LastPrice.Time.ToDateTime().ToUniversalTime(),
// Value = response.LastPrice.Price,
// IsHistoricalData = false,
// };
// await _eventBus.Broadcast(message);
// pricesBuffer.Add(message);
//}
if (response.Trade != null)
{
var message = new PriceChange()

View File

@ -145,9 +145,14 @@ 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), 1m, 1.5m);
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;
//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)
@ -155,6 +160,12 @@ 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 _)
@ -210,7 +221,7 @@ namespace KLHZ.Trader.Core.Exchange.Services
{
var assetType = _tradeDataProvider.GetAssetTypeByFigi(message.Figi);
var loggedDeclisions = 0;
if (BotModeSwitcher.CanSell())
if (!message.IsHistoricalData && BotModeSwitcher.CanSell())
{
var assetsForClose = _tradeDataProvider.Accounts
.SelectMany(a => a.Value.Assets.Values)

View File

@ -1,4 +1,5 @@
using KLHZ.Trader.Core.Contracts.Declisions.Interfaces;
using Google.Protobuf.WellKnownTypes;
using KLHZ.Trader.Core.Contracts.Declisions.Interfaces;
using KLHZ.Trader.Core.Contracts.Messaging.Dtos;
using KLHZ.Trader.Core.Contracts.Messaging.Dtos.Interfaces;
using KLHZ.Trader.Core.DataLayer;

View File

@ -4,13 +4,7 @@
{
public static decimal CaclProfit(decimal openPrice, decimal closePrice, decimal comission, decimal leverage, bool isShort)
{
var summComission = closePrice * comission + openPrice * comission;
var priceDiff = closePrice - openPrice;
if (isShort)
{
priceDiff *= -1;
}
var diff = priceDiff * leverage - summComission;
var diff = ((isShort ? (closePrice - openPrice) : (openPrice - closePrice)) - closePrice * comission - openPrice * comission) * leverage;
return diff;
}
}

View File

@ -1,6 +1,7 @@
using KLHZ.Trader.Core.Contracts.Messaging.Dtos;
using KLHZ.Trader.Core.Contracts.Messaging.Interfaces;
using KLHZ.Trader.Core.DataLayer;
using KLHZ.Trader.Core.DataLayer.Entities.Prices;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -24,7 +25,7 @@ namespace KLHZ.Trader.Service.Controllers
{
try
{
var time = DateTime.UtcNow.AddMinutes(-35);
var time = DateTime.UtcNow.AddMinutes(-40);
using var context1 = await _dbContextFactory.CreateDbContextAsync();
context1.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
var data = await context1.PriceChanges