1+ use chrono:: { DateTime , TimeZone , Utc } ;
12//HACK: Methods should be implemented on the central interface struct, following <https://github.com/wisespace-io/binance-rs>.
2- use v_exchanges_adapters:: binance:: BinanceOption ;
3-
43use color_eyre:: eyre:: Result ;
5- use v_utils:: trades:: { Pair , Timeframe } ;
6- use serde:: Serialize ;
4+ use serde:: { Deserialize , Serialize } ;
5+ use serde_json:: Value ;
6+ use serde_with:: { DisplayFromStr , serde_as} ;
7+ use v_exchanges_adapters:: binance:: BinanceOption ;
8+ use v_utils:: trades:: { Kline , Ohlc , Pair , Timeframe } ;
79
8- use crate :: binance:: futures:: core:: * ;
10+ use crate :: { binance:: futures:: core:: * , core :: Klines } ;
911
10- //? should the `symbol` and `tf` be strs, while only the top-most level interface takes in the general interfaces?
11- pub async fn klines ( generic_client : & v_exchanges_adapters:: Client , pair : Pair , tf : Timeframe , limit : Option < u16 > , start_time : Option < u64 > , end_time : Option < u64 > ) -> Result < Vec < Kline > > {
12+ pub async fn klines ( client : & v_exchanges_adapters:: Client , pair : Pair , tf : Timeframe , limit : Option < u32 > , start_time : Option < u64 > , end_time : Option < u64 > ) -> Result < Klines > {
1213 #[ derive( Serialize ) ]
1314 pub struct KlineParams {
1415 pub symbol : String ,
1516 pub interval : String ,
1617 #[ serde( skip_serializing_if = "Option::is_none" ) ]
17- pub limit : Option < u16 > ,
18+ pub limit : Option < u32 > ,
1819 #[ serde( rename = "startTime" , skip_serializing_if = "Option::is_none" ) ]
1920 pub start_time : Option < u64 > ,
2021 #[ serde( rename = "endTime" , skip_serializing_if = "Option::is_none" ) ]
@@ -29,7 +30,66 @@ pub async fn klines(generic_client: &v_exchanges_adapters::Client, pair: Pair, t
2930 end_time,
3031 } ;
3132
32- //TODO!!!: have the function take the vec of options, instead of hardcoding `[BinanceOption::Default]`
33- let klines: Vec < Kline > = generic_client. get ( "/fapi/v1/klines" , Some ( & params) , [ BinanceOption :: Default ] ) . await . unwrap ( ) ;
34- Ok ( klines)
33+ let kline_responses: Vec < KlineResponse > = client. get ( "/fapi/v1/klines" , Some ( & params) , [ BinanceOption :: Default ] ) . await . unwrap ( ) ;
34+ let klines: Vec < Kline > = kline_responses. into_iter ( ) . map ( Kline :: from) . collect ( ) ;
35+
36+ Ok ( Klines { v : klines, tf, oi : Vec :: new ( ) } )
37+ }
38+
39+ /** # Ex: ```json
40+ [1731448080000,\"88591.90\",\"88630.90\",\"88560.00\",\"88574.10\",\"173.581\",1731448139999,\"15378315.48720\",2800,\"113.654\",\"10069629.84420\",\"0\"]
41+ ```
42+ **/
43+ #[ serde_as]
44+ #[ derive( Debug , Serialize , Deserialize , Clone ) ]
45+ pub struct KlineResponse {
46+ pub open_time : i64 ,
47+ #[ serde_as( as = "DisplayFromStr" ) ]
48+ pub open : f64 ,
49+ #[ serde_as( as = "DisplayFromStr" ) ]
50+ pub close : f64 ,
51+ #[ serde_as( as = "DisplayFromStr" ) ]
52+ pub high : f64 ,
53+ #[ serde_as( as = "DisplayFromStr" ) ]
54+ pub low : f64 ,
55+ #[ serde_as( as = "DisplayFromStr" ) ]
56+ pub volume : f64 ,
57+ pub close_time : i64 ,
58+ #[ serde_as( as = "DisplayFromStr" ) ]
59+ pub quote_asset_volume : f64 ,
60+ pub number_of_trades : usize ,
61+ #[ serde_as( as = "DisplayFromStr" ) ]
62+ pub taker_buy_base_asset_volume : f64 ,
63+ #[ serde_as( as = "DisplayFromStr" ) ]
64+ pub taker_buy_quote_asset_volume : f64 ,
65+
66+ __ignore : Option < Value > ,
67+ }
68+ impl From < KlineResponse > for Kline {
69+ fn from ( k : KlineResponse ) -> Self {
70+ let ohlc = Ohlc {
71+ open : k. open ,
72+ high : k. high ,
73+ low : k. low ,
74+ close : k. close ,
75+ } ;
76+ Kline {
77+ open_time : DateTime :: from_timestamp_millis ( k. open_time ) . unwrap ( ) ,
78+ ohlc,
79+ volume_quote : k. quote_asset_volume ,
80+ //TODO!!!!!!: before adding check that it is not less than start_time + tf
81+ trades : Some ( k. number_of_trades ) ,
82+ taker_buy_volume_quote : Some ( k. taker_buy_quote_asset_volume ) ,
83+ close_time : Some ( Utc . timestamp_millis_opt ( k. close_time ) . unwrap ( ) ) ,
84+ }
85+ }
86+ }
87+
88+ #[ cfg( test) ]
89+ mod tests {
90+ #[ test]
91+ fn kline_core ( ) {
92+ let raw_str = "[1731448080000,\" 88591.90\" ,\" 88630.90\" ,\" 88560.00\" ,\" 88574.10\" ,\" 173.581\" ,1731448139999,\" 15378315.48720\" ,2800,\" 113.654\" ,\" 10069629.84420\" ,\" 0\" ]" ;
93+ let _: super :: Kline = serde_json:: from_str ( raw_str) . unwrap ( ) ;
94+ }
3595}
0 commit comments