diff --git a/src/future/table_mgmt.rs b/src/future/table_mgmt.rs index 80860c5..a6e2ee0 100644 --- a/src/future/table_mgmt.rs +++ b/src/future/table_mgmt.rs @@ -339,3 +339,92 @@ pub async fn select_listuped_positions() -> Result, Box Result, Box> { + let select_table_name = String::from("future_ordered_coin_list"); + let select_columns = String::from("*"); + let mut select_condition_build = String::from("WHERE order_type = 'CLOSING' AND status = 'FILLED'"); + + let select_condition = Some(select_condition_build); + + let data_struct = PositionCoinList::new(); + + let select_result = try_select_record( + &select_table_name, + &select_columns, + &select_condition, + &data_struct, + ) + .await; + + if select_result.is_ok() { + Ok(select_result.unwrap()) + } else { + eprint!("select_listup_positions() error!"); + Err("error")? + } +} + +pub async fn move_closed_positions() { + let closed_positions = select_closed_positions().await.unwrap(); + + if closed_positions.len() != 0 { + let delete_table_name = String::from("future_ordered_coin_list"); + let insert_table_name = String::from("future_closed_coin_list"); + let insert_columns = vec![ + "order_type", + "status", + "symbol", + "order_id", + "position", + "registered_server_epoch", + "transact_time", + "close_time", + "used_usdt", + "expected_get_usdt", + "pnl", + "entry_price", + "current_price", + "target_percent", + "stoploss_percent", + "base_qty_ordered", + "pure_profit_percent", + "minimum_profit_percent", + "maximum_profit_percent", + ]; + + for element in closed_positions { + let mut insert_values = vec![ + element.order_type.to_string(), // order_type + element.status, // status + element.symbol, // symbol + element.order_id.to_string(), // order_id + element.position.to_string(), // position + element.registered_server_epoch.to_string(),// registered_server_epoch + element.transact_time.to_string(), // transact_time + element.close_time.to_string(), // close_time + element.used_usdt.to_string(), // used_usdt + element.expected_get_usdt.to_string(), // expected_get_usdt + element.pnl.to_string(), // pnl + element.entry_price.to_string(), // entry_price + element.current_price.to_string(), // current_price + element.target_percent.to_string(), // target_percent + element.stoploss_percent.to_string(), // stoploss_percent + element.base_qty_ordered.to_string(), // base_qty_ordered + element.pure_profit_percent.to_string(), // pure_profit_percent + element.minimum_profit_percent.to_string(), // minimum_profit_percent + element.maximum_profit_percent.to_string(), // maximum_profit_percent + ]; + + insert_one_record(&insert_table_name, &insert_columns, &insert_values).await; + + let mut condition_build = String::from("WHERE id = "); + condition_build.push_str(element.id.to_string().as_str()); + // condition_build.push_str(" AND symbol = \'"); + // condition_build.push_str(element.symbol.as_str()); + // condition_build.push('\''); + delete_record(&delete_table_name, &condition_build).await; + } + + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index d86c956..d4b7a95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1569,6 +1569,32 @@ async fn main() -> Result<(), Box> { } }); + // Task#XX: move closed positions + tokio::task::spawn(async move { + sleep(Duration::from_secs(20)).await; + let mut elapsed_time = 0; + loop { + let instant = Instant::now(); + + let result = future::table_mgmt::move_closed_positions().await; + + // send Task#0 a message to notify running on + // match result { + // Ok(T) => { + // } + // Err(E) => {} + // } + + // sleep as much as the loop recurs per 1 second if all operation finished within 1 second. + elapsed_time = instant.elapsed().as_millis(); + + if 100 > elapsed_time { + sleep(Duration::from_millis((100 - elapsed_time) as u64)).await; + } + } + }); + + loop {}