// 更新区间极值指数 func UpdateAll() error { log.Println("开始更新区间极值指标") // 数据保存目录 dataDir, err := config.GetDataDir() if err != nil { return err } // 获取所有股票 stocks, err := stock.GetAll() if err != nil { return err } //log.Printf("共有股票%d只", len(stocks)) for _, stock := range stocks { // 更新每只股票的指标 err = updateStock(stock.Code, dataDir) if err != nil { log.Fatal(err) } } log.Println("区间极值指标更新完毕") return err }
func saveSystem() error { dataDir, err := config.GetDataDir() if err != nil { return err } filePath := filepath.Join(dataDir, dataFileName) // 打开文件 file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0x777) if err != nil { return err } defer file.Close() file.WriteString(fmt.Sprintf("Codes = %d %v\n", len(currentTurtleTradingSystem.Codes), currentTurtleTradingSystem.Codes)) file.WriteString(fmt.Sprintf("StartAmount = %f\n", currentTurtleTradingSystem.StartAmount)) file.WriteString(fmt.Sprintf("Commission = %f\n", currentTurtleTradingSystem.Commission)) file.WriteString(fmt.Sprintf("StartDate = %s\n", currentTurtleTradingSystem.StartDate)) file.WriteString(fmt.Sprintf("EndDate = %s\n", currentTurtleTradingSystem.EndDate)) file.WriteString(fmt.Sprintf("Start\t[Holding = %d N = %d Enter = %d Exit = %d Stop = %d]\n", currentTurtleTradingSystem.Start.Holding, currentTurtleTradingSystem.Start.N, currentTurtleTradingSystem.Start.Enter, currentTurtleTradingSystem.Start.Exit, currentTurtleTradingSystem.Start.Stop)) file.WriteString(fmt.Sprintf("End\t[Holding = %d N = %d Enter = %d Exit = %d Stop = %d]\n", currentTurtleTradingSystem.End.Holding, currentTurtleTradingSystem.End.N, currentTurtleTradingSystem.End.Enter, currentTurtleTradingSystem.End.Exit, currentTurtleTradingSystem.End.Stop)) file.WriteString(fmt.Sprintf("Current\t[Holding = %d N = %d Enter = %d Exit = %d Stop = %d]\n", currentTurtleTradingSystem.Current.Holding, currentTurtleTradingSystem.Current.N, currentTurtleTradingSystem.Current.Enter, currentTurtleTradingSystem.Current.Exit, currentTurtleTradingSystem.Current.Stop)) file.WriteString(fmt.Sprintf("CurrentProfit = %.3f\n", currentTurtleTradingSystem.CurrentProfit)) file.WriteString(fmt.Sprintf("CurrentProfit = %.3f%%\n", currentTurtleTradingSystem.CurrentProfitPercent*100)) file.WriteString(fmt.Sprintf("Best\t[Holding = %d N = %d Enter = %d Exit = %d Stop = %d]\n", currentTurtleTradingSystem.Best.Holding, currentTurtleTradingSystem.Best.N, currentTurtleTradingSystem.Best.Enter, currentTurtleTradingSystem.Best.Exit, currentTurtleTradingSystem.Best.Stop)) file.WriteString(fmt.Sprintf("BestProfit = %.3f\n", currentTurtleTradingSystem.BestProfit)) file.WriteString(fmt.Sprintf("BestProfit = %.3f%%\n", currentTurtleTradingSystem.BestProfitPercent*100)) file.WriteString(fmt.Sprintf("CalculatingAmount = %d\n", currentTurtleTradingSystem.CalculatingAmount)) file.WriteString(fmt.Sprintf("CalculatedAmount = %d\n", currentTurtleTradingSystem.CalculatedAmount)) file.WriteString(fmt.Sprintf("CalculatedSeconds = %d\n", currentTurtleTradingSystem.CalculatedSeconds)) file.WriteString(fmt.Sprintf("RemainTips = %s\n", currentTurtleTradingSystem.RemainTips)) return nil }
// 更新股票历史 func UpdateAll() error { log.Print("开始更新股票历史") // 数据保存目录 dataDir, err := config.GetDataDir() if err != nil { return err } // 获取所有的股票 stocks, err := stock.GetAll() if err != nil { return err } chanSend := make(chan int, updateGoroutinesCount) chanReceive := make(chan int) // 并发获取股票历史 go func() { for _, stock := range stocks { go func(code string) { // 更新每只股票的历史 err = updateStock(code, dataDir) if err != nil { log.Fatal(err) } <-chanSend chanReceive <- 1 }(stock.Code) chanSend <- 1 } }() // 阻塞,直到所有股票更新完历史 for _, _ = range stocks { <-chanReceive } log.Print("股票历史更新成功") return err }
// 获取股票列表 func GetAll() ([]Stock, error) { // 数据保存目录 dataDir, err := config.GetDataDir() if err != nil { return nil, err } // 股票列表文件路径 filePath := filepath.Join(dataDir, stocksFileName) _, err = os.Stat(filePath) if os.IsNotExist(err) { // 如果股票列表文件不存在,则从纳斯达克下载 stocks, err := downloadFromNasdaq100() if err != nil { return nil, err } // 保存下载的股票 return stocks, save(stocks, filePath) } return load(filePath) }