Update bot market type

This commit is contained in:
2025-12-11 23:32:06 +07:00
parent 35df25915f
commit a254db6d24
20 changed files with 1986 additions and 44 deletions

View File

@@ -0,0 +1,56 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Managing.Infrastructure.Databases.Migrations
{
/// <inheritdoc />
public partial class AddTradingTypeToBacktestsAndBots : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "Ticker",
table: "Bots",
type: "text",
nullable: false,
oldClrType: typeof(int),
oldType: "integer");
migrationBuilder.AddColumn<string>(
name: "TradingType",
table: "Bots",
type: "text",
nullable: false,
defaultValue: "Spot");
migrationBuilder.AddColumn<int>(
name: "TradingType",
table: "Backtests",
type: "integer",
nullable: false,
defaultValue: 1);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "TradingType",
table: "Bots");
migrationBuilder.DropColumn(
name: "TradingType",
table: "Backtests");
migrationBuilder.AlterColumn<int>(
name: "Ticker",
table: "Bots",
type: "integer",
nullable: false,
oldClrType: typeof(string),
oldType: "text");
}
}
}

View File

@@ -259,6 +259,9 @@ namespace Managing.Infrastructure.Databases.Migrations
b.Property<int>("Timeframe")
.HasColumnType("integer");
b.Property<int>("TradingType")
.HasColumnType("integer");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("timestamp with time zone");
@@ -348,8 +351,9 @@ namespace Managing.Infrastructure.Databases.Migrations
.IsRequired()
.HasColumnType("text");
b.Property<int>("Ticker")
.HasColumnType("integer");
b.Property<string>("Ticker")
.IsRequired()
.HasColumnType("text");
b.Property<int>("TradeLosses")
.HasColumnType("integer");
@@ -357,6 +361,10 @@ namespace Managing.Infrastructure.Databases.Migrations
b.Property<int>("TradeWins")
.HasColumnType("integer");
b.Property<string>("TradingType")
.IsRequired()
.HasColumnType("text");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("timestamp with time zone");

View File

@@ -50,6 +50,10 @@ public class BacktestEntity
[Required]
public int Timeframe { get; set; }
// Stored trading type as enum numeric value for direct filtering
[Required]
public int TradingType { get; set; }
// Comma-separated indicator types for filtering, e.g., "EMA_CROSS,MACD_CROSS"
[Column(TypeName = "text")]
public string IndicatorsCsv { get; set; } = string.Empty;

View File

@@ -13,6 +13,8 @@ public class BotEntity
public Ticker Ticker { get; set; }
public TradingType TradingType { get; set; }
public int UserId { get; set; }
[ForeignKey("UserId")] public UserEntity User { get; set; }

View File

@@ -161,6 +161,7 @@ public class ManagingDbContext : DbContext
entity.Property(e => e.Name).IsRequired().HasMaxLength(255);
entity.Property(e => e.Ticker).HasMaxLength(32);
entity.Property(e => e.Timeframe).IsRequired();
entity.Property(e => e.TradingType).IsRequired();
entity.Property(e => e.IndicatorsCsv).HasColumnType("text");
entity.Property(e => e.IndicatorsCount).IsRequired();
entity.Property(e => e.PositionsJson).HasColumnType("jsonb");
@@ -522,6 +523,8 @@ public class ManagingDbContext : DbContext
entity.HasKey(e => e.Identifier);
entity.Property(e => e.Identifier).IsRequired().HasMaxLength(255);
entity.Property(e => e.Name).IsRequired().HasMaxLength(255);
entity.Property(e => e.Ticker).IsRequired().HasConversion<string>();
entity.Property(e => e.TradingType).IsRequired().HasConversion<string>();
entity.Property(e => e.Status).IsRequired().HasConversion<string>();
entity.Property(e => e.CreateDate).IsRequired();
entity.Property(e => e.StartupTime).IsRequired();

View File

@@ -438,6 +438,8 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
baseQuery = baseQuery.Where(b => b.Duration >= filter.DurationMin.Value);
if (filter.DurationMax.HasValue)
baseQuery = baseQuery.Where(b => b.Duration <= filter.DurationMax.Value);
if (filter.TradingType.HasValue)
baseQuery = baseQuery.Where(b => b.TradingType == (int)filter.TradingType.Value);
}
var entities = await baseQuery.ToListAsync().ConfigureAwait(false);
@@ -503,6 +505,8 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
baseQuery = baseQuery.Where(b => b.Duration >= filter.DurationMin.Value);
if (filter.DurationMax.HasValue)
baseQuery = baseQuery.Where(b => b.Duration <= filter.DurationMax.Value);
if (filter.TradingType.HasValue)
baseQuery = baseQuery.Where(b => b.TradingType == (int)filter.TradingType.Value);
}
var afterQueryMs = stopwatch.ElapsedMilliseconds;
@@ -642,6 +646,8 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
baseQuery = baseQuery.Where(b => b.Duration >= filter.DurationMin.Value);
if (filter.DurationMax.HasValue)
baseQuery = baseQuery.Where(b => b.Duration <= filter.DurationMax.Value);
if (filter.TradingType.HasValue)
baseQuery = baseQuery.Where(b => b.TradingType == (int)filter.TradingType.Value);
}
var afterQueryMs = stopwatch.ElapsedMilliseconds;

View File

@@ -350,6 +350,7 @@ public static class PostgreSqlMappers
Name = backtest.Config?.Name ?? string.Empty,
Ticker = backtest.Config?.Ticker.ToString() ?? string.Empty,
Timeframe = (int)backtest.Config.Timeframe,
TradingType = (int)backtest.Config.TradingType,
IndicatorsCsv = string.Join(',', backtest.Config.Scenario.Indicators.Select(i => i.Type.ToString())),
IndicatorsCount = backtest.Config.Scenario.Indicators.Count,
PositionsJson = JsonConvert.SerializeObject(backtest.Positions.Values.ToList(), jsonSettings),
@@ -750,6 +751,7 @@ public static class PostgreSqlMappers
CreateDate = entity.CreateDate,
Name = entity.Name,
Ticker = entity.Ticker,
TradingType = entity.TradingType,
StartupTime = entity.StartupTime,
LastStartTime = entity.LastStartTime,
LastStopTime = entity.LastStopTime,
@@ -782,6 +784,7 @@ public static class PostgreSqlMappers
CreateDate = bot.CreateDate,
Name = bot.Name,
Ticker = bot.Ticker,
TradingType = bot.TradingType,
StartupTime = bot.StartupTime,
LastStartTime = bot.LastStartTime,
LastStopTime = bot.LastStopTime,