47 lines
1.3 KiB
Swift
47 lines
1.3 KiB
Swift
//
|
|
// CandleCache.swift
|
|
// KissMeSacks
|
|
//
|
|
// Created by ened-book-m1 on 7/7/24.
|
|
//
|
|
|
|
import KissMe
|
|
|
|
public class CandleCache {
|
|
|
|
private var candleCaches: [String: [Domestic.Candle]] = [:]
|
|
|
|
func getCandle(productNo: String, day: String, time: String) -> Domestic.Candle? {
|
|
let basePath = URL.currentDirectory().appending(path: "data")
|
|
let candleUrl = basePath.appending(path: "\(productNo)/min/candle-\(day).log")
|
|
|
|
let candles: [Domestic.Candle]
|
|
|
|
if let cache = candleCaches[candleUrl.path] {
|
|
candles = cache
|
|
}
|
|
else {
|
|
do {
|
|
candles = try [Domestic.Candle].readCsv(fromFile: candleUrl)
|
|
candleCaches[candleUrl.path] = candles
|
|
} catch {
|
|
printError(error)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
guard let candle = candles.first(where: { $0.stockConclusionTime == time }) else {
|
|
return nil
|
|
}
|
|
return candle
|
|
}
|
|
|
|
|
|
func getPrice(productNo: String, day: String, time: String) -> Int? {
|
|
guard let candle = getCandle(productNo: productNo, day: day, time: time) else {
|
|
return nil
|
|
}
|
|
return Int(candle.currentStockPrice)
|
|
}
|
|
}
|