Add filtering

This commit is contained in:
Sik Yoon 2024-01-07 19:35:13 +09:00
parent a1dbccb1bb
commit 9a863b82f9

View File

@ -175,7 +175,61 @@ pub async fn list_up_for_buy(
}
try_join_all(task_vec).await?;
let final_filtered_data = filtered_data_3rd_arc.lock().await.clone();
// 4th filtering: BollingerBand (len:20, multiplier 2) 5 previous_30m_price (close or low price) < lower_band
let filtered_data_3rd = filtered_data_3rd_arc.lock().await.clone();
let mut filtered_data_4th: Vec<FilteredData> = Vec::new();
let mut filtered_data_4th_arc: Arc<Mutex<Vec<FilteredData>>> =
Arc::new(Mutex::new(filtered_data_4th));
let mut task_vec = Vec::new();
let bollingerbands = bollingerband(20, 2.0, &alldata.rt_price_30m_vec, &filtered_data_3rd).await?;
for element in filtered_data_3rd {
let mut rt_30m_vec: Vec<RealtimePriceData> = Vec::new();
let mut bb_vec: Vec<BollingerBandData> = Vec::new();
let rt_price_30m_vec_c: Vec<(String, Vec<RealtimePriceData>)> = alldata.rt_price_30m_vec.clone();
let bollingerbands_c = bollingerbands.clone();
let filtered_data_4th_arc_c = Arc::clone(&filtered_data_4th_arc);
task_vec.push(tokio::spawn(async move {
let rt_30m_option = rt_price_30m_vec_c
.iter()
.position(|x| *x.0 == element.symbol);
let bb_option_30m = bollingerbands_c.iter().position(|x| x.0 == element.symbol);
if rt_30m_option.is_some() && bb_option_30m.is_some() {
rt_30m_vec = rt_price_30m_vec_c[rt_30m_option.unwrap()].1.clone();
bb_vec = bollingerbands_c[bb_option_30m.unwrap()].1.clone();
let server_epoch = server_epoch().await;
if rt_30m_vec.len() >= 6 && bb_vec.len() >= 6 && rt_30m_vec.last().unwrap().close_time > server_epoch {
let bb_search_result = bb_vec.binary_search_by_key(
&rt_30m_vec.last().unwrap().close_time,
|BollingerBandData {
sma,
upperband,
lowerband,
close_time,
}| *close_time,
);
if bb_search_result.is_ok() {
if bb_vec[bb_search_result.unwrap()-1].lowerband > rt_30m_vec[rt_30m_vec.len()-2].low_price ||
bb_vec[bb_search_result.unwrap()-2].lowerband > rt_30m_vec[rt_30m_vec.len()-3].low_price ||
bb_vec[bb_search_result.unwrap()-3].lowerband > rt_30m_vec[rt_30m_vec.len()-4].low_price ||
bb_vec[bb_search_result.unwrap()-4].lowerband > rt_30m_vec[rt_30m_vec.len()-5].low_price ||
bb_vec[bb_search_result.unwrap()-5].lowerband > rt_30m_vec[rt_30m_vec.len()-6].low_price
{
let mut filtered_data_4th_lock = filtered_data_4th_arc_c.lock().await;
let mut filtered_data = FilteredData::new();
filtered_data.symbol = element.symbol.clone();
filtered_data.closetime = rt_30m_vec.last().unwrap().close_time;
filtered_data.current_price = rust_decimal::prelude::FromPrimitive::from_f64(rt_30m_vec.last().unwrap().close_price).unwrap();;
filtered_data_4th_lock.push(filtered_data);
}
}
}
}
}));
}
try_join_all(task_vec).await?;
let final_filtered_data = filtered_data_4th_arc.lock().await.clone();
insert_pre_suggested_coins(3, false, &final_filtered_data, &alldata).await;
Ok(())