Implement "shorts all resume" command
This commit is contained in:
@@ -65,14 +65,46 @@ extension KissConsole {
|
||||
print("\t주식 단축 종목코드", output.shortProductCode)
|
||||
}
|
||||
|
||||
func getShorts(productNo: String) async throws -> Bool {
|
||||
func getShortsLastDate(productNo: String) -> Date? {
|
||||
let startDate = Date().addingTimeInterval(-365 * SecondsForOneDay)
|
||||
var backDate = Date()
|
||||
|
||||
/// -29일씩 이전으로 돌아가면서, 마지막으로 csv 로 저장했던 날짜를 찾는다.
|
||||
while startDate < backDate {
|
||||
let day = backDate.yyyyMM + "01"
|
||||
let fileUrl = KissConsole.shortsFileUrl(productNo: productNo, day: day)
|
||||
guard let _ = fileUrl.isFileExists else {
|
||||
backDate = backDate.addingTimeInterval(-29 * SecondsForOneDay)
|
||||
continue
|
||||
}
|
||||
guard let shorts = try? [DomesticExtra.Shorts].readCsv(fromFile: fileUrl) else {
|
||||
return Date.date(yyyyMMdd: day, HHmmss: "000000")
|
||||
}
|
||||
guard let (yyyy, mm, dd) = shorts.first?.stockBusinessDate.yyyyMMdd else {
|
||||
return Date.date(yyyyMMdd: day, HHmmss: "000000")
|
||||
}
|
||||
guard let newDate = backDate.changing(year: yyyy, month: mm, day: dd) else {
|
||||
return Date.date(yyyyMMdd: day, HHmmss: "000000")
|
||||
}
|
||||
return newDate.addingTimeInterval(SecondsForOneDay)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getShorts(productNo: String, startAt: Date?) async throws -> Bool {
|
||||
guard let product = getProduct(shortCode: productNo) else {
|
||||
print("Invalid product \(productNo)")
|
||||
return false
|
||||
}
|
||||
|
||||
let endDate = Date()
|
||||
let startDate = endDate.addingTimeInterval(-365 * SecondsForOneDay)
|
||||
let startDate: Date
|
||||
if let startAt = startAt {
|
||||
startDate = startAt
|
||||
}
|
||||
else {
|
||||
startDate = endDate.addingTimeInterval(-365 * SecondsForOneDay)
|
||||
}
|
||||
print("Getting \(product.isinCode), \(startDate.yyyyMMdd) ~ \(endDate.yyyyMMdd)")
|
||||
|
||||
let result = try await KissAccount.getShortSellingBalance(isinCode: product.isinCode, startDate: startDate, endDate: endDate)
|
||||
@@ -97,7 +129,16 @@ extension KissConsole {
|
||||
}
|
||||
|
||||
let fileUrl = KissConsole.shortsFileUrl(productNo: productNo, day: month+"01")
|
||||
try descBlock.writeCsv(toFile: fileUrl, localized: false)
|
||||
try descBlock.mergeCsv(toFile: fileUrl, merging: { this, file in
|
||||
var merged = this
|
||||
for old in file {
|
||||
if nil == this.first(where: { $0.stockBusinessDate == old.stockBusinessDate }) {
|
||||
merged.append(old)
|
||||
}
|
||||
}
|
||||
merged.sort(by: { $0.stockBusinessDate > $1.stockBusinessDate })
|
||||
return merged
|
||||
}, localized: false)
|
||||
}
|
||||
}
|
||||
try await Task.sleep(nanoseconds: 1_000_000_000 / PreferredShortsTPS)
|
||||
|
||||
@@ -656,7 +656,7 @@ extension KissConsole {
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
Task {
|
||||
await KissContext.shared.update(resuming: false)
|
||||
if args.count == 1, args[0] == "resume" {
|
||||
if args.count == 1, args[0].lowercased() == "resume" {
|
||||
await KissContext.shared.update(resuming: true)
|
||||
}
|
||||
semaphore.signal()
|
||||
@@ -854,7 +854,7 @@ extension KissConsole {
|
||||
return
|
||||
}
|
||||
do {
|
||||
_ = try await getShorts(productNo: productNo)
|
||||
_ = try await getShorts(productNo: productNo, startAt: nil)
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
@@ -867,7 +867,15 @@ extension KissConsole {
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
Task {
|
||||
do {
|
||||
let success = try await getShorts(productNo: item.shortCode)
|
||||
let startAt: Date?
|
||||
if args.count == 1, args[0].lowercased() == "resume" {
|
||||
startAt = getShortsLastDate(productNo: item.shortCode)
|
||||
}
|
||||
else {
|
||||
startAt = nil
|
||||
}
|
||||
|
||||
let success = try await getShorts(productNo: item.shortCode, startAt: startAt)
|
||||
print("DONE \(success)")
|
||||
} catch {
|
||||
print(error)
|
||||
|
||||
Reference in New Issue
Block a user