diff --git a/src/future/mod.rs b/src/future/mod.rs index f1dcff2..17ff3af 100644 --- a/src/future/mod.rs +++ b/src/future/mod.rs @@ -131,6 +131,8 @@ pub struct PositionCoinList { pub pnl: f64, pub entry_price: Decimal, pub current_price: Decimal, + pub target_percent: f64, + pub stoploss_percent: f64, pub base_qty_ordered: Decimal, pub pure_profit_percent: f64, pub minimum_profit_percent: f64, @@ -154,6 +156,8 @@ impl PositionCoinList { pnl: 0.0, entry_price: Decimal::new(0, 8), current_price: Decimal::new(0, 8), + target_percent: 0.0, + stoploss_percent: 0.0, base_qty_ordered: Decimal::new(0, 8), pure_profit_percent: 0.0, minimum_profit_percent: 0.0, diff --git a/src/initialization.rs b/src/initialization.rs index 062e612..7bfb242 100644 --- a/src/initialization.rs +++ b/src/initialization.rs @@ -1316,6 +1316,55 @@ async fn initialize_database() { ("pnl", "double", None), ("entry_price", "decimal(16,8)", None), ("current_price", "decimal(16,8)", None), + ("target_percent", "double", None), + ("stoploss_percent", "double", None), + ("base_qty_ordered", "decimal(16,8)", None), + ("pure_profit_percent", "double", None), + ("minimum_profit_percent", "double", None), + ("maximum_profit_percent", "double", None), + ]; + let table_condition = None; + + if exists_result == false { + let mut result = new_table(&table_name, &initial_table, &table_condition).await; + if result.is_err() { + loop { + result = new_table(&table_name, &initial_table, &table_condition).await; + if result.is_ok() { + break; + } + sleep(Duration::from_millis(10)).await; + } + } + } + + println!("Ok"); + } + + { + // future_ordered_coin_list + print!("table 'future_closed_coin_list'..."); + io::stdout().flush(); + + let table_name = String::from("future_closed_coin_list"); + let exists_result = exists_table(&table_name).await; + let initial_table = vec![ + ("id", "integer", Some("PK, AI, UN")), + ("order_type", "char(20)", None), // POSITIONING, CLOSING + ("status", "char(20)", None), // LISTUP, FILLED, PARTIALLY_FILLED + ("symbol", "char(20)", None), + ("order_id", "bigint", Some("UN")), + ("position", "char(20)", None), + ("registered_server_epoch", "bigint", None), + ("transact_time", "bigint", None), + ("close_time", "bigint", None), + ("used_usdt", "decimal(16,8)", None), + ("expected_get_usdt", "double", None), + ("pnl", "double", None), + ("entry_price", "decimal(16,8)", None), + ("current_price", "decimal(16,8)", None), + ("target_percent", "double", None), + ("stoploss_percent", "double", None), ("base_qty_ordered", "decimal(16,8)", None), ("pure_profit_percent", "double", None), ("minimum_profit_percent", "double", None), diff --git a/src/strategy_team/future_strategy_long.rs b/src/strategy_team/future_strategy_long.rs index 899ff05..1a05f3a 100644 --- a/src/strategy_team/future_strategy_long.rs +++ b/src/strategy_team/future_strategy_long.rs @@ -33,34 +33,27 @@ pub async fn list_up_for_buy( // sma3_close(current) > sma3_open (current), sma3_close(prev) < sma3_open (prev) let mut keys_to_remove: HashSet = HashSet::new(); - let ema3 = ema(30, &alldata.rt_price_1m_vec, &filtered_data).await?; - let sma3_open = sma_open(3, &alldata.rt_price_30m_vec, &filtered_data).await?; - let tema3: HashMap> = tema(3, &alldata.rt_price_30m_vec, &filtered_data).await?; + let ema30 = ema(30, &alldata.rt_price_1m_vec, &filtered_data).await?; + let ema200 = ema(200, &alldata.rt_price_1m_vec, &filtered_data).await?; let server_epoch = get_server_epoch().await; for (symbol, values) in &mut filtered_data { let mut do_buy = false; - let price_and_closetime = get_current_price_decimal(&symbol, &alldata.rt_price_30m_vec).await; - if let (Some(sma3_open_vec), Some(tema3_vec), Some(ema3_vec), Some(current_info)) = (sma3_open.get(symbol), tema3.get(symbol), ema3.get(symbol), price_and_closetime) { - if sma3_open_vec.len() > 10 - && tema3_vec.len() > 10 - && ema3_vec.len() > 10 - && ema3_vec.last().unwrap().close_time > server_epoch - && sma3_open_vec.last().unwrap().close_time == tema3_vec.last().unwrap().close_time - && sma3_open_vec.last().unwrap().close_time > server_epoch - && tema3_vec.last().unwrap().close_time > server_epoch + let price_and_closetime = get_current_price_decimal(&symbol, &alldata.rt_price_1m_vec).await; + if let (Some(ema30_vec), Some(ema200_vec), Some(current_info)) = (ema30.get(symbol), ema200.get(symbol), price_and_closetime) { + if ema30_vec.len() > 10 + && ema200_vec.len() > 10 + && ema30_vec.last().unwrap().close_time > server_epoch + && ema30_vec.last().unwrap().close_time == ema200_vec.last().unwrap().close_time { - if sma3_open_vec[sma3_open_vec.len()-1].sma_value < tema3_vec[tema3_vec.len()-1].tema_value - && sma3_open_vec[sma3_open_vec.len()-2].sma_value > tema3_vec[tema3_vec.len()-2].tema_value - && sma3_open_vec[sma3_open_vec.len()-1].sma_value < sma3_open_vec[sma3_open_vec.len()-2].sma_value - && sma3_open_vec[sma3_open_vec.len()-2].sma_value < sma3_open_vec[sma3_open_vec.len()-3].sma_value - && sma3_open_vec[sma3_open_vec.len()-3].sma_value < sma3_open_vec[sma3_open_vec.len()-4].sma_value - && tema3_vec[tema3_vec.len()-1].tema_value > tema3_vec[tema3_vec.len()-2].tema_value - && tema3_vec[tema3_vec.len()-2].tema_value < tema3_vec[tema3_vec.len()-3].tema_value - && tema3_vec[tema3_vec.len()-3].tema_value < tema3_vec[tema3_vec.len()-4].tema_value - && current_info.0.to_f64().is_some_and(|a| ema3_vec.last().unwrap().ema_value < a) + if ema30_vec.last().unwrap().ema_value > ema200_vec.last().unwrap().ema_value + && ema30_vec[ema30_vec.len()-1].ema_value - ema200_vec[ema30_vec.len()-1].ema_value > ema30_vec[ema30_vec.len()-2].ema_value - ema200_vec[ema30_vec.len()-2].ema_value + && ema30_vec[ema30_vec.len()-2].ema_value - ema200_vec[ema30_vec.len()-2].ema_value > ema30_vec[ema30_vec.len()-3].ema_value - ema200_vec[ema30_vec.len()-3].ema_value + && ema30_vec[ema30_vec.len()-3].ema_value - ema200_vec[ema30_vec.len()-3].ema_value > ema30_vec[ema30_vec.len()-4].ema_value - ema200_vec[ema30_vec.len()-4].ema_value + && ema30_vec[ema30_vec.len()-4].ema_value - ema200_vec[ema30_vec.len()-4].ema_value > ema30_vec[ema30_vec.len()-5].ema_value - ema200_vec[ema30_vec.len()-5].ema_value + && ema30_vec[ema30_vec.len()-5].ema_value - ema200_vec[ema30_vec.len()-5].ema_value > ema30_vec[ema30_vec.len()-6].ema_value - ema200_vec[ema30_vec.len()-6].ema_value { - values.closetime = current_info.1; values.current_price = current_info.0; + values.closetime = current_info.1; do_buy = true; } } @@ -71,6 +64,84 @@ pub async fn list_up_for_buy( } remove_keys(&mut filtered_data, keys_to_remove).await; + // current ADX(15, 15) > 20, current ADX > prev ADX + let mut keys_to_remove: HashSet = HashSet::new(); + let adx_vec = adx(15, 15, &alldata.rt_price_1m_vec, &filtered_data).await?; + for (symbol, values) in &mut filtered_data { + if let Some(adx_vec) = adx_vec.get(symbol) { + if let Some(last_idx) = adx_vec.iter().position(|elem| elem.close_time == values.closetime) { + if adx_vec.len() > 10 + && adx_vec[last_idx].adx > 20.0 + && adx_vec[last_idx].adx < 30.0 + && adx_vec[last_idx].adx > adx_vec[last_idx-1].adx + && adx_vec[last_idx].adx > adx_vec[last_idx-2].adx + && adx_vec[last_idx].adx > adx_vec[last_idx-3].adx + && adx_vec[last_idx-1].adx > adx_vec[last_idx-2].adx + && adx_vec[last_idx-1].adx > adx_vec[last_idx-3].adx + && adx_vec[last_idx-2].adx > adx_vec[last_idx-3].adx { + } else { + keys_to_remove.insert(symbol.clone()); + } + } else { + keys_to_remove.insert(symbol.clone()); + } + } else { + keys_to_remove.insert(symbol.clone()); + } + } + remove_keys(&mut filtered_data, keys_to_remove).await; + + // supertrend(ATR period 30, multiplier: 3.0, 1m close price) + let mut keys_to_remove: HashSet = HashSet::new(); + let supertrend_1m_map = + supertrend(30, 3.0, true, &alldata.rt_price_1m_vec, &filtered_data).await?; + for (symbol, values) in &mut filtered_data { + let mut do_buy = false; + if let Some(supertrend_vec) = supertrend_1m_map.get(symbol) + { + if supertrend_vec.last().unwrap().close_time == values.closetime + && supertrend_vec.last().unwrap().area == SuperTrendArea::UP { + do_buy = true; + } + } + + if do_buy == false { + keys_to_remove.insert(symbol.clone()); + } + } + remove_keys(&mut filtered_data, keys_to_remove).await; + + // set target_price and stop_loss + let mut keys_to_remove: HashSet = HashSet::new(); + for (symbol, values) in &mut filtered_data { + let mut do_buy = false; + if let Some(realtime_price_vec) = alldata.rt_price_1m_vec.get(symbol) + { + let element_number = 30; + if let Some(truncated_vec) = realtime_price_vec.get(realtime_price_vec.len()-element_number..) { + + let min_price = truncated_vec + .iter() + .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()) / values.current_price.to_f64().unwrap(); + values.stoploss = rust_decimal::prelude::FromPrimitive::from_f64(stoploss_percent).unwrap(); + let target_percent = stoploss_percent.abs() * 2.0; + values.target_price = rust_decimal::prelude::FromPrimitive::from_f64(target_percent).unwrap(); + if stoploss_percent < - 0.07 { + do_buy = true; + } + } + } + } + + if do_buy == false { + keys_to_remove.insert(symbol.clone()); + } + } + remove_keys(&mut filtered_data, keys_to_remove).await; + // current Tema(15) > current Tema(30) // let mut keys_to_remove: HashSet = HashSet::new(); // let tema_10 = tema(10, &alldata.rt_price_30m_vec, &filtered_data).await?; @@ -247,7 +318,7 @@ pub async fn list_up_for_buy( // } // remove_keys(&mut filtered_data, keys_to_remove).await; - let final_filtered_data = future_duplicate_filter(&filtered_data, &future_exchange_info_map).await?; + let final_filtered_data = future_duplicate_filter(Position::Long, &filtered_data, &future_exchange_info_map).await?; insert_future_coins(Position::Long, server_epoch, &final_filtered_data).await?; Ok(()) @@ -260,65 +331,9 @@ 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 filtered_symbols: HashMap = HashMap::new(); - for element in &filled_positions { - filtered_symbols.insert(element.symbol.clone(), FilteredDataValue::new()); - } - let sma3_open = sma_open(3, &all_data.rt_price_30m_vec, &filtered_symbols).await?; - let tema3 = tema(3, &all_data.rt_price_30m_vec, &filtered_symbols).await?; - let server_epoch = get_server_epoch().await; for element in filled_positions { - let mut is_sell = false; - let mut over_turned = false; - if let (Some(sma3_open_vec), Some(tema3_vec)) = (sma3_open.get(&element.symbol), tema3.get(&element.symbol)) { - if tema3_vec.len() > 10 && sma3_open_vec.len() > 10 { - if sma3_open_vec.last().unwrap().close_time == tema3_vec.last().unwrap().close_time - && sma3_open_vec.last().unwrap().close_time > server_epoch - && sma3_open_vec.last().unwrap().sma_value > tema3_vec.last().unwrap().tema_value { - over_turned = true; - - } - } - } - - let opclo_sample_length: usize = 60; // 15 candle samsples - let mut target_profit_percent = 0.0; - if let Some(price_30m_vec) = all_data.rt_price_30m_vec.get(&element.symbol) { - let vec_len = price_30m_vec.len(); - if let Some(candles) = - price_30m_vec.get(vec_len - opclo_sample_length - 2..vec_len - 1) - { - let windows = candles.windows(2); - let mut sum_amplitude_candles = 0.0; - let mut sum_ratio_amp_body = 0.0; - let mut average_amplitude = 0.0; - - for window in windows { - sum_amplitude_candles += ((window.last().unwrap().high_price - - window.last().unwrap().low_price) - * 100.0) - / window.first().unwrap().close_price; - } - let average_amplitude = sum_amplitude_candles / opclo_sample_length as f64; // percent unit - let mut amplitude_variance = 0.0; - - let windows = candles.windows(2); - for window in windows { - amplitude_variance += ((((window.last().unwrap().high_price - - window.last().unwrap().low_price) - * 100.0) - / window.first().unwrap().close_price) - - average_amplitude) - .powi(2); - } - amplitude_variance = amplitude_variance / (opclo_sample_length - 1) as f64; - let standard_deviation_amplitude = amplitude_variance.sqrt(); - target_profit_percent = - average_amplitude - (standard_deviation_amplitude * 1.5); - } - } + let mut is_sell = false; // TODO: BNB 코인이 있으면 @@ -338,11 +353,9 @@ pub async fn list_up_for_sell(all_data: &AllData, futures_exchange_info_map: &Ha // if over_turned == true && server_epoch - element.close_time > 1_800_000 { // is_sell = true; // } else - if target_profit_percent.is_normal() && element.pure_profit_percent > target_profit_percent + 0.7 { + if element.pure_profit_percent >= element.target_percent { is_sell = true; - } else if target_profit_percent.is_normal() && element.pure_profit_percent < (target_profit_percent * -1.0) - 0.7 { - is_sell = true; - } else if server_epoch - element.registered_server_epoch > 1_800_000 * 5 && element.pure_profit_percent >= 0.35{ + } else if element.pure_profit_percent <= element.stoploss_percent { is_sell = true; } diff --git a/src/strategy_team/future_strategy_short.rs b/src/strategy_team/future_strategy_short.rs index b3a1cb2..67b3455 100644 --- a/src/strategy_team/future_strategy_short.rs +++ b/src/strategy_team/future_strategy_short.rs @@ -31,36 +31,29 @@ pub async fn list_up_for_buy( filtered_data.insert(symbol.clone(), FilteredDataValue::new()); } - // sma3_close(current) < sma3_open (current), sma3_close(prev) > sma3_open (prev) + // sma3_close(current) > sma3_open (current), sma3_close(prev) < sma3_open (prev) let mut keys_to_remove: HashSet = HashSet::new(); - let ema3 = ema(30, &alldata.rt_price_1m_vec, &filtered_data).await?; - let sma3_open = sma_open(3, &alldata.rt_price_30m_vec, &filtered_data).await?; - let tema3 = tema(3, &alldata.rt_price_30m_vec, &filtered_data).await?; + let ema30 = ema(30, &alldata.rt_price_1m_vec, &filtered_data).await?; + let ema200 = ema(200, &alldata.rt_price_1m_vec, &filtered_data).await?; let server_epoch = get_server_epoch().await; for (symbol, values) in &mut filtered_data { let mut do_buy = false; - let price_and_closetime = get_current_price_decimal(&symbol, &alldata.rt_price_30m_vec).await; - if let (Some(sma3_open_vec), Some(tema3_vec), Some(ema3_vec), Some(current_info)) = (sma3_open.get(symbol), tema3.get(symbol), ema3.get(symbol), price_and_closetime) { - if sma3_open_vec.len() > 10 - && tema3_vec.len() > 10 - && ema3_vec.len() > 10 - && ema3_vec.last().unwrap().close_time > server_epoch - && sma3_open_vec.last().unwrap().close_time == tema3_vec.last().unwrap().close_time - && sma3_open_vec.last().unwrap().close_time > server_epoch - && tema3_vec.last().unwrap().close_time > server_epoch + let price_and_closetime = get_current_price_decimal(&symbol, &alldata.rt_price_1m_vec).await; + if let (Some(ema30_vec), Some(ema200_vec), Some(current_info)) = (ema30.get(symbol), ema200.get(symbol), price_and_closetime) { + if ema30_vec.len() > 10 + && ema200_vec.len() > 10 + && ema30_vec.last().unwrap().close_time > server_epoch + && ema30_vec.last().unwrap().close_time == ema200_vec.last().unwrap().close_time { - if sma3_open_vec[sma3_open_vec.len()-1].sma_value > tema3_vec[tema3_vec.len()-1].tema_value - && sma3_open_vec[sma3_open_vec.len()-2].sma_value < tema3_vec[tema3_vec.len()-2].tema_value - && sma3_open_vec[sma3_open_vec.len()-1].sma_value > sma3_open_vec[sma3_open_vec.len()-2].sma_value - && sma3_open_vec[sma3_open_vec.len()-2].sma_value > sma3_open_vec[sma3_open_vec.len()-3].sma_value - && sma3_open_vec[sma3_open_vec.len()-3].sma_value > sma3_open_vec[sma3_open_vec.len()-4].sma_value - && tema3_vec[tema3_vec.len()-1].tema_value < tema3_vec[tema3_vec.len()-2].tema_value - && tema3_vec[tema3_vec.len()-2].tema_value > tema3_vec[tema3_vec.len()-3].tema_value - && tema3_vec[tema3_vec.len()-3].tema_value > tema3_vec[tema3_vec.len()-4].tema_value - && current_info.0.to_f64().is_some_and(|a| ema3_vec.last().unwrap().ema_value > a) + if ema30_vec.last().unwrap().ema_value < ema200_vec.last().unwrap().ema_value + && ema30_vec[ema30_vec.len()-1].ema_value - ema200_vec[ema30_vec.len()-1].ema_value < ema30_vec[ema30_vec.len()-2].ema_value - ema200_vec[ema30_vec.len()-2].ema_value + && ema30_vec[ema30_vec.len()-2].ema_value - ema200_vec[ema30_vec.len()-2].ema_value < ema30_vec[ema30_vec.len()-3].ema_value - ema200_vec[ema30_vec.len()-3].ema_value + && ema30_vec[ema30_vec.len()-3].ema_value - ema200_vec[ema30_vec.len()-3].ema_value < ema30_vec[ema30_vec.len()-4].ema_value - ema200_vec[ema30_vec.len()-4].ema_value + && ema30_vec[ema30_vec.len()-4].ema_value - ema200_vec[ema30_vec.len()-4].ema_value < ema30_vec[ema30_vec.len()-5].ema_value - ema200_vec[ema30_vec.len()-5].ema_value + && ema30_vec[ema30_vec.len()-5].ema_value - ema200_vec[ema30_vec.len()-5].ema_value < ema30_vec[ema30_vec.len()-6].ema_value - ema200_vec[ema30_vec.len()-6].ema_value { - values.closetime = current_info.1; values.current_price = current_info.0; + values.closetime = current_info.1; do_buy = true; } } @@ -71,6 +64,84 @@ pub async fn list_up_for_buy( } remove_keys(&mut filtered_data, keys_to_remove).await; + // current ADX(15, 15) > 20, current ADX > prev ADX + let mut keys_to_remove: HashSet = HashSet::new(); + let adx_vec = adx(15, 15, &alldata.rt_price_1m_vec, &filtered_data).await?; + for (symbol, values) in &mut filtered_data { + if let Some(adx_vec) = adx_vec.get(symbol) { + if let Some(last_idx) = adx_vec.iter().position(|elem| elem.close_time == values.closetime) { + if adx_vec.len() > 10 + && adx_vec[last_idx].adx > 20.0 + && adx_vec[last_idx].adx < 30.0 + && adx_vec[last_idx].adx > adx_vec[last_idx-1].adx + && adx_vec[last_idx].adx > adx_vec[last_idx-2].adx + && adx_vec[last_idx].adx > adx_vec[last_idx-3].adx + && adx_vec[last_idx-1].adx > adx_vec[last_idx-2].adx + && adx_vec[last_idx-1].adx > adx_vec[last_idx-3].adx + && adx_vec[last_idx-2].adx > adx_vec[last_idx-3].adx { + } else { + keys_to_remove.insert(symbol.clone()); + } + } else { + keys_to_remove.insert(symbol.clone()); + } + } else { + keys_to_remove.insert(symbol.clone()); + } + } + remove_keys(&mut filtered_data, keys_to_remove).await; + + // supertrend(ATR period 30, multiplier: 3.0, 1m close price) + let mut keys_to_remove: HashSet = HashSet::new(); + let supertrend_1m_map = + supertrend(30, 3.0, true, &alldata.rt_price_1m_vec, &filtered_data).await?; + for (symbol, values) in &mut filtered_data { + let mut do_buy = false; + if let Some(supertrend_vec) = supertrend_1m_map.get(symbol) + { + if supertrend_vec.last().unwrap().close_time == values.closetime + && supertrend_vec.last().unwrap().area == SuperTrendArea::DOWN { + do_buy = true; + } + } + + if do_buy == false { + keys_to_remove.insert(symbol.clone()); + } + } + remove_keys(&mut filtered_data, keys_to_remove).await; + + // set target_price and stop_loss + let mut keys_to_remove: HashSet = HashSet::new(); + for (symbol, values) in &mut filtered_data { + let mut do_buy = false; + if let Some(realtime_price_vec) = alldata.rt_price_1m_vec.get(symbol) + { + let element_number = 30; + if let Some(truncated_vec) = realtime_price_vec.get(realtime_price_vec.len()-element_number..) { + + let max_price = truncated_vec + .iter() + .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) / values.current_price.to_f64().unwrap(); + values.stoploss = rust_decimal::prelude::FromPrimitive::from_f64(stoploss_percent).unwrap(); + let target_percent = stoploss_percent.abs() * 2.0; + values.target_price = rust_decimal::prelude::FromPrimitive::from_f64(target_percent).unwrap(); + if stoploss_percent < - 0.07 { + do_buy = true; + } + } + } + } + + if do_buy == false { + keys_to_remove.insert(symbol.clone()); + } + } + remove_keys(&mut filtered_data, keys_to_remove).await; + // current Tema(15) < current Tema(30) // let mut keys_to_remove: HashSet = HashSet::new(); // let tema_10 = tema(10, &alldata.rt_price_30m_vec, &filtered_data).await?; @@ -247,7 +318,7 @@ pub async fn list_up_for_buy( // } // remove_keys(&mut filtered_data, keys_to_remove).await; - let final_filtered_data = future_duplicate_filter(&filtered_data, &future_exchange_info_map).await?; + let final_filtered_data = future_duplicate_filter(Position::Short, &filtered_data, &future_exchange_info_map).await?; insert_future_coins(Position::Short, server_epoch, &final_filtered_data).await?; Ok(()) @@ -260,65 +331,10 @@ 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 filtered_symbols: HashMap = HashMap::new(); - for element in &filled_positions { - filtered_symbols.insert(element.symbol.clone(), FilteredDataValue::new()); - } - let sma3_open = sma_open(3, &all_data.rt_price_30m_vec, &filtered_symbols).await?; - let tema3 = tema(3, &all_data.rt_price_30m_vec, &filtered_symbols).await?; - let server_epoch = get_server_epoch().await; for element in filled_positions { let mut is_sell = false; - let mut over_turned = false; - if let (Some(sma3_open_vec), Some(tema3_vec)) = (sma3_open.get(&element.symbol), tema3.get(&element.symbol)) { - if tema3_vec.len() > 10 && sma3_open_vec.len() > 10 { - if sma3_open_vec.last().unwrap().close_time == tema3_vec.last().unwrap().close_time - && sma3_open_vec.last().unwrap().close_time > server_epoch - && sma3_open_vec.last().unwrap().sma_value < tema3_vec.last().unwrap().tema_value { - over_turned = true; - } - } - } - - let opclo_sample_length: usize = 60; // 15 candle samsples - let mut target_profit_percent = 0.0; - if let Some(price_30m_vec) = all_data.rt_price_30m_vec.get(&element.symbol) { - let vec_len = price_30m_vec.len(); - if let Some(candles) = - price_30m_vec.get(vec_len - opclo_sample_length - 2..vec_len - 1) - { - let windows = candles.windows(2); - let mut sum_amplitude_candles = 0.0; - let mut sum_ratio_amp_body = 0.0; - let mut average_amplitude = 0.0; - - for window in windows { - sum_amplitude_candles += ((window.last().unwrap().high_price - - window.last().unwrap().low_price) - * 100.0) - / window.first().unwrap().close_price; - } - let average_amplitude = sum_amplitude_candles / opclo_sample_length as f64; // percent unit - let mut amplitude_variance = 0.0; - - let windows = candles.windows(2); - for window in windows { - amplitude_variance += ((((window.last().unwrap().high_price - - window.last().unwrap().low_price) - * 100.0) - / window.first().unwrap().close_price) - - average_amplitude) - .powi(2); - } - amplitude_variance = amplitude_variance / (opclo_sample_length - 1) as f64; - let standard_deviation_amplitude = amplitude_variance.sqrt(); - target_profit_percent = - average_amplitude - (standard_deviation_amplitude * 1.5); - } - } - + // TODO: BNB 코인이 있으면 // let base_qty_to_be_ordered = // element.base_qty_ordered.round_dp_with_strategy( @@ -328,19 +344,9 @@ pub async fn list_up_for_sell(all_data: &AllData, futures_exchange_info_map: &Ha // 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 target_profit_percent.is_normal() && element.pure_profit_percent > target_profit_percent + 0.7 { + if element.pure_profit_percent >= element.target_percent { is_sell = true; - } else if target_profit_percent.is_normal() && element.pure_profit_percent < (target_profit_percent * -1.0) - 0.7 { - is_sell = true; - } else if server_epoch - element.registered_server_epoch > 1_800_000 * 5 && element.pure_profit_percent >= 0.35{ + } else if element.pure_profit_percent <= element.stoploss_percent { is_sell = true; } diff --git a/src/strategy_team/mod.rs b/src/strategy_team/mod.rs index 4dd5404..163b6ac 100644 --- a/src/strategy_team/mod.rs +++ b/src/strategy_team/mod.rs @@ -37,6 +37,7 @@ use crate::value_estimation_team::indicators::tema::{tema, TemaData}; use crate::value_estimation_team::indicators::wiliams_percent_r::{ wiliams_percent_r, WiliamsPercentR, }; +use crate::future::Position; use futures::future::try_join_all; use reqwest::{Client, ClientBuilder}; use rust_decimal::prelude::Zero; @@ -159,6 +160,7 @@ pub async fn duplicate_filter( } pub async fn future_duplicate_filter( + position: Position, original_filtered_data: &HashMap, future_exchange_info_map: &HashMap ) -> Result, Box> { @@ -173,8 +175,11 @@ pub async fn future_duplicate_filter( let mut exists_condition_build = String::from("symbol=\'"); exists_condition_build.push_str(symbol.as_str()); - exists_condition_build.push_str("\' AND close_time="); - exists_condition_build.push_str(filtered_data.closetime.to_string().as_str()); + // exists_condition_build.push_str("\' AND close_time="); + // exists_condition_build.push_str(filtered_data.closetime.to_string().as_str()); + exists_condition_build.push_str("\' AND position="); + exists_condition_build.push_str(position.to_string().as_str()); + let exists_condition = Some(exists_condition_build); let exists_condition_c = exists_condition.clone(); let inspect_table_name_1_c = inspect_table_name_1.clone(); diff --git a/src/strategy_team/strategy_manager.rs b/src/strategy_team/strategy_manager.rs index 1d8da4e..cee043d 100644 --- a/src/strategy_team/strategy_manager.rs +++ b/src/strategy_team/strategy_manager.rs @@ -158,6 +158,8 @@ pub async fn insert_future_coins( "pnl", "entry_price", "current_price", + "target_percent", + "stoploss_percent", "base_qty_ordered", "pure_profit_percent", "minimum_profit_percent", @@ -179,6 +181,8 @@ pub async fn insert_future_coins( 0.0.to_string(), // pnl 0.0.to_string(), // entry_price 0.0.to_string(), // current_price + filtered_data.target_price.to_string(), // target_percent + filtered_data.stoploss.to_string(), // stoploss_percent 0.0.to_string(), // base_qty_ordered 0.0.to_string(), // pure_profit_percent 0.0.to_string(), // minimum_profit_percent