Repository: yasstake/rbot (language: python)

Issue:
Staging
* Implement arhcive log download
* Implement PublicWs
* implement download gap
* Fix: rest api will work
* replace AccountCoins to Coins
* Add profit info to Order(from logger)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

- **新機能**
  - BinanceとBybitの設定更新に伴い、`exchange_name`フィールドを`MarketConfig`に追加しました。
  - BinanceとBybitマーケットのテスト機能を強化しました。
  - メッセージ処理用の新しいenum、構造体、変換関数を追加しました。
  - 非同期処理、ログ記録、エラーハンドリングの改善を行いました。
  - `MarketConfig`を使用してバランスを更新する新しいメソッドを追加しました。
  - データベースの操作とキャッシュの更新に関する新機能を追加しました。
  - セッション管理に`session_id`フィールドを追加し、データベースとの連携を強化しました。

- **バグ修正**
  - BinanceとBybitの設定と市場操作に関連するコードを整理し、バグを修正しました。
  - データベースの初期化方法を変更し、一貫性と信頼性を向上させました。

- **リファクタリング**
  - コードの構造を調整し、モジュールの可視性を改善しました。
  - 不要なメソッドやフィールドの削除、メソッドシグネチャの更新を行いました。
  - ログ構造を再構築し、不要な機能を削除しました。

- **ドキュメント**
  - コードの説明とメソッドに関するドキュメントを更新しました。

<!-- end of auto-generated comment: release notes by coderabbit.ai -->


Discussion / hints:

Fix patch:
diff --git a/Cargo.toml b/Cargo.toml
index eea73e06..ce712339 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -63,28 +63,24 @@ tokio-stream = { version = "0.1.14" }
 rusqlite = { version = "0.31.0", features = ["bundled"] }
 
 
-polars = { version = "0.35", features = ["lazy"] }
-polars-core = { version = "0.35", features = [
+polars = { version = "0.37.0", features = ["lazy", "ndarray",
     "ndarray",
     "temporal",
     "dtype-time",
     "object",
     "round_series",
-] }
-polars-lazy = { version = "0.35", features = [
+    "lazy",
     "dynamic_group_by",
     "round_series",
     "pivot",
 ] }
-polars-time = { version = "0.35" }
-polars-ops = { version = "0.35" }
-pyo3-polars = { version = "0.9" }
+pyo3-polars = { version = "0.11.3" }
 
 
 ndarray = { version = "0.15" }
 numpy = { version = "0.20" }
 
-reqwest = { version = "0.11", features = ["blocking", "gzip"] }
+reqwest = { version = "0.12.1", features = ["gzip", "blocking"] }
 futures = { version = "0.3" }
 flate2 = { version = "1.0.24" }
 
diff --git a/exchanges/binance/Cargo.toml b/exchanges/binance/Cargo.toml
index 38514caa..55f0c2e2 100644
--- a/exchanges/binance/Cargo.toml
+++ b/exchanges/binance/Cargo.toml
@@ -25,7 +25,7 @@ serde_json = {workspace = true}
 
 crossbeam-channel = {workspace = true}
 csv = {workspace = true}
-polars-core = {workspace = true}
+polars = {workspace = true}
 pyo3-polars = {workspace = true}
 
 tokio = {workspace = true}
diff --git a/exchanges/bybit/Cargo.toml b/exchanges/bybit/Cargo.toml
index ab735039..1f72b737 100644
--- a/exchanges/bybit/Cargo.toml
+++ b/exchanges/bybit/Cargo.toml
@@ -27,7 +27,7 @@ serde_json = {workspace = true}
 
 crossbeam-channel = {workspace = true}
 csv = {workspace = true}
-polars-core = {workspace = true}
+polars = {workspace = true}
 pyo3-polars = {workspace = true}
 
 tokio = {workspace = true}
diff --git a/exchanges/bybit/src/market.rs b/exchanges/bybit/src/market.rs
index cbdd0e29..9500cbce 100644
--- a/exchanges/bybit/src/market.rs
+++ b/exchanges/bybit/src/market.rs
@@ -9,7 +9,7 @@ use crossbeam_channel::Sender;
 use csv::StringRecord;
 
 use futures::StreamExt;
-use polars_core::export::num::FromPrimitive;
+use polars::export::num::FromPrimitive;
 use pyo3::ffi::getter;
 
 use std::sync::{Arc, Mutex, RwLock};
diff --git a/exchanges/bybit/src/message.rs b/exchanges/bybit/src/message.rs
index ac310998..0c0eb1de 100644
--- a/exchanges/bybit/src/message.rs
+++ b/exchanges/bybit/src/message.rs
@@ -1113,7 +1113,6 @@ impl Into<AccountCoins> for BybitAccountResponse {
 #[allow(unused_variables)]
 mod bybit_message_test {
     use super::*;
-    use polars_core::utils::rayon::result;
     use rbot_lib::common::init_debug_log;
 
     use crate::message::{
diff --git a/exchanges/bybit/src/rest.rs b/exchanges/bybit/src/rest.rs
index f5cadedb..666aa4f2 100644
--- a/exchanges/bybit/src/rest.rs
+++ b/exchanges/bybit/src/rest.rs
@@ -5,7 +5,7 @@ use std::convert;
 use std::fmt::format;
 
 use csv::StringRecord;
-use polars_core::export::num::FromPrimitive;
+use polars::export::num::FromPrimitive;
 use rbot_lib::common::time_string;
 use rbot_lib::common::AccountCoins;
 use rbot_lib::common::AccountPair;
diff --git a/rbot_lib/Cargo.toml b/rbot_lib/Cargo.toml
index 763e962b..e5f42b31 100644
--- a/rbot_lib/Cargo.toml
+++ b/rbot_lib/Cargo.toml
@@ -20,11 +20,7 @@ serde_json = {workspace=true}
 serde_with = {workspace=true}
 env_logger = {workspace = true}
 
-polars = {workspace=true, features=["lazy"]}
-polars-core = {workspace=true, features=["ndarray", "temporal", "dtype-time", "object", "round_series"]}
-polars-lazy = {workspace=true, features=["dynamic_group_by", "round_series", "pivot"]}
-polars-time = {workspace=true}
-polars-ops = {workspace=true}
+polars = {workspace=true}
 pyo3-polars = {workspace=true}
 
 strum_macros = {workspace=true}
diff --git a/rbot_lib/src/common/order.rs b/rbot_lib/src/common/order.rs
index 679e2f77..4521eb49 100644
--- a/rbot_lib/src/common/order.rs
+++ b/rbot_lib/src/common/order.rs
@@ -11,10 +11,10 @@ use super::MarketConfig;
 use super::MarketMessage;
 use super::SEC;
 use async_std::stream::Cloned;
-use polars_core::prelude::DataFrame;
-use polars_core::prelude::NamedFrom;
-use polars_core::prelude::TimeUnit;
-use polars_core::series::Series;
+use polars::prelude::DataFrame;
+use polars::prelude::NamedFrom;
+use polars::prelude::TimeUnit;
+use polars::series::Series;
 use pyo3::pyclass;
 use pyo3::pymethods;
 use reqwest::Proxy;
diff --git a/rbot_lib/src/common/orderbook.rs b/rbot_lib/src/common/orderbook.rs
index f76d0bd9..2eb3ebba 100644
--- a/rbot_lib/src/common/orderbook.rs
+++ b/rbot_lib/src/common/orderbook.rs
@@ -8,7 +8,7 @@ use std::{
 };
 
 use once_cell::sync::Lazy;
-use polars_core::{
+use polars::{
     prelude::{DataFrame, NamedFrom},
     series::Series,
 };
diff --git a/rbot_lib/src/common/util.rs b/rbot_lib/src/common/util.rs
index 98ac7332..4cc9d069 100644
--- a/rbot_lib/src/common/util.rs
+++ b/rbot_lib/src/common/util.rs
@@ -6,7 +6,7 @@
 
 use hmac::{Hmac, Mac};
 use once_cell::sync::Lazy;
-use polars_core::export::num::FromPrimitive;
+use polars::export::num::FromPrimitive;
 use rust_decimal::Decimal;
 use serde::{de, Deserialize as _, Deserializer, Serialize, Serializer};
 use serde_derive::Deserialize;
diff --git a/rbot_lib/src/db/df.rs b/rbot_lib/src/db/df.rs
index a08664f9..c33fe783 100644
--- a/rbot_lib/src/db/df.rs
+++ b/rbot_lib/src/db/df.rs
@@ -10,12 +10,13 @@ use polars::prelude::Duration;
 use polars::prelude::DynamicGroupOptions;
 use polars::prelude::NamedFrom;
 use polars::prelude::Series;
-use polars_core::prelude::SortOptions;
+use polars::prelude::SortOptions;
 
-use polars_lazy::frame::pivot::pivot;
-use polars_lazy::prelude::{col, LazyFrame};
-use polars_lazy::prelude::IntoLazy;
-use polars_time::ClosedWindow;
+use polars::prelude::pivot::pivot;
+use polars::prelude::col;
+use polars::prelude::LazyFrame;
+use polars::prelude::IntoLazy;
+use polars::prelude::ClosedWindow;
 
 #[allow(non_upper_case_globals)]
 #[allow(non_snake_case)]
@@ -115,11 +116,11 @@ pub fn select_df_lazy(df: &DataFrame, start_time: MicroSec, end_time: MicroSec)
 }
 
 pub fn start_time_df(df: &DataFrame) -> Option<MicroSec> {
-    df.column(KEY::time_stamp).unwrap().min()
+    df.column(KEY::time_stamp).unwrap().min().unwrap()
 }
 
 pub fn end_time_df(df: &DataFrame) -> Option<MicroSec> {
-    df.column(KEY::time_stamp).unwrap().max()
+    df.column(KEY::time_stamp).unwrap().max().unwrap()
 }
 
 pub fn merge_df(df1: &DataFrame, df2: &DataFrame) -> DataFrame {
diff --git a/rbot_lib/src/db/sqlite.rs b/rbot_lib/src/db/sqlite.rs
index 6e879d16..6efb5184 100644
--- a/rbot_lib/src/db/sqlite.rs
+++ b/rbot_lib/src/db/sqlite.rs
@@ -7,10 +7,9 @@ use anyhow::Context;
 
 use numpy::IntoPyArray;
 use numpy::PyArray2;
-use once_cell::sync::Lazy;
 use polars::prelude::DataFrame;
 use polars::prelude::Float64Type;
-use polars_core::prelude::IndexOrder;
+use polars::prelude::IndexOrder;
 use pyo3::{Py, Python};
 use pyo3_polars::PyDataFrame;
 use rusqlite::params_from_iter;
@@ -26,7 +25,6 @@ use tokio::task::JoinHandle;
 //use std::thread::JoinHandle;
 //use std::thread::spawn;
 
-use crate::common::env_rbot_db_root;
 use crate::common::MarketStream;
 
 //use crossbeam_channel::Receiver;
@@ -680,8 +678,8 @@ impl TradeTable {
     }
 
     pub fn set_cache_ohlcvv(&mut self, df: DataFrame) {
-        let start_time: MicroSec = df.column(KEY::time_stamp).unwrap().min().unwrap();
-        let end_time: MicroSec = df.column(KEY::time_stamp).unwrap().max().unwrap();
+        let start_time: MicroSec = df.column(KEY::time_stamp).unwrap().min().unwrap().unwrap();
+        let end_time: MicroSec = df.column(KEY::time_stamp).unwrap().max().unwrap().unwrap();
 
         let head = select_df(&self.cache_ohlcvv, 0, start_time);
         let tail = select_df(&self.cache_ohlcvv, end_time, 0);
diff --git a/rbot_lib/src/net/ws.rs b/rbot_lib/src/net/ws.rs
index 3ebf2884..19c805a4 100644
--- a/rbot_lib/src/net/ws.rs
+++ b/rbot_lib/src/net/ws.rs
@@ -7,7 +7,6 @@ use futures::stream::SplitStream;
 use futures::SinkExt;
 use futures::Stream;
 use futures::StreamExt;
-use serde_derive::{Deserialize, Serialize};
 use strum::Display;
 use tokio::net::TcpStream;
 use tokio::sync::Mutex;
diff --git a/rbot_market/src/market.rs b/rbot_market/src/market.rs
index ebbd41e9..99a88e74 100644
--- a/rbot_market/src/market.rs
+++ b/rbot_market/src/market.rs
@@ -3,11 +3,7 @@
 use crossbeam_channel::Sender;
 use rbot_lib::common::AccountCoins;
 use rbot_lib::common::MarketMessage;
-use rbot_lib::db::db_full_path;
 use rust_decimal_macros::dec;
-use tokio_stream::Stream;
-use tokio_stream::StreamExt;
-use std::iter::Filter;
 use std::sync::{Arc, Mutex, RwLock};
 
 use pyo3_polars::PyDataFrame;
@@ -16,8 +12,6 @@ use rbot_lib::common::OrderBook;
 use rbot_lib::net::RestApi;
 use rust_decimal::Decimal;
 
-use tokio::task::spawn;
-
 use anyhow::anyhow;
 #[allow(unused_imports)]
 use anyhow::Context;
@@ -504,8 +498,8 @@ where
             return Ok((PyDataFrame(bids), PyDataFrame(asks)));
         }
 
-        let bids_edge: f64 = bids.column(KEY::price).unwrap().max().unwrap();
-        let asks_edge: f64 = asks.column(KEY::price).unwrap().min().unwrap();
+        let bids_edge: f64 = bids.column(KEY::price).unwrap().max().unwrap().unwrap();
+        let asks_edge: f64 = asks.column(KEY::price).unwrap().min().unwrap().unwrap();
 
         if asks_edge < bids_edge {
             log::warn!("bids_edge({}) < asks_edge({})", bids_edge, asks_edge);
diff --git a/rbot_server/src/server.rs b/rbot_server/src/server.rs
index 668bc0a4..415fbc04 100644
--- a/rbot_server/src/server.rs
+++ b/rbot_server/src/server.rs
@@ -89,7 +89,8 @@ pub fn start_board_server() -> anyhow::Result<()> {
 pub fn get_rest_orderbook(exchange_name: &str, config: &MarketConfig) -> anyhow::Result<OrderBook> {
     let path = OrderBookList::make_path(exchange_name, config);
 
-    let r = reqwest::blocking::get(&format!("http://localhost:8080/board/vec/{}", path))?.bytes()?;
+    let r = reqwest::blocking::get(&format!("http://localhost:8080/board/vec/{}", path))?
+            .bytes()?;
 
     let board = OrderBook::from_bin(exchange_name, config, r.to_vec())?;
 
diff --git a/rbot_session/Cargo.toml b/rbot_session/Cargo.toml
index d2a1f955..8a5d8bf1 100644
--- a/rbot_session/Cargo.toml
+++ b/rbot_session/Cargo.toml
@@ -16,11 +16,8 @@ anyhow = {workspace = true}
 rust_decimal = {workspace = true}
 rust_decimal_macros = {workspace = true}
 
-polars = {workspace=true, features=["lazy"]}
-polars-core = {workspace=true, features=["ndarray", "temporal", "dtype-time", "object", "round_series"]}
-polars-lazy = {workspace=true, features=["dynamic_group_by", "round_series", "pivot"]}
-polars-time = {workspace=true}
-polars-ops = {workspace=true}
+
+polars = {workspace=true}
 pyo3-polars = {workspace=true}
 
 serde = {workspace = true}
diff --git a/rbot_session/src/logger.rs b/rbot_session/src/logger.rs
index 0303ed99..19e727d7 100644
--- a/rbot_session/src/logger.rs
+++ b/rbot_session/src/logger.rs
@@ -4,7 +4,8 @@ use std::{
     io::{BufRead, BufReader, Write},
 };
 
-use polars_core::{datatypes::TimeUnit, frame::DataFrame, prelude::NamedFrom, series::Series, export::num::ToPrimitive};
+use polars::{datatypes::TimeUnit, export::num::ToPrimitive};
+use polars::prelude::{DataFrame, NamedFrom, Series};
 use pyo3::{pyclass, pymethods, PyResult};
 use pyo3_polars::PyDataFrame;
 use serde_derive::{Deserialize, Serialize};
@@ -47,13 +48,6 @@ pub struct TimeIndicator {
     pub value2: Option<f64>
 }
 
-pub struct TimeIndicatorVec(Vec<TimeIndicator>);
-
-impl Default for TimeIndicatorVec {
-    fn default() -> Self {
-        TimeIndicatorVec(vec![])
-    }
-}
 
 #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
 pub struct Position {
diff --git a/rbot_session/src/runner.rs b/rbot_session/src/runner.rs
index 331622ce..d3e69603 100644
--- a/rbot_session/src/runner.rs
+++ b/rbot_session/src/runner.rs
@@ -347,9 +347,6 @@ impl Runner {
         let agent_name = agent_name.extract::<String>().unwrap();
         log::info!("Agent name: {}", agent_name);
 
-        let time = NOW() / 1_000_000;
-
-        //self.agent_id = format!("{}{}", agent_name, Runner::int_to_base64(time));
         self.agent_id = format!("{}", agent_name);
 
         Ok(())


Test patch:
diff --git a/rbot/test/tutorial/basilbot_backtest.ipynb b/rbot/test/tutorial/basilbot_backtest.ipynb
index e6e20115..63fe44d8 100644
--- a/rbot/test/tutorial/basilbot_backtest.ipynb
+++ b/rbot/test/tutorial/basilbot_backtest.ipynb
@@ -194,8 +194,9 @@
    "outputs": [],
    "source": [
     "log = session.log\n",
-    "orders = log.orders\n",
-    "orders.head(100)"
+    "orders = log.order\n",
+    "\n",
+    "orders"
    ]
   },
   {