Clone
2
主要函数说明
phyer edited this page 2025-01-19 21:47:15 +08:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

func (co Coaster) RPushSample(cr *Core, sp Sample, ctype string) (*Sample, error)

RPushSample函数的主要作用是向Coaster对象中添加新的数据样本Sample支持不同类型的数据处理。让我分析一下这个函数

func (co Coaster) RPushSample(cr *Core, sp Sample, ctype string) (*Sample, error) {
    cd := Candle{}
    spjs, _ := json.Marshal(sp)
    logrus.Debug("RPushSample spjs: ", string(spjs))

    // 1. 处理K线数据
    if ctype == "candle" {
        json.Unmarshal(spjs, &cd)
        // 将字符串数据转换为float64
        cd.Data[0] = cd.Data[0]  // 时间戳保持不变
        cd.Data[1], _ = strconv.ParseFloat(cd.Data[1].(string), 64)  // 开盘价
        cd.Data[2], _ = strconv.ParseFloat(cd.Data[2].(string), 64)  // 最高价
        cd.Data[3], _ = strconv.ParseFloat(cd.Data[3].(string), 64)  // 最低价
        cd.Data[4], _ = strconv.ParseFloat(cd.Data[4].(string), 64)  // 收盘价
        cd.Data[5], _ = strconv.ParseFloat(cd.Data[5].(string), 64)  // 成交量
        cd.Data[6], _ = strconv.ParseFloat(cd.Data[6].(string), 64)  // 成交额
        
        sm, err := co.CandleList.RPush(&cd)
        if err == nil {
            now := time.Now().UnixMilli()
            co.LastUpdateTime = now
            co.CandleList.LastUpdateTime = now
            co.UpdateNickName = GetRandomString(12)
            co.CandleList.UpdateNickName = GetRandomString(12)
        }
        return &sm, err
    }

    // 2. 处理MA数据
    mx := MaX{}
    if ctype == "ma7" || ctype == "ma30" {
        json.Unmarshal(spjs, &mx)
        var sm Sample
        var err error
        
        if ctype == "ma7" {
            sm, err = co.Ma7List.RPush(&mx)
            if err == nil {
                now := time.Now().UnixMilli()
                co.LastUpdateTime = now
                co.Ma7List.UpdateNickName = GetRandomString(12)
                co.Ma7List.LastUpdateTime = now
            }
        } else {
            sm, err = co.Ma30List.RPush(&mx)
            if err == nil {
                now := time.Now().UnixMilli()
                co.LastUpdateTime = now
                co.Ma30List.UpdateNickName = GetRandomString(12)
                co.Ma30List.LastUpdateTime = now
            }
        }
        return &sm, err
    }
    
    return nil, nil
}

主要功能:

  1. 数据类型支持:

    • candle: K线数据
    • ma7: 7周期移动平均线
    • ma30: 30周期移动平均线
  2. 对于K线数据

    • 解析K线数据的各个字段开高低收量额
    • 将字符串格式转换为float64
    • 更新时间戳和标识符
  3. 对于MA数据

    • 分别处理MA7和MA30
    • 更新相应的列表和时间戳
  4. 数据更新:

    • 更新最后修改时间
    • 生成新的UpdateNickName
    • 维护数据的时序性

这个函数在交易系统中扮演着重要角色:

  1. 维护市场数据的实时更新
  2. 处理不同类型的技术指标
  3. 确保数据的时序性和完整性
  4. 提供数据追踪和更新记录