61 lines
2.2 KiB
Swift
61 lines
2.2 KiB
Swift
//
|
|
// KissConsole+CSV.swift
|
|
// KissMeConsole
|
|
//
|
|
// Created by ened-book-m1 on 2023/05/29.
|
|
//
|
|
|
|
import Foundation
|
|
import KissMe
|
|
|
|
|
|
extension KissConsole {
|
|
|
|
func writeShop(_ shopItems: [DomesticShop.Product], fileUrl: URL) {
|
|
var stringCsv: String = ""
|
|
let header = "baseDate,shortCode,isinCode,marketCategory,itemName,corporationNo,\n"
|
|
stringCsv.append(header)
|
|
for item in shopItems {
|
|
let stringItem = [item.baseDate,
|
|
item.shortCode,
|
|
item.isinCode,
|
|
item.marketCategory,
|
|
item.itemName.trimmingCharacters(in: .whitespaces),
|
|
item.corporationNo].joined(separator: ",")
|
|
stringCsv.append(stringItem)
|
|
stringCsv.append("\n")
|
|
}
|
|
|
|
do {
|
|
try stringCsv.write(toFile: fileUrl.path, atomically: true, encoding: .utf8)
|
|
print("wrote \(fileUrl.lastPathComponent) with \(shopItems.count)")
|
|
} catch {
|
|
print("\(error)")
|
|
}
|
|
}
|
|
|
|
func writeCandle(_ prices: [MinutePriceResult.OutputPrice], fileUrl: URL) {
|
|
var stringCsv: String = "stockBusinessDate,stockConclusionTime,accumulatedTradingAmount,currentStockPrice,secondStockPrice,highestStockPrice,lowestStockPrice,conclusionVolume,\n"
|
|
let header = ""
|
|
stringCsv.append(header)
|
|
for item in prices {
|
|
let stringItem = [item.stockBusinessDate,
|
|
item.stockConclusionTime,
|
|
item.accumulatedTradingAmount,
|
|
item.currentStockPrice,
|
|
item.secondStockPrice,
|
|
item.highestStockPrice,
|
|
item.lowestStockPrice,
|
|
item.conclusionVolume].joined(separator: ",")
|
|
stringCsv.append(stringItem)
|
|
stringCsv.append("\n")
|
|
}
|
|
do {
|
|
try stringCsv.write(toFile: fileUrl.path, atomically: true, encoding: .utf8)
|
|
print("wrote \(fileUrl.lastPathComponent) with \(prices.count)")
|
|
} catch {
|
|
print("\(error)")
|
|
}
|
|
}
|
|
}
|