From a147535dc77f4b6eaced754ebf3df4a9820dd2fa Mon Sep 17 00:00:00 2001 From: Sik Yoon Date: Sat, 22 Jul 2023 20:28:32 +0900 Subject: [PATCH] Apply formatter --- src/coex/strategy_team.rs | 123 ++++++++---------- .../indicators/bollingerband.rs | 37 +++--- src/value_estimation_team/indicators/sma.rs | 12 +- 3 files changed, 82 insertions(+), 90 deletions(-) diff --git a/src/coex/strategy_team.rs b/src/coex/strategy_team.rs index 3c075ed..9f9c782 100644 --- a/src/coex/strategy_team.rs +++ b/src/coex/strategy_team.rs @@ -14,15 +14,15 @@ use crate::value_estimation_team::indicators::rsi::RsiData; use crate::value_estimation_team::indicators::sma::SmaData; use crate::value_estimation_team::indicators::stoch_rsi::{StochRsiDData, StochRsiKData}; use crate::value_estimation_team::indicators::supertrend::{supertrend, SupertrendData}; -use futures::future::try_join_all; use csv::{DeserializeRecordsIter, StringRecord}; +use futures::future::try_join_all; use rust_decimal::prelude::ToPrimitive; use rust_decimal::Decimal; use serde::Deserialize; use sqlx::FromRow; use std::{cmp::Ordering, sync::Arc}; -use tokio::time::{sleep, Duration, Instant}; use tokio::sync::Mutex; +use tokio::time::{sleep, Duration, Instant}; use crate::signal_association::signal_decision::*; @@ -930,11 +930,10 @@ pub async fn execute_strategists( pub async fn execute_strategist_for_test_temp( alldata: &AllData, ) -> Result<(), Box> { - - // 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>> = Arc::new(Mutex::new(filtered_2nd_symbols)); // (symbol, closetime) + let mut filtered_2nd_symbols_arc: Arc>> = + 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 { @@ -943,33 +942,31 @@ pub async fn execute_strategist_for_test_temp( 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 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; 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(); + opclo_30m_vec = rt_price_30m_vec_c[opclo_30m_option.unwrap()].1.clone(); supertrend_vec = supertrend_option_30m.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, + 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; + 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)); } @@ -987,7 +984,8 @@ pub async fn execute_strategist_for_test_temp( let inspect_table_name_4 = String::from("suggested_coin_list"); let mut filtered_3rd_symbols: Vec<(String, i64)> = Vec::new(); - let mut filtered_3rd_symbols_arc: Arc>> = Arc::new(Mutex::new(filtered_3rd_symbols)); // (symbol, closetime) + let mut filtered_3rd_symbols_arc: Arc>> = + Arc::new(Mutex::new(filtered_3rd_symbols)); // (symbol, closetime) let mut task_vec = Vec::new(); let filtered_2nd_iter = filtered_2nd_symbols_arc.lock().await.clone().into_iter(); @@ -1005,10 +1003,14 @@ pub async fn execute_strategist_for_test_temp( 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; + 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 inspect_result_1 == false && inspect_result_2 == false @@ -1016,8 +1018,7 @@ pub async fn execute_strategist_for_test_temp( && inspect_result_4 == false { let mut filtered_3rd_symbols_lock = filtered_3rd_symbols_arc_c.lock().await; - filtered_3rd_symbols_lock - .push(element_c); + filtered_3rd_symbols_lock.push(element_c); } })); } @@ -1032,49 +1033,51 @@ pub async fn execute_strategist_for_test_temp( &filtered_3rd_symbols_mutex, ) .await?; - - let bb10_30m_data: Vec<(String, Vec)> = value_estimation_team::indicators::bollingerband::bollingerband( - 10, - 3.0, - &sma10_30m_data, - &alldata.rt_price_30m_vec, - &filtered_3rd_symbols_mutex, - ) - .await?; + + let bb10_30m_data: Vec<(String, Vec)> = + value_estimation_team::indicators::bollingerband::bollingerband( + 10, + 3.0, + &sma10_30m_data, + &alldata.rt_price_30m_vec, + &filtered_3rd_symbols_mutex, + ) + .await?; let mut task_vec = Vec::new(); let mut filtered_4th_symbols: Vec<(String, i64)> = Vec::new(); - let mut filtered_4th_symbols_arc: Arc>> = Arc::new(Mutex::new(filtered_4th_symbols)); // (symbol, closetime) + let mut filtered_4th_symbols_arc: Arc>> = + Arc::new(Mutex::new(filtered_4th_symbols)); // (symbol, closetime) for element in filtered_3rd_iter { let mut bb10_30m_vec: Vec = 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); - + if bb10_30m_option_c.is_some() { bb10_30m_vec = bb10_30m_data[bb10_30m_option_c.unwrap()].1.clone(); - + 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.price_vec) - .await - .unwrap(); + .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, + 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); + let mut filtered_4th_symbols_lock = + filtered_4th_symbols_arc_c.lock().await; + filtered_4th_symbols_lock.push(element_c); } } })); @@ -1082,7 +1085,7 @@ pub async fn execute_strategist_for_test_temp( } } try_join_all(task_vec).await?; - + // 4th filtering: RSI (length: 10, 30m close price) the current index should be lower than 30. let filtered_4th_iter = filtered_4th_symbols_arc.lock().await.clone().into_iter(); let mut rsi10_30m_data: Vec<(String, Vec)> = Vec::new(); @@ -1117,31 +1120,19 @@ pub async fn execute_strategist_for_test_temp( } } } - + // 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 { 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); + 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 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; + 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( @@ -1167,7 +1158,7 @@ pub async fn execute_strategist_for_test_temp( } } } - + // 6th filtering condition: MACD // let mut opclo_30m_vec: Vec = Vec::new(); // let mut ema3_1d_vec: &Vec = &Vec::new(); diff --git a/src/value_estimation_team/indicators/bollingerband.rs b/src/value_estimation_team/indicators/bollingerband.rs index e3d3a77..be7f69e 100644 --- a/src/value_estimation_team/indicators/bollingerband.rs +++ b/src/value_estimation_team/indicators/bollingerband.rs @@ -5,11 +5,11 @@ use crate::database_control::*; use crate::value_estimation_team::datapoints::price_data::RealtimePriceData; use crate::value_estimation_team::indicators::sma::SmaData; use csv::{DeserializeRecordsIter, StringRecord}; +use futures::future::try_join_all; use serde::Deserialize; use sqlx::FromRow; -use tokio::{fs::*, io::AsyncWriteExt, time::*, sync::Mutex}; -use futures::future::try_join_all; -use std::{sync::Arc}; +use std::sync::Arc; +use tokio::{fs::*, io::AsyncWriteExt, sync::Mutex, time::*}; #[derive(Clone, Debug)] pub struct BollingerBandData { @@ -33,7 +33,7 @@ impl BollingerBandData { // Binance Bollingerband (SMA) pub async fn bollingerband( - period: usize, // this value should be same as moving size of sma + period: usize, // this value should be same as moving size of sma sd_factor: f64, input_sma_data: &Vec<(String, Vec)>, input_rt_data: &Vec<(String, Vec)>, @@ -75,27 +75,27 @@ pub async fn bollingerband( let result = read_rt_data_vec_c[rt_index].1.binary_search_by_key( &read_sma_data_vec_c[sma_index].1.first().unwrap().close_time, |RealtimePriceData { - opclo_price, - open_price, - close_price, - high_price, - low_price, - close_time, - quote_asset_volume, - candle_type, - }| *close_time, + opclo_price, + open_price, + close_price, + high_price, + low_price, + close_time, + quote_asset_volume, + candle_type, + }| *close_time, ); - + match result { Ok(T) => { if T <= period - 1 { let mut read_data_iter = - read_sma_data_vec_c[sma_index].1.iter(); + read_sma_data_vec_c[sma_index].1.iter(); for _ in T..period - 1 { read_data_iter.next(); } let window_iter = - read_rt_data_vec_c[rt_index].1.windows(period); + read_rt_data_vec_c[rt_index].1.windows(period); for buffer in window_iter { let mut sd_mean = 0.0; let mut standard_deviation = 0.0; @@ -128,7 +128,8 @@ pub async fn bollingerband( Err(E) => {} } let mut bb_data_wrapper_lock = bb_data_wrapper_arc_c.lock().await; - bb_data_wrapper_lock.push((symbol_c.0.clone(), bb_data_vec.clone())); + bb_data_wrapper_lock + .push((symbol_c.0.clone(), bb_data_vec.clone())); } })); } @@ -139,6 +140,6 @@ pub async fn bollingerband( } } try_join_all(task_vec).await?; - let a = bb_data_wrapper_arc.lock().await.to_owned(); + let a = bb_data_wrapper_arc.lock().await.to_owned(); Ok(a) } diff --git a/src/value_estimation_team/indicators/sma.rs b/src/value_estimation_team/indicators/sma.rs index de74946..4a53527 100644 --- a/src/value_estimation_team/indicators/sma.rs +++ b/src/value_estimation_team/indicators/sma.rs @@ -4,11 +4,11 @@ use crate::database_control::*; use crate::value_estimation_team::datapoints::price_data::RealtimePriceData; use csv::{DeserializeRecordsIter, StringRecord}; +use futures::future::try_join_all; use serde::Deserialize; use sqlx::FromRow; -use tokio::{fs::*, io::AsyncWriteExt, time::*, sync::Mutex}; -use futures::future::try_join_all; -use std::{sync::Arc}; +use std::sync::Arc; +use tokio::{fs::*, io::AsyncWriteExt, sync::Mutex, time::*}; #[derive(Clone, Debug)] pub struct SmaData { @@ -36,7 +36,7 @@ pub async fn sma( } let mut sma_data_wrapper: Vec<(String, Vec)> = Vec::new(); - let mut sma_data_wrapper_arc = Arc::new(Mutex::new(sma_data_wrapper)); + let mut sma_data_wrapper_arc = Arc::new(Mutex::new(sma_data_wrapper)); // let mut sma_data_vec: Vec = Vec::new(); // let mut sma_data_vec_arc = Arc::new(sma_data_vec); @@ -45,7 +45,7 @@ pub async fn sma( let symbol_search_result = input_rt_data.iter().position(|x| x.0 == *symbol.0); // let sma_data_vec_arc_c = Arc::clone(&sma_data_vec_arc); // sma_data_vec_arc_c.clear(); - + match symbol_search_result { Some(T) => { let sma_data_wrapper_arc_c = Arc::clone(&sma_data_wrapper_arc); @@ -80,6 +80,6 @@ pub async fn sma( } } try_join_all(task_vec).await?; - let a = sma_data_wrapper_arc.lock().await.to_owned(); + let a = sma_data_wrapper_arc.lock().await.to_owned(); Ok(a) }