use pretty_simple_display::{DebugPretty, DisplaySimple};
use serde::{Deserialize, Serialize};
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct FundingChartData {
pub current_interest: f64,
pub interest_8h: f64,
pub data: Vec<FundingDataPoint>,
}
impl FundingChartData {
pub fn new() -> Self {
Self {
current_interest: 0.0,
interest_8h: 0.0,
data: Vec::new(),
}
}
}
impl Default for FundingChartData {
fn default() -> Self {
Self::new()
}
}
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct FundingDataPoint {
pub index_price: f64,
pub interest_8h: f64,
pub timestamp: u64,
}
impl FundingDataPoint {
pub fn new(index_price: f64, interest_8h: f64, timestamp: u64) -> Self {
Self {
index_price,
interest_8h,
timestamp,
}
}
}
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct FundingRateData {
pub timestamp: u64,
pub index_price: f64,
pub interest_8h: f64,
pub interest_1h: f64,
pub prev_index_price: f64,
}
impl FundingRateData {
pub fn new(
timestamp: u64,
index_price: f64,
interest_8h: f64,
interest_1h: f64,
prev_index_price: f64,
) -> Self {
Self {
timestamp,
index_price,
interest_8h,
interest_1h,
prev_index_price,
}
}
}
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct TradingViewChartData {
pub status: String,
pub ticks: Vec<u64>,
pub open: Vec<f64>,
pub high: Vec<f64>,
pub low: Vec<f64>,
pub close: Vec<f64>,
pub volume: Vec<f64>,
pub cost: Vec<f64>,
}
impl TradingViewChartData {
pub fn new() -> Self {
Self {
status: "ok".to_string(),
ticks: Vec::new(),
open: Vec::new(),
high: Vec::new(),
low: Vec::new(),
close: Vec::new(),
volume: Vec::new(),
cost: Vec::new(),
}
}
#[allow(clippy::too_many_arguments)]
pub fn add_candle(
&mut self,
timestamp: u64,
open: f64,
high: f64,
low: f64,
close: f64,
volume: f64,
cost: f64,
) {
self.ticks.push(timestamp);
self.open.push(open);
self.high.push(high);
self.low.push(low);
self.close.push(close);
self.volume.push(volume);
self.cost.push(cost);
}
}
impl Default for TradingViewChartData {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_funding_chart_data_creation() {
let chart_data = FundingChartData::new();
assert_eq!(chart_data.current_interest, 0.0);
assert_eq!(chart_data.interest_8h, 0.0);
assert!(chart_data.data.is_empty());
}
#[test]
fn test_trading_view_chart_data_creation() {
let mut chart_data = TradingViewChartData::new();
chart_data.add_candle(
1640995200000, 45000.0, 45500.0, 44800.0, 45200.0, 100.0, 4520000.0, );
assert_eq!(chart_data.ticks.len(), 1);
assert_eq!(chart_data.open[0], 45000.0);
assert_eq!(chart_data.high[0], 45500.0);
}
#[test]
fn test_serde() {
let funding_data = FundingRateData::new(
1640995200000, 45000.0, 0.0001, 0.00001, 44900.0, );
let json = serde_json::to_string(&funding_data).unwrap();
let deserialized: FundingRateData = serde_json::from_str(&json).unwrap();
assert_eq!(funding_data.timestamp, deserialized.timestamp);
assert_eq!(funding_data.index_price, deserialized.index_price);
}
}