krx-rs 0.1.0

KRX Open API를 위한 Rust 클라이언트
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use super::{
    ApiResponse, deserialize_krx_date, deserialize_optional_f64, deserialize_optional_percentage,
    deserialize_optional_u64,
};
use crate::error::Result;
use chrono::NaiveDate;
use polars::prelude::*;
use serde::Deserialize;

/// ETF 일별매매정보 레코드
#[derive(Debug, Deserialize)]
pub struct EtfDailyRecord {
    /// 기준일자
    #[serde(rename = "BAS_DD", deserialize_with = "deserialize_krx_date")]
    pub base_date: NaiveDate,

    /// 종목코드
    #[serde(rename = "ISU_CD")]
    pub issue_code: String,

    /// 종목명
    #[serde(rename = "ISU_NM")]
    pub issue_name: String,

    /// 종가
    #[serde(rename = "TDD_CLSPRC", deserialize_with = "deserialize_optional_f64")]
    pub close_price: Option<f64>,

    /// 시가
    #[serde(rename = "TDD_OPNPRC", deserialize_with = "deserialize_optional_f64")]
    pub open_price: Option<f64>,

    /// 고가
    #[serde(rename = "TDD_HGPRC", deserialize_with = "deserialize_optional_f64")]
    pub high_price: Option<f64>,

    /// 저가
    #[serde(rename = "TDD_LWPRC", deserialize_with = "deserialize_optional_f64")]
    pub low_price: Option<f64>,

    /// 대비
    #[serde(
        rename = "CMPPREVDD_PRC",
        deserialize_with = "deserialize_optional_f64"
    )]
    pub price_change: Option<f64>,

    /// 등락률 (%)
    #[serde(
        rename = "FLUC_RT",
        deserialize_with = "deserialize_optional_percentage"
    )]
    pub fluctuation_rate: Option<f64>,

    /// 거래량
    #[serde(rename = "ACC_TRDVOL", deserialize_with = "deserialize_optional_u64")]
    pub trading_volume: Option<u64>,

    /// 거래대금
    #[serde(rename = "ACC_TRDVAL", deserialize_with = "deserialize_optional_u64")]
    pub trading_value: Option<u64>,

    /// 시가총액
    #[serde(rename = "MKTCAP", deserialize_with = "deserialize_optional_u64")]
    pub market_cap: Option<u64>,

    /// 상장주식수
    #[serde(rename = "LIST_SHRS", deserialize_with = "deserialize_optional_u64")]
    pub listed_shares: Option<u64>,

    /// NAV (순자산가치)
    #[serde(rename = "NAV", deserialize_with = "deserialize_optional_f64")]
    pub nav: Option<f64>,

    /// 기초지수명
    #[serde(rename = "IDX_IND_NM")]
    pub index_indicator_name: String,

    /// 목적지수
    #[serde(
        rename = "OBJ_STKPRC_IDX",
        deserialize_with = "deserialize_optional_f64"
    )]
    pub objective_stock_price_index: Option<f64>,

    /// 지수대비
    #[serde(
        rename = "CMPPREVDD_IDX",
        deserialize_with = "deserialize_optional_f64"
    )]
    pub index_change: Option<f64>,

    /// 지수등락률
    #[serde(rename = "FLUC_RT_IDX", deserialize_with = "deserialize_optional_f64")]
    pub index_fluctuation_rate: Option<f64>,

    /// 투자자산순자산총액
    #[serde(
        rename = "INVSTASST_NETASST_TOTAMT",
        deserialize_with = "deserialize_optional_u64"
    )]
    pub investment_asset_net_total_amount: Option<u64>,
}

/// ETN 일별매매정보 레코드
#[derive(Debug, Deserialize)]
pub struct EtnDailyRecord {
    /// 기준일자
    #[serde(rename = "BAS_DD", deserialize_with = "deserialize_krx_date")]
    pub base_date: NaiveDate,

    /// 종목코드
    #[serde(rename = "ISU_CD")]
    pub issue_code: String,

    /// 종목명
    #[serde(rename = "ISU_NM")]
    pub issue_name: String,

    /// 종가
    #[serde(rename = "TDD_CLSPRC", deserialize_with = "deserialize_optional_f64")]
    pub close_price: Option<f64>,

    /// 시가
    #[serde(rename = "TDD_OPNPRC", deserialize_with = "deserialize_optional_f64")]
    pub open_price: Option<f64>,

    /// 고가
    #[serde(rename = "TDD_HGPRC", deserialize_with = "deserialize_optional_f64")]
    pub high_price: Option<f64>,

    /// 저가
    #[serde(rename = "TDD_LWPRC", deserialize_with = "deserialize_optional_f64")]
    pub low_price: Option<f64>,

    /// 대비
    #[serde(
        rename = "CMPPREVDD_PRC",
        deserialize_with = "deserialize_optional_f64"
    )]
    pub price_change: Option<f64>,

    /// 등락률 (%)
    #[serde(
        rename = "FLUC_RT",
        deserialize_with = "deserialize_optional_percentage"
    )]
    pub fluctuation_rate: Option<f64>,

    /// 거래량
    #[serde(rename = "ACC_TRDVOL", deserialize_with = "deserialize_optional_u64")]
    pub trading_volume: Option<u64>,

    /// 거래대금
    #[serde(rename = "ACC_TRDVAL", deserialize_with = "deserialize_optional_u64")]
    pub trading_value: Option<u64>,

    /// 시가총액
    #[serde(rename = "MKTCAP", deserialize_with = "deserialize_optional_u64")]
    pub market_cap: Option<u64>,

    /// 상장주식수
    #[serde(rename = "LIST_SHRS", deserialize_with = "deserialize_optional_u64")]
    pub listed_shares: Option<u64>,

    /// 기초지수명
    #[serde(rename = "IDX_IND_NM")]
    pub index_indicator_name: String,

    /// 목적지수
    #[serde(
        rename = "OBJ_STKPRC_IDX",
        deserialize_with = "deserialize_optional_f64"
    )]
    pub objective_stock_price_index: Option<f64>,

    /// 지수대비
    #[serde(
        rename = "CMPPREVDD_IDX",
        deserialize_with = "deserialize_optional_f64"
    )]
    pub index_change: Option<f64>,

    /// 지수등락률
    #[serde(rename = "FLUC_RT_IDX", deserialize_with = "deserialize_optional_f64")]
    pub index_fluctuation_rate: Option<f64>,

    /// 지시가격금액
    #[serde(
        rename = "INDIC_VAL_AMT",
        deserialize_with = "deserialize_optional_u64"
    )]
    pub indicative_value_amount: Option<u64>,

    /// 1증권당지시가격
    #[serde(
        rename = "PER1SECU_INDIC_VAL",
        deserialize_with = "deserialize_optional_f64"
    )]
    pub per_security_indicative_value: Option<f64>,
}

/// ELW 일별매매정보 레코드
#[derive(Debug, Deserialize)]
pub struct ElwDailyRecord {
    /// 기준일자
    #[serde(rename = "BAS_DD", deserialize_with = "deserialize_krx_date")]
    pub base_date: NaiveDate,

    /// 종목코드
    #[serde(rename = "ISU_CD")]
    pub issue_code: String,

    /// 종목명
    #[serde(rename = "ISU_NM")]
    pub issue_name: String,

    /// 종가
    #[serde(rename = "TDD_CLSPRC", deserialize_with = "deserialize_optional_f64")]
    pub close_price: Option<f64>,

    /// 시가
    #[serde(rename = "TDD_OPNPRC", deserialize_with = "deserialize_optional_f64")]
    pub open_price: Option<f64>,

    /// 고가
    #[serde(rename = "TDD_HGPRC", deserialize_with = "deserialize_optional_f64")]
    pub high_price: Option<f64>,

    /// 저가
    #[serde(rename = "TDD_LWPRC", deserialize_with = "deserialize_optional_f64")]
    pub low_price: Option<f64>,

    /// 대비
    #[serde(
        rename = "CMPPREVDD_PRC",
        deserialize_with = "deserialize_optional_f64"
    )]
    pub price_change: Option<f64>,

    /// 거래량
    #[serde(rename = "ACC_TRDVOL", deserialize_with = "deserialize_optional_u64")]
    pub trading_volume: Option<u64>,

    /// 거래대금
    #[serde(rename = "ACC_TRDVAL", deserialize_with = "deserialize_optional_u64")]
    pub trading_value: Option<u64>,

    /// 시가총액
    #[serde(rename = "MKTCAP", deserialize_with = "deserialize_optional_u64")]
    pub market_cap: Option<u64>,

    /// 상장주식수
    #[serde(rename = "LIST_SHRS", deserialize_with = "deserialize_optional_u64")]
    pub listed_shares: Option<u64>,

    /// 기초자산명
    #[serde(rename = "ULY_NM")]
    pub underlying_name: String,

    /// 기초자산가격
    #[serde(rename = "ULY_PRC")]
    pub underlying_price: String,

    /// 기초자산대비
    #[serde(
        rename = "CMPPREVDD_PRC_ULY",
        deserialize_with = "deserialize_optional_f64"
    )]
    pub underlying_price_change: Option<f64>,

    /// 기초자산등락률
    #[serde(
        rename = "FLUC_RT_ULY",
        deserialize_with = "deserialize_optional_percentage"
    )]
    pub underlying_fluctuation_rate: Option<f64>,
}

/// ETF 일별매매정보를 DataFrame으로 변환
pub fn parse_etf_daily(response: ApiResponse<EtfDailyRecord>) -> Result<DataFrame> {
    let records = response.data;

    if records.is_empty() {
        return Ok(DataFrame::empty());
    }

    let mut dates = Vec::with_capacity(records.len());
    let mut issue_codes = Vec::with_capacity(records.len());
    let mut issue_names = Vec::with_capacity(records.len());
    let mut close_prices = Vec::with_capacity(records.len());
    let mut price_changes = Vec::with_capacity(records.len());
    let mut fluctuation_rates = Vec::with_capacity(records.len());
    let mut trading_volumes = Vec::with_capacity(records.len());
    let mut trading_values = Vec::with_capacity(records.len());
    let mut navs = Vec::with_capacity(records.len());
    let mut index_names = Vec::with_capacity(records.len());
    let mut objective_indices = Vec::with_capacity(records.len());

    for record in records {
        dates.push(record.base_date.format("%Y-%m-%d").to_string());
        issue_codes.push(record.issue_code);
        issue_names.push(record.issue_name);
        close_prices.push(record.close_price);
        price_changes.push(record.price_change);
        fluctuation_rates.push(record.fluctuation_rate);
        trading_volumes.push(record.trading_volume.map(|v| v as i64));
        trading_values.push(record.trading_value.map(|v| v as i64));
        navs.push(record.nav);
        index_names.push(record.index_indicator_name);
        objective_indices.push(record.objective_stock_price_index);
    }

    let df = df! {
        "날짜" => dates,
        "종목코드" => issue_codes,
        "종목명" => issue_names,
        "종가" => close_prices,
        "대비" => price_changes,
        "등락률" => fluctuation_rates,
        "거래량" => trading_volumes,
        "거래대금" => trading_values,
        "NAV" => navs,
        "기초지수명" => index_names,
        "목적지수" => objective_indices,
    }?;

    Ok(df)
}

/// ETN 일별매매정보를 DataFrame으로 변환
pub fn parse_etn_daily(response: ApiResponse<EtnDailyRecord>) -> Result<DataFrame> {
    let records = response.data;

    if records.is_empty() {
        return Ok(DataFrame::empty());
    }

    let mut dates = Vec::with_capacity(records.len());
    let mut issue_codes = Vec::with_capacity(records.len());
    let mut issue_names = Vec::with_capacity(records.len());
    let mut close_prices = Vec::with_capacity(records.len());
    let mut price_changes = Vec::with_capacity(records.len());
    let mut fluctuation_rates = Vec::with_capacity(records.len());
    let mut trading_volumes = Vec::with_capacity(records.len());
    let mut trading_values = Vec::with_capacity(records.len());
    let mut index_names = Vec::with_capacity(records.len());
    let mut indicative_values = Vec::with_capacity(records.len());

    for record in records {
        dates.push(record.base_date.format("%Y-%m-%d").to_string());
        issue_codes.push(record.issue_code);
        issue_names.push(record.issue_name);
        close_prices.push(record.close_price);
        price_changes.push(record.price_change);
        fluctuation_rates.push(record.fluctuation_rate);
        trading_volumes.push(record.trading_volume.map(|v| v as i64));
        trading_values.push(record.trading_value.map(|v| v as i64));
        index_names.push(record.index_indicator_name);
        indicative_values.push(record.per_security_indicative_value);
    }

    let df = df! {
        "날짜" => dates,
        "종목코드" => issue_codes,
        "종목명" => issue_names,
        "종가" => close_prices,
        "대비" => price_changes,
        "등락률" => fluctuation_rates,
        "거래량" => trading_volumes,
        "거래대금" => trading_values,
        "기초지수명" => index_names,
        "1증권당지시가격" => indicative_values,
    }?;

    Ok(df)
}

/// ELW 일별매매정보를 DataFrame으로 변환
pub fn parse_elw_daily(response: ApiResponse<ElwDailyRecord>) -> Result<DataFrame> {
    let records = response.data;

    if records.is_empty() {
        return Ok(DataFrame::empty());
    }

    let mut dates = Vec::with_capacity(records.len());
    let mut issue_codes = Vec::with_capacity(records.len());
    let mut issue_names = Vec::with_capacity(records.len());
    let mut close_prices = Vec::with_capacity(records.len());
    let mut price_changes = Vec::with_capacity(records.len());
    let mut trading_volumes = Vec::with_capacity(records.len());
    let mut trading_values = Vec::with_capacity(records.len());
    let mut underlying_names = Vec::with_capacity(records.len());
    let mut underlying_prices = Vec::with_capacity(records.len());
    let mut underlying_fluctuation_rates = Vec::with_capacity(records.len());

    for record in records {
        dates.push(record.base_date.format("%Y-%m-%d").to_string());
        issue_codes.push(record.issue_code);
        issue_names.push(record.issue_name);
        close_prices.push(record.close_price);
        price_changes.push(record.price_change);
        trading_volumes.push(record.trading_volume.map(|v| v as i64));
        trading_values.push(record.trading_value.map(|v| v as i64));
        underlying_names.push(record.underlying_name);
        underlying_prices.push(record.underlying_price);
        underlying_fluctuation_rates.push(record.underlying_fluctuation_rate);
    }

    let df = df! {
        "날짜" => dates,
        "종목코드" => issue_codes,
        "종목명" => issue_names,
        "종가" => close_prices,
        "대비" => price_changes,
        "거래량" => trading_volumes,
        "거래대금" => trading_values,
        "기초자산명" => underlying_names,
        "기초자산가격" => underlying_prices,
        "기초자산등락률" => underlying_fluctuation_rates,
    }?;

    Ok(df)
}