gitee.com/quant1x/engine@v1.8.4/config/config_trader.go (about)

     1  package config
     2  
     3  import "gitee.com/quant1x/exchange"
     4  
     5  // TraderRole 交易员角色
     6  type TraderRole int
     7  
     8  const (
     9  	RoleDisable TraderRole = iota // 禁止自动化交易
    10  	RolePython                    // python脚本自动化交易
    11  	RoleProxy                     // 代理交易模式
    12  	RoleManual                    // 人工干预, 作用同
    13  )
    14  
    15  const (
    16  	sectorIgnorePrefix = "-"
    17  	sectorPrefixLength = len(sectorIgnorePrefix)
    18  )
    19  
    20  // TraderParameter 预览交易通道参数
    21  type TraderParameter struct {
    22  	AccountId                   string              `name:"账号ID" yaml:"account_id" dataframe:"888xxxxxxx"`                                      // 账号ID
    23  	OrderPath                   string              `name:"订单路径" yaml:"order_path"`                                                             // 订单路径
    24  	TopN                        int                 `name:"TopN" yaml:"top_n" default:"3"`                                                      // 最多输出前多少名个股
    25  	HaveETF                     bool                `name:"是否包含ETF" yaml:"have_etf" default:"false"`                                            // 是否包含ETF
    26  	PriceCageRatio              float64             `name:"价格笼子比例" yaml:"price_cage_ratio" default:"0.02"`                                      // 价格笼子比例, 默认2%, 小于0就是无限制
    27  	MinimumPriceFluctuationUnit float64             `name:"价格变动最小单位" yaml:"minimum_price_fluctuation_unit" default:"0.10"`                      // 价格最小变动单位, 默认0.10
    28  	AnnualInterestRate          float64             `name:"年利率" yaml:"annual_interest_rate" default:"1.65"`                                     // 2024年2月18日建设银行1年期存款利率1.65%
    29  	StampDutyRateForBuy         float64             `name:"买入印花税" yaml:"stamp_duty_rate_for_buy" default:"0.0000"`                              // 印花说-买入, 没有
    30  	StampDutyRateForSell        float64             `name:"卖出印花税" yaml:"stamp_duty_rate_for_sell" default:"0.0010"`                             // 印花说-卖出, 默认是千分之1
    31  	TransferRate                float64             `name:"过户费" yaml:"transfer_rate" default:"0.0006"`                                          // 过户费, 双向, 默认是万分之6
    32  	CommissionRate              float64             `name:"佣金率" yaml:"commission_rate" default:"0.00025"`                                       // 券商佣金, 双向, 默认万分之2.5
    33  	CommissionMin               float64             `name:"佣金最低" yaml:"commission_min" default:"5.0000"`                                        // 券商佣金最低, 双向, 默认5.00
    34  	PositionRatio               float64             `name:"持仓占比" yaml:"position_ratio" default:"0.5000"`                                        // 当日持仓占比, 默认50%
    35  	KeepCash                    float64             `name:"保留现金" yaml:"keep_cash" default:"10000.00"`                                           // 保留现金, 默认10000.00
    36  	BuyAmountMax                float64             `name:"可买最大金额" yaml:"buy_amount_max" default:"250000.00"`                                   // 买入最大金额, 默认250000.00
    37  	BuyAmountMin                float64             `name:"可买最小金额" yaml:"buy_amount_min" default:"1000.00"`                                     // 买入最小金额, 默认1000.00
    38  	Role                        TraderRole          `name:"角色" yaml:"role" default:"3"`                                                         // 交易员角色, 默认是需要人工干预, 系统不做自动交易处理
    39  	ProxyUrl                    string              `name:"代理URL" yaml:"proxy_url" default:"http://127.0.0.1:18168/qmt"`                        // 禁止使用公网地址
    40  	Strategies                  []StrategyParameter `name:"策略集合" yaml:"strategies"`                                                             // 策略集合
    41  	CancelSession               TradingSession      `name:"撤单时段" yaml:"cancel" default:"09:15:00~09:19:59,09:25:00~11:29:59,13:00:00~14:59:59"` // 可撤单配置
    42  	UndertakeRatio              float64             `name:"承接比" yaml:"undertake_ratio" default:"0.8000"`
    43  }
    44  
    45  // TotalNumberOfTargets 统计标的总数
    46  func (t TraderParameter) TotalNumberOfTargets() int {
    47  	total := 0
    48  	for _, v := range t.Strategies {
    49  		total += v.NumberOfTargets()
    50  	}
    51  	return total
    52  }
    53  
    54  // ResetPositionRatio 重置仓位占比
    55  func (t TraderParameter) ResetPositionRatio() {
    56  	remainingRatio := 1.00
    57  	strategyCount := len(t.Strategies)
    58  	var unassignedStrategies []*StrategyParameter
    59  	for i := 0; i < strategyCount; i++ {
    60  		v := &(t.Strategies[i])
    61  		if !v.BuyEnable() {
    62  			continue
    63  		}
    64  		// 校对个股最大资金
    65  		if v.FeeMax > t.BuyAmountMax {
    66  			v.FeeMax = t.BuyAmountMax
    67  		}
    68  		// 校对个股最小资金
    69  		if v.FeeMin < t.BuyAmountMin {
    70  			v.FeeMin = t.BuyAmountMin
    71  		}
    72  		if v.Weight > 1.00 {
    73  			v.Weight = 1.00
    74  		}
    75  		if v.Weight > 0 {
    76  			remainingRatio -= v.Weight
    77  		} else {
    78  			unassignedStrategies = append(unassignedStrategies, v)
    79  		}
    80  	}
    81  	remainingCount := len(unassignedStrategies)
    82  	if remainingRatio > 0 && remainingCount > 0 {
    83  		averageFundPercentage := remainingRatio / float64(remainingCount)
    84  		for i, v := range unassignedStrategies {
    85  			v.Weight = averageFundPercentage
    86  			if i+1 == remainingCount {
    87  				v.Weight = remainingRatio
    88  			}
    89  			remainingRatio -= v.Weight
    90  		}
    91  	}
    92  }
    93  
    94  // DailyRiskFreeRate 计算每日无风险利率
    95  func (t TraderParameter) DailyRiskFreeRate(date string) float64 {
    96  	date = exchange.FixTradeDate(date)
    97  	year := date[0:4]
    98  	start := year + "-01-01"
    99  	end := year + "-12-31"
   100  	dates := exchange.DateRange(start, end)
   101  	count := len(dates)
   102  	return t.AnnualInterestRate / float64(count)
   103  }
   104  
   105  // TraderConfig 获取交易配置
   106  func TraderConfig() TraderParameter {
   107  	trader := GlobalConfig.Trader
   108  	trader.ResetPositionRatio()
   109  	return trader
   110  }
   111  
   112  // GetStrategyParameterByCode 通过策略编码查找规则
   113  func GetStrategyParameterByCode(strategyCode uint64) *StrategyParameter {
   114  	strategies := TraderConfig().Strategies
   115  	for _, v := range strategies {
   116  		if v.Auto && v.Id == strategyCode {
   117  			return &v
   118  		}
   119  	}
   120  	return nil
   121  }