Skip to content

Commit 965e8ee

Browse files
committed
update longport-candlesticks
1 parent 69c0acb commit 965e8ee

File tree

17 files changed

+70
-121
lines changed

17 files changed

+70
-121
lines changed

examples/rust/account_asset/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1313
let (ctx, _) = TradeContext::try_new(config).await?;
1414

1515
let resp = ctx.account_balance(None).await?;
16-
println!("{:?}", resp);
16+
println!("{resp:?}");
1717
Ok(())
1818
}

examples/rust/http_client/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1313
.response::<String>()
1414
.send()
1515
.await?;
16-
println!("{}", resp);
16+
println!("{resp}");
1717
Ok(())
1818
}

examples/rust/submit_order/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2525
)
2626
.submitted_price(decimal!(50i32));
2727
let resp = ctx.submit_order(opts).await?;
28-
println!("{:?}", resp);
28+
println!("{resp:?}");
2929
Ok(())
3030
}

examples/rust/subscribe_candlesticks/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1919
.await?;
2020

2121
while let Some(event) = receiver.recv().await {
22-
println!("{:?}", event);
22+
println!("{event:?}");
2323
}
2424
Ok(())
2525
}

examples/rust/subscribe_quote/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2121
)
2222
.await?;
2323
while let Some(event) = receiver.recv().await {
24-
println!("{:?}", event);
24+
println!("{event:?}");
2525
}
2626
Ok(())
2727
}

examples/rust/today_orders/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1414

1515
let resp = ctx.today_orders(None).await?;
1616
for obj in resp {
17-
println!("{:?}", obj);
17+
println!("{obj:?}");
1818
}
1919
Ok(())
2020
}

rust/crates/candlesticks/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
edition.workspace = true
33
name = "longport-candlesticks"
4-
version = "3.0.10"
4+
version = "3.0.11"
55
description = "LongPort candlestick utils for Rust"
66
license = "MIT OR Apache-2.0"
77

rust/crates/candlesticks/src/market.rs

Lines changed: 27 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ impl Market {
195195
#[must_use]
196196
pub fn merge_trade<H, TS, C, T, P, V, R>(
197197
&self,
198-
ts: TS,
199198
half_days: H,
200199
period: Period,
201200
input: Option<C>,
@@ -204,17 +203,22 @@ impl Market {
204203
) -> UpdateAction<C>
205204
where
206205
H: Days,
207-
TS: TradeSessionType,
206+
TS: TradeSessionType + Eq,
208207
C: CandlestickType<PriceType = P, VolumeType = V, TurnoverType = R, TradeSessionType = TS>,
209-
T: TradeType<PriceType = P, VolumeType = V, TurnoverType = R>,
208+
T: TradeType<PriceType = P, VolumeType = V, TurnoverType = R, TradeSessionType = TS>,
210209
P: PartialOrd + Add<Output = P>,
211210
V: Add<Output = V> + Zero,
212211
R: Add<Output = R> + Zero,
213212
{
213+
let trade_session = trade.trade_session();
214+
214215
debug_assert!(period != Period::Day);
216+
if let Some(input_trade_session) = input.as_ref().map(|c| c.trade_session()) {
217+
debug_assert!(input_trade_session == trade_session);
218+
}
215219

216220
let Some(time) = self.candlestick_time(
217-
ts,
221+
trade_session,
218222
half_days,
219223
period,
220224
trade.time().to_timezone(self.timezone),
@@ -260,7 +264,7 @@ impl Market {
260264
close: trade.price(),
261265
volume: trade.volume(),
262266
turnover: trade.turnover(self.lot_size),
263-
trade_session: ts,
267+
trade_session,
264268
});
265269
UpdateAction::AppendNew {
266270
confirmed: None,
@@ -279,7 +283,7 @@ impl Market {
279283
close: prev.close(),
280284
volume: V::zero(),
281285
turnover: R::zero(),
282-
trade_session: ts,
286+
trade_session,
283287
});
284288

285289
if update_fields.contains(UpdateFields::PRICE) {
@@ -306,23 +310,31 @@ impl Market {
306310
#[must_use]
307311
pub fn merge_quote<H, TS, C, Q, P, V, R>(
308312
&self,
309-
ts: TS,
310313
half_days: H,
311314
period: Period,
312315
input: Option<C>,
313316
quote: &Q,
314317
) -> UpdateAction<C>
315318
where
316319
H: Days,
317-
TS: TradeSessionType,
320+
TS: TradeSessionType + Eq,
318321
C: CandlestickType<PriceType = P, VolumeType = V, TurnoverType = R, TradeSessionType = TS>,
319-
Q: QuoteType<PriceType = P, VolumeType = V, TurnoverType = R>,
322+
Q: QuoteType<PriceType = P, VolumeType = V, TurnoverType = R, TradeSessionType = TS>,
320323
{
324+
let trade_session = quote.trade_session();
325+
321326
debug_assert!(period == Period::Day);
327+
if let Some(input_trade_session) = input.as_ref().map(|c| c.trade_session()) {
328+
debug_assert!(input_trade_session == trade_session);
329+
}
322330

323331
let tz = self.timezone;
324-
let Some(time) = self.candlestick_time(ts, half_days, period, quote.time().to_timezone(tz))
325-
else {
332+
let Some(time) = self.candlestick_time(
333+
trade_session,
334+
half_days,
335+
period,
336+
quote.time().to_timezone(tz),
337+
) else {
326338
return UpdateAction::None;
327339
};
328340

@@ -336,7 +348,7 @@ impl Market {
336348
close: quote.last_done(),
337349
volume: quote.volume(),
338350
turnover: quote.turnover(),
339-
trade_session: ts,
351+
trade_session,
340352
}))
341353
}
342354
None => UpdateAction::AppendNew {
@@ -349,7 +361,7 @@ impl Market {
349361
close: quote.last_done(),
350362
volume: quote.volume(),
351363
turnover: quote.turnover(),
352-
trade_session: ts,
364+
trade_session,
353365
}),
354366
},
355367
Some(prev) if time > prev.time() => UpdateAction::AppendNew {
@@ -362,93 +374,14 @@ impl Market {
362374
close: quote.last_done(),
363375
volume: quote.volume(),
364376
turnover: quote.turnover(),
365-
trade_session: ts,
377+
trade_session,
366378
}),
367379
},
368380
_ => UpdateAction::None,
369381
}
370382
}
371383

372-
#[must_use]
373-
pub fn merge_candlestick<H, C>(
374-
&self,
375-
period: Period,
376-
input: Option<C>,
377-
candlestick: &C,
378-
) -> UpdateAction<C>
379-
where
380-
H: Days,
381-
C: CandlestickType,
382-
C::PriceType: PartialOrd,
383-
C::VolumeType: Add<Output = C::VolumeType>,
384-
C::TurnoverType: Add<Output = C::TurnoverType>,
385-
{
386-
let Some(time) = self.candlestick_time(
387-
candlestick.trade_session(),
388-
false,
389-
period,
390-
candlestick.time(),
391-
) else {
392-
return UpdateAction::None;
393-
};
394-
395-
match input {
396-
Some(prev) if time == prev.time() => {
397-
let mut res_candlestick = prev;
398-
399-
res_candlestick.set_high(if candlestick.high() > res_candlestick.high() {
400-
candlestick.high()
401-
} else {
402-
res_candlestick.high()
403-
});
404-
405-
res_candlestick.set_low(if candlestick.low() < res_candlestick.low() {
406-
candlestick.low()
407-
} else {
408-
res_candlestick.low()
409-
});
410-
411-
res_candlestick.set_close(candlestick.close());
412-
413-
res_candlestick.set_volume(candlestick.volume() + res_candlestick.volume());
414-
res_candlestick.set_turnover(candlestick.turnover() + res_candlestick.turnover());
415-
416-
UpdateAction::UpdateLast(res_candlestick)
417-
}
418-
None => UpdateAction::AppendNew {
419-
confirmed: None,
420-
new: C::new(CandlestickComponents {
421-
time: time.to_timezone(time_tz::timezones::db::UTC),
422-
open: candlestick.open(),
423-
high: candlestick.high(),
424-
low: candlestick.low(),
425-
close: candlestick.close(),
426-
volume: candlestick.volume(),
427-
turnover: candlestick.turnover(),
428-
trade_session: candlestick.trade_session(),
429-
}),
430-
},
431-
Some(prev) if time > prev.time() => UpdateAction::AppendNew {
432-
confirmed: Some(prev),
433-
new: C::new(CandlestickComponents {
434-
time: time.to_timezone(time_tz::timezones::db::UTC),
435-
open: candlestick.open(),
436-
high: candlestick.high(),
437-
low: candlestick.low(),
438-
close: candlestick.close(),
439-
volume: candlestick.volume(),
440-
turnover: candlestick.turnover(),
441-
trade_session: candlestick.trade_session(),
442-
}),
443-
},
444-
_ => UpdateAction::None,
445-
}
446-
}
447-
448-
pub fn candlestick_trade_session(
449-
&self,
450-
candlestick_time: OffsetDateTime,
451-
) -> Option<TradeSessionKind> {
384+
pub fn trade_session(&self, candlestick_time: OffsetDateTime) -> Option<TradeSessionKind> {
452385
let candlestick_time = candlestick_time.to_timezone(self.timezone);
453386
for (idx, trade_sessions) in self.trade_sessions.iter().enumerate() {
454387
for TradeSession {

rust/crates/candlesticks/src/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use time::OffsetDateTime;
22

3+
use crate::TradeSessionType;
4+
35
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
46
#[allow(non_camel_case_types)]
57
#[repr(u8)]
@@ -55,6 +57,7 @@ pub trait QuoteType {
5557
type PriceType;
5658
type VolumeType;
5759
type TurnoverType;
60+
type TradeSessionType;
5861

5962
fn time(&self) -> OffsetDateTime;
6063
fn open(&self) -> Self::PriceType;
@@ -63,6 +66,7 @@ pub trait QuoteType {
6366
fn last_done(&self) -> Self::PriceType;
6467
fn volume(&self) -> Self::VolumeType;
6568
fn turnover(&self) -> Self::TurnoverType;
69+
fn trade_session(&self) -> Self::TradeSessionType;
6670
}
6771

6872
bitflags::bitflags! {
@@ -77,9 +81,11 @@ pub trait TradeType {
7781
type PriceType;
7882
type VolumeType;
7983
type TurnoverType;
84+
type TradeSessionType: TradeSessionType;
8085

8186
fn time(&self) -> OffsetDateTime;
8287
fn price(&self) -> Self::PriceType;
8388
fn volume(&self) -> Self::VolumeType;
8489
fn turnover(&self, lot_size: i32) -> Self::TurnoverType;
90+
fn trade_session(&self) -> Self::TradeSessionType;
8591
}

rust/crates/candlesticks/tests/cn_ts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn sh_trade_session() {
66
let market = CN;
77

88
assert_eq!(
9-
market.candlestick_trade_session(datetime!(2024-1-1 15:00:00 +8)),
9+
market.trade_session(datetime!(2024-1-1 15:00:00 +8)),
1010
Some(TRADE_SESSION_INTRADAY)
1111
);
1212
}

0 commit comments

Comments
 (0)