# Generated by Django 5.2.10 on 2026-02-07 09:17

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


class Migration(migrations.Migration):

    dependencies = [
        (
            "strategy",
            "0003_portfoliostrategyassignmentmodel_portfolio_s_portfol_55e41a_idx_and_more",
        ),
    ]

    operations = [
        migrations.CreateModel(
            name="PositionManagementRuleModel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("name", models.CharField(max_length=200, verbose_name="规则名称")),
                ("description", models.TextField(blank=True, verbose_name="规则描述")),
                (
                    "is_active",
                    models.BooleanField(db_index=True, default=True, verbose_name="是否启用"),
                ),
                (
                    "price_precision",
                    models.PositiveSmallIntegerField(
                        default=2,
                        validators=[
                            django.core.validators.MinValueValidator(0),
                            django.core.validators.MaxValueValidator(8),
                        ],
                        verbose_name="价格精度",
                    ),
                ),
                ("variables_schema", models.JSONField(blank=True, default=list)),
                (
                    "buy_condition_expr",
                    models.TextField(
                        blank=True,
                        help_text="示例: current_price <= support_price and volume_ratio >= 1.2",
                        verbose_name="买入触发条件表达式",
                    ),
                ),
                (
                    "sell_condition_expr",
                    models.TextField(
                        blank=True,
                        help_text="示例: current_price >= resistance_price or regime_score < 0.4",
                        verbose_name="卖出触发条件表达式",
                    ),
                ),
                (
                    "buy_price_expr",
                    models.TextField(
                        help_text="示例: breakout_price * (1 + slippage_pct)",
                        verbose_name="建议买入价表达式",
                    ),
                ),
                (
                    "sell_price_expr",
                    models.TextField(
                        help_text="示例: buy_price * (1 + target_return_pct)",
                        verbose_name="建议卖出价表达式",
                    ),
                ),
                (
                    "stop_loss_expr",
                    models.TextField(
                        help_text="示例: min(structure_low, buy_price - 2 * atr)",
                        verbose_name="止损价表达式",
                    ),
                ),
                (
                    "take_profit_expr",
                    models.TextField(
                        help_text="示例: buy_price + 2 * (buy_price - stop_loss_price)",
                        verbose_name="止盈价表达式",
                    ),
                ),
                (
                    "position_size_expr",
                    models.TextField(
                        help_text="示例: (account_equity * risk_per_trade_pct) / abs(buy_price - stop_loss_price)",
                        verbose_name="仓位计算表达式",
                    ),
                ),
                ("metadata", models.JSONField(blank=True, default=dict)),
                ("created_at", models.DateTimeField(auto_now_add=True, verbose_name="创建时间")),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="更新时间")),
                (
                    "strategy",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="position_management_rule",
                        to="strategy.strategymodel",
                        verbose_name="所属策略",
                    ),
                ),
            ],
            options={
                "verbose_name": "仓位管理规则",
                "verbose_name_plural": "仓位管理规则",
                "db_table": "position_management_rule",
                "ordering": ["-updated_at"],
                "indexes": [
                    models.Index(
                        fields=["strategy", "is_active"], name="position_ma_strateg_eed198_idx"
                    ),
                    models.Index(
                        fields=["is_active", "-updated_at"], name="position_ma_is_acti_297fa9_idx"
                    ),
                ],
            },
        ),
    ]
