New filtering

This commit is contained in:
Sik Yoon 2024-01-13 04:17:56 +09:00
parent f0bf95e218
commit dd234f2448
2 changed files with 748 additions and 652 deletions

View File

@ -1,424 +1,425 @@
// pub async fn strategist_001(
// alldata: &AllData,
// ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// // print rt_price for debugging
// // let a = alldata.rt_price_30m_vec.iter().position(|a| a.0 == "BTCUSDT");
// // println!("BTCUSDT: {:?}", alldata.rt_price_30m_vec[a.unwrap()].1.last().unwrap());
use crate::value_estimation_team::indicators::bollingerband::bollingerband;
// // // 1st filtering: supertrend(ATR period 10, multiplier: 1.3, 30m close price), the area should be in SELL area.
// // let mut filtered_2nd_symbols: Vec<(String, i64)> = Vec::new();
// // let mut filtered_2nd_symbols_arc: Arc<Mutex<Vec<(String, i64)>>> =
// // Arc::new(Mutex::new(filtered_2nd_symbols)); // (symbol, closetime)
// // let mut task_vec = Vec::new();
// // let valid_symbol_vec_c = alldata.valid_symbol_vec.clone();
// // for symbol in valid_symbol_vec_c {
// // let mut opclo_30m_vec: Vec<RealtimePriceData> = Vec::new();
// // let mut supertrend_vec: Vec<SupertrendData> = Vec::new();
// // let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
// // let filtered_2nd_symbols_arc_c = Arc::clone(&filtered_2nd_symbols_arc);
// // task_vec.push(tokio::spawn(async move {
// // let opclo_30m_option = rt_price_30m_vec_c.iter().position(|x| *x.0 == symbol);
// // let supertrend_option_30m =
// // supertrend(&symbol, &rt_price_30m_vec_c, 10, 1.3, true).await;
use super::{
dec, decimal_add, decimal_sub, decimal_div, ema, exists_record, insert_pre_suggested_coins,
limit_order_sell, rsi, select_filled_buy_orders, stoch_rsi, supertrend, try_join_all, AllData,
Arc, Client, ClientBuilder, Decimal, EmaData, ExchangeInfo, FilteredData, Mutex,
RealtimePriceData, RoundingStrategy, RsiData, StochRsiData, SupertrendData, TradeFee, update_record3, adx, AdxData, server_epoch, MacdData, ema_macd,
BollingerBandData, ToPrimitive
};
// // if opclo_30m_option.is_some() && supertrend_option_30m.is_some() {
// // opclo_30m_vec = rt_price_30m_vec_c[opclo_30m_option.unwrap()].1.clone();
// // supertrend_vec = supertrend_option_30m.unwrap();
// BB lowerband + SuperTrend + StochRSI
// SuperTrend length: 20, multiplier: 1.5, BUY signal
// ADX(10, 10) < 25.0
pub async fn list_up_for_buy(
alldata: AllData,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// print rt_price for debugging
// let a = alldata.rt_price_30m_vec.iter().position(|a| a.0 == "BTCUSDT");
// println!("BTCUSDT: {:?}", alldata.rt_price_30m_vec[a.unwrap()].1.last().unwrap());
// // if opclo_30m_vec.len() >= 3 && supertrend_vec.len() >= 3 {
// // let supertrend_search_result = supertrend_vec.binary_search_by_key(
// // &opclo_30m_vec.last().unwrap().close_time,
// // |SupertrendData {
// // band_value,
// // signal,
// // area,
// // close_time,
// // }| *close_time,
// // );
// // if supertrend_search_result.is_ok() {
// // if supertrend_vec[supertrend_search_result.unwrap()]
// // .area
// // .contains("DOWN")
// // {
// // let mut filtered_2nd_symbols_lock =
// // filtered_2nd_symbols_arc_c.lock().await;
// // filtered_2nd_symbols_lock
// // .push((symbol.clone(), opclo_30m_vec.last().unwrap().close_time));
// // }
// // }
// // }
// // }
// // }));
// // }
// // try_join_all(task_vec).await?;
// 1st filtering: lookup tables if the tradepair is already there
let inspect_table_name_1 = String::from("buy_ordered_coin_list");
let inspect_table_name_2 = String::from("sell_ordered_coin_list");
let inspect_table_name_3 = String::from("pre_suggested_coin_list");
let inspect_table_name_4 = String::from("suggested_coin_list");
// // 1st filtering: making basic form
// let mut filtered_2nd_symbols: Vec<(String, i64)> = Vec::new();
// let mut filtered_2nd_symbols_arc: Arc<Mutex<Vec<(String, i64)>>> =
// Arc::new(Mutex::new(filtered_2nd_symbols)); // (symbol, closetime)
// let mut task_vec = Vec::new();
// let valid_symbol_vec_c = alldata.valid_symbol_vec.clone();
// for symbol in valid_symbol_vec_c {
// let mut opclo_30m_vec: Vec<RealtimePriceData> = Vec::new();
let mut filtered_data_1st: Vec<FilteredData> = Vec::new();
let mut filtered_data_1st_arc: Arc<Mutex<Vec<FilteredData>>> =
Arc::new(Mutex::new(filtered_data_1st));
let mut task_vec = Vec::new();
// let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
// let filtered_2nd_symbols_arc_c = Arc::clone(&filtered_2nd_symbols_arc);
// task_vec.push(tokio::spawn(async move {
// let opclo_30m_option = rt_price_30m_vec_c.iter().position(|x| *x.0 == symbol);
for symbol in &alldata.valid_symbol_vec {
let mut exists_condition_build = String::from("symbol=\'");
exists_condition_build.push_str(symbol.as_str());
exists_condition_build.push_str("\' AND registerer=");
exists_condition_build.push_str(1.to_string().as_str());
// exists_condition_build.push_str("\' AND close_time=");
// exists_condition_build.push_str(element.1.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();
let inspect_table_name_2_c = inspect_table_name_2.clone();
let inspect_table_name_3_c = inspect_table_name_3.clone();
let inspect_table_name_4_c = inspect_table_name_4.clone();
let symbol_c = symbol.clone();
let filtered_data_1st_arc_c = Arc::clone(&filtered_data_1st_arc);
task_vec.push(tokio::spawn(async move {
let mut filtered_data = FilteredData::new();
let inspect_result_1 =
exists_record(&inspect_table_name_1_c, &exists_condition_c).await;
let inspect_result_2 =
exists_record(&inspect_table_name_2_c, &exists_condition_c).await;
let inspect_result_3 =
exists_record(&inspect_table_name_3_c, &exists_condition_c).await;
let inspect_result_4 =
exists_record(&inspect_table_name_4_c, &exists_condition_c).await;
// if opclo_30m_option.is_some() {
// opclo_30m_vec = rt_price_30m_vec_c[opclo_30m_option.unwrap()].1.clone();
if inspect_result_1 == false
&& inspect_result_2 == false
&& inspect_result_3 == false
&& inspect_result_4 == false
{
let mut filtered_data_1st_lock = filtered_data_1st_arc_c.lock().await;
// if opclo_30m_vec.len() >= 3 {
// let mut filtered_2nd_symbols_lock =
// filtered_2nd_symbols_arc_c.lock().await;
// filtered_2nd_symbols_lock
// .push((symbol.clone(), opclo_30m_vec.last().unwrap().close_time));
// }
// }
// }));
// }
// try_join_all(task_vec).await?;
filtered_data.symbol = symbol_c;
// // 2nd filtering: lookup tables if the tradepair is already there
// let inspect_table_name_1 = String::from("buy_ordered_coin_list");
// let inspect_table_name_2 = String::from("sell_ordered_coin_list");
// let inspect_table_name_3 = String::from("pre_suggested_coin_list");
// let inspect_table_name_4 = String::from("suggested_coin_list");
filtered_data_1st_lock.push(filtered_data);
}
}));
}
try_join_all(task_vec).await?;
// let mut filtered_3rd_symbols: Vec<(String, i64)> = Vec::new();
// let mut filtered_3rd_symbols_arc: Arc<Mutex<Vec<(String, i64)>>> =
// Arc::new(Mutex::new(filtered_3rd_symbols)); // (symbol, closetime)
// let mut task_vec = Vec::new();
// 2nd filtering: supertrend(ATR period 20, multiplier: 2, 30m close price)
let filtered_data_1st = filtered_data_1st_arc.lock().await.clone();
let mut filtered_data_2nd: Vec<FilteredData> = Vec::new();
let mut filtered_data_2nd_arc: Arc<Mutex<Vec<FilteredData>>> =
Arc::new(Mutex::new(filtered_data_2nd));
let mut task_vec = Vec::new();
// let filtered_2nd_iter = filtered_2nd_symbols_arc.lock().await.clone().into_iter();
// for element in filtered_2nd_iter {
// let mut exists_condition_build = String::from("symbol=\'");
// exists_condition_build.push_str(element.0.as_str());
// exists_condition_build.push_str("\' AND registerer=");
// exists_condition_build.push_str(1.to_string().as_str());
// // exists_condition_build.push_str("\' AND close_time=");
// // exists_condition_build.push_str(element.1.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();
// let inspect_table_name_2_c = inspect_table_name_2.clone();
// let inspect_table_name_3_c = inspect_table_name_3.clone();
// let inspect_table_name_4_c = inspect_table_name_4.clone();
// let element_c = element.clone();
// let filtered_3rd_symbols_arc_c = Arc::clone(&filtered_3rd_symbols_arc);
// task_vec.push(tokio::spawn(async move {
// let inspect_result_1 =
// exists_record(&inspect_table_name_1_c, &exists_condition_c).await;
// let inspect_result_2 =
// exists_record(&inspect_table_name_2_c, &exists_condition_c).await;
// let inspect_result_3 =
// exists_record(&inspect_table_name_3_c, &exists_condition_c).await;
// let inspect_result_4 =
// exists_record(&inspect_table_name_4_c, &exists_condition_c).await;
for element in filtered_data_1st {
let mut rt_30m_vec: Vec<RealtimePriceData> = Vec::new();
let mut supertrend_vec: Vec<SupertrendData> = Vec::new();
let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
let filtered_data_2nd_arc_c = Arc::clone(&filtered_data_2nd_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 supertrend_option_30m =
supertrend(&element.symbol, &rt_price_30m_vec_c, 20, 2.0, true).await;
// if inspect_result_1 == false
// && inspect_result_2 == false
// && inspect_result_3 == false
// && inspect_result_4 == false
// {
// let mut filtered_3rd_symbols_lock = filtered_3rd_symbols_arc_c.lock().await;
// filtered_3rd_symbols_lock.push(element_c);
// }
// }));
// }
// try_join_all(task_vec).await?;
if rt_30m_option.is_some() && supertrend_option_30m.is_some() {
rt_30m_vec = rt_price_30m_vec_c[rt_30m_option.unwrap()].1.clone();
supertrend_vec = supertrend_option_30m.unwrap();
let server_epoch = server_epoch().await;
if rt_30m_vec.len() >= 3 && supertrend_vec.len() >= 3 && rt_30m_vec.last().unwrap().close_time > server_epoch {
let supertrend_search_result = supertrend_vec.binary_search_by_key(
&rt_30m_vec.last().unwrap().close_time,
|SupertrendData {
band_value,
signal,
area,
close_time,
}| *close_time,
);
if supertrend_search_result.is_ok() {
let current_price: Decimal = rust_decimal::prelude::FromPrimitive::from_f64(rt_30m_vec.last().unwrap().close_price).unwrap();
let mut filtered_data_2nd_lock = filtered_data_2nd_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 = current_price;
if supertrend_vec[supertrend_search_result.unwrap()].area.contains("DOWN")
&& supertrend_vec[supertrend_search_result.unwrap()].band_value > element.current_price.to_f64().unwrap()
{
let band_value: Decimal = rust_decimal::prelude::FromPrimitive::from_f64(supertrend_vec[supertrend_search_result.unwrap()].band_value).unwrap();
let stop_loss = decimal_sub(filtered_data.current_price, decimal_div(decimal_sub(band_value, filtered_data.current_price), dec!(2)));
filtered_data.stoploss = stop_loss;
filtered_data_2nd_lock.push(filtered_data);
} else if supertrend_vec[supertrend_search_result.unwrap()].area.contains("UP")
&& supertrend_vec[supertrend_search_result.unwrap()].band_value < element.current_price.to_f64().unwrap()
{
let band_value: Decimal = rust_decimal::prelude::FromPrimitive::from_f64(supertrend_vec[supertrend_search_result.unwrap()].band_value).unwrap();
filtered_data.stoploss = band_value;
filtered_data_2nd_lock.push(filtered_data);
}
}
}
}
}));
}
try_join_all(task_vec).await?;
// // 3rd filtering: BollingerBand (length 10, stddev: 2.5, 30m close price) the current price should be under the lowerband of BB.
// let filtered_3rd_symbols_c = filtered_3rd_symbols_arc.lock().await.clone();
// let sma10_30m_data: Vec<(String, Vec<SmaData>)> = sma(
// 10,
// &alldata.rt_price_30m_vec,
// &filtered_3rd_symbols_c,
// )
// .await?;
// 3rd filtering: the latest 5 30m candle close prices > EMA 200
let filtered_data_2nd = filtered_data_2nd_arc.lock().await.clone();
let mut filtered_data_3rd: Vec<FilteredData> = Vec::new();
let mut filtered_data_3rd_arc: Arc<Mutex<Vec<FilteredData>>> =
Arc::new(Mutex::new(filtered_data_3rd));
let mut task_vec = Vec::new();
let ema_vec = ema(200, &alldata.rt_price_30m_vec, &filtered_data_2nd).await?;
for element in filtered_data_2nd {
let mut opclo_30m_vec = alldata.rt_price_30m_vec.clone();
let mut supertrend_vec: Vec<SupertrendData> = Vec::new();
let ema_vec_c = ema_vec.clone();
let filtered_data_3rd_arc_c = Arc::clone(&filtered_data_3rd_arc);
task_vec.push(tokio::spawn(async move {
let ema_search_result = ema_vec_c.iter().position(|x| x.0 == element.symbol);
let candle_search_result = opclo_30m_vec.iter().position(|x| x.0 == element.symbol);
if ema_search_result.is_some() && candle_search_result.is_some() {
let search_result = ema_vec_c[ema_search_result.unwrap()].1.binary_search_by_key(
&opclo_30m_vec[candle_search_result.unwrap()].1.last().unwrap().close_time,
|EmaData {
ema_value,
close_time,
}| *close_time);
if search_result.is_ok_and(|x| ema_vec_c[ema_search_result.unwrap()].1[search_result.unwrap()].ema_value < opclo_30m_vec[candle_search_result.unwrap()].1[opclo_30m_vec[candle_search_result.unwrap()].1.len()-1].close_price) &&
search_result.is_ok_and(|x| ema_vec_c[ema_search_result.unwrap()].1[search_result.unwrap()-1].ema_value < opclo_30m_vec[candle_search_result.unwrap()].1[opclo_30m_vec[candle_search_result.unwrap()].1.len()-2].close_price) &&
search_result.is_ok_and(|x| ema_vec_c[ema_search_result.unwrap()].1[search_result.unwrap()-2].ema_value < opclo_30m_vec[candle_search_result.unwrap()].1[opclo_30m_vec[candle_search_result.unwrap()].1.len()-3].close_price) &&
search_result.is_ok_and(|x| ema_vec_c[ema_search_result.unwrap()].1[search_result.unwrap()-3].ema_value < opclo_30m_vec[candle_search_result.unwrap()].1[opclo_30m_vec[candle_search_result.unwrap()].1.len()-4].close_price) &&
search_result.is_ok_and(|x| ema_vec_c[ema_search_result.unwrap()].1[search_result.unwrap()-4].ema_value < opclo_30m_vec[candle_search_result.unwrap()].1[opclo_30m_vec[candle_search_result.unwrap()].1.len()-5].close_price) {
let mut filtered_3rd_symbols_lock =
filtered_data_3rd_arc_c.lock().await;
let mut filtered_data = FilteredData::new();
filtered_data.symbol = element.symbol.clone();
filtered_data.closetime = element.closetime;
filtered_data.current_price = element.current_price;
filtered_data.stoploss = element.stoploss;
filtered_data.target_price = element.target_price;
// let bb10_30m_data: Vec<(String, Vec<BollingerBandData>)> =
// bollingerband(
// 10,
// 3.0,
// &sma10_30m_data,
// &alldata.rt_price_30m_vec,
// &filtered_3rd_symbols_c,
// )
// .await?;
filtered_3rd_symbols_lock.push(filtered_data);
}
}
}));
}
try_join_all(task_vec).await?;
// 4th filtering: 0.5% <= the average amplitude of the latest 10 30m candles <= 1.0%
let filtered_data_3rd_c = 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();
for element in filtered_data_3rd_c {
let mut supertrend_vec: Vec<SupertrendData> = Vec::new();
let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
let filtered_data_4th_arc_c = Arc::clone(&filtered_data_4th_arc);
// let mut task_vec = Vec::new();
// let mut filtered_4th_symbols: Vec<(String, i64)> = Vec::new();
// let mut filtered_4th_symbols_arc: Arc<Mutex<Vec<(String, i64)>>> =
// Arc::new(Mutex::new(filtered_4th_symbols)); // (symbol, closetime)
// for element in filtered_3rd_symbols_c {
// let mut bb10_30m_vec: Vec<BollingerBandData> = Vec::new();
// let bb10_30m_option = bb10_30m_data.iter().position(|x| *x.0 == element.0);
// let bb10_30m_option_c = bb10_30m_option.clone();
// let element_c = element.clone();
// let filtered_4th_symbols_arc_c = Arc::clone(&filtered_4th_symbols_arc);
task_vec.push(tokio::spawn(async move {
let position_idx = rt_price_30m_vec_c.iter().position(|elem| elem.0 == element.symbol);
if position_idx.is_some() {
let vec_len = rt_price_30m_vec_c[position_idx.unwrap()].1.len();
if vec_len >= 11 {
let candles = rt_price_30m_vec_c[position_idx.unwrap()].1.get(vec_len-12..vec_len-1).unwrap();
let windows = candles.windows(2);
let mut average_amplitude = 0.0;
// if bb10_30m_option_c.is_some() {
// bb10_30m_vec = bb10_30m_data[bb10_30m_option_c.unwrap()].1.clone();
for window in windows {
average_amplitude += (window.last().unwrap().high_price - window.last().unwrap().low_price) / window.first().unwrap().close_price;
}
average_amplitude /= 10.0;
// if bb10_30m_vec.len() >= 3 {
// let bb10_30m_vec_c = bb10_30m_vec.clone();
// let current_price = get_current_price(&element_c.0, &alldata.rt_price_30m_vec)
// .await
// .unwrap();
// task_vec.push(tokio::spawn(async move {
// let bb_search_result = bb10_30m_vec_c.binary_search_by_key(
// &element_c.1,
// |&BollingerBandData {
// sma,
// upperband,
// lowerband,
// close_time,
// }| close_time,
// );
// if bb_search_result.is_ok() {
// if bb10_30m_vec_c[bb_search_result.unwrap()].lowerband > current_price {
// let mut filtered_4th_symbols_lock =
// filtered_4th_symbols_arc_c.lock().await;
// filtered_4th_symbols_lock.push(element_c);
// }
// }
// }));
// }
// }
// }
// try_join_all(task_vec).await?;
if 0.005 <= average_amplitude && average_amplitude <= 0.01 {
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 = element.closetime;
filtered_data.current_price = element.current_price;
filtered_data.stoploss = element.stoploss;
filtered_data.target_price = element.target_price;
filtered_data_4th_lock.push(filtered_data);
}
}
}
}));
}
try_join_all(task_vec).await?;
// // 4th filtering: the current price should be lower than lowest price from the latest 20 candles.
// let mut task_vec = Vec::new();
// let filtered_4th_symbols_c = filtered_4th_symbols_arc.lock().await.clone();
// let mut filtered_5th_symbols: Vec<(String, i64)> = Vec::new();
// let mut filtered_5th_symbols_arc: Arc<Mutex<Vec<(String, i64)>>> =
// Arc::new(Mutex::new(filtered_5th_symbols)); // (symbol, closetime)
// for element in filtered_4th_symbols_c {
// let rt_price_30m_option = alldata.rt_price_30m_vec.iter().position(|x| *x.0 == element.0);
// let element_c = element.clone();
// let filtered_5th_symbols_arc_c = Arc::clone(&filtered_5th_symbols_arc);
// 5th filtering: 30m StochRSI (RSI_len: 10, StochRSI_len: 10, K: 3, D: 3) previous K < 5 && current K > previous K
let filtered_data_4th_c = filtered_data_4th_arc.lock().await.clone();
let mut filtered_data_5th: Vec<FilteredData> = Vec::new();
let mut filtered_data_5th_arc: Arc<Mutex<Vec<FilteredData>>> =
Arc::new(Mutex::new(filtered_data_5th));
let mut task_vec = Vec::new();
let stoch_rsis = stoch_rsi(10, 10, 3, 3, &alldata.rt_price_30m_vec, &filtered_data_4th_c).await?;
for element in filtered_data_4th_c {
let stoch_rsis_c = stoch_rsis.clone();
let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
let filtered_data_5th_arc_c = Arc::clone(&filtered_data_5th_arc);
// if rt_price_30m_option.is_some() {
// let mut rt_price_30m_vec_c = alldata.rt_price_30m_vec[rt_price_30m_option.unwrap()].1.clone();
// let current_price = get_current_price(&element_c.0, &alldata.rt_price_30m_vec)
// .await
// .unwrap();
task_vec.push(tokio::spawn(async move {
let position_idx = stoch_rsis_c.iter().position(|elem| elem.0 == element.symbol);
if position_idx.is_some() {
let stoch_rsi_vec = stoch_rsis_c[position_idx.unwrap()].1.clone();
let search_result = stoch_rsi_vec.iter().position(|x| x.close_time == element.closetime);
if search_result.is_some_and(|a| stoch_rsi_vec[a-1].k < 5.0 && stoch_rsi_vec[a].k > stoch_rsi_vec[a-1].k) {
let mut filtered_data_5th_lock = filtered_data_5th_arc_c.lock().await;
let mut filtered_data = FilteredData::new();
filtered_data.symbol = element.symbol.clone();
filtered_data.closetime = element.closetime;
filtered_data.current_price = element.current_price;
filtered_data.stoploss = element.stoploss;
filtered_data.target_price = element.target_price;
filtered_data_5th_lock.push(filtered_data);
}
}
}));
}
try_join_all(task_vec).await?;
// task_vec.push(tokio::spawn(async move {
// if rt_price_30m_vec_c.len() >= 21 {
// rt_price_30m_vec_c.pop();
// rt_price_30m_vec_c.reverse();
// rt_price_30m_vec_c.truncate(20);
let final_filtered_data = filtered_data_5th_arc.lock().await.clone();
insert_pre_suggested_coins(1, false, &final_filtered_data, &alldata).await;
// let lowest_price_option = rt_price_30m_vec_c.iter()
// .enumerate()
// .min_by(|(_, a), (_, b)| {
// a.low_price
// .partial_cmp(&b.low_price)
// .expect("Nan was forbidden.")
// })
// .map(|(index, _)| index);
Ok(())
}
// if lowest_price_option.is_some() {
// let lowest_price = rt_price_30m_vec_c[lowest_price_option.unwrap()].low_price;
// if lowest_price > current_price {
// let mut filtered_5th_symbols_lock =
// filtered_5th_symbols_arc_c.lock().await;
// filtered_5th_symbols_lock.push(element_c);
// }
// }
// }
// }));
// }
// }
// try_join_all(task_vec).await?;
pub async fn list_up_for_sell(
all_data: &AllData,
exchange_info_vec: &Vec<ExchangeInfo>,
trade_fee_vec: &Vec<TradeFee>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let filled_buy_orders = select_filled_buy_orders(1).await?;
// // 5th filtering: RSI (length: 10, 30m close price) the current index should be lower than 30.
// let filtered_5th_symbol_c = filtered_5th_symbols_arc.lock().await.clone();
// let mut rsi10_30m_data: Vec<(String, Vec<RsiData>)> = rsi(
// 10,
// &alldata.rt_price_30m_vec,
// &filtered_5th_symbol_c,
// )
// .await?;
// let mut task_vec = Vec::new();
// let mut filtered_6th_symbols: Vec<(String, i64)> = Vec::new(); // (symbol, closetime)
// let mut filtered_6th_symbols_arc: Arc<Mutex<Vec<(String, i64)>>> =
// Arc::new(Mutex::new(filtered_6th_symbols)); // (symbol, closetime)
if !filled_buy_orders.is_empty() {
let client = ClientBuilder::new()
.timeout(tokio::time::Duration::from_millis(5000))
.build()
.unwrap();
let mut supertrend_vec: Vec<SupertrendData> = Vec::new();
let server_epoch = server_epoch().await;
// for element in filtered_5th_symbol_c {
// let rsi10_30m_option = rsi10_30m_data.iter().position(|x| *x.0 == element.0);
// let filtered_6th_symbols_arc_c = Arc::clone(&filtered_6th_symbols_arc);
let mut filtered_symbol_vec: Vec<FilteredData> = Vec::new();
for element in &filled_buy_orders {
let filltered_data = FilteredData{
symbol: element.symbol.clone(),
closetime: 0,
stoploss: dec!(0),
target_price: dec!(0),
current_price: dec!(0),
};
filtered_symbol_vec.push(filltered_data);
}
let stoch_rsis = stoch_rsi(10, 10, 3, 3, &all_data.rt_price_30m_vec, &filtered_symbol_vec).await?;
for element in filled_buy_orders {
if element.used_usdt >= dec!(10.0) {
let lot_step_size_option = exchange_info_vec
.iter()
.position(|exchange_info| exchange_info.symbol == element.symbol);
let quote_commission_precision_option = exchange_info_vec
.iter()
.position(|exchange_info| exchange_info.symbol == element.symbol);
let search_result = stoch_rsis.iter().position(|x| x.0 == element.symbol);
let opclo_30m_option = all_data
.rt_price_30m_vec
.iter()
.position(|x| *x.0 == element.symbol);
let supertrend_option_30m =
supertrend(&element.symbol, &all_data.rt_price_30m_vec, 20, 1.5, true).await;
// if rsi10_30m_option.is_some() {
// let mut rsi10_30m_vec = rsi10_30m_data[rsi10_30m_option.unwrap()].1.clone();
if lot_step_size_option.is_some()
&& quote_commission_precision_option.is_some()
&& search_result.is_some()
&& opclo_30m_option.is_some()
&& supertrend_option_30m.is_some()
{
// update stoploss
supertrend_vec = supertrend_option_30m.unwrap();
let band_value: Decimal = rust_decimal::prelude::FromPrimitive::from_f64(supertrend_vec.last().unwrap().band_value).unwrap();
if supertrend_vec.last().unwrap().area.contains("UP")
&& band_value > element.stoploss {
let update_table_name = String::from("buy_ordered_coin_list");
let update_value = vec![
(String::from("stoploss"), band_value.to_string()),
];
let update_condition = vec![(String::from("id"), element.id.to_string())];
update_record3(&update_table_name, &update_value, &update_condition)
.await
.unwrap();
}
let lot_step_size = exchange_info_vec[lot_step_size_option.unwrap()].stepsize;
let quote_commission_precision = exchange_info_vec
[quote_commission_precision_option.unwrap()]
.quote_commission_precision;
let base_qty_to_be_ordered =
element.base_qty_ordered.round_dp_with_strategy(
lot_step_size.normalize().scale(),
RoundingStrategy::ToZero,
);
let stoch_rsi_k = stoch_rsis[search_result.unwrap()].1.last().unwrap().k;
let stoch_rsi_k_prev = stoch_rsis[search_result.unwrap()].1[stoch_rsis[search_result.unwrap()].1.len()-2].k;
if (element.is_long == 0 || element.is_long == 1)
&& !element.current_price.is_zero()
{
if element.current_price >= element.target_price
{
limit_order_sell(
&element,
element.current_price,
base_qty_to_be_ordered,
&client,
&exchange_info_vec,
&trade_fee_vec,
)
.await;
} else if element.current_price <= element.stoploss {
limit_order_sell(
&element,
element.current_price,
base_qty_to_be_ordered,
&client,
&exchange_info_vec,
&trade_fee_vec,
)
.await;
} else if server_epoch - element.transact_time > (1_800_000) * 20 {
limit_order_sell(
&element,
element.current_price,
base_qty_to_be_ordered,
&client,
&exchange_info_vec,
&trade_fee_vec,
)
.await;
} else if element.pure_profit_percent > 3.0 {
limit_order_sell(
&element,
element.current_price,
base_qty_to_be_ordered,
&client,
&exchange_info_vec,
&trade_fee_vec,
)
.await;
} else if stoch_rsi_k > 90.0 {
limit_order_sell(
&element,
element.current_price,
base_qty_to_be_ordered,
&client,
&exchange_info_vec,
&trade_fee_vec,
)
.await;
}
// TODO: sell_count가 1일 때 적용하기
// else if (supertrend_vec
// .last()
// .unwrap()
// .signal
// .as_ref()
// .is_some_and(|x| x.contains("SELL"))
// || supertrend_vec.last().unwrap().area.contains("DOWN"))
// && (supertrend_vec.last().unwrap().close_time > element.close_time)
// {
// println!(
// "SELL signal selling {} {:.2}",
// element.symbol, element.pure_profit_percent
// );
// limit_order_sell(
// &element,
// element.current_price,
// base_qty_to_be_ordered,
// &client,
// &exchange_info_vec,
// &trade_fee_vec,
// )
// .await;
// }
}
}
}
}
}
// if rsi10_30m_vec.len() >= 3 {
// let element_c = element.clone();
// task_vec.push(tokio::spawn(async move {
// let rsi_search_result = rsi10_30m_vec.binary_search_by_key(
// &element.1,
// |&RsiData {
// rsi_value,
// close_time,
// }| close_time,
// );
// if rsi_search_result.is_ok() {
// if rsi10_30m_vec[rsi_search_result.unwrap()].rsi_value <= 20.0 {
// let mut filtered_6th_symbols_lock =
// filtered_6th_symbols_arc_c.lock().await;
// filtered_6th_symbols_lock.push(element_c);
// }
// }
// }));
// }
// }
// }
// try_join_all(task_vec).await?;
// // 6th filtering: StochRSI (RSI length: 14, Stoch length: 14, smooth k: 3, smooth d: 3) smooth kn <= 10, kn-1 <= 25.0
// let filtered_6th_symbol_c = filtered_6th_symbols_arc.lock().await.clone();
// let mut rsi14_30m_data: Vec<(String, Vec<RsiData>)> = rsi(
// 14,
// &alldata.rt_price_30m_vec,
// &filtered_6th_symbol_c,
// )
// .await?;
// let stoch_rsi_data = stoch_rsi(&rsi14_30m_data, 14, 3, 3).await?;
// let mut stoch_rsi14_30m_vec: Vec<StochRsiData> = Vec::new();
// let mut filtered_7th_symbols: Vec<(String, i64)> = Vec::new(); // (symbol, closetime)
// for element in filtered_6th_symbol_c {
// let stoch_rsi14_30m_option = stoch_rsi_data.iter().position(|x| *x.0 == element.0);
// if stoch_rsi14_30m_option.is_some() {
// stoch_rsi14_30m_vec = stoch_rsi_data[stoch_rsi14_30m_option.unwrap()].1.clone();
// if stoch_rsi14_30m_vec.len() >= 3 {
// let stoch_rsi_search_result = stoch_rsi14_30m_vec.binary_search_by_key(
// &element.1,
// |&StochRsiData {
// k,
// d,
// close_time,
// }| close_time,
// );
// if stoch_rsi_search_result.is_ok() {
// if stoch_rsi14_30m_vec[stoch_rsi_search_result.unwrap()].k <= 10.0 && stoch_rsi14_30m_vec[stoch_rsi_search_result.unwrap()-1].k <= 25.0 {
// filtered_7th_symbols.push(element);
// }
// }
// }
// }
// }
// // 7th filtering: heatmap volume(MA length 10, std length 10, 30m close price), the current candle should be over than high at least.
// let mut filtered_8th_symbols: Vec<(String, i64)> = Vec::new(); // (symbol, closetime)
// for element in filtered_7th_symbols {
// let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
// let opclo_30m_option = rt_price_30m_vec_c.iter().position(|x| *x.0 == element.0);
// if opclo_30m_option.is_some() {
// let opclo_30m_vec = rt_price_30m_vec_c[opclo_30m_option.unwrap()].1.clone();
// if opclo_30m_vec.len() >= 3 {
// let heatmap_volume_option =
// heatmap_volume(&element.0, &rt_price_30m_vec_c, 10, 10, 4.0, 2.5, 1.0, -0.5)
// .await;
// if heatmap_volume_option.is_some() {
// let heatmap_volume_vec = heatmap_volume_option.unwrap();
// let heatmap_search_result = heatmap_volume_vec.binary_search_by_key(
// &element.1,
// |HeatmapVolumeData {
// heatmap_value,
// heatmap_level,
// close_time,
// }| *close_time,
// );
// if heatmap_search_result.is_ok() {
// if heatmap_volume_vec[heatmap_search_result.unwrap()].heatmap_level
// == HeatMapLevel::Medium
// ||
// heatmap_volume_vec[heatmap_search_result.unwrap()].heatmap_level
// == HeatMapLevel::High
// || heatmap_volume_vec[heatmap_search_result.unwrap()].heatmap_level
// == HeatMapLevel::ExtraHigh
// {
// filtered_8th_symbols.push(element);
// }
// }
// }
// }
// }
// }
// // final job: adding price information to filtered results
// let mut filtered_symbols: Vec<(String, i64, f64)> = Vec::new(); // (symbol, closetime, current price)
// let mut filtered_symbols_arc = Arc::new(Mutex::new(filtered_symbols));
// let mut task_vec = Vec::new();
// for element in filtered_8th_symbols {
// let mut filtered_symbols_arc_c = Arc::clone(&filtered_symbols_arc);
// let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
// let elememt_c = element.clone();
// task_vec.push(tokio::spawn(async move {
// let opclo_30m_option = rt_price_30m_vec_c.iter().position(|x| *x.0 == element.0);
// if opclo_30m_option.is_some() {
// if rt_price_30m_vec_c[opclo_30m_option.unwrap()]
// .1
// .last()
// .is_some()
// {
// let mut filtered_symbols_lock: tokio::sync::MutexGuard<
// '_,
// Vec<(String, i64, f64)>,
// > = filtered_symbols_arc_c.lock().await;
// filtered_symbols_lock.push((
// elememt_c.0,
// elememt_c.1,
// rt_price_30m_vec_c[opclo_30m_option.unwrap()]
// .1
// .last()
// .unwrap()
// .close_price,
// ));
// }
// }
// }));
// }
// try_join_all(task_vec).await?;
// // 6th filtering condition: MACD
// // let mut opclo_30m_vec: Vec<RealtimePriceData> = Vec::new();
// // let mut ema3_1d_vec: &Vec<EmaData> = &Vec::new();
// // let mut ema10_1d_vec: &Vec<EmaData> = &Vec::new();
// // let mut filtered_7th_symbols: Vec<(String, i64)> = Vec::new(); // (symbol, closetime)
// // for element in filtered_6th_symbols {
// // let ema3_1d_option = alldata.ema3_1d_data.iter().position(|x| *x.0 == *element.0);
// // let ema10_1d_option = alldata.ema10_1d_data.iter().position(|x| *x.0 == *element.0);
// // if ema3_1d_option.is_some() && ema10_1d_option.is_some() {
// // ema3_1d_vec = &alldata.ema3_1d_data[ema3_1d_option.unwrap()].1;
// // ema10_1d_vec = &alldata.ema10_1d_data[ema10_1d_option.unwrap()].1;
// // if ema3_1d_vec.len() > 20 && ema10_1d_vec.len() > 20 {
// // let macd_vec = ema_macd(&ema3_1d_vec, &ema10_1d_vec, 10).await?;
// // // let macd_search_result = macd_vec.binary_search_by_key(&element.1, |&EmaMacd{macd_value, close_time}|close_time);
// // // if macd_search_result.is_ok() {
// // if macd_vec.last().unwrap().macd_value - macd_vec[macd_vec.len() -2].macd_value >= 0.0 {
// // filtered_7th_symbols.push(element);
// // }
// // // }
// // }
// // }
// // }
// let a = filtered_symbols_arc.lock().await.clone();
// insert_pre_suggested_coins(1, false, &a, alldata).await;
// Ok(())
// }
Ok(())
}

View File

@ -1,277 +1,372 @@
// pub async fn strategist_002(
// alldata: &AllData,
// ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// // print rt_price for debugging
// // let a = alldata.rt_price_30m_vec.iter().position(|a| a.0 == "BTCUSDT");
// // println!("BTCUSDT: {:?}", alldata.rt_price_30m_vec[a.unwrap()].1.last().unwrap());
use crate::value_estimation_team::indicators::bollingerband::bollingerband;
// // 1st filtering: making basic form
// let mut filtered_2nd_symbols: Vec<(String, i64)> = Vec::new();
// let mut filtered_2nd_symbols_arc: Arc<Mutex<Vec<(String, i64)>>> =
// Arc::new(Mutex::new(filtered_2nd_symbols)); // (symbol, closetime)
// let mut task_vec = Vec::new();
// let valid_symbol_vec_c = alldata.valid_symbol_vec.clone();
// for symbol in valid_symbol_vec_c {
// let mut opclo_30m_vec: Vec<RealtimePriceData> = Vec::new();
use super::{
dec, decimal_add, decimal_sub, decimal_div, ema, exists_record, insert_pre_suggested_coins,
limit_order_sell, rsi, select_filled_buy_orders, stoch_rsi, supertrend, try_join_all, AllData,
Arc, Client, ClientBuilder, Decimal, EmaData, ExchangeInfo, FilteredData, Mutex,
RealtimePriceData, RoundingStrategy, RsiData, StochRsiData, SupertrendData, TradeFee, update_record3, adx, AdxData, server_epoch, MacdData, ema_macd,
BollingerBandData, ToPrimitive
};
// let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
// let filtered_2nd_symbols_arc_c = Arc::clone(&filtered_2nd_symbols_arc);
// task_vec.push(tokio::spawn(async move {
// let opclo_30m_option = rt_price_30m_vec_c.iter().position(|x| *x.0 == symbol);
// BB lowerband + SuperTrend + StochRSI
// SuperTrend length: 20, multiplier: 1.5, BUY signal
// ADX(10, 10) < 25.0
pub async fn list_up_for_buy(
alldata: AllData,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// print rt_price for debugging
// let a = alldata.rt_price_30m_vec.iter().position(|a| a.0 == "BTCUSDT");
// println!("BTCUSDT: {:?}", alldata.rt_price_30m_vec[a.unwrap()].1.last().unwrap());
// if opclo_30m_option.is_some() {
// opclo_30m_vec = rt_price_30m_vec_c[opclo_30m_option.unwrap()].1.clone();
// 1st filtering: lookup tables if the tradepair is already there
let inspect_table_name_1 = String::from("buy_ordered_coin_list");
let inspect_table_name_2 = String::from("sell_ordered_coin_list");
let inspect_table_name_3 = String::from("pre_suggested_coin_list");
let inspect_table_name_4 = String::from("suggested_coin_list");
// if opclo_30m_vec.len() >= 3 {
// let mut filtered_2nd_symbols_lock =
// filtered_2nd_symbols_arc_c.lock().await;
// filtered_2nd_symbols_lock
// .push((symbol.clone(), opclo_30m_vec.last().unwrap().close_time));
// }
// }
// }));
// }
// try_join_all(task_vec).await?;
let mut filtered_data_1st: Vec<FilteredData> = Vec::new();
let mut filtered_data_1st_arc: Arc<Mutex<Vec<FilteredData>>> =
Arc::new(Mutex::new(filtered_data_1st));
let mut task_vec = Vec::new();
// // 2nd filtering: lookup tables if the tradepair is already there
// let inspect_table_name_1 = String::from("buy_ordered_coin_list");
// let inspect_table_name_2 = String::from("sell_ordered_coin_list");
// let inspect_table_name_3 = String::from("pre_suggested_coin_list");
// let inspect_table_name_4 = String::from("suggested_coin_list");
for symbol in &alldata.valid_symbol_vec {
let mut exists_condition_build = String::from("symbol=\'");
exists_condition_build.push_str(symbol.as_str());
exists_condition_build.push_str("\' AND registerer=");
exists_condition_build.push_str(2.to_string().as_str());
// exists_condition_build.push_str("\' AND close_time=");
// exists_condition_build.push_str(element.1.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();
let inspect_table_name_2_c = inspect_table_name_2.clone();
let inspect_table_name_3_c = inspect_table_name_3.clone();
let inspect_table_name_4_c = inspect_table_name_4.clone();
let symbol_c = symbol.clone();
let filtered_data_1st_arc_c = Arc::clone(&filtered_data_1st_arc);
task_vec.push(tokio::spawn(async move {
let mut filtered_data = FilteredData::new();
let inspect_result_1 =
exists_record(&inspect_table_name_1_c, &exists_condition_c).await;
let inspect_result_2 =
exists_record(&inspect_table_name_2_c, &exists_condition_c).await;
let inspect_result_3 =
exists_record(&inspect_table_name_3_c, &exists_condition_c).await;
let inspect_result_4 =
exists_record(&inspect_table_name_4_c, &exists_condition_c).await;
// let mut filtered_3rd_symbols: Vec<(String, i64)> = Vec::new();
// let mut filtered_3rd_symbols_arc: Arc<Mutex<Vec<(String, i64)>>> =
// Arc::new(Mutex::new(filtered_3rd_symbols)); // (symbol, closetime)
// let mut task_vec = Vec::new();
if inspect_result_1 == false
&& inspect_result_2 == false
&& inspect_result_3 == false
&& inspect_result_4 == false
{
let mut filtered_data_1st_lock = filtered_data_1st_arc_c.lock().await;
// let filtered_2nd_iter = filtered_2nd_symbols_arc.lock().await.clone().into_iter();
// for element in filtered_2nd_iter {
// let mut exists_condition_build = String::from("symbol=\'");
// exists_condition_build.push_str(element.0.as_str());
// exists_condition_build.push_str("\' AND registerer=");
// exists_condition_build.push_str(2.to_string().as_str());
// // exists_condition_build.push_str("\' AND close_time=");
// // exists_condition_build.push_str(element.1.to_string().as_str());
filtered_data.symbol = symbol_c;
// 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();
// let inspect_table_name_2_c = inspect_table_name_2.clone();
// let inspect_table_name_3_c = inspect_table_name_3.clone();
// let inspect_table_name_4_c = inspect_table_name_4.clone();
// let element_c = element.clone();
// let filtered_3rd_symbols_arc_c = Arc::clone(&filtered_3rd_symbols_arc);
// task_vec.push(tokio::spawn(async move {
// let inspect_result_1 =
// exists_record(&inspect_table_name_1_c, &exists_condition_c).await;
// let inspect_result_2 =
// exists_record(&inspect_table_name_2_c, &exists_condition_c).await;
// let inspect_result_3 =
// exists_record(&inspect_table_name_3_c, &exists_condition_c).await;
// let inspect_result_4 =
// exists_record(&inspect_table_name_4_c, &exists_condition_c).await;
filtered_data_1st_lock.push(filtered_data);
}
}));
}
try_join_all(task_vec).await?;
// if inspect_result_1 == false
// && inspect_result_2 == false
// && inspect_result_3 == false
// && inspect_result_4 == false
// {
// let mut filtered_3rd_symbols_lock = filtered_3rd_symbols_arc_c.lock().await;
// filtered_3rd_symbols_lock.push(element_c);
// }
// }));
// }
// try_join_all(task_vec).await?;
// 2nd filtering: BollingerBand (len:10, multiplier 2.5) previous_30m_price (close or low price) < lower_band
let filtered_data_1st = filtered_data_1st_arc.lock().await.clone();
let mut filtered_data_2nd: Vec<FilteredData> = Vec::new();
let mut filtered_data_2nd_arc: Arc<Mutex<Vec<FilteredData>>> =
Arc::new(Mutex::new(filtered_data_2nd));
let mut task_vec = Vec::new();
let bollingerbands = bollingerband(10, 2.5, &alldata.rt_price_30m_vec, &filtered_data_1st).await?;
for element in filtered_data_1st {
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_2nd_arc_c = Arc::clone(&filtered_data_2nd_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);
// // 3rd filtering: EMA30 > EMA 150
// let filtered_3rd_symbols_c = filtered_3rd_symbols_arc.lock().await.clone();
// let ema30_30m_data: Vec<(String, Vec<EmaData>)> = ema(
// 30,
// &alldata.rt_price_30m_vec,
// &filtered_3rd_symbols_c,
// )
// .await?;
// let ema150_30m_data: Vec<(String, Vec<EmaData>)> = ema(
// 150,
// &alldata.rt_price_30m_vec,
// &filtered_3rd_symbols_c,
// )
// .await?;
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() >= 3 && bb_vec.len() >= 3 && 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()].lowerband > rt_30m_vec[rt_30m_vec.len()-1].close_price
{
let mut filtered_data_2nd_lock = filtered_data_2nd_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_2nd_lock.push(filtered_data);
}
}
}
}
}));
}
try_join_all(task_vec).await?;
// let mut task_vec = Vec::new();
// let mut filtered_4th_symbols: Vec<(String, i64)> = Vec::new();
// let mut filtered_4th_symbols_arc: Arc<Mutex<Vec<(String, i64)>>> =
// Arc::new(Mutex::new(filtered_4th_symbols)); // (symbol, closetime)
// for element in filtered_3rd_symbols_c {
// let mut ema30_30m_vec: Vec<EmaData> = Vec::new();
// let mut ema150_30m_vec: Vec<EmaData> = Vec::new();
// let ema30_30m_option = ema30_30m_data.iter().position(|x| *x.0 == element.0);
// let ema30_30m_option_c = ema30_30m_option.clone();
// let ema150_30m_option = ema150_30m_data.iter().position(|x| *x.0 == element.0);
// let ema150_30m_option_c = ema150_30m_option.clone();
// let element_c = element.clone();
// let filtered_4th_symbols_arc_c = Arc::clone(&filtered_4th_symbols_arc);
// 3rd filtering: supertrend(ATR period 7, multiplier: 1.5, 30m close price), area should be DOWN
let filtered_data_2nd = filtered_data_2nd_arc.lock().await.clone();
let mut filtered_data_3rd: Vec<FilteredData> = Vec::new();
let mut filtered_data_3rd_arc: Arc<Mutex<Vec<FilteredData>>> =
Arc::new(Mutex::new(filtered_data_3rd));
let mut task_vec = Vec::new();
// if ema30_30m_option_c.is_some() && ema150_30m_option_c.is_some() {
// ema30_30m_vec = ema30_30m_data[ema30_30m_option.unwrap()].1.clone();
// ema150_30m_vec = ema150_30m_data[ema150_30m_option.unwrap()].1.clone();
for element in filtered_data_2nd {
let mut rt_30m_vec: Vec<RealtimePriceData> = Vec::new();
let mut supertrend_vec: Vec<SupertrendData> = Vec::new();
let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
let filtered_data_3rd_arc_c = Arc::clone(&filtered_data_3rd_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 supertrend_option_30m =
supertrend(&element.symbol, &rt_price_30m_vec_c, 7, 1.5, true).await;
// if ema30_30m_vec.len() >= 10 && ema150_30m_vec.len() >= 10 {
// let ema30_30m_vec_c = ema30_30m_vec.clone();
// let ema150_30m_vec_c = ema150_30m_vec.clone();
if rt_30m_option.is_some() && supertrend_option_30m.is_some() {
rt_30m_vec = rt_price_30m_vec_c[rt_30m_option.unwrap()].1.clone();
supertrend_vec = supertrend_option_30m.unwrap();
let server_epoch = server_epoch().await;
if rt_30m_vec.len() >= 3 && supertrend_vec.len() >= 3 && rt_30m_vec.last().unwrap().close_time > server_epoch {
let supertrend_search_result = supertrend_vec.binary_search_by_key(
&rt_30m_vec.last().unwrap().close_time,
|SupertrendData {
band_value,
signal,
area,
close_time,
}| *close_time,
);
if supertrend_search_result.is_ok() {
if supertrend_vec[supertrend_search_result.unwrap()].area.contains("DOWN")
&& supertrend_vec[supertrend_search_result.unwrap()].band_value > element.current_price.to_f64().unwrap()
{
let mut filtered_data_3rd_lock = filtered_data_3rd_arc_c.lock().await;
let mut filtered_data = FilteredData::new();
filtered_data.symbol = element.symbol.clone();
filtered_data.closetime = element.closetime;
filtered_data.current_price = element.current_price;
filtered_data.target_price = rust_decimal::prelude::FromPrimitive::from_f64(supertrend_vec[supertrend_search_result.unwrap()].band_value).unwrap();
let stop_loss = decimal_sub(filtered_data.current_price, decimal_div(decimal_sub(filtered_data.target_price, filtered_data.current_price), dec!(2)));
filtered_data.stoploss = stop_loss;
filtered_data_3rd_lock.push(filtered_data);
}
}
}
}
}));
}
try_join_all(task_vec).await?;
// task_vec.push(tokio::spawn(async move {
// let ema30_search_result = ema30_30m_vec_c.binary_search_by_key(
// &element_c.1,
// |&EmaData {
// ema_value,
// close_time,
// }| close_time,
// );
// let ema150_search_result = ema150_30m_vec_c.binary_search_by_key(
// &element_c.1,
// |&EmaData {
// ema_value,
// close_time,
// }| close_time,
// );
// 4th filtering: the latest 5 30m candle close prices > EMA 200
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 ema_vec = ema(200, &alldata.rt_price_30m_vec, &filtered_data_3rd).await?;
for element in filtered_data_3rd {
let mut opclo_30m_vec = alldata.rt_price_30m_vec.clone();
let mut supertrend_vec: Vec<SupertrendData> = Vec::new();
let ema_vec_c = ema_vec.clone();
let filtered_data_4th_arc_c = Arc::clone(&filtered_data_4th_arc);
task_vec.push(tokio::spawn(async move {
let ema_search_result = ema_vec_c.iter().position(|x| x.0 == element.symbol);
let candle_search_result = opclo_30m_vec.iter().position(|x| x.0 == element.symbol);
if ema_search_result.is_some() && candle_search_result.is_some() {
let search_result = ema_vec_c[ema_search_result.unwrap()].1.binary_search_by_key(
&opclo_30m_vec[candle_search_result.unwrap()].1.last().unwrap().close_time,
|EmaData {
ema_value,
close_time,
}| *close_time);
if search_result.is_ok_and(|x| ema_vec_c[ema_search_result.unwrap()].1[search_result.unwrap()].ema_value < opclo_30m_vec[candle_search_result.unwrap()].1[opclo_30m_vec[candle_search_result.unwrap()].1.len()-1].close_price) &&
search_result.is_ok_and(|x| ema_vec_c[ema_search_result.unwrap()].1[search_result.unwrap()-1].ema_value < opclo_30m_vec[candle_search_result.unwrap()].1[opclo_30m_vec[candle_search_result.unwrap()].1.len()-2].close_price) &&
search_result.is_ok_and(|x| ema_vec_c[ema_search_result.unwrap()].1[search_result.unwrap()-2].ema_value < opclo_30m_vec[candle_search_result.unwrap()].1[opclo_30m_vec[candle_search_result.unwrap()].1.len()-3].close_price) &&
search_result.is_ok_and(|x| ema_vec_c[ema_search_result.unwrap()].1[search_result.unwrap()-3].ema_value < opclo_30m_vec[candle_search_result.unwrap()].1[opclo_30m_vec[candle_search_result.unwrap()].1.len()-4].close_price) &&
search_result.is_ok_and(|x| ema_vec_c[ema_search_result.unwrap()].1[search_result.unwrap()-4].ema_value < opclo_30m_vec[candle_search_result.unwrap()].1[opclo_30m_vec[candle_search_result.unwrap()].1.len()-5].close_price) {
let mut filtered_4th_symbols_lock =
filtered_data_4th_arc_c.lock().await;
let mut filtered_data = FilteredData::new();
filtered_data.symbol = element.symbol.clone();
filtered_data.closetime = element.closetime;
filtered_data.current_price = element.current_price;
filtered_data.stoploss = element.stoploss;
filtered_data.target_price = element.target_price;
// if ema30_search_result.is_ok() && ema150_search_result.is_ok() {
// if ema30_30m_vec_c[ema30_search_result.unwrap()-3].ema_value > ema150_30m_vec_c[ema150_search_result.unwrap()-3].ema_value &&
// ema30_30m_vec_c[ema30_search_result.unwrap()-2].ema_value > ema150_30m_vec_c[ema150_search_result.unwrap()-2].ema_value &&
// ema30_30m_vec_c[ema30_search_result.unwrap()-1].ema_value > ema150_30m_vec_c[ema150_search_result.unwrap()-1].ema_value &&
// ema30_30m_vec_c[ema30_search_result.unwrap()].ema_value > ema150_30m_vec_c[ema150_search_result.unwrap()].ema_value &&
// ema30_30m_vec_c[ema30_search_result.unwrap()].ema_value > ema30_30m_vec_c[ema30_search_result.unwrap()-1].ema_value &&
// ema30_30m_vec_c[ema30_search_result.unwrap()-1].ema_value > ema30_30m_vec_c[ema30_search_result.unwrap()-2].ema_value &&
// ema30_30m_vec_c[ema30_search_result.unwrap()-2].ema_value > ema30_30m_vec_c[ema30_search_result.unwrap()-3].ema_value
// {
// let mut filtered_4th_symbols_lock =
// filtered_4th_symbols_arc_c.lock().await;
// filtered_4th_symbols_lock.push(element_c);
// }
// }
// }));
// }
// }
// }
// try_join_all(task_vec).await?;
filtered_4th_symbols_lock.push(filtered_data);
}
}
}));
}
try_join_all(task_vec).await?;
// 5th filtering: 0.5% <= the average amplitude of the latest 10 30m candles <= 1.0%
let filtered_data_4th_c = filtered_data_4th_arc.lock().await.clone();
let mut filtered_data_5th: Vec<FilteredData> = Vec::new();
let mut filtered_data_5th_arc: Arc<Mutex<Vec<FilteredData>>> =
Arc::new(Mutex::new(filtered_data_5th));
let mut task_vec = Vec::new();
for element in filtered_data_4th_c {
let mut supertrend_vec: Vec<SupertrendData> = Vec::new();
let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
let filtered_data_5th_arc_c = Arc::clone(&filtered_data_5th_arc);
// // 6th filtering: StochRSI (RSI length: 10, Stoch length: 10, smooth k: 3, smooth d: 3) smooth dn == 0, kn == 0, kn-1 < 5
// let filtered_4th_symbol_c = filtered_4th_symbols_arc.lock().await.clone();
// let mut rsi10_30m_data: Vec<(String, Vec<RsiData>)> = rsi(
// 10,
// &alldata.rt_price_30m_vec,
// &filtered_4th_symbol_c,
// )
// .await?;
// let stoch_rsi_data = stoch_rsi(&rsi10_30m_data, 10, 3, 3).await?;
// let mut stoch_rsi10_30m_vec: Vec<StochRsiData> = Vec::new();
// let mut filtered_5th_symbols: Vec<(String, i64)> = Vec::new(); // (symbol, closetime)
// for element in filtered_4th_symbol_c {
// let stoch_rsi10_30m_option = stoch_rsi_data.iter().position(|x| *x.0 == element.0);
task_vec.push(tokio::spawn(async move {
let position_idx = rt_price_30m_vec_c.iter().position(|elem| elem.0 == element.symbol);
if position_idx.is_some() {
let vec_len = rt_price_30m_vec_c[position_idx.unwrap()].1.len();
if vec_len >= 11 {
let candles = rt_price_30m_vec_c[position_idx.unwrap()].1.get(vec_len-12..vec_len-1).unwrap();
let windows = candles.windows(2);
let mut average_amplitude = 0.0;
// if stoch_rsi10_30m_option.is_some() {
// stoch_rsi10_30m_vec = stoch_rsi_data[stoch_rsi10_30m_option.unwrap()].1.clone();
for window in windows {
average_amplitude += (window.last().unwrap().high_price - window.last().unwrap().low_price) / window.first().unwrap().close_price;
}
average_amplitude /= 10.0;
// if stoch_rsi10_30m_vec.len() >= 3 {
// let stoch_rsi_search_result = stoch_rsi10_30m_vec.binary_search_by_key(
// &element.1,
// |&StochRsiData {
// k,
// d,
// close_time,
// }| close_time,
// );
// if stoch_rsi_search_result.is_ok() {
// if stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap()].k <= 5.0 &&
// stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap()].d <= 5.0 &&
// stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap()-1].k <= 5.0 {
// filtered_5th_symbols.push(element);
// }
// }
// }
// }
// }
if 0.005 <= average_amplitude && average_amplitude <= 0.01 {
let mut filtered_data_5th_lock = filtered_data_5th_arc_c.lock().await;
let mut filtered_data = FilteredData::new();
filtered_data.symbol = element.symbol.clone();
filtered_data.closetime = element.closetime;
filtered_data.current_price = element.current_price;
filtered_data.stoploss = element.stoploss;
filtered_data.target_price = element.target_price;
filtered_data_5th_lock.push(filtered_data);
}
}
}
}));
}
try_join_all(task_vec).await?;
// // 7th filtering: heatmap volume(MA length 10, std length 10, 30m close price), the current candle should be over than high at least.
// let mut filtered_6th_symbols: Vec<(String, i64)> = Vec::new(); // (symbol, closetime)
// for element in filtered_5th_symbols {
// let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
// let opclo_30m_option = rt_price_30m_vec_c.iter().position(|x| *x.0 == element.0);
// if opclo_30m_option.is_some() {
// let opclo_30m_vec = rt_price_30m_vec_c[opclo_30m_option.unwrap()].1.clone();
let final_filtered_data = filtered_data_5th_arc.lock().await.clone();
insert_pre_suggested_coins(2, false, &final_filtered_data, &alldata).await;
// if opclo_30m_vec.len() >= 3 {
// let heatmap_volume_option =
// heatmap_volume(&element.0, &rt_price_30m_vec_c, 10, 10, 4.0, 2.5, 1.0, -0.5)
// .await;
// if heatmap_volume_option.is_some() {
// let heatmap_volume_vec = heatmap_volume_option.unwrap();
// let heatmap_search_result = heatmap_volume_vec.binary_search_by_key(
// &element.1,
// |HeatmapVolumeData {
// heatmap_value,
// heatmap_level,
// close_time,
// }| *close_time,
// );
// if heatmap_search_result.is_ok() {
// if
// // heatmap_volume_vec[heatmap_search_result.unwrap()].heatmap_level
// // == HeatMapLevel::Medium
// // ||
// heatmap_volume_vec[heatmap_search_result.unwrap()].heatmap_level
// == HeatMapLevel::High
// || heatmap_volume_vec[heatmap_search_result.unwrap()].heatmap_level
// == HeatMapLevel::ExtraHigh
// {
// filtered_6th_symbols.push(element);
// }
// }
// }
// }
// }
// }
Ok(())
}
// // final job: adding price information to filtered results
// let mut filtered_symbols: Vec<(String, i64, f64)> = Vec::new(); // (symbol, closetime, current price)
// let mut filtered_symbols_arc = Arc::new(Mutex::new(filtered_symbols));
// let mut task_vec = Vec::new();
// for element in filtered_6th_symbols {
// let mut filtered_symbols_arc_c = Arc::clone(&filtered_symbols_arc);
// let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
pub async fn list_up_for_sell(
all_data: &AllData,
exchange_info_vec: &Vec<ExchangeInfo>,
trade_fee_vec: &Vec<TradeFee>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let filled_buy_orders = select_filled_buy_orders(2).await?;
// let elememt_c = element.clone();
// task_vec.push(tokio::spawn(async move {
// let opclo_30m_option = rt_price_30m_vec_c.iter().position(|x| *x.0 == element.0);
// if opclo_30m_option.is_some() {
// if rt_price_30m_vec_c[opclo_30m_option.unwrap()]
// .1
// .last()
// .is_some()
// {
// let mut filtered_symbols_lock: tokio::sync::MutexGuard<
// '_,
// Vec<(String, i64, f64)>,
// > = filtered_symbols_arc_c.lock().await;
// filtered_symbols_lock.push((
// elememt_c.0,
// elememt_c.1,
// rt_price_30m_vec_c[opclo_30m_option.unwrap()]
// .1
// .last()
// .unwrap()
// .close_price,
// ));
// }
// }
// }));
// }
if !filled_buy_orders.is_empty() {
let client = ClientBuilder::new()
.timeout(tokio::time::Duration::from_millis(5000))
.build()
.unwrap();
let mut supertrend_vec: Vec<SupertrendData> = Vec::new();
let server_epoch = server_epoch().await;
for element in filled_buy_orders {
if element.used_usdt >= dec!(10.0) {
let lot_step_size_option = exchange_info_vec
.iter()
.position(|exchange_info| exchange_info.symbol == element.symbol);
let quote_commission_precision_option = exchange_info_vec
.iter()
.position(|exchange_info| exchange_info.symbol == element.symbol);
if lot_step_size_option.is_some()
&& quote_commission_precision_option.is_some()
{
let lot_step_size = exchange_info_vec[lot_step_size_option.unwrap()].stepsize;
let quote_commission_precision = exchange_info_vec
[quote_commission_precision_option.unwrap()]
.quote_commission_precision;
let base_qty_to_be_ordered =
element.base_qty_ordered.round_dp_with_strategy(
lot_step_size.normalize().scale(),
RoundingStrategy::ToZero,
);
// try_join_all(task_vec).await?;
if (element.is_long == 0 || element.is_long == 1)
&& !element.current_price.is_zero()
{
if element.current_price >= element.target_price
{
limit_order_sell(
&element,
element.current_price,
base_qty_to_be_ordered,
&client,
&exchange_info_vec,
&trade_fee_vec,
)
.await;
} else if element.current_price <= element.stoploss {
limit_order_sell(
&element,
element.current_price,
base_qty_to_be_ordered,
&client,
&exchange_info_vec,
&trade_fee_vec,
)
.await;
} else if server_epoch - element.transact_time > (1_800_000) * 20 {
limit_order_sell(
&element,
element.current_price,
base_qty_to_be_ordered,
&client,
&exchange_info_vec,
&trade_fee_vec,
)
.await;
}
// TODO: sell_count가 1일 때 적용하기
// else if (supertrend_vec
// .last()
// .unwrap()
// .signal
// .as_ref()
// .is_some_and(|x| x.contains("SELL"))
// || supertrend_vec.last().unwrap().area.contains("DOWN"))
// && (supertrend_vec.last().unwrap().close_time > element.close_time)
// {
// println!(
// "SELL signal selling {} {:.2}",
// element.symbol, element.pure_profit_percent
// );
// limit_order_sell(
// &element,
// element.current_price,
// base_qty_to_be_ordered,
// &client,
// &exchange_info_vec,
// &trade_fee_vec,
// )
// .await;
// }
}
}
}
}
}
// let a = filtered_symbols_arc.lock().await.clone();
// insert_pre_suggested_coins(2, false, &a, alldata).await;
// Ok(())
// }
Ok(())
}