diff --git a/src/strategy_team/future_strategy_long.rs b/src/strategy_team/future_strategy_long.rs index 0ce4106..b6000fe 100644 --- a/src/strategy_team/future_strategy_long.rs +++ b/src/strategy_team/future_strategy_long.rs @@ -8,7 +8,7 @@ use super::{ Arc, BollingerBandData, Client, ClientBuilder, Decimal, DemaData, EmaData, ExchangeInfo, FilteredDataValue, HashMap, HashSet, HeatMapLevel, HeatmapVolumeData, MacdData, Mutex, RealtimePriceData, RoundingStrategy, RsiData, StochRsiData, SuperTrendArea, SuperTrendSignal, - SupertrendData, TemaData, ToPrimitive, TradeFee, WiliamsPercentR, future_duplicate_filter, insert_future_coins + SupertrendData, TemaData, ToPrimitive, TradeFee, WiliamsPercentR, future_duplicate_filter, insert_future_coins, get_current_price_decimal }; use crate::future::{Position, FuturesExchangeInfo}; @@ -31,61 +31,94 @@ pub async fn list_up_for_buy( } // current Tema(15) < current Tema(60) - let mut keys_to_remove: HashSet = HashSet::new(); - let tema_15 = tema(15, &alldata.rt_price_1m_vec, &filtered_data).await?; - let tema_60 = tema(60, &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; - if let (Some(tema15_vec), Some(tema60_vec)) = (tema_15.get(symbol), tema_60.get(symbol)) { - if tema15_vec.len() > 10 - && tema60_vec.len() > 10 - && tema15_vec.last().unwrap().close_time == tema60_vec.last().unwrap().close_time - && tema15_vec.last().unwrap().close_time > server_epoch - && tema60_vec.last().unwrap().close_time > server_epoch - { - if tema15_vec.last().unwrap().tema_value > tema60_vec.last().unwrap().tema_value { - do_buy = true; - } - } - } - if do_buy == false { - keys_to_remove.insert(symbol.clone()); - } - } - remove_keys(&mut filtered_data, keys_to_remove).await; + // let mut keys_to_remove: HashSet = HashSet::new(); + // let tema_15 = tema(15, &alldata.rt_price_1m_vec, &filtered_data).await?; + // let tema_60 = tema(60, &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; + // if let (Some(tema15_vec), Some(tema60_vec)) = (tema_15.get(symbol), tema_60.get(symbol)) { + // if tema15_vec.len() > 10 + // && tema60_vec.len() > 10 + // && tema15_vec.last().unwrap().close_time == tema60_vec.last().unwrap().close_time + // && tema15_vec.last().unwrap().close_time > server_epoch + // && tema60_vec.last().unwrap().close_time > server_epoch + // { + // if tema15_vec.last().unwrap().tema_value > tema60_vec.last().unwrap().tema_value { + // do_buy = true; + // } + // } + // } + // if do_buy == false { + // keys_to_remove.insert(symbol.clone()); + // } + // } + // remove_keys(&mut filtered_data, keys_to_remove).await; // Heatmap volume: filtering close price with Extra High is over the previous candle from 30 previous candles - let mut keys_to_remove: HashSet = HashSet::new(); - let heatmap_volumes = heatmap_volume( - 60, - 60, - 4.0, - 2.5, - 1.0, - -0.5, - &filtered_data, - &alldata.rt_price_1m_vec, - ) - .await?; + // let mut keys_to_remove: HashSet = HashSet::new(); + // let heatmap_volumes = heatmap_volume( + // 60, + // 60, + // 4.0, + // 2.5, + // 1.0, + // -0.5, + // &filtered_data, + // &alldata.rt_price_1m_vec, + // ) + // .await?; + // let server_epoch = get_server_epoch().await; + // for (symbol, values) in &mut filtered_data { + // let mut do_buy = false; + // if let (Some(heatmap_volume_vec), Some(rt_price_vec), Some(rt_price_vec_30m)) = (heatmap_volumes.get(symbol), alldata.rt_price_1m_vec.get(symbol), alldata.rt_price_30m_vec.get(symbol)) { + // if heatmap_volume_vec.len() > 100 + // && heatmap_volume_vec.last().unwrap().close_time > server_epoch + // && rt_price_vec.last().unwrap().close_time == heatmap_volume_vec.last().unwrap().close_time + // && heatmap_volume_vec[heatmap_volume_vec.len()-2].heatmap_level == HeatMapLevel::ExtraHigh + // && rt_price_vec[rt_price_vec.len()-2].candle_type == CandleType::DOWN { + // let current_price: Decimal = rust_decimal::prelude::FromPrimitive::from_f64( + // rt_price_vec_30m.last().unwrap().close_price, + // ) + // .unwrap(); + // values.closetime = heatmap_volume_vec.last().unwrap().close_time; + // values.current_price = current_price; + // do_buy = true; + // } + // } + + // if do_buy == false { + // keys_to_remove.insert(symbol.clone()); + // } + // } + // remove_keys(&mut filtered_data, keys_to_remove).await; + + // StochRSI (RSI_len: 30, StochRSI_len: 30, K: 2, D: 2) K_current < 70, K_prev < 70, K_prev_1 < 70 let server_epoch = get_server_epoch().await; + let mut keys_to_remove: HashSet = HashSet::new(); + let stoch_rsis = stoch_rsi(30, 30, 2, 2, &alldata.rt_price_1m_vec, &filtered_data).await?; for (symbol, values) in &mut filtered_data { let mut do_buy = false; - if let (Some(heatmap_volume_vec), Some(rt_price_vec), Some(rt_price_vec_30m)) = (heatmap_volumes.get(symbol), alldata.rt_price_1m_vec.get(symbol), alldata.rt_price_30m_vec.get(symbol)) { - if heatmap_volume_vec.len() > 100 - && heatmap_volume_vec.last().unwrap().close_time > server_epoch - && rt_price_vec.last().unwrap().close_time == heatmap_volume_vec.last().unwrap().close_time - && heatmap_volume_vec[heatmap_volume_vec.len()-2].heatmap_level == HeatMapLevel::ExtraHigh - && rt_price_vec[rt_price_vec.len()-2].candle_type == CandleType::DOWN { - let current_price: Decimal = rust_decimal::prelude::FromPrimitive::from_f64( - rt_price_vec_30m.last().unwrap().close_price, - ) - .unwrap(); - values.closetime = heatmap_volume_vec.last().unwrap().close_time; - values.current_price = current_price; - do_buy = true; + let price_and_closetime = get_current_price_decimal(&symbol, &alldata.rt_price_1m_vec).await; + if let (Some(stoch_rsi_vec), Some(current_info)) = (stoch_rsis.get(symbol), price_and_closetime) { + if server_epoch < current_info.1 { + let search_result = stoch_rsi_vec + .iter() + .position(|x| x.close_time == current_info.1); + if stoch_rsi_vec.len() > 10 + && search_result.is_some_and(|a| { + stoch_rsi_vec[a].k > 20.0 + && stoch_rsi_vec[a].k > stoch_rsi_vec[a].d + && stoch_rsi_vec[a - 1].k < 20.0 + && stoch_rsi_vec[a - 2].k < 20.0 + }) + { + values.closetime = current_info.1; + values.current_price = current_info.0; + do_buy = true; + } } - } + } if do_buy == false { keys_to_remove.insert(symbol.clone()); @@ -93,7 +126,6 @@ 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?; insert_future_coins(Position::Long, server_epoch, &final_filtered_data).await?; @@ -125,7 +157,7 @@ pub async fn list_up_for_sell() -> Result<(), Box= (600_000) * 1 { + } else if server_epoch - element.transact_time >= (300_000) * 1 { // time up selling is_sell = true; } diff --git a/src/strategy_team/future_strategy_short.rs b/src/strategy_team/future_strategy_short.rs index 9f815d7..9f60960 100644 --- a/src/strategy_team/future_strategy_short.rs +++ b/src/strategy_team/future_strategy_short.rs @@ -8,7 +8,7 @@ use super::{ Arc, BollingerBandData, Client, ClientBuilder, Decimal, DemaData, EmaData, ExchangeInfo, FilteredDataValue, HashMap, HashSet, HeatMapLevel, HeatmapVolumeData, MacdData, Mutex, RealtimePriceData, RoundingStrategy, RsiData, StochRsiData, SuperTrendArea, SuperTrendSignal, - SupertrendData, TemaData, ToPrimitive, TradeFee, WiliamsPercentR, future_duplicate_filter, insert_future_coins + SupertrendData, TemaData, ToPrimitive, TradeFee, WiliamsPercentR, future_duplicate_filter, insert_future_coins, get_current_price_decimal }; use crate::future::{Position, FuturesExchangeInfo}; @@ -31,69 +31,100 @@ pub async fn list_up_for_buy( } // current Tema(15) < current Tema(60) - let mut keys_to_remove: HashSet = HashSet::new(); - let tema_15 = tema(15, &alldata.rt_price_1m_vec, &filtered_data).await?; - let tema_60 = tema(60, &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; - if let (Some(tema15_vec), Some(tema60_vec)) = (tema_15.get(symbol), tema_60.get(symbol)) { - if tema15_vec.len() > 10 - && tema60_vec.len() > 10 - && tema15_vec.last().unwrap().close_time == tema60_vec.last().unwrap().close_time - && tema15_vec.last().unwrap().close_time > server_epoch - && tema60_vec.last().unwrap().close_time > server_epoch - { - if tema15_vec.last().unwrap().tema_value < tema60_vec.last().unwrap().tema_value { - do_buy = true; - } - } - } - if do_buy == false { - keys_to_remove.insert(symbol.clone()); - } - } - remove_keys(&mut filtered_data, keys_to_remove).await; + // let mut keys_to_remove: HashSet = HashSet::new(); + // let tema_15 = tema(15, &alldata.rt_price_1m_vec, &filtered_data).await?; + // let tema_60 = tema(60, &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; + // if let (Some(tema15_vec), Some(tema60_vec)) = (tema_15.get(symbol), tema_60.get(symbol)) { + // if tema15_vec.len() > 10 + // && tema60_vec.len() > 10 + // && tema15_vec.last().unwrap().close_time == tema60_vec.last().unwrap().close_time + // && tema15_vec.last().unwrap().close_time > server_epoch + // && tema60_vec.last().unwrap().close_time > server_epoch + // { + // if tema15_vec.last().unwrap().tema_value < tema60_vec.last().unwrap().tema_value { + // do_buy = true; + // } + // } + // } + // if do_buy == false { + // keys_to_remove.insert(symbol.clone()); + // } + // } + // remove_keys(&mut filtered_data, keys_to_remove).await; // Heatmap volume: filtering close price with Extra High is over the previous candle from 30 previous candles - let mut keys_to_remove: HashSet = HashSet::new(); - let heatmap_volumes = heatmap_volume( - 60, - 60, - 4.0, - 2.5, - 1.0, - -0.5, - &filtered_data, - &alldata.rt_price_1m_vec, - ) - .await?; + // let mut keys_to_remove: HashSet = HashSet::new(); + // let heatmap_volumes = heatmap_volume( + // 60, + // 60, + // 4.0, + // 2.5, + // 1.0, + // -0.5, + // &filtered_data, + // &alldata.rt_price_1m_vec, + // ) + // .await?; + // let server_epoch = get_server_epoch().await; + // for (symbol, values) in &mut filtered_data { + // let mut do_buy = false; + // if let (Some(heatmap_volume_vec), Some(rt_price_vec), Some(rt_price_vec_30m)) = (heatmap_volumes.get(symbol), alldata.rt_price_1m_vec.get(symbol), alldata.rt_price_30m_vec.get(symbol)) { + // if heatmap_volume_vec.len() > 100 + // && heatmap_volume_vec.last().unwrap().close_time > server_epoch + // && rt_price_vec.last().unwrap().close_time == heatmap_volume_vec.last().unwrap().close_time + // && heatmap_volume_vec[heatmap_volume_vec.len()-2].heatmap_level == HeatMapLevel::ExtraHigh + // && rt_price_vec[rt_price_vec.len()-2].candle_type == CandleType::DOWN { + // let current_price: Decimal = rust_decimal::prelude::FromPrimitive::from_f64( + // rt_price_vec_30m.last().unwrap().close_price, + // ) + // .unwrap(); + // values.closetime = heatmap_volume_vec.last().unwrap().close_time; + // values.current_price = current_price; + // do_buy = true; + // } + // } + + // if do_buy == false { + // keys_to_remove.insert(symbol.clone()); + // } + // } + // remove_keys(&mut filtered_data, keys_to_remove).await; + + // StochRSI (RSI_len: 30, StochRSI_len: 30, K: 2, D: 2) K_current < 70, K_prev < 70, K_prev_1 < 70 let server_epoch = get_server_epoch().await; + let mut keys_to_remove: HashSet = HashSet::new(); + let stoch_rsis = stoch_rsi(30, 30, 2, 2, &alldata.rt_price_1m_vec, &filtered_data).await?; for (symbol, values) in &mut filtered_data { let mut do_buy = false; - if let (Some(heatmap_volume_vec), Some(rt_price_vec), Some(rt_price_vec_30m)) = (heatmap_volumes.get(symbol), alldata.rt_price_1m_vec.get(symbol), alldata.rt_price_30m_vec.get(symbol)) { - if heatmap_volume_vec.len() > 100 - && heatmap_volume_vec.last().unwrap().close_time > server_epoch - && rt_price_vec.last().unwrap().close_time == heatmap_volume_vec.last().unwrap().close_time - && heatmap_volume_vec[heatmap_volume_vec.len()-2].heatmap_level == HeatMapLevel::ExtraHigh - && rt_price_vec[rt_price_vec.len()-2].candle_type == CandleType::DOWN { - let current_price: Decimal = rust_decimal::prelude::FromPrimitive::from_f64( - rt_price_vec_30m.last().unwrap().close_price, - ) - .unwrap(); - values.closetime = heatmap_volume_vec.last().unwrap().close_time; - values.current_price = current_price; - do_buy = true; + let price_and_closetime = get_current_price_decimal(&symbol, &alldata.rt_price_1m_vec).await; + if let (Some(stoch_rsi_vec), Some(current_info)) = (stoch_rsis.get(symbol), price_and_closetime) { + if server_epoch < current_info.1 { + let search_result = stoch_rsi_vec + .iter() + .position(|x| x.close_time == current_info.1); + if stoch_rsi_vec.len() > 10 + && search_result.is_some_and(|a| { + stoch_rsi_vec[a].k < 80.0 + && stoch_rsi_vec[a].k < stoch_rsi_vec[a].d + && stoch_rsi_vec[a - 1].k > 80.0 + && stoch_rsi_vec[a - 2].k > 80.0 + }) + { + values.closetime = current_info.1; + values.current_price = current_info.0; + do_buy = true; + } } - } + } if do_buy == false { keys_to_remove.insert(symbol.clone()); } } remove_keys(&mut filtered_data, keys_to_remove).await; - - let final_filtered_data = future_duplicate_filter(&filtered_data, &future_exchange_info_map).await?; insert_future_coins(Position::Short, server_epoch, &final_filtered_data).await?; @@ -125,7 +156,7 @@ pub async fn list_up_for_sell() -> Result<(), Box= (600_000) * 1 { + } else if server_epoch - element.transact_time >= (300_000) * 1 { // time up selling is_sell = true; } diff --git a/src/strategy_team/mod.rs b/src/strategy_team/mod.rs index 30ad2f0..b535a0e 100644 --- a/src/strategy_team/mod.rs +++ b/src/strategy_team/mod.rs @@ -233,14 +233,15 @@ pub async fn get_current_price_f64( pub async fn get_current_price_decimal( symbol: &String, rt_price_map: &HashMap>, -) -> Option { +) -> Option<(Decimal, i64)> { if let Some(rt_vec) = rt_price_map.get(symbol) { if rt_vec.last().is_some_and(|a| a.close_price.is_normal()) { let current_price = rust_decimal::prelude::FromPrimitive::from_f64( rt_vec.last().unwrap().close_price, ) .unwrap(); - return Some(current_price); + let close_time = rt_vec.last().unwrap().close_time; + return Some((current_price, close_time)); } } None