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
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using Managing.Common;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||
|
||||
[BsonCollection("FundingRates")]
|
||||
public class FundingRateDto : Document
|
||||
{
|
||||
public Enums.Ticker Ticker { get; set; }
|
||||
public decimal Rate { get; set; }
|
||||
public Enums.TradingExchanges Exchange { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
public Enums.TradeDirection Direction { get; set; }
|
||||
}
|
||||
@@ -16,7 +16,8 @@ namespace Managing.Infrastructure.Databases.MongoDb;
|
||||
|
||||
public static class MongoMappers
|
||||
{
|
||||
#region Statistics
|
||||
#region Statistics
|
||||
|
||||
internal static TopVolumeTickerDto Map(TopVolumeTicker topVolumeTicker)
|
||||
{
|
||||
return new TopVolumeTickerDto
|
||||
@@ -44,6 +45,7 @@ public static class MongoMappers
|
||||
#endregion
|
||||
|
||||
#region Accounts
|
||||
|
||||
internal static AccountDto Map(Account request)
|
||||
{
|
||||
return new AccountDto
|
||||
@@ -84,9 +86,11 @@ public static class MongoMappers
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Workers
|
||||
|
||||
internal static WorkerDto Map(Worker worker)
|
||||
{
|
||||
return new WorkerDto
|
||||
@@ -118,6 +122,7 @@ public static class MongoMappers
|
||||
#endregion
|
||||
|
||||
#region Backtests
|
||||
|
||||
internal static Backtest Map(BacktestDto b)
|
||||
{
|
||||
return new Backtest(
|
||||
@@ -165,7 +170,8 @@ public static class MongoMappers
|
||||
|
||||
#endregion
|
||||
|
||||
#region Candles
|
||||
#region Candles
|
||||
|
||||
public static Candle Map(CandleDto candle)
|
||||
{
|
||||
if (candle == null)
|
||||
@@ -213,10 +219,10 @@ public static class MongoMappers
|
||||
return candles.ConvertAll(candle => Map(candle));
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Positions
|
||||
|
||||
public static PositionDto Map(Position position)
|
||||
{
|
||||
var p = new PositionDto
|
||||
@@ -267,9 +273,13 @@ public static class MongoMappers
|
||||
|
||||
public static Position Map(PositionDto dto)
|
||||
{
|
||||
var position = new Position(dto.AccountName, originDirection: dto.OriginDirection, dto.Ticker, Map(dto.MoneyManagement), dto.Initiator, dto.Date)
|
||||
var position = new Position(dto.AccountName, originDirection: dto.OriginDirection, dto.Ticker,
|
||||
Map(dto.MoneyManagement), dto.Initiator, dto.Date)
|
||||
{
|
||||
Open = new Trade(date: dto.Open.Date, direction: dto.Open.Direction, status: dto.Open.Status, tradeType: dto.Open.TradeType, ticker: dto.Open.Ticker, quantity: dto.Open.Quantity, price: dto.Open.Price, leverage: dto.Open.Leverage, exchangeOrderId: dto.Open.ExchangeOrderId, message: dto.Open.Message),
|
||||
Open = new Trade(date: dto.Open.Date, direction: dto.Open.Direction, status: dto.Open.Status,
|
||||
tradeType: dto.Open.TradeType, ticker: dto.Open.Ticker, quantity: dto.Open.Quantity,
|
||||
price: dto.Open.Price, leverage: dto.Open.Leverage, exchangeOrderId: dto.Open.ExchangeOrderId,
|
||||
message: dto.Open.Message),
|
||||
ProfitAndLoss = new ProfitAndLoss { Realized = dto.ProfitAndLoss },
|
||||
Status = dto.Status,
|
||||
SignalIdentifier = dto.SignalIdentifier,
|
||||
@@ -278,17 +288,26 @@ public static class MongoMappers
|
||||
|
||||
if (dto.StopLoss != null)
|
||||
{
|
||||
position.StopLoss = new Trade(date: dto.StopLoss.Date, direction: dto.StopLoss.Direction, status: dto.StopLoss.Status, tradeType: dto.StopLoss.TradeType, ticker: dto.StopLoss.Ticker, quantity: dto.StopLoss.Quantity, price: dto.StopLoss.Price, leverage: dto.StopLoss.Leverage, exchangeOrderId: dto.StopLoss.ExchangeOrderId, message: dto.StopLoss.Message);
|
||||
position.StopLoss = new Trade(date: dto.StopLoss.Date, direction: dto.StopLoss.Direction,
|
||||
status: dto.StopLoss.Status, tradeType: dto.StopLoss.TradeType, ticker: dto.StopLoss.Ticker,
|
||||
quantity: dto.StopLoss.Quantity, price: dto.StopLoss.Price, leverage: dto.StopLoss.Leverage,
|
||||
exchangeOrderId: dto.StopLoss.ExchangeOrderId, message: dto.StopLoss.Message);
|
||||
}
|
||||
|
||||
if (dto.TakeProfit1 != null)
|
||||
{
|
||||
position.TakeProfit1 = new Trade(date: dto.TakeProfit1.Date, direction: dto.TakeProfit1.Direction, status: dto.TakeProfit1.Status, tradeType: dto.TakeProfit1.TradeType, ticker: dto.TakeProfit1.Ticker, quantity: dto.TakeProfit1.Quantity, price: dto.TakeProfit1.Price, leverage: dto.TakeProfit1.Leverage, exchangeOrderId: dto.TakeProfit1.ExchangeOrderId, message: dto.TakeProfit1.Message);
|
||||
position.TakeProfit1 = new Trade(date: dto.TakeProfit1.Date, direction: dto.TakeProfit1.Direction,
|
||||
status: dto.TakeProfit1.Status, tradeType: dto.TakeProfit1.TradeType, ticker: dto.TakeProfit1.Ticker,
|
||||
quantity: dto.TakeProfit1.Quantity, price: dto.TakeProfit1.Price, leverage: dto.TakeProfit1.Leverage,
|
||||
exchangeOrderId: dto.TakeProfit1.ExchangeOrderId, message: dto.TakeProfit1.Message);
|
||||
}
|
||||
|
||||
if (dto.TakeProfit2 != null)
|
||||
{
|
||||
position.TakeProfit2 = new Trade(date: dto.TakeProfit2.Date, direction: dto.TakeProfit2.Direction, status: dto.TakeProfit2.Status, tradeType: dto.TakeProfit2.TradeType, ticker: dto.TakeProfit2.Ticker, quantity: dto.TakeProfit2.Quantity, price: dto.TakeProfit2.Price, leverage: dto.TakeProfit2.Leverage, exchangeOrderId: dto.TakeProfit2.ExchangeOrderId, message: dto.TakeProfit2.Message);
|
||||
position.TakeProfit2 = new Trade(date: dto.TakeProfit2.Date, direction: dto.TakeProfit2.Direction,
|
||||
status: dto.TakeProfit2.Status, tradeType: dto.TakeProfit2.TradeType, ticker: dto.TakeProfit2.Ticker,
|
||||
quantity: dto.TakeProfit2.Quantity, price: dto.TakeProfit2.Price, leverage: dto.TakeProfit2.Leverage,
|
||||
exchangeOrderId: dto.TakeProfit2.ExchangeOrderId, message: dto.TakeProfit2.Message);
|
||||
}
|
||||
|
||||
return position;
|
||||
@@ -302,6 +321,7 @@ public static class MongoMappers
|
||||
#endregion
|
||||
|
||||
#region Signals
|
||||
|
||||
public static SignalDto Map(Signal signal)
|
||||
{
|
||||
return new SignalDto
|
||||
@@ -330,7 +350,8 @@ public static class MongoMappers
|
||||
|
||||
#endregion
|
||||
|
||||
#region Scenarios
|
||||
#region Scenarios
|
||||
|
||||
public static ScenarioDto Map(Scenario scenario)
|
||||
{
|
||||
return new ScenarioDto()
|
||||
@@ -373,6 +394,7 @@ public static class MongoMappers
|
||||
CyclePeriods = strategyDto.CyclePeriods
|
||||
};
|
||||
}
|
||||
|
||||
internal static StrategyDto Map(Strategy strategy)
|
||||
{
|
||||
var dto = new StrategyDto
|
||||
@@ -430,6 +452,7 @@ public static class MongoMappers
|
||||
#endregion
|
||||
|
||||
#region Money Management
|
||||
|
||||
public static MoneyManagementDto Map(MoneyManagement request)
|
||||
{
|
||||
if (request == null) return null;
|
||||
@@ -497,16 +520,25 @@ public static class MongoMappers
|
||||
Scenario = new ScenarioDto
|
||||
{
|
||||
Name = spotlight.Scenario.Name,
|
||||
Strategies = spotlight.Scenario.Strategies.ConvertAll(spotlightScenarioStrategy => Map(spotlightScenarioStrategy))
|
||||
Strategies =
|
||||
spotlight.Scenario.Strategies.ConvertAll(
|
||||
spotlightScenarioStrategy => Map(spotlightScenarioStrategy))
|
||||
},
|
||||
TickerSignals = spotlight.TickerSignals.ConvertAll(spotlightTickerSignal => new TickerSignalDto
|
||||
{
|
||||
Ticker = spotlightTickerSignal.Ticker,
|
||||
FiveMinutes = spotlightTickerSignal.FiveMinutes?.ConvertAll(spotlightTickerSignalFiveMinute => Map(spotlightTickerSignalFiveMinute)) ?? new List<SignalDto>(),
|
||||
FifteenMinutes = spotlightTickerSignal.FifteenMinutes?.ConvertAll(spotlightTickerSignalFifteenMinute => Map(spotlightTickerSignalFifteenMinute)) ?? new List<SignalDto>(),
|
||||
OneHour = spotlightTickerSignal.OneHour?.ConvertAll(spotlightTickerSignalOneHour => Map(spotlightTickerSignalOneHour)) ?? new List<SignalDto>(),
|
||||
FourHour = spotlightTickerSignal.FourHour?.ConvertAll(spotlightTickerSignalFourHour => Map(spotlightTickerSignalFourHour)) ?? new List<SignalDto>(),
|
||||
OneDay = spotlightTickerSignal.OneDay?.ConvertAll(spotlightTickerSignalOneDay => Map(spotlightTickerSignalOneDay)) ?? new List<SignalDto>()
|
||||
FiveMinutes =
|
||||
spotlightTickerSignal.FiveMinutes?.ConvertAll(spotlightTickerSignalFiveMinute =>
|
||||
Map(spotlightTickerSignalFiveMinute)) ?? new List<SignalDto>(),
|
||||
FifteenMinutes =
|
||||
spotlightTickerSignal.FifteenMinutes?.ConvertAll(spotlightTickerSignalFifteenMinute =>
|
||||
Map(spotlightTickerSignalFifteenMinute)) ?? new List<SignalDto>(),
|
||||
OneHour = spotlightTickerSignal.OneHour?.ConvertAll(spotlightTickerSignalOneHour =>
|
||||
Map(spotlightTickerSignalOneHour)) ?? new List<SignalDto>(),
|
||||
FourHour = spotlightTickerSignal.FourHour?.ConvertAll(spotlightTickerSignalFourHour =>
|
||||
Map(spotlightTickerSignalFourHour)) ?? new List<SignalDto>(),
|
||||
OneDay = spotlightTickerSignal.OneDay?.ConvertAll(spotlightTickerSignalOneDay =>
|
||||
Map(spotlightTickerSignalOneDay)) ?? new List<SignalDto>()
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -528,16 +560,23 @@ public static class MongoMappers
|
||||
{
|
||||
Scenario = new Scenario(name: spotlight.Scenario.Name)
|
||||
{
|
||||
Strategies = spotlight.Scenario.Strategies.ConvertAll(spotlightScenarioStrategy => Map(spotlightScenarioStrategy))
|
||||
Strategies =
|
||||
spotlight.Scenario.Strategies.ConvertAll(
|
||||
spotlightScenarioStrategy => Map(spotlightScenarioStrategy))
|
||||
},
|
||||
TickerSignals = spotlight.TickerSignals.ConvertAll(spotlightTickerSignal => new TickerSignal
|
||||
{
|
||||
Ticker = spotlightTickerSignal.Ticker,
|
||||
FiveMinutes = spotlightTickerSignal.FiveMinutes.ConvertAll(spotlightTickerSignalFiveMinute => Map(spotlightTickerSignalFiveMinute)),
|
||||
FifteenMinutes = spotlightTickerSignal.FifteenMinutes.ConvertAll(spotlightTickerSignalFifteenMinute => Map(spotlightTickerSignalFifteenMinute)),
|
||||
OneHour = spotlightTickerSignal.OneHour.ConvertAll(spotlightTickerSignalOneHour => Map(spotlightTickerSignalOneHour)),
|
||||
FourHour = spotlightTickerSignal.FourHour.ConvertAll(spotlightTickerSignalFourHour => Map(spotlightTickerSignalFourHour)),
|
||||
OneDay = spotlightTickerSignal.OneDay.ConvertAll(spotlightTickerSignalOneDay => Map(spotlightTickerSignalOneDay))
|
||||
FiveMinutes = spotlightTickerSignal.FiveMinutes.ConvertAll(spotlightTickerSignalFiveMinute =>
|
||||
Map(spotlightTickerSignalFiveMinute)),
|
||||
FifteenMinutes = spotlightTickerSignal.FifteenMinutes.ConvertAll(spotlightTickerSignalFifteenMinute =>
|
||||
Map(spotlightTickerSignalFifteenMinute)),
|
||||
OneHour = spotlightTickerSignal.OneHour.ConvertAll(spotlightTickerSignalOneHour =>
|
||||
Map(spotlightTickerSignalOneHour)),
|
||||
FourHour = spotlightTickerSignal.FourHour.ConvertAll(spotlightTickerSignalFourHour =>
|
||||
Map(spotlightTickerSignalFourHour)),
|
||||
OneDay = spotlightTickerSignal.OneDay.ConvertAll(spotlightTickerSignalOneDay =>
|
||||
Map(spotlightTickerSignalOneDay))
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -675,4 +714,31 @@ public static class MongoMappers
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public static FundingRate Map(FundingRateDto fundingRate)
|
||||
{
|
||||
if (fundingRate == null)
|
||||
return null;
|
||||
|
||||
return new FundingRate
|
||||
{
|
||||
Exchange = fundingRate.Exchange,
|
||||
Rate = fundingRate.Rate,
|
||||
Ticker = fundingRate.Ticker,
|
||||
Date = fundingRate.Date,
|
||||
Direction = fundingRate.Direction
|
||||
};
|
||||
}
|
||||
|
||||
public static FundingRateDto Map(FundingRate fundingRate)
|
||||
{
|
||||
return new FundingRateDto
|
||||
{
|
||||
Exchange = fundingRate.Exchange,
|
||||
Rate = fundingRate.Rate,
|
||||
Ticker = fundingRate.Ticker,
|
||||
Date = fundingRate.Date,
|
||||
Direction = fundingRate.Direction
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -8,21 +8,24 @@ namespace Managing.Infrastructure.Databases;
|
||||
|
||||
public class StatisticRepository : IStatisticRepository
|
||||
{
|
||||
private readonly IMongoRepository<BestTraderDto> _bestTrader;
|
||||
private readonly IMongoRepository<BestTraderDto> _bestTrader;
|
||||
private readonly IMongoRepository<BadTraderDto> _badTrader;
|
||||
private readonly IMongoRepository<TopVolumeTickerDto> _topRepository;
|
||||
private readonly IMongoRepository<SpotlighOverviewDto> _spotlightRepository;
|
||||
private readonly IMongoRepository<FundingRateDto> _fundingRateRepository;
|
||||
|
||||
public StatisticRepository(
|
||||
IMongoRepository<TopVolumeTickerDto> topRepository,
|
||||
IMongoRepository<SpotlighOverviewDto> spotlightRepository,
|
||||
IMongoRepository<BestTraderDto> bestTrader,
|
||||
IMongoRepository<BadTraderDto> badTrader)
|
||||
IMongoRepository<BadTraderDto> badTrader,
|
||||
IMongoRepository<FundingRateDto> fundingRateRepository)
|
||||
{
|
||||
_topRepository = topRepository;
|
||||
_spotlightRepository = spotlightRepository;
|
||||
_bestTrader = bestTrader;
|
||||
_badTrader = badTrader;
|
||||
_fundingRateRepository = fundingRateRepository;
|
||||
}
|
||||
|
||||
public async Task InsertTopVolumeTicker(TopVolumeTicker topVolumeTicker)
|
||||
@@ -62,7 +65,7 @@ public class StatisticRepository : IStatisticRepository
|
||||
|
||||
public void UpdateBestTrader(Trader trader)
|
||||
{
|
||||
var t = _bestTrader.FindOne(t => t.Address == trader.Address);
|
||||
var t = _bestTrader.FindOne(t => t.Address == trader.Address);
|
||||
var dto = MongoMappers.Map(trader);
|
||||
dto.Id = t.Id;
|
||||
_bestTrader.Update(dto);
|
||||
@@ -100,4 +103,28 @@ public class StatisticRepository : IStatisticRepository
|
||||
{
|
||||
await _badTrader.DeleteOneAsync(t => t.Address == trader.Address);
|
||||
}
|
||||
}
|
||||
|
||||
public List<FundingRate> GetFundingRates()
|
||||
{
|
||||
return _fundingRateRepository.FindAll().Select(MongoMappers.Map).ToList();
|
||||
}
|
||||
|
||||
public async Task RemoveFundingRate(FundingRate oldRate)
|
||||
{
|
||||
var rate = _fundingRateRepository.FindOne(r => r.Ticker == oldRate.Ticker && r.Exchange == oldRate.Exchange);
|
||||
await _fundingRateRepository.DeleteOneAsync(r => r.Id == rate.Id);
|
||||
}
|
||||
|
||||
public async Task InsertFundingRate(FundingRate newRate)
|
||||
{
|
||||
await _fundingRateRepository.InsertOneAsync(MongoMappers.Map(newRate));
|
||||
}
|
||||
|
||||
public void UpdateFundingRate(FundingRate oldRate, FundingRate newRate)
|
||||
{
|
||||
var f = _fundingRateRepository.FindOne(t => t.Ticker == oldRate.Ticker && t.Exchange == oldRate.Exchange);
|
||||
var dto = MongoMappers.Map(newRate);
|
||||
dto.Id = f.Id;
|
||||
_fundingRateRepository.Update(dto);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user