From 2fa4696e8110753689bec9a1448a51f6e21efa27 Mon Sep 17 00:00:00 2001 From: Sik Yoon Date: Sat, 1 Jun 2024 02:56:21 +0900 Subject: [PATCH] Update filtering --- src/strategy_team/future_strategy_long.rs | 40 ++++++++++------------ src/strategy_team/future_strategy_short.rs | 18 ++++++++-- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/strategy_team/future_strategy_long.rs b/src/strategy_team/future_strategy_long.rs index f5d7404..76d3e08 100644 --- a/src/strategy_team/future_strategy_long.rs +++ b/src/strategy_team/future_strategy_long.rs @@ -125,11 +125,13 @@ pub async fn list_up_for_buy( .min_by(|x, y| x.opclo_price.partial_cmp(&y.opclo_price).unwrap()) .unwrap().opclo_price; if values.current_price.to_f64().is_some_and(|a| a > min_price) { - let stoploss_percent = ((min_price - values.current_price.to_f64().unwrap()) * 100.0) / (values.current_price.to_f64().unwrap() * 2.0); + let mut stoploss_percent = ((min_price - values.current_price.to_f64().unwrap()) * 100.0) / values.current_price.to_f64().unwrap(); + stoploss_percent = (stoploss_percent * 100.0).floor() / 100.0; values.stoploss = rust_decimal::prelude::FromPrimitive::from_f64(stoploss_percent).unwrap(); - let target_percent = stoploss_percent.abs() * 1.25; + let mut target_percent = stoploss_percent.abs() * 1.5; + target_percent = (target_percent * 100.0).floor() / 100.0; values.target_price = rust_decimal::prelude::FromPrimitive::from_f64(target_percent).unwrap(); - if stoploss_percent < - 0.07 { + if stoploss_percent < - 0.07 && stoploss_percent > -1.5 { do_buy = true; } } @@ -331,33 +333,27 @@ pub async fn list_up_for_sell(all_data: &AllData, futures_exchange_info_map: &Ha .timeout(tokio::time::Duration::from_millis(5000)) .build() .unwrap(); + + let mut supertrend_vec: Vec = Vec::new(); let server_epoch = get_server_epoch().await; + let mut filtered_symbols: HashMap = HashMap::new(); + for element in &filled_positions { + filtered_symbols.insert(element.symbol.clone(), FilteredDataValue::new()); + } + let supertrend_1m_map = + supertrend(30, 3.0, true, &all_data.rt_price_1m_vec, &filtered_symbols).await?; + for element in filled_positions { let mut is_sell = false; - - - // TODO: BNB 코인이 있으면 - // let base_qty_to_be_ordered = - // element.base_qty_ordered.round_dp_with_strategy( - // lot_step_size.normalize().scale(), - // RoundingStrategy::ToZero, - // ); - // TODO: BNB 코인이 없으면 - + if !element.current_price.is_zero() { - // if element.pure_profit_percent >= target_profit_percent * 2.5 { - // is_sell = true; - // } else if element.pure_profit_percent <= target_profit_percent * -2.0 { - // is_sell = true; - // } - // if over_turned == true && server_epoch - element.close_time > 1_800_000 { - // is_sell = true; - // } else if element.pure_profit_percent >= element.target_percent { is_sell = true; } else if element.pure_profit_percent <= element.stoploss_percent { is_sell = true; - } else if server_epoch - element.close_time > 1_800_000 { + } else if server_epoch - element.close_time > 7_200_000 { + is_sell = true; + } else if supertrend_1m_map.get(&element.symbol).is_some_and(|a| a.last().is_some_and(|b| b.close_time > server_epoch && b.area == SuperTrendArea::DOWN)) { is_sell = true; } diff --git a/src/strategy_team/future_strategy_short.rs b/src/strategy_team/future_strategy_short.rs index 1ac51ea..0828e01 100644 --- a/src/strategy_team/future_strategy_short.rs +++ b/src/strategy_team/future_strategy_short.rs @@ -125,11 +125,13 @@ pub async fn list_up_for_buy( .max_by(|x, y| x.opclo_price.partial_cmp(&y.opclo_price).unwrap()) .unwrap().opclo_price; if values.current_price.to_f64().is_some_and(|a| a < max_price) { - let stoploss_percent = ((values.current_price.to_f64().unwrap() - max_price) * 100.0) / (values.current_price.to_f64().unwrap() * 2.0); + let mut stoploss_percent = ((values.current_price.to_f64().unwrap() - max_price) * 100.0) / values.current_price.to_f64().unwrap(); + stoploss_percent = (stoploss_percent * 100.0).floor() / 100.0; values.stoploss = rust_decimal::prelude::FromPrimitive::from_f64(stoploss_percent).unwrap(); - let target_percent = stoploss_percent.abs() * 1.25; + let mut target_percent = stoploss_percent.abs() * 1.5; + target_percent = (target_percent * 100.0).floor() / 100.0; values.target_price = rust_decimal::prelude::FromPrimitive::from_f64(target_percent).unwrap(); - if stoploss_percent < - 0.07 { + if stoploss_percent < - 0.07 && stoploss_percent > -1.5 { do_buy = true; } } @@ -331,7 +333,15 @@ pub async fn list_up_for_sell(all_data: &AllData, futures_exchange_info_map: &Ha .timeout(tokio::time::Duration::from_millis(5000)) .build() .unwrap(); + + let mut supertrend_vec: Vec = Vec::new(); let server_epoch = get_server_epoch().await; + let mut filtered_symbols: HashMap = HashMap::new(); + for element in &filled_positions { + filtered_symbols.insert(element.symbol.clone(), FilteredDataValue::new()); + } + let supertrend_1m_map = + supertrend(30, 3.0, true, &all_data.rt_price_1m_vec, &filtered_symbols).await?; for element in filled_positions { let mut is_sell = false; @@ -350,6 +360,8 @@ pub async fn list_up_for_sell(all_data: &AllData, futures_exchange_info_map: &Ha is_sell = true; } else if server_epoch - element.close_time > 1_800_000 { is_sell = true; + } else if supertrend_1m_map.get(&element.symbol).is_some_and(|a| a.last().is_some_and(|b| b.close_time > server_epoch && b.area == SuperTrendArea::UP)) { + is_sell = true; } if is_sell == true {