# Generated by Django 5.2.10 on 2026-02-05 07:29

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name="FactorDefinitionModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "code",
                    models.CharField(
                        db_index=True, max_length=50, unique=True, verbose_name="因子代码"
                    ),
                ),
                ("name", models.CharField(max_length=100, verbose_name="因子名称")),
                (
                    "category",
                    models.CharField(
                        choices=[
                            ("value", "价值"),
                            ("quality", "质量"),
                            ("growth", "成长"),
                            ("momentum", "动量"),
                            ("volatility", "波动"),
                            ("liquidity", "流动性"),
                            ("technical", "技术"),
                        ],
                        max_length=20,
                        verbose_name="因子类别",
                    ),
                ),
                ("description", models.TextField(blank=True, verbose_name="因子描述")),
                ("data_source", models.CharField(max_length=50, verbose_name="数据来源")),
                ("data_field", models.CharField(max_length=100, verbose_name="数据字段")),
                (
                    "direction",
                    models.CharField(
                        choices=[("positive", "正向"), ("negative", "反向"), ("neutral", "中性")],
                        default="positive",
                        max_length=20,
                        verbose_name="因子方向",
                    ),
                ),
                (
                    "update_frequency",
                    models.CharField(default="daily", max_length=20, verbose_name="更新频率"),
                ),
                ("is_active", models.BooleanField(default=True, verbose_name="是否启用")),
                ("min_data_points", models.IntegerField(default=20, verbose_name="最小数据点")),
                ("allow_missing", models.BooleanField(default=False, verbose_name="允许缺失")),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
            ],
            options={
                "verbose_name": "因子定义",
                "verbose_name_plural": "因子定义",
                "db_table": "factor_definition",
                "ordering": ["category", "code"],
            },
        ),
        migrations.CreateModel(
            name="FactorPortfolioConfigModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("name", models.CharField(max_length=100, unique=True, verbose_name="配置名称")),
                ("description", models.TextField(blank=True, verbose_name="配置描述")),
                ("factor_weights", models.JSONField(default=dict, verbose_name="因子权重")),
                (
                    "universe",
                    models.CharField(default="all_a", max_length=20, verbose_name="股票池"),
                ),
                (
                    "min_market_cap",
                    models.DecimalField(
                        blank=True,
                        decimal_places=2,
                        max_digits=18,
                        null=True,
                        verbose_name="最小市值（亿）",
                    ),
                ),
                (
                    "max_market_cap",
                    models.DecimalField(
                        blank=True,
                        decimal_places=2,
                        max_digits=18,
                        null=True,
                        verbose_name="最大市值（亿）",
                    ),
                ),
                (
                    "max_pe",
                    models.DecimalField(
                        blank=True,
                        decimal_places=4,
                        max_digits=10,
                        null=True,
                        verbose_name="最大PE",
                    ),
                ),
                (
                    "min_pe",
                    models.DecimalField(
                        blank=True,
                        decimal_places=4,
                        max_digits=10,
                        null=True,
                        verbose_name="最小PE",
                    ),
                ),
                (
                    "max_pb",
                    models.DecimalField(
                        blank=True,
                        decimal_places=4,
                        max_digits=10,
                        null=True,
                        verbose_name="最大PB",
                    ),
                ),
                (
                    "max_debt_ratio",
                    models.FloatField(blank=True, null=True, verbose_name="最大资产负债率（%）"),
                ),
                ("top_n", models.IntegerField(default=30, verbose_name="选股数量")),
                (
                    "rebalance_frequency",
                    models.CharField(default="monthly", max_length=20, verbose_name="调仓频率"),
                ),
                (
                    "weight_method",
                    models.CharField(
                        default="equal_weight", max_length=50, verbose_name="权重方式"
                    ),
                ),
                ("max_sector_weight", models.FloatField(default=0.4, verbose_name="最大行业权重")),
                (
                    "max_single_stock_weight",
                    models.FloatField(default=0.05, verbose_name="最大单股权重"),
                ),
                ("is_active", models.BooleanField(default=True, verbose_name="是否启用")),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
            ],
            options={
                "verbose_name": "因子组合配置",
                "verbose_name_plural": "因子组合配置",
                "db_table": "factor_portfolio_config",
                "ordering": ["-is_active", "-created_at"],
            },
        ),
        migrations.CreateModel(
            name="FactorExposureModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "stock_code",
                    models.CharField(db_index=True, max_length=20, verbose_name="股票代码"),
                ),
                ("trade_date", models.DateField(db_index=True, verbose_name="交易日期")),
                (
                    "factor_code",
                    models.CharField(db_index=True, max_length=50, verbose_name="因子代码"),
                ),
                (
                    "factor_value",
                    models.DecimalField(decimal_places=6, max_digits=18, verbose_name="因子值"),
                ),
                (
                    "percentile_rank",
                    models.DecimalField(decimal_places=4, max_digits=5, verbose_name="百分位排名"),
                ),
                (
                    "z_score",
                    models.DecimalField(decimal_places=6, max_digits=10, verbose_name="Z得分"),
                ),
                (
                    "normalized_score",
                    models.DecimalField(
                        decimal_places=2, default=0.0, max_digits=6, verbose_name="标准化得分"
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
            ],
            options={
                "verbose_name": "因子暴露度",
                "verbose_name_plural": "因子暴露度",
                "db_table": "factor_exposure",
                "ordering": ["-trade_date", "-percentile_rank"],
                "indexes": [
                    models.Index(
                        fields=["trade_date", "factor_code"], name="factor_expo_trade_d_4bfb44_idx"
                    ),
                    models.Index(
                        fields=["stock_code", "trade_date"], name="factor_expo_stock_c_c19dc3_idx"
                    ),
                ],
                "unique_together": {("stock_code", "trade_date", "factor_code")},
            },
        ),
        migrations.CreateModel(
            name="FactorPerformanceModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "factor_code",
                    models.CharField(db_index=True, max_length=50, verbose_name="因子代码"),
                ),
                ("period_start", models.DateField(verbose_name="期间开始")),
                ("period_end", models.DateField(verbose_name="期间结束")),
                ("total_return", models.FloatField(verbose_name="总收益率")),
                ("annual_return", models.FloatField(verbose_name="年化收益率")),
                ("sharpe_ratio", models.FloatField(verbose_name="夏普比率")),
                ("max_drawdown", models.FloatField(verbose_name="最大回撤")),
                ("win_rate", models.FloatField(verbose_name="胜率")),
                ("avg_rank", models.FloatField(verbose_name="平均排名")),
                ("turnover_rate", models.FloatField(default=0.0, verbose_name="换手率")),
                ("created_at", models.DateTimeField(auto_now_add=True)),
            ],
            options={
                "verbose_name": "因子表现",
                "verbose_name_plural": "因子表现",
                "db_table": "factor_performance",
                "ordering": ["-period_start", "factor_code"],
                "unique_together": {("factor_code", "period_start", "period_end")},
            },
        ),
        migrations.CreateModel(
            name="FactorPortfolioHoldingModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("trade_date", models.DateField(db_index=True, verbose_name="交易日期")),
                ("stock_code", models.CharField(max_length=20, verbose_name="股票代码")),
                ("stock_name", models.CharField(max_length=100, verbose_name="股票名称")),
                (
                    "weight",
                    models.DecimalField(decimal_places=4, max_digits=5, verbose_name="权重"),
                ),
                (
                    "factor_score",
                    models.DecimalField(decimal_places=4, max_digits=10, verbose_name="因子得分"),
                ),
                ("rank", models.IntegerField(verbose_name="排名")),
                ("sector", models.CharField(blank=True, max_length=50, verbose_name="行业")),
                ("factor_scores", models.JSONField(default=dict, verbose_name="因子得分明细")),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "config",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="holdings",
                        to="factor.factorportfolioconfigmodel",
                        verbose_name="配置",
                    ),
                ),
            ],
            options={
                "verbose_name": "因子组合持仓",
                "verbose_name_plural": "因子组合持仓",
                "db_table": "factor_portfolio_holdings",
                "ordering": ["-trade_date", "rank"],
                "indexes": [
                    models.Index(
                        fields=["config", "trade_date"], name="factor_port_config__cda0d6_idx"
                    ),
                    models.Index(
                        fields=["trade_date", "rank"], name="factor_port_trade_d_e73825_idx"
                    ),
                ],
                "unique_together": {("config", "trade_date", "stock_code")},
            },
        ),
    ]
