# Generated by Django 5.2.9 on 2026-01-04

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='FeeConfigModel',
            options={
                'verbose_name': '交易费率配置',
                'verbose_name_plural': '交易费率配置',
                'ordering': ['asset_type', 'config_name'],
                'db_table': 'simulated_fee_config',
            },
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('config_name', models.CharField(max_length=100, unique=True, verbose_name='配置名称')),
                ('asset_type', models.CharField(choices=[('all', '通用'), ('equity', '股票'), ('fund', '基金'), ('bond', '债券')], default='all', max_length=20, verbose_name='资产类型')),
                ('commission_rate_buy', models.FloatField(default=0.0003, help_text='默认0.03%', verbose_name='买入手续费率')),
                ('commission_rate_sell', models.FloatField(default=0.0003, help_text='默认0.03%', verbose_name='卖出手续费率')),
                ('min_commission', models.FloatField(default=5.0, help_text='不足按此收取', verbose_name='最低手续费(元)')),
                ('stamp_duty_rate', models.FloatField(default=0.001, help_text='默认0.1%,仅股票', verbose_name='印花税率(卖出)')),
                ('transfer_fee_rate', models.FloatField(default=2e-05, help_text='默认0.002%', verbose_name='过户费率')),
                ('min_transfer_fee', models.FloatField(default=0.0, verbose_name='最低过户费(元)')),
                ('slippage_rate', models.FloatField(default=0.001, help_text='默认0.1%', verbose_name='滑点率')),
                ('is_default', models.BooleanField(default=False, db_index=True, verbose_name='是否为默认配置')),
                ('is_active', models.BooleanField(default=True, db_index=True, verbose_name='是否启用')),
                ('description', models.TextField(blank=True, verbose_name='配置说明')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='更新时间')),
            ],
        ),
        migrations.CreateModel(
            name='SimulatedAccountModel',
            options={
                'verbose_name': '模拟账户',
                'verbose_name_plural': '模拟账户',
                'ordering': ['-created_at'],
                'db_table': 'simulated_account',
            },
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('account_name', models.CharField(max_length=100, unique=True, verbose_name='账户名称')),
                ('account_type', models.CharField(choices=[('real', '真实账户'), ('simulated', '模拟账户')], default='simulated', max_length=20, verbose_name='账户类型')),
                ('initial_capital', models.DecimalField(decimal_places=2, max_digits=15, verbose_name='初始资金(元)')),
                ('current_cash', models.DecimalField(decimal_places=2, max_digits=15, verbose_name='当前现金(元)')),
                ('current_market_value', models.DecimalField(decimal_places=2, default=0, max_digits=15, verbose_name='当前持仓市值(元)')),
                ('total_value', models.DecimalField(decimal_places=2, max_digits=15, verbose_name='总资产(元)')),
                ('total_return', models.FloatField(default=0.0, verbose_name='总收益率(%)')),
                ('annual_return', models.FloatField(default=0.0, verbose_name='年化收益率(%)')),
                ('max_drawdown', models.FloatField(default=0.0, verbose_name='最大回撤(%)')),
                ('sharpe_ratio', models.FloatField(default=0.0, verbose_name='夏普比率')),
                ('win_rate', models.FloatField(default=0.0, verbose_name='胜率(%)')),
                ('total_trades', models.IntegerField(default=0, verbose_name='总交易次数')),
                ('winning_trades', models.IntegerField(default=0, verbose_name='盈利交易次数')),
                ('start_date', models.DateField(auto_now_add=True, verbose_name='开始日期')),
                ('last_trade_date', models.DateField(blank=True, null=True, verbose_name='最后交易日期')),
                ('is_active', models.BooleanField(default=True, db_index=True, verbose_name='是否激活')),
                ('auto_trading_enabled', models.BooleanField(default=True, verbose_name='启用自动交易')),
                ('max_position_pct', models.FloatField(default=20.0, verbose_name='单资产最大持仓比例(%)')),
                ('max_total_position_pct', models.FloatField(default=95.0, verbose_name='总持仓比例上限(%)')),
                ('stop_loss_pct', models.FloatField(blank=True, null=True, verbose_name='止损比例(%)')),
                ('commission_rate', models.FloatField(default=0.0003, verbose_name='手续费率')),
                ('slippage_rate', models.FloatField(default=0.001, verbose_name='滑点率')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='更新时间')),
            ],
        ),
        migrations.CreateModel(
            name='PositionModel',
            options={
                'verbose_name': '模拟持仓',
                'verbose_name_plural': '模拟持仓',
                'ordering': ['-market_value'],
                'db_table': 'simulated_position',
            },
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('asset_code', models.CharField(db_index=True, max_length=20, verbose_name='资产代码')),
                ('asset_name', models.CharField(max_length=100, verbose_name='资产名称')),
                ('asset_type', models.CharField(choices=[('equity', '股票'), ('fund', '基金'), ('bond', '债券')], max_length=20, verbose_name='资产类型')),
                ('quantity', models.IntegerField(verbose_name='持仓数量')),
                ('available_quantity', models.IntegerField(verbose_name='可卖数量')),
                ('avg_cost', models.DecimalField(decimal_places=4, max_digits=10, verbose_name='平均成本(元)')),
                ('total_cost', models.DecimalField(decimal_places=2, max_digits=15, verbose_name='总成本(元)')),
                ('current_price', models.DecimalField(decimal_places=4, max_digits=10, verbose_name='当前价格(元)')),
                ('market_value', models.DecimalField(decimal_places=2, max_digits=15, verbose_name='市值(元)')),
                ('unrealized_pnl', models.DecimalField(decimal_places=2, max_digits=15, verbose_name='浮动盈亏(元)')),
                ('unrealized_pnl_pct', models.FloatField(verbose_name='浮动盈亏率(%)')),
                ('first_buy_date', models.DateField(verbose_name='首次买入日期')),
                ('last_update_date', models.DateField(auto_now=True, verbose_name='最后更新日期')),
                ('signal_id', models.IntegerField(blank=True, null=True, verbose_name='关联信号ID')),
                ('entry_reason', models.CharField(blank=True, max_length=200, verbose_name='入场原因')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='更新时间')),
                ('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='positions', to='simulated_trading.simulatedaccountmodel', verbose_name='所属账户')),
            ],
        ),
        migrations.CreateModel(
            name='SimulatedTradeModel',
            options={
                'verbose_name': '模拟交易记录',
                'verbose_name_plural': '模拟交易记录',
                'ordering': ['-execution_date', '-execution_time'],
                'db_table': 'simulated_trade',
            },
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('asset_code', models.CharField(db_index=True, max_length=20, verbose_name='资产代码')),
                ('asset_name', models.CharField(max_length=100, verbose_name='资产名称')),
                ('asset_type', models.CharField(max_length=20, verbose_name='资产类型')),
                ('action', models.CharField(choices=[('buy', '买入'), ('sell', '卖出')], max_length=10, verbose_name='交易动作')),
                ('quantity', models.IntegerField(verbose_name='交易数量')),
                ('price', models.DecimalField(decimal_places=4, max_digits=10, verbose_name='成交价格(元)')),
                ('amount', models.DecimalField(decimal_places=2, max_digits=15, verbose_name='成交金额(元)')),
                ('commission', models.DecimalField(decimal_places=2, default=0, max_digits=10, verbose_name='手续费(元)')),
                ('slippage', models.DecimalField(decimal_places=2, default=0, max_digits=10, verbose_name='滑点损失(元)')),
                ('total_cost', models.DecimalField(decimal_places=2, max_digits=15, verbose_name='总成本(元)')),
                ('realized_pnl', models.DecimalField(blank=True, decimal_places=2, max_digits=15, null=True, verbose_name='已实现盈亏(元)')),
                ('realized_pnl_pct', models.FloatField(blank=True, null=True, verbose_name='已实现盈亏率(%)')),
                ('reason', models.CharField(blank=True, max_length=200, verbose_name='交易原因')),
                ('signal_id', models.IntegerField(blank=True, null=True, verbose_name='关联信号ID')),
                ('order_date', models.DateField(db_index=True, verbose_name='订单日期')),
                ('execution_date', models.DateField(verbose_name='执行日期')),
                ('execution_time', models.DateTimeField(auto_now_add=True, verbose_name='执行时间')),
                ('status', models.CharField(choices=[('pending', '待执行'), ('executed', '已执行'), ('cancelled', '已取消'), ('failed', '执行失败')], default='pending', max_length=20, verbose_name='订单状态')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')),
                ('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='trades', to='simulated_trading.simulatedaccountmodel', verbose_name='所属账户')),
            ],
        ),
        # 添加索引
        migrations.AddIndex(
            model_name='feeconfigmodel',
            index=models.Index(fields=['asset_type', 'is_active'], name='fee_config_asset_idx'),
        ),
        migrations.AddIndex(
            model_name='feeconfigmodel',
            index=models.Index(fields=['is_default', 'is_active'], name='fee_config_default_idx'),
        ),
        migrations.AddIndex(
            model_name='simulatedaccountmodel',
            index=models.Index(fields=['is_active', 'auto_trading_enabled'], name='account_active_idx'),
        ),
        migrations.AddIndex(
            model_name='simulatedaccountmodel',
            index=models.Index(fields=['-start_date'], name='account_start_idx'),
        ),
        migrations.AddIndex(
            model_name='positionmodel',
            index=models.Index(fields=['account', 'asset_code'], name='position_account_asset_idx'),
        ),
        migrations.AddIndex(
            model_name='positionmodel',
            index=models.Index(fields=['-market_value'], name='position_value_idx'),
        ),
        migrations.AddIndex(
            model_name='simulatedtrademodel',
            index=models.Index(fields=['account', '-execution_date'], name='trade_account_date_idx'),
        ),
        migrations.AddIndex(
            model_name='simulatedtrademodel',
            index=models.Index(fields=['asset_code', '-execution_date'], name='trade_asset_date_idx'),
        ),
        migrations.AddIndex(
            model_name='simulatedtrademodel',
            index=models.Index(fields=['-execution_date'], name='trade_date_idx'),
        ),
        # 添加唯一约束
        migrations.AlterUniqueTogether(
            name='positionmodel',
            unique_together={('account', 'asset_code')},
        ),
    ]
