gitee.com/quant1x/engine@v1.8.4/rules/rule_base.go (about)

     1  package rules
     2  
     3  import (
     4  	"gitee.com/quant1x/engine/config"
     5  	"gitee.com/quant1x/engine/factors"
     6  	"gitee.com/quant1x/gox/exception"
     7  	"gitee.com/quant1x/gox/logger"
     8  	"gitee.com/quant1x/num"
     9  )
    10  
    11  func init() {
    12  	err := RegisterFunc(KRuleBase, "基础规则", ruleBase)
    13  	if err != nil {
    14  		logger.Fatalf("%+v", err)
    15  	}
    16  }
    17  
    18  var (
    19  	ErrRangeOfOpeningTurnZ          = exception.New(errorRuleBase+0, "非开盘换手范围")
    20  	ErrRangeOfOpeningQuantityRatio  = exception.New(errorRuleBase+1, "非开盘量比范围")
    21  	ErrRangeOfOpeningChangeRate     = exception.New(errorRuleBase+2, "非开盘涨跌幅范围")
    22  	ErrRangeOfFundFlow              = exception.New(errorRuleBase+3, "非资金流出范围")
    23  	ErrHistoryNotExist              = exception.New(errorRuleBase+4, "没有找到history数据")
    24  	ErrRiskOfGapDown                = exception.New(errorRuleBase+5, "开盘存在向下跳空缺口")
    25  	ErrExchangeNotExist             = exception.New(errorRuleBase+6, "没有找到history数据")
    26  	ErrRangeOfChangeRate            = exception.New(errorRuleBase+7, "非实时涨跌幅范围")
    27  	ErrRangeOfFinancingBalanceRatio = exception.New(errorRuleBase+8, "融资余额占比过大")
    28  )
    29  
    30  // ruleBase 基础规则
    31  func ruleBase(ruleParameter config.RuleParameter, snapshot factors.QuoteSnapshot) error {
    32  	// 基础过滤规则
    33  	securityCode := snapshot.SecurityCode
    34  	// 1. 开盘换手Z的逻辑
    35  	if num.IsNaN(snapshot.OpenTurnZ) || !ruleParameter.OpenTurnZ.Validate(snapshot.OpenTurnZ) {
    36  		return ErrRangeOfOpeningTurnZ
    37  	}
    38  	// 2. 当日 - 开盘量比
    39  	if num.IsNaN(snapshot.OpenQuantityRatio) || !ruleParameter.OpenQuantityRatio.Validate(snapshot.OpenQuantityRatio) {
    40  		return ErrRangeOfOpeningQuantityRatio
    41  	}
    42  	// 3. 当日 - 开盘涨幅
    43  	if num.IsNaN(snapshot.OpeningChangeRate) || !ruleParameter.OpenChangeRate.Validate(snapshot.OpeningChangeRate) {
    44  		return ErrRangeOfOpeningChangeRate
    45  	}
    46  	// 3.1 当日 - 涨幅
    47  	if num.IsNaN(snapshot.ChangeRate) || !ruleParameter.ChangeRate.Validate(snapshot.ChangeRate) {
    48  		return ErrRangeOfChangeRate
    49  	}
    50  	//// 4. 委托量
    51  	//if snapshot.AverageBiddingVolume > RuleParameters.BiddingVolumeMax || snapshot.AverageBiddingVolume < RuleParameters.BiddingVolumeMin {
    52  	//	return false
    53  	//}
    54  	//// 5. 力度-测试
    55  	//if snapshot.ChangePower < 0 {
    56  	//	return false
    57  	//}
    58  	// 6. exchange 过滤
    59  	misc := factors.GetL5Misc(securityCode)
    60  	if misc == nil {
    61  		//return ErrExchangeNotExist
    62  	} else {
    63  		//// 6.1 资金流向
    64  		//if exchange.FundFlow != 0 && (exchange.FundFlow/TenThousand) < RuleParameters.MaxReduceAmount {
    65  		//	return ErrRangeOfFundFlow
    66  		//}
    67  		// 6.2 检查融资余额占比
    68  		if misc.RZYEZB > 0 && misc.RZYEZB >= ruleParameter.FinancingBalanceRatio {
    69  			return ErrRangeOfFinancingBalanceRatio
    70  		}
    71  	}
    72  	// 7. 历史数据
    73  	history := factors.GetL5History(securityCode)
    74  	if history == nil {
    75  		return ErrHistoryNotExist
    76  	} else {
    77  		// 7.1 开盘存在跳空缺口
    78  		if !ruleParameter.GapDown && history.LOW >= snapshot.Open {
    79  			return ErrRiskOfGapDown
    80  		}
    81  	}
    82  	// 规则通过
    83  	return nil
    84  }