Skip to content

Commit 98c861d

Browse files
committed
feat: data: lsr
1 parent cbc3f1b commit 98c861d

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/data.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use v_exchanges::bitmex::Bitmex;
1+
use v_exchanges::{binance::Binance, bitmex::Bitmex};
22

33
#[tokio::main]
44
async fn main() {
@@ -7,4 +7,8 @@ async fn main() {
77
let bm = Bitmex::default();
88
let bvol = bm.bvol(2).await.unwrap();
99
dbg!(&bvol);
10+
11+
let bn = Binance::default();
12+
let lsr = bn.global_lsr_account(("BTC", "USDT").into(), "5m".into(), 24 * 12 + 1, "Global".into()).await.unwrap();
13+
dbg!(&lsr[..2]);
1014
}

v_exchanges/src/binance/data.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use adapters::binance::{BinanceHttpUrl, BinanceOption};
2+
use chrono::{DateTime, Utc};
3+
use derive_more::{Display, FromStr};
4+
use eyre::Result;
5+
use serde::Deserialize;
6+
use serde_json::json;
7+
use v_utils::{
8+
Percent,
9+
trades::{Pair, Timeframe},
10+
};
11+
12+
use super::Binance;
13+
14+
#[derive(Clone, Debug, Display, FromStr)]
15+
pub enum LsrWho {
16+
Global,
17+
Top,
18+
}
19+
impl From<&str> for LsrWho {
20+
fn from(s: &str) -> Self {
21+
Self::from_str(s).unwrap()
22+
}
23+
}
24+
25+
impl Binance {
26+
pub async fn global_lsr_account(&self, pair: Pair, tf: Timeframe, limit: u32, who: LsrWho) -> Result<Vec<Lsr>> {
27+
let allowed_range = 1..=500;
28+
//TODO!!: add a `limit outside of range` error, generic for all exchanges
29+
assert!(allowed_range.contains(&limit));
30+
31+
let ending = match who {
32+
LsrWho::Global => "globalLongShortAccountRatio",
33+
LsrWho::Top => "topLongShortPositionRatio",
34+
};
35+
let params = json!({
36+
"symbol": pair.to_string(),
37+
"period": tf.format_binance()?,
38+
"limit": limit,
39+
});
40+
let r: serde_json::Value = self
41+
.0
42+
.get(&format!("/futures/data/{ending}"), &params, [BinanceOption::HttpUrl(BinanceHttpUrl::FuturesUsdM)])
43+
.await?;
44+
let r: Vec<LsrResponse> = serde_json::from_value(r).unwrap();
45+
Ok(r.into_iter().map(|r| r.into()).collect())
46+
}
47+
}
48+
#[derive(Clone, Debug, Default, Deserialize)]
49+
#[serde(rename_all = "camelCase")]
50+
pub struct LsrResponse {
51+
pub symbol: String,
52+
pub long_account: String,
53+
pub long_short_ratio: String,
54+
pub short_account: String,
55+
pub timestamp: i64,
56+
}
57+
#[derive(Clone, Debug, Default, Copy)]
58+
pub struct Lsr {
59+
pub time: DateTime<Utc>,
60+
pub pair: Pair,
61+
pub long: Percent,
62+
pub short: Percent,
63+
}
64+
impl Lsr {
65+
pub fn ratio(&self) -> f64 {
66+
*self.long / *self.short
67+
}
68+
}
69+
impl From<LsrResponse> for Lsr {
70+
fn from(r: LsrResponse) -> Self {
71+
Self {
72+
time: DateTime::from_timestamp_millis(r.timestamp).unwrap(),
73+
pair: Pair::from_str(&r.symbol).unwrap(),
74+
long: Percent::from_str(&r.long_account).unwrap(),
75+
short: Percent::from_str(&r.short_account).unwrap(),
76+
}
77+
}
78+
}

v_exchanges/src/binance/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod data;
12
mod futures;
23
mod market;
34
mod spot;

0 commit comments

Comments
 (0)