Add synthApi (#27)

* Add synthApi

* Put confidence for Synth proba

* Update the code

* Update readme

* Fix bootstraping

* fix github build

* Update the endpoints for scenario

* Add scenario and update backtest modal

* Update bot modal

* Update interfaces for synth

* add synth to backtest

* Add Kelly criterion and better signal

* Update signal confidence

* update doc

* save leaderboard and prediction

* Update nswag to generate ApiClient in the correct path

* Unify the trading modal

* Save miner and prediction

* Update messaging and block new signal until position not close when flipping off

* Rename strategies to indicators

* Update doc

* Update chart + add signal name

* Fix signal direction

* Update docker webui

* remove crypto npm

* Clean
This commit is contained in:
Oda
2025-07-03 00:13:42 +07:00
committed by GitHub
parent 453806356d
commit a547c4a040
103 changed files with 9916 additions and 810 deletions

View File

@@ -0,0 +1,59 @@
using Managing.Domain.Synth.Models;
namespace Managing.Application.Abstractions.Repositories;
/// <summary>
/// Repository interface for Synth-related data operations
/// Provides MongoDB persistence for leaderboard and individual predictions data
/// </summary>
public interface ISynthRepository
{
/// <summary>
/// Gets cached leaderboard data by cache key
/// </summary>
/// <param name="cacheKey">The cache key to search for</param>
/// <returns>Cached leaderboard data if found, null otherwise</returns>
Task<SynthMinersLeaderboard?> GetLeaderboardAsync(string cacheKey);
/// <summary>
/// Saves leaderboard data to MongoDB
/// </summary>
/// <param name="leaderboard">The leaderboard data to save</param>
Task SaveLeaderboardAsync(SynthMinersLeaderboard leaderboard);
/// <summary>
/// Gets individual cached prediction data by asset, parameters, and miner UIDs
/// </summary>
/// <param name="asset">Asset symbol</param>
/// <param name="timeIncrement">Time increment in seconds</param>
/// <param name="timeLength">Time length in seconds</param>
/// <param name="minerUids">List of miner UIDs to get predictions for</param>
/// <param name="isBacktest">Whether this is backtest data</param>
/// <param name="signalDate">Signal date for backtest data</param>
/// <returns>List of cached individual predictions</returns>
Task<List<SynthPrediction>> GetIndividualPredictionsAsync(
string asset,
int timeIncrement,
int timeLength,
List<int> minerUids,
bool isBacktest,
DateTime? signalDate);
/// <summary>
/// Saves individual prediction data to MongoDB
/// </summary>
/// <param name="prediction">The individual prediction data to save</param>
Task SaveIndividualPredictionAsync(SynthPrediction prediction);
/// <summary>
/// Saves multiple individual predictions to MongoDB in batch
/// </summary>
/// <param name="predictions">The list of individual predictions to save</param>
Task SaveIndividualPredictionsAsync(List<SynthPrediction> predictions);
/// <summary>
/// Cleans up old cached data beyond the retention period
/// </summary>
/// <param name="retentionDays">Number of days to retain data</param>
Task CleanupOldDataAsync(int retentionDays = 30);
}

View File

@@ -0,0 +1,59 @@
using Managing.Domain.Synth.Models;
namespace Managing.Application.Abstractions.Services;
/// <summary>
/// Interface for communicating with the Synth API
/// </summary>
public interface ISynthApiClient
{
/// <summary>
/// Fetches the current leaderboard from Synth API
/// </summary>
/// <param name="config">Synth configuration containing API key and settings</param>
/// <returns>List of miners with their rankings and stats</returns>
Task<List<MinerInfo>> GetLeaderboardAsync(SynthConfiguration config);
/// <summary>
/// Fetches historical leaderboard data from Synth API for a specific time range
/// </summary>
/// <param name="startTime">Start time for historical data (ISO 8601 format)</param>
/// <param name="endTime">End time for historical data (ISO 8601 format)</param>
/// <param name="config">Synth configuration containing API key and settings</param>
/// <returns>List of miners with their historical rankings and stats</returns>
Task<List<MinerInfo>> GetHistoricalLeaderboardAsync(DateTime startTime, DateTime endTime, SynthConfiguration config);
/// <summary>
/// Fetches latest predictions from specified miners
/// </summary>
/// <param name="minerUids">List of miner UIDs to get predictions from</param>
/// <param name="asset">Asset symbol (e.g., "BTC", "ETH")</param>
/// <param name="timeIncrement">Time interval in seconds between each prediction point</param>
/// <param name="timeLength">Total prediction time length in seconds</param>
/// <param name="config">Synth configuration containing API key and settings</param>
/// <returns>List of predictions from the specified miners</returns>
Task<List<MinerPrediction>> GetMinerPredictionsAsync(
List<int> minerUids,
string asset,
int timeIncrement,
int timeLength,
SynthConfiguration config);
/// <summary>
/// Fetches historical predictions from specified miners for a specific time point
/// </summary>
/// <param name="minerUids">List of miner UIDs to get predictions from</param>
/// <param name="asset">Asset symbol (e.g., "BTC", "ETH")</param>
/// <param name="startTime">Start time for historical predictions (when the prediction was made)</param>
/// <param name="timeIncrement">Time interval in seconds between each prediction point</param>
/// <param name="timeLength">Total prediction time length in seconds</param>
/// <param name="config">Synth configuration containing API key and settings</param>
/// <returns>List of historical predictions from the specified miners</returns>
Task<List<MinerPrediction>> GetHistoricalMinerPredictionsAsync(
List<int> minerUids,
string asset,
DateTime startTime,
int timeIncrement,
int timeLength,
SynthConfiguration config);
}

View File

@@ -0,0 +1,109 @@
using Managing.Domain.Bots;
using Managing.Domain.MoneyManagements;
using Managing.Domain.Strategies;
using Managing.Domain.Synth.Models;
using static Managing.Common.Enums;
namespace Managing.Application.Abstractions.Services;
/// <summary>
/// Service interface for Synth prediction business logic and probability calculations
/// </summary>
public interface ISynthPredictionService
{
/// <summary>
/// Calculates the probability of price reaching a target within a specified time horizon
/// </summary>
/// <param name="asset">Asset symbol (e.g., "BTC", "ETH")</param>
/// <param name="currentPrice">Current market price</param>
/// <param name="targetPrice">Target price to reach</param>
/// <param name="timeHorizonSeconds">Time horizon in seconds</param>
/// <param name="isLongPosition">True for long positions (liquidation when price drops), false for short positions (liquidation when price rises)</param>
/// <param name="config">Synth configuration for this operation</param>
/// <returns>Probability as a decimal between 0.0 and 1.0</returns>
Task<decimal> GetProbabilityOfTargetPriceAsync(
string asset,
decimal currentPrice,
decimal targetPrice,
int timeHorizonSeconds,
bool isLongPosition,
SynthConfiguration config);
/// <summary>
/// Gets probabilities for multiple price thresholds at once
/// </summary>
/// <param name="asset">Asset symbol</param>
/// <param name="currentPrice">Current market price</param>
/// <param name="priceThresholds">Dictionary of threshold names to prices</param>
/// <param name="timeHorizonSeconds">Time horizon in seconds</param>
/// <param name="isLongPosition">True for long positions, false for short positions</param>
/// <param name="config">Synth configuration for this operation</param>
/// <param name="isBacktest">Parameter for backtest</param>
/// <param name="signalDate">Signal date</param>
/// <returns>Dictionary of threshold names to probabilities</returns>
Task<Dictionary<string, decimal>> GetMultipleThresholdProbabilitiesAsync(
string asset,
decimal currentPrice,
Dictionary<string, decimal> priceThresholds,
int timeHorizonSeconds,
bool isLongPosition,
SynthConfiguration config,
bool isBacktest,
DateTime signalDate);
/// <summary>
/// Clears cached predictions (useful for testing or forced refresh)
/// </summary>
void ClearCache();
/// <summary>
/// Clears cached predictions from MongoDB asynchronously
/// </summary>
Task ClearCacheAsync();
/// <summary>
/// Validates a trading signal using Synth predictions to check for adverse price movements
/// </summary>
/// <param name="signal">The trading signal containing ticker, direction, candle data, and other context</param>
/// <param name="currentPrice">Current market price (required)</param>
/// <param name="botConfig">Bot configuration with Synth settings</param>
/// <param name="isBacktest">Whether this is a backtest</param>
/// <param name="customThresholds">Custom probability thresholds for decision-making. If null, uses default thresholds.</param>
/// <returns>Comprehensive signal validation result including confidence, probabilities, and risk analysis</returns>
Task<SignalValidationResult> ValidateSignalAsync(Signal signal, decimal currentPrice,
TradingBotConfig botConfig, bool isBacktest, Dictionary<string, decimal> customThresholds = null);
/// <summary>
/// Performs risk assessment before opening a position
/// </summary>
/// <param name="ticker">Trading ticker</param>
/// <param name="direction">Position direction</param>
/// <param name="currentPrice">Current market price</param>
/// <param name="botConfig">Bot configuration with Synth settings</param>
/// <param name="isBacktest">Whether this is a backtest</param>
/// <returns>True if position should be allowed, false if blocked</returns>
Task<bool> AssessPositionRiskAsync(Ticker ticker, TradeDirection direction, decimal currentPrice,
TradingBotConfig botConfig, bool isBacktest);
/// <summary>
/// Monitors liquidation risk for an open position
/// </summary>
/// <param name="ticker">Trading ticker</param>
/// <param name="direction">Position direction</param>
/// <param name="currentPrice">Current market price</param>
/// <param name="liquidationPrice">Position liquidation price</param>
/// <param name="positionIdentifier">Position identifier for logging</param>
/// <param name="botConfig">Bot configuration with Synth settings</param>
/// <returns>Risk assessment result</returns>
Task<SynthRiskResult> MonitorPositionRiskAsync(Ticker ticker, TradeDirection direction, decimal currentPrice,
decimal liquidationPrice, string positionIdentifier, TradingBotConfig botConfig);
/// <summary>
/// Estimates liquidation price based on money management settings
/// </summary>
/// <param name="currentPrice">Current market price</param>
/// <param name="direction">Position direction</param>
/// <param name="moneyManagement">Money management settings</param>
/// <returns>Estimated liquidation price</returns>
decimal EstimateLiquidationPrice(decimal currentPrice, TradeDirection direction, MoneyManagement moneyManagement);
}

View File

@@ -1,7 +1,9 @@
using Managing.Domain.Accounts;
using Managing.Domain.Bots;
using Managing.Domain.Scenarios;
using Managing.Domain.Statistics;
using Managing.Domain.Strategies;
using Managing.Domain.Synth.Models;
using Managing.Domain.Trades;
using Managing.Infrastructure.Evm.Models.Privy;
using static Managing.Common.Enums;
@@ -37,4 +39,15 @@ public interface ITradingService
void UpdateStrategy(Indicator indicator);
Task<IEnumerable<Position>> GetBrokerPositions(Account account);
Task<PrivyInitAddressResponse> InitPrivyWallet(string publicAddress);
// Synth API integration methods
Task<SignalValidationResult> ValidateSynthSignalAsync(Signal signal, decimal currentPrice,
TradingBotConfig botConfig,
bool isBacktest);
Task<bool> AssessSynthPositionRiskAsync(Ticker ticker, TradeDirection direction, decimal currentPrice,
TradingBotConfig botConfig, bool isBacktest);
Task<SynthRiskResult> MonitorSynthPositionRiskAsync(Ticker ticker, TradeDirection direction, decimal currentPrice,
decimal liquidationPrice, string positionIdentifier, TradingBotConfig botConfig);
}