Files
managing-apps/src/Managing.Infrastructure.Exchanges/ExchangeService.cs
Oda 029ba5f40e Add funding rate watcher (#2)
* Add FundingRate interfaces and worker

* Add build on PR

* Remove zip

* Specify the solution path

* Add build for worker too

* Set up StatisticService.cs for funding rate

* Add Fundingrate alerts

* Send alert when big funding rate change + add SlashCommands.cs for fundingrate

* Remove fixtures

* Refact names

* Renames
2024-07-19 08:31:09 +07:00

250 lines
9.6 KiB
C#

using Managing.Application.Abstractions.Repositories;
using Managing.Application.Abstractions.Services;
using Managing.Domain.Accounts;
using Managing.Domain.Candles;
using Managing.Domain.Statistics;
using Managing.Domain.Trades;
using Managing.Infrastructure.Exchanges.Abstractions;
using Microsoft.Extensions.Logging;
using static Managing.Common.Enums;
namespace Managing.Infrastructure.Exchanges
{
public class ExchangeService : IExchangeService
{
private readonly ILogger<ExchangeService> _logger;
private readonly ICandleRepository _candleRepository;
private readonly IEnumerable<IExchangeProcessor> _exchangeProcessor;
public ExchangeService(ILogger<ExchangeService> logger, ICandleRepository candleRepository, IEnumerable<IExchangeProcessor> processor)
{
_logger = logger;
_candleRepository = candleRepository;
_exchangeProcessor = processor;
}
#region Trades
public async Task<Trade> OpenTrade(
Account account,
Ticker ticker,
TradeDirection direction,
decimal price,
decimal quantity,
decimal? leverage = null,
TradeType tradeType = TradeType.Limit,
bool reduceOnly = false,
bool isForPaperTrading = false,
DateTime? currentDate = null,
bool ioc = true)
{
_logger.LogInformation($"OpenMarketTrade - {ticker} - Type: {tradeType} - {direction} - Price: {price} - Quantity: {quantity} - Leverage: {leverage}");
if (isForPaperTrading)
{
return BuildEmptyTrade(ticker, price, quantity, direction, leverage, tradeType, currentDate.Value, reduceOnly ? TradeStatus.PendingOpen : TradeStatus.Filled);
}
var processor = GetProcessor(account);
return await processor.OpenTrade(account, ticker, direction, price, quantity, leverage, tradeType, reduceOnly, isForPaperTrading, currentDate, ioc);
}
private IExchangeProcessor GetProcessor(Account account)
{
return _exchangeProcessor.First(e => e.Exchange() == account.Exchange);
}
public Trade BuildEmptyTrade(Ticker ticker, decimal price, decimal quantity, TradeDirection direction, decimal? leverage, TradeType tradeType,
DateTime dateTime, TradeStatus tradeStatus)
{
return new Trade(dateTime, direction, tradeStatus, tradeType, ticker, quantity, price, leverage,
Guid.NewGuid().ToString(), "EmptyTrade");
}
public async Task<Trade> OpenStopLoss(Account account, Ticker ticker, TradeDirection originalDirection, decimal stopLossPrice,
decimal quantity, bool isForPaperTrading = false, DateTime? currentDate = null)
{
return await OpenTrade(
account,
ticker,
originalDirection == TradeDirection.Long ? TradeDirection.Short : TradeDirection.Long,
stopLossPrice,
quantity,
tradeType: TradeType.StopLoss,
isForPaperTrading: isForPaperTrading,
currentDate: currentDate,
reduceOnly: true);
}
public async Task<Trade> OpenTakeProfit(Account account, Ticker ticker, TradeDirection originalDirection, decimal takeProfitPrice,
decimal quantity, bool isForPaperTrading = false, DateTime? currentDate = null)
{
return await OpenTrade(
account,
ticker,
originalDirection == TradeDirection.Long ? TradeDirection.Short : TradeDirection.Long,
takeProfitPrice,
quantity,
tradeType: TradeType.TakeProfit,
isForPaperTrading: isForPaperTrading,
currentDate: currentDate,
reduceOnly: true);
}
public Task<Trade> ClosePosition(Account account, Position position, decimal lastPrice, bool isForPaperTrading = false)
{
var direction = position.OriginDirection == TradeDirection.Long ? TradeDirection.Short : TradeDirection.Long;
if (isForPaperTrading)
{
var fake = BuildEmptyTrade(position.Open.Ticker,
lastPrice,
position.Open.Quantity,
direction,
position.Open.Leverage,
TradeType.Market,
position.Open.Date,
TradeStatus.Filled);
return Task.FromResult(fake);
}
var processor = GetProcessor(account);
var closedTrade = processor.OpenTrade(
account,
position.Ticker,
direction,
lastPrice,
position.Open.Quantity,
position.Open.Leverage,
tradeType: TradeType.Market,
reduceOnly: true).Result;
if (account.Exchange != Common.Enums.TradingExchanges.Evm)
{
closedTrade.Price = processor.GetTrade(account, closedTrade.ExchangeOrderId, closedTrade.Ticker).Result.Price;
}
return Task.FromResult(closedTrade);
}
#endregion
public async Task<bool> CancelOrder(Account account, Ticker ticker)
{
var processor = GetProcessor(account);
return await processor.CancelOrder(account, ticker);
}
public async Task<Trade> GetTrade(Account account, string orderId, Ticker ticker)
{
var processor = GetProcessor(account);
return await processor.GetTrade(account, orderId, ticker);
}
public async Task<Trade> GetTrade(string reference, string orderId, Ticker ticker)
{
var processor = _exchangeProcessor.First(e => e.Exchange() == Common.Enums.TradingExchanges.Evm);
return await processor.GetTrade(reference, orderId, ticker);
}
public Task<List<FundingRate>> GetFundingRates()
{
var processor = _exchangeProcessor.First(e => e.Exchange() == Common.Enums.TradingExchanges.Evm);
return processor.GetFundingRates();
}
public async Task<List<Trade>> GetTrades(Account account, Ticker ticker)
{
var processor = GetProcessor(account);
return await processor.GetTrades(account, ticker);
}
public async Task<List<Candle>> GetCandles(Account account, Ticker ticker, DateTime startDate, Timeframe timeframe)
{
var processor = GetProcessor(account);
return await processor.GetCandles(account, ticker, startDate, timeframe);
}
public async Task<List<Candle>> GetCandlesInflux(TradingExchanges exchange, Ticker ticker, DateTime startDate, Timeframe timeframe)
{
var candlesFromRepo = await _candleRepository.GetCandles(exchange, ticker, timeframe, startDate);
return candlesFromRepo.ToList();
}
public async Task<decimal> GetBalance(Account account, bool isForPaperTrading = false)
{
if (isForPaperTrading)
{
return 1000;
}
var processor = GetProcessor(account);
return await processor.GetBalance(account);
}
public decimal GetFee(Account account, bool isForPaperTrading = false)
{
var processor = GetProcessor(account);
return processor.GetFee(account);
}
public decimal GetPrice(Account account, Ticker ticker, DateTime date)
{
var processor = GetProcessor(account);
return processor.GetPrice(account, ticker, date);
}
public Candle GetCandle(Account account, Ticker ticker, DateTime date)
{
var processor = GetProcessor(account);
return processor.GetCandle(account, ticker, date);
}
public async Task<decimal> GetQuantityInPosition(Account account, Ticker ticker)
{
var processor = GetProcessor(account);
return await processor.GetQuantityInPosition(account, ticker);
}
public decimal GetVolume(Account account, Ticker ticker)
{
var processor = GetProcessor(account);
return processor.GetVolume(account, ticker);
}
public async Task<List<Ticker>> GetTickers(Account account, Timeframe timeframe)
{
var tickers = await _candleRepository.GetTickersAsync(account.Exchange, timeframe, DateTime.UtcNow.AddDays(-2));
return tickers.ToList();
}
public decimal GetBestPrice(Account account, Ticker ticker, decimal lastPrice, decimal quantity, TradeDirection direction)
{
if (account.Exchange == Common.Enums.TradingExchanges.Evm)
{
return GetPrice(account, ticker, DateTime.UtcNow);
}
return GetOrderbook(account, ticker).GetBestPrice(direction, quantity);
}
public Orderbook GetOrderbook(Account account, Ticker ticker)
{
var processor = GetProcessor(account);
return processor.GetOrderbook(account, ticker);
}
public async Task<List<Balance>> GetBalances(Account account, bool isForPaperTrading = false)
{
var processor = GetProcessor(account);
return await processor.GetBalances(account, isForPaperTrading);
}
public async Task<List<Trade>> GetOpenOrders(Account account, Ticker ticker)
{
var processor = GetProcessor(account);
return await processor.GetOrders(account, ticker);
}
}
}