Fix candle day bug

This commit is contained in:
2023-06-02 08:22:36 +09:00
parent f80a2ffb0c
commit 18cb0e5541
5 changed files with 60 additions and 12 deletions

View File

@@ -129,7 +129,7 @@ extension Domestic {
}
public var body: [String: Any] {
[
"FID_COND_MRKT_DIV_CODE": "",
"FID_COND_MRKT_DIV_CODE": "J",
"FID_INPUT_ISCD": productNo,
"FID_INPUT_DATE_1": startDate,
"FID_INPUT_DATE_2": endDate,
@@ -140,7 +140,7 @@ extension Domestic {
public var result: KResult? = nil
public let credential: Credential
public var responseDataLoggable: Bool {
return false
return true
}
@@ -150,8 +150,8 @@ extension Domestic {
public let accessToken: String
let productNo: String
let startDate: String /// yyyyMMdd
let endDate: String /// yyyyMMdd
let startDate: String /// yyyyMMdd (Older date)
let endDate: String /// yyyyMMdd (Newer date)
let period: PeriodDivision
let priceType: PriceType

View File

@@ -707,7 +707,7 @@ public struct PeriodPriceResult: Codable {
case per = "per"
case eps = "eps"
case pbr = "pbr"
case totalOutstandingloanRate = "whol_loan_rmnd_rate"
case totalOutstandingloanRate = "itewhol_loan_rmnd_ratem name"
}
}

View File

@@ -41,6 +41,25 @@ extension String {
}
extension Date {
public var yyyyMMdd_split: (year: Int, month: Int, day: Int)? {
let sets: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
let components = Calendar.current.dateComponents(sets, from: self)
guard let year = components.year, let month = components.month, let day = components.day else {
return nil
}
return (year, month, day)
}
public var HHmmss_split: (hour: Int, minute: Int, second: Int)? {
let sets: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
let components = Calendar.current.dateComponents(sets, from: self)
guard let hour = components.hour, let minute = components.minute, let second = components.second else {
return nil
}
return (hour, minute, second)
}
public func changing(hour: Int, min: Int, sec: Int, timeZone: String = "KST") -> Date? {
let sets: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
var components = Calendar.current.dateComponents(sets, from: self)

View File

@@ -19,7 +19,13 @@ let SecondsForOneDay: TimeInterval = 60 * 60 * 24
extension KissConsole {
var last250Days: (startDate: Date, endDate: Date) {
let endDate = Date().changing(hour: 0, min: 0, sec: 0)!
var curDate = Date()
if let (hour, _, _) = curDate.HHmmss_split {
if hour < 9 {
curDate.addTimeInterval(-SecondsForOneDay)
}
}
let endDate = curDate.changing(hour: 0, min: 0, sec: 0)!
let startDate = endDate.addingTimeInterval(-250 * SecondsForOneDay)
return (startDate, endDate)
}
@@ -27,7 +33,13 @@ extension KissConsole {
var last52Weeks: (startDate: Date, endDate: Date) {
// TODO: , ?
let endDate = Date().changing(hour: 0, min: 0, sec: 0)!
var curDate = Date()
if let (hour, _, _) = curDate.HHmmss_split {
if hour < 9 {
curDate.addTimeInterval(-SecondsForOneDay)
}
}
let endDate = curDate.changing(hour: 0, min: 0, sec: 0)!
let startDate = endDate.addingTimeInterval(-52 * 7 * SecondsForOneDay)
return (startDate, endDate)
}
@@ -61,25 +73,38 @@ extension KissConsole {
currentCandleShortCode = nil
}
var nextTime = startDate
var reqStartDate = startDate
var reqEndDate = reqStartDate.addingTimeInterval(99 * SecondsForOneDay)
if reqEndDate > endDate {
reqEndDate = endDate
}
var candles = [Domestic.CandlePeriod]()
var count = 0
while true {
count += 1
print("\(period) price \(productNo) from \(reqStartDate.yyyyMMdd_HHmmss_forTime) to \(reqEndDate.yyyyMMdd_HHmmss_forTime)")
let result = try await account!.getPeriodPrice(productNo: productNo, startDate: startDate, endDate: endDate, period: period)
let result = try await account!.getPeriodPrice(productNo: productNo, startDate: reqStartDate, endDate: reqEndDate, period: period)
if let prices = result.output2, prices.isEmpty == false {
candles.append(contentsOf: prices)
if let last = prices.last {
// TODO: nextTime ...
if let (yyyy, mm, dd) = last.stockBusinessDate.yyyyMMdd {
print("next: \(last.stockBusinessDate)")
reqStartDate.change(year: yyyy, month: mm, day: dd+1)
reqEndDate = reqStartDate.addingTimeInterval(99 * SecondsForOneDay)
if reqEndDate > endDate {
reqEndDate = endDate
}
}
}
try await Task.sleep(nanoseconds: 1_000_000_000 / PreferredCandleTPS)
}
else {
print("\(period) price finished")
break
}
}
@@ -118,6 +143,7 @@ extension KissConsole {
let more = (count > 0)
count += 1
print("minute price \(productNo) from \(nextTime.yyyyMMdd_HHmmss_forTime) \(more)")
let result = try await account!.getMinutePrice(productNo: productNo, startTodayTime: nextTime, more: more)
if let prices = result.output2, prices.isEmpty == false {

View File

@@ -584,6 +584,9 @@ extension KissConsole {
}
let (startDate, endDate) = last250Days
// let startDate = Date().changing(year: 2022, month: 12, day: 1)!
// let endDate = Date().changing(year: 2022, month: 12, day: 30)!
let semaphore = DispatchSemaphore(value: 0)
Task {
let success = await getCandle(productNo: productNo, period: .daily, startDate: startDate, endDate: endDate)