Add new filtering based on StochRSI into strategy1

This commit is contained in:
Sik Yoon 2023-08-15 02:02:14 +09:00
parent 9f6678c69d
commit 4823b01d09

View File

@ -106,7 +106,7 @@ pub async fn execute_strategists(
all_data: &AllData,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
strategist_001(all_data).await?;
strategist_002(all_data).await?;
// strategist_002(all_data).await?;
Ok(())
}
@ -275,42 +275,85 @@ pub async fn strategist_001(
// 4th filtering: RSI (length: 10, 30m close price) the current index should be lower than 28.
let filtered_4th_symbol_c = filtered_4th_symbols_arc.lock().await.clone();
let mut rsi10_30m_data: Vec<(String, Vec<RsiData>)> = Vec::new();
rsi(
let mut rsi10_30m_data: Vec<(String, Vec<RsiData>)> = rsi(
10,
&alldata.rt_price_30m_vec,
&mut rsi10_30m_data,
&filtered_4th_symbol_c,
)
.await?;
let mut rsi10_30m_vec: Vec<RsiData> = Vec::new();
let mut task_vec = Vec::new();
let mut filtered_5th_symbols: Vec<(String, i64)> = Vec::new(); // (symbol, closetime)
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_symbol_c {
let rsi10_30m_option = rsi10_30m_data.iter().position(|x| *x.0 == element.0);
let filtered_5th_symbols_arc_c = Arc::clone(&filtered_5th_symbols_arc);
if rsi10_30m_option.is_some() {
rsi10_30m_vec = rsi10_30m_data[rsi10_30m_option.unwrap()].1.clone();
let mut rsi10_30m_vec = rsi10_30m_data[rsi10_30m_option.unwrap()].1.clone();
if rsi10_30m_vec.len() >= 3 {
let rsi_search_result = rsi10_30m_vec.binary_search_by_key(
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 <= 28.0 {
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?;
// 5th filtering: StochRSI (RSI length: 14, Stoch length: 14, smooth k: 3, smooth d: 3) smooth k should be lower than 20.
let filtered_5th_symbol_c = filtered_5th_symbols_arc.lock().await.clone();
let mut rsi14_30m_data: Vec<(String, Vec<RsiData>)> = rsi(
14,
&alldata.rt_price_30m_vec,
&filtered_5th_symbol_c,
)
.await?;
let stoch_rsi_data = stoch_rsi(&rsi10_30m_data, 14, 3, 3).await?;
let mut stoch_rsi14_30m_vec: Vec<StochRsiData> = Vec::new();
let mut filtered_6th_symbols: Vec<(String, i64)> = Vec::new(); // (symbol, closetime)
for element in filtered_5th_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,
|&RsiData {
rsi_value,
|&StochRsiData {
k,
d,
close_time,
}| close_time,
);
if rsi_search_result.is_ok() {
if rsi10_30m_vec[rsi_search_result.unwrap()].rsi_value <= 28.0 {
filtered_5th_symbols.push(element);
if stoch_rsi_search_result.is_ok() {
if stoch_rsi14_30m_vec[stoch_rsi_search_result.unwrap()].k <= 20.0 {
filtered_6th_symbols.push(element);
}
}
}
}
}
// 5th 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 {
// 6th 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_7th_symbols: Vec<(String, i64)> = Vec::new(); // (symbol, closetime)
for element in filtered_6th_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() {
@ -338,7 +381,7 @@ pub async fn strategist_001(
|| heatmap_volume_vec[heatmap_search_result.unwrap()].heatmap_level
== HeatMapLevel::ExtraHigh
{
filtered_6th_symbols.push(element);
filtered_7th_symbols.push(element);
}
}
}
@ -350,7 +393,7 @@ pub async fn strategist_001(
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 {
for element in filtered_7th_symbols {
let mut filtered_symbols_arc_c = Arc::clone(&filtered_symbols_arc);
let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
@ -420,30 +463,25 @@ pub async fn strategist_002(
) -> 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");
let b = alldata.rt_price_30m_vec.iter().position(|a| a.0 == "XRPUSDT");
// println!("BTCUSDT: {:?}", alldata.rt_price_30m_vec[a.unwrap()].1.last().unwrap());
// 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();
filtered_2nd_symbols.push((String::from("BTCUSDT"), alldata.rt_price_30m_vec[a.unwrap()].1.last().unwrap().close_time));
filtered_2nd_symbols.push((String::from("XRPUSDT"), alldata.rt_price_30m_vec[b.unwrap()].1.last().unwrap().close_time));
// 4th filtering: RSI (length: 10, 30m close price) the current index should be lower than 28.
let mut rsi10_30m_data: Vec<(String, Vec<RsiData>)> = Vec::new();
rsi(
10,
let mut rsi10_30m_data: Vec<(String, Vec<RsiData>)> = rsi(
14,
&alldata.rt_price_30m_vec,
&mut rsi10_30m_data,
&filtered_2nd_symbols,
)
.await?;
let stoch_rsi = stoch_rsi(&rsi10_30m_data, 10, 3, 3).await;
let mut a = stoch_rsi.unwrap();
a.reverse();
a.truncate(30);
a.reverse();
println!("BTCUSDT StochRsi: {:?}", a);
let stoch_rsi = stoch_rsi(&rsi10_30m_data, 14, 3, 3).await?;
// for element in filtered_2nd_symbols {
// let rsi10_30m_option = rsi10_30m_data.iter().position(|x| *x.0 == element.0);