Save and improved sell all

This commit is contained in:
2026-08-07 05:47:52 +02:00
parent 02cd4c6347
commit e6db04f612
5 changed files with 161 additions and 34 deletions
+37 -9
View File
@@ -5,22 +5,50 @@ pub mod strategy;
pub mod tradelog;
pub mod types;
use tokio::io::{self, AsyncBufReadExt, BufReader};
use tokio::sync::watch;
use std::sync::Arc;
use chrono::Local;
use tokio::{
io::{self, AsyncBufReadExt, BufReader},
sync::watch,
};
use crate::bot::Bot;
async fn shutdown_listener(tx: watch::Sender<bool>) {
async fn shutdown_listener(bot: Arc<Bot>, tx: watch::Sender<bool>) -> anyhow::Result<()> {
let stdin = BufReader::new(io::stdin());
let mut lines = stdin.lines();
while let Ok(Some(line)) = lines.next_line().await {
if line.trim().eq_ignore_ascii_case("exit") {
println!("Shutdown requested.");
let _ = tx.send(true);
break;
match line.to_lowercase().trim() {
"exit" | "shutdown" => {
let _ = tx.send(true);
}
"save" => {
log::info!("Saving trades");
let now = Local::now();
let formatted_time = now.format("%m-%d-%H-%M").to_string();
let filename = format!("sol-hun-{}.csv", formatted_time);
let mut writer = csv::Writer::from_path(&filename)?;
for trade in bot.trade_log.lock().await.iter() {
writer.serialize(trade)?;
}
writer.flush()?;
log::info!("Saved trades to {}", filename);
}
_ => {}
}
}
Ok(())
}
#[tokio::main]
@@ -31,9 +59,9 @@ async fn main() -> anyhow::Result<()> {
let (shutdown_tx, shutdown_rx) = watch::channel(false);
tokio::spawn(shutdown_listener(shutdown_tx));
let bot = Bot::new().await?;
tokio::spawn(shutdown_listener(bot.clone(), shutdown_tx));
bot.start(shutdown_rx).await
}