|
| 1 | +use std::fmt; |
| 2 | + |
| 3 | +use chrono::{DateTime, TimeZone, Utc}; |
| 4 | +use color_eyre::eyre::Result; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | +use serde_json::{Value, json}; |
| 7 | +use serde_with::{DisplayFromStr, serde_as}; |
| 8 | +use thiserror::Error; |
| 9 | +use v_exchanges_adapters::bybit::{BybitHttpUrl, BybitOption}; |
| 10 | +use v_utils::{ |
| 11 | + trades::{Kline, Ohlc, Pair, Timeframe}, |
| 12 | + utils::filter_nulls, |
| 13 | +}; |
| 14 | + |
| 15 | +use crate::core::Klines; |
| 16 | + |
| 17 | +//MOVE: centralized error module |
| 18 | +#[derive(Debug)] |
| 19 | +struct LimitOutOfRangeError { |
| 20 | + allowed: std::ops::RangeInclusive<u32>, |
| 21 | + provided: u32, |
| 22 | +} |
| 23 | +impl fmt::Display for LimitOutOfRangeError { |
| 24 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 25 | + write!(f, "Limit out of range. Allowed: {:?}, provided: {}", self.allowed, self.provided) |
| 26 | + } |
| 27 | +} |
| 28 | +impl std::error::Error for LimitOutOfRangeError {} |
| 29 | + |
| 30 | +pub async fn klines(client: &v_exchanges_adapters::Client, pair: Pair, tf: Timeframe, limit: u32, start_time: Option<u64>, end_time: Option<u64>) -> Result<Klines> { |
| 31 | + let range = 1..=1000; |
| 32 | + if !range.contains(&limit) { |
| 33 | + return Err(LimitOutOfRangeError { allowed: range, provided: limit }.into()); |
| 34 | + } |
| 35 | + |
| 36 | + let mut params = filter_nulls(json!({ |
| 37 | + "category": "linear", // can be ["linear", "inverse", "spot"] afaiu, could drive some generics with this later, but for now hardcode |
| 38 | + "symbol": pair.to_string(), |
| 39 | + "interval": tf.format_bybit()?, |
| 40 | + "limit": limit, |
| 41 | + "startTime": start_time, |
| 42 | + "endTime": end_time, |
| 43 | + })); |
| 44 | + let kline_response: KlineResponse = client.get("/v5/market/kline", ¶ms, [BybitOption::Default]).await.unwrap(); |
| 45 | + |
| 46 | + let mut klines = Vec::new(); |
| 47 | + for k in kline_response.result.list { |
| 48 | + if kline_response.time > k.0 + tf.duration().num_milliseconds() { |
| 49 | + klines.push(Kline { |
| 50 | + open_time: Utc.timestamp_millis(k.0), |
| 51 | + ohlc: Ohlc { |
| 52 | + open: k.1, |
| 53 | + close: k.2, |
| 54 | + high: k.3, |
| 55 | + low: k.4, |
| 56 | + }, |
| 57 | + volume_quote: k.5, |
| 58 | + trades: None, |
| 59 | + taker_buy_volume_quote: None, |
| 60 | + }); |
| 61 | + } |
| 62 | + } |
| 63 | + Ok(Klines { v: klines, tf, oi: Vec::new() }) |
| 64 | +} |
| 65 | + |
| 66 | +#[derive(Debug, Serialize, Deserialize)] |
| 67 | +#[serde(rename_all = "camelCase")] |
| 68 | +pub struct KlineResponse { |
| 69 | + pub result: ResponseResult, |
| 70 | + pub ret_code: i32, |
| 71 | + pub ret_ext_info: std::collections::HashMap<String, serde_json::Value>, |
| 72 | + pub ret_msg: String, |
| 73 | + pub time: i64, |
| 74 | +} |
| 75 | + |
| 76 | +#[derive(Debug, Serialize, Deserialize)] |
| 77 | +#[serde(rename_all = "camelCase")] |
| 78 | +pub struct ResponseResult { |
| 79 | + pub category: String, |
| 80 | + pub list: Vec<KlineData>, |
| 81 | + pub symbol: String, |
| 82 | +} |
| 83 | + |
| 84 | +#[serde_as] |
| 85 | +#[derive(Debug, Serialize, Deserialize)] |
| 86 | +pub struct KlineData( |
| 87 | + #[serde_as(as = "DisplayFromStr")] pub i64, |
| 88 | + #[serde_as(as = "DisplayFromStr")] pub f64, |
| 89 | + #[serde_as(as = "DisplayFromStr")] pub f64, |
| 90 | + #[serde_as(as = "DisplayFromStr")] pub f64, |
| 91 | + #[serde_as(as = "DisplayFromStr")] pub f64, |
| 92 | + #[serde_as(as = "DisplayFromStr")] pub f64, |
| 93 | + #[serde_as(as = "DisplayFromStr")] pub f64, |
| 94 | +); |
0 commit comments