gitee.com/quant1x/engine@v1.8.4/factors/dataset_wide_rotation.go (about) 1 package factors 2 3 import ( 4 "gitee.com/quant1x/engine/cache" 5 "gitee.com/quant1x/exchange" 6 "gitee.com/quant1x/gox/api" 7 ) 8 9 //var ( 10 // wideTableMutex sync.RWMutex 11 // routineLocalWideTable = map[string][]SecurityFeature{} 12 //) 13 // 14 //// updateCacheWideKLines 更新缓存宽表 15 //func updateCacheWideKLines(securityCode string, lines []SecurityFeature) { 16 // if len(lines) == 0 { 17 // return 18 // } 19 // wideTableMutex.Lock() 20 // defer wideTableMutex.Unlock() 21 // routineLocalWideTable[securityCode] = lines 22 // 23 //} 24 // 25 //// CheckoutWideKLines 捡出指定日期的K线数据 26 //func CheckoutWideKLines(code, date string) []SecurityFeature { 27 // securityCode := exchange.CorrectSecurityCode(code) 28 // date = exchange.FixTradeDate(date) 29 // // 1. 取缓存的K线 30 // wideTableMutex.RLock() 31 // cacheLines, ok := routineLocalWideTable[securityCode] 32 // wideTableMutex.RUnlock() 33 // if !ok { 34 // cacheLines = loadWideTable(securityCode) 35 // updateCacheWideKLines(securityCode, cacheLines) 36 // } 37 // rows := len(cacheLines) 38 // if rows == 0 { 39 // return nil 40 // } 41 // // 1.1 检查是否最新数据 42 // kline := cacheLines[rows-1] 43 // if kline.Date < date { 44 // // 数据太旧, 重新加载 45 // cacheLines = loadWideTable(securityCode) 46 // updateCacheWideKLines(securityCode, cacheLines) 47 // } 48 // // 2. 对齐数据缓存的日期, 过滤可能存在停牌没有数据的情况 49 // offset := checkWideTableOffset(cacheLines, date) 50 // if offset < 0 { 51 // return nil 52 // } 53 // // 3. 返回指定日期前的K线数据 54 // lines := cacheLines[0 : rows-offset] 55 // return lines 56 //} 57 58 // loadWideTable 加载基础K线 59 func loadWideTable(securityCode string) []SecurityFeature { 60 filename := cache.WideFilename(securityCode) 61 var lines []SecurityFeature 62 _ = api.CsvToSlices(filename, &lines) 63 return lines 64 } 65 66 // 矫正日期的偏移 67 func checkWideTableOffset(lines []SecurityFeature, date string) (offset int) { 68 rows := len(lines) 69 offset = 0 70 for i := 0; i < rows; i++ { 71 klineDate := lines[rows-1-i].Date 72 if klineDate < date { 73 return -1 74 } else if klineDate == date { 75 break 76 } else { 77 offset++ 78 } 79 } 80 if offset+1 >= rows { 81 return -1 82 } 83 return 84 } 85 86 // CheckoutWideTableByDate 捡出指定日期的K线数据 87 // 88 // TODO: 扩展数据不适用内存缓存, 减小内存压力 89 func CheckoutWideTableByDate(code, date string) []SecurityFeature { 90 securityCode := exchange.CorrectSecurityCode(code) 91 date = exchange.FixTradeDate(date) 92 cacheLines := loadWideTable(securityCode) 93 rows := len(cacheLines) 94 if rows == 0 { 95 return nil 96 } 97 // 2. 对齐数据缓存的日期, 过滤可能存在停牌没有数据的情况 98 offset := checkWideTableOffset(cacheLines, date) 99 if offset < 0 { 100 return nil 101 } 102 // 3. 返回指定日期前的K线数据 103 lines := cacheLines[0 : rows-offset] 104 return lines 105 }