Apply formatter
This commit is contained in:
parent
8d74ec64de
commit
16440be796
|
|
@ -636,6 +636,13 @@ async fn update_repeat_task(
|
||||||
),
|
),
|
||||||
decimal_sub(dec!(1), trade_fee),
|
decimal_sub(dec!(1), trade_fee),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// TODO: sell_count >=1 이면 expected_get_usdt 는 한번만 tradefee만 적용하여 업데이트 할 것. 현재는 수수료를 2번 (매수,매도)를 계산함. 아래 변수에 든 값으로 업데이트 하면 됨
|
||||||
|
// let expected_get_usdt =
|
||||||
|
// decimal_mul(base_qty_to_be_ordered, price).round_dp_with_strategy(
|
||||||
|
// quote_commission_precision,
|
||||||
|
// RoundingStrategy::ToZero,
|
||||||
|
// );
|
||||||
let pure_profit_percent = ((expected_get_usdt.to_f64().unwrap()
|
let pure_profit_percent = ((expected_get_usdt.to_f64().unwrap()
|
||||||
/ element.used_usdt.to_f64().unwrap())
|
/ element.used_usdt.to_f64().unwrap())
|
||||||
- 1.0)
|
- 1.0)
|
||||||
|
|
@ -725,7 +732,8 @@ pub async fn limit_order_sell(
|
||||||
.iter()
|
.iter()
|
||||||
.position(|trade_fee| trade_fee.symbol == buy_ordered_coin.symbol);
|
.position(|trade_fee| trade_fee.symbol == buy_ordered_coin.symbol);
|
||||||
if quote_asset_precision_option.is_some() && trade_fee_option.is_some() {
|
if quote_asset_precision_option.is_some() && trade_fee_option.is_some() {
|
||||||
let quote_asset_precision = exchange_info_vec[quote_asset_precision_option.unwrap()].quote_asset_precision;
|
let quote_asset_precision =
|
||||||
|
exchange_info_vec[quote_asset_precision_option.unwrap()].quote_asset_precision;
|
||||||
let trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
let trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
||||||
|
|
||||||
let get_usdt = decimal_mul(sell_base_quantity, sell_base_price)
|
let get_usdt = decimal_mul(sell_base_quantity, sell_base_price)
|
||||||
|
|
@ -837,7 +845,9 @@ pub async fn limit_order_sell(
|
||||||
.iter()
|
.iter()
|
||||||
.position(|trade_fee| trade_fee.symbol == buy_ordered_coin.symbol);
|
.position(|trade_fee| trade_fee.symbol == buy_ordered_coin.symbol);
|
||||||
if quote_asset_precision_option.is_some() && trade_fee_option.is_some() {
|
if quote_asset_precision_option.is_some() && trade_fee_option.is_some() {
|
||||||
let quote_asset_precision = exchange_info_vec[quote_asset_precision_option.unwrap()].quote_asset_precision;
|
let quote_asset_precision = exchange_info_vec
|
||||||
|
[quote_asset_precision_option.unwrap()]
|
||||||
|
.quote_asset_precision;
|
||||||
let trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
let trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
||||||
let get_usdt = rust_decimal::prelude::FromStr::from_str(
|
let get_usdt = rust_decimal::prelude::FromStr::from_str(
|
||||||
T.get("cummulativeQuoteQty").unwrap().as_str().unwrap(),
|
T.get("cummulativeQuoteQty").unwrap().as_str().unwrap(),
|
||||||
|
|
@ -871,7 +881,8 @@ pub async fn limit_order_sell(
|
||||||
quote_asset_precision,
|
quote_asset_precision,
|
||||||
RoundingStrategy::ToZero,
|
RoundingStrategy::ToZero,
|
||||||
);
|
);
|
||||||
insert_value_container.push(sell_price.to_string()); // sell_price
|
insert_value_container.push(sell_price.to_string());
|
||||||
|
// sell_price
|
||||||
} else {
|
} else {
|
||||||
insert_value_container.push(0.0.to_string()); // sell_price
|
insert_value_container.push(0.0.to_string()); // sell_price
|
||||||
}
|
}
|
||||||
|
|
@ -1194,7 +1205,8 @@ pub async fn cancel_buy_order(
|
||||||
.position(|exchange_info| exchange_info.symbol == order.symbol);
|
.position(|exchange_info| exchange_info.symbol == order.symbol);
|
||||||
|
|
||||||
if trade_fee_option.is_some() && base_asset_precision_option.is_some() {
|
if trade_fee_option.is_some() && base_asset_precision_option.is_some() {
|
||||||
let trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
let trade_fee =
|
||||||
|
trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
||||||
|
|
||||||
let base_qty_ordered = rust_decimal::prelude::FromStr::from_str(
|
let base_qty_ordered = rust_decimal::prelude::FromStr::from_str(
|
||||||
T.get("executedQty").unwrap().as_str().unwrap(),
|
T.get("executedQty").unwrap().as_str().unwrap(),
|
||||||
|
|
@ -1204,7 +1216,9 @@ pub async fn cancel_buy_order(
|
||||||
let base_qty_fee_adjusted =
|
let base_qty_fee_adjusted =
|
||||||
decimal_mul(base_qty_ordered, decimal_sub(dec!(1), trade_fee));
|
decimal_mul(base_qty_ordered, decimal_sub(dec!(1), trade_fee));
|
||||||
|
|
||||||
let base_asset_precision = exchange_info_vec[base_asset_precision_option.unwrap()].base_asset_precision;
|
let base_asset_precision = exchange_info_vec
|
||||||
|
[base_asset_precision_option.unwrap()]
|
||||||
|
.base_asset_precision;
|
||||||
let buy_price = decimal_div(cummulative_quote_qty, base_qty_ordered)
|
let buy_price = decimal_div(cummulative_quote_qty, base_qty_ordered)
|
||||||
.round_dp_with_strategy(8, RoundingStrategy::ToZero);
|
.round_dp_with_strategy(8, RoundingStrategy::ToZero);
|
||||||
|
|
||||||
|
|
@ -1231,9 +1245,15 @@ pub async fn cancel_buy_order(
|
||||||
|
|
||||||
// update available_usdt
|
// update available_usdt
|
||||||
if order.used_usdt > cummulative_quote_qty {
|
if order.used_usdt > cummulative_quote_qty {
|
||||||
add_available_usdt(decimal_sub(order.used_usdt, cummulative_quote_qty));
|
add_available_usdt(decimal_sub(
|
||||||
|
order.used_usdt,
|
||||||
|
cummulative_quote_qty,
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
sub_available_usdt(decimal_sub(cummulative_quote_qty, order.used_usdt));
|
sub_available_usdt(decimal_sub(
|
||||||
|
cummulative_quote_qty,
|
||||||
|
order.used_usdt,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
println!("partially buy {}", order.symbol);
|
println!("partially buy {}", order.symbol);
|
||||||
}
|
}
|
||||||
|
|
@ -1357,7 +1377,7 @@ pub async fn cancel_sell_order(
|
||||||
insert_value_container.push(order.buy_price.to_string()); // buy_price
|
insert_value_container.push(order.buy_price.to_string()); // buy_price
|
||||||
insert_value_container.push(0.0.to_string()); // current_price
|
insert_value_container.push(0.0.to_string()); // current_price
|
||||||
insert_value_container.push(order.stoploss.to_string()); // stoploss
|
insert_value_container.push(order.stoploss.to_string()); // stoploss
|
||||||
insert_value_container.push(order.target_price.to_string()); // current_price
|
insert_value_container.push(order.target_price.to_string()); // target_price
|
||||||
insert_value_container.push(order.base_qty_ordered.to_string()); // base_qty_ordered
|
insert_value_container.push(order.base_qty_ordered.to_string()); // base_qty_ordered
|
||||||
insert_value_container.push(order.base_qty_ordered.to_string()); // base_qty_fee_adjusted
|
insert_value_container.push(order.base_qty_ordered.to_string()); // base_qty_fee_adjusted
|
||||||
insert_value_container.push(0.0.to_string()); // pure_profit_percent
|
insert_value_container.push(0.0.to_string()); // pure_profit_percent
|
||||||
|
|
@ -1377,8 +1397,11 @@ pub async fn cancel_sell_order(
|
||||||
.position(|trade_fee| trade_fee.symbol == order.symbol);
|
.position(|trade_fee| trade_fee.symbol == order.symbol);
|
||||||
|
|
||||||
if quote_asset_precision_option.is_some() && trade_fee_option.is_some() {
|
if quote_asset_precision_option.is_some() && trade_fee_option.is_some() {
|
||||||
let quote_asset_precision = exchange_info_vec[quote_asset_precision_option.unwrap()].quote_asset_precision;
|
let quote_asset_precision = exchange_info_vec
|
||||||
let trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
[quote_asset_precision_option.unwrap()]
|
||||||
|
.quote_asset_precision;
|
||||||
|
let trade_fee =
|
||||||
|
trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
||||||
if base_qty_executed == base_qty_ordered {
|
if base_qty_executed == base_qty_ordered {
|
||||||
// FILLED case
|
// FILLED case
|
||||||
// update status FILLED
|
// update status FILLED
|
||||||
|
|
@ -1423,7 +1446,8 @@ pub async fn cancel_sell_order(
|
||||||
pure_profit_percent.to_string(),
|
pure_profit_percent.to_string(),
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
let update_condition = vec![(String::from("id"), order.id.to_string())];
|
let update_condition =
|
||||||
|
vec![(String::from("id"), order.id.to_string())];
|
||||||
update_record3(&table_name, &update_values, &update_condition)
|
update_record3(&table_name, &update_values, &update_condition)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
@ -1448,7 +1472,10 @@ pub async fn cancel_sell_order(
|
||||||
RoundingStrategy::ToZero,
|
RoundingStrategy::ToZero,
|
||||||
);
|
);
|
||||||
let pure_profit_percent = decimal_mul(
|
let pure_profit_percent = decimal_mul(
|
||||||
decimal_sub(decimal_div(get_usdt_fee_adjusted, get_usdt), dec!(1)),
|
decimal_sub(
|
||||||
|
decimal_div(get_usdt_fee_adjusted, get_usdt),
|
||||||
|
dec!(1),
|
||||||
|
),
|
||||||
dec!(100),
|
dec!(100),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -1475,13 +1502,15 @@ pub async fn cancel_sell_order(
|
||||||
pure_profit_percent.to_string(),
|
pure_profit_percent.to_string(),
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
let update_condition = vec![(String::from("id"), order.id.to_string())];
|
let update_condition =
|
||||||
|
vec![(String::from("id"), order.id.to_string())];
|
||||||
update_record3(&table_name, &update_values, &update_condition)
|
update_record3(&table_name, &update_values, &update_condition)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// insert record in [buy_ordered_coin_list]
|
// insert record in [buy_ordered_coin_list]
|
||||||
let rest_base_qty = decimal_sub(base_qty_ordered, base_qty_executed);
|
let rest_base_qty =
|
||||||
|
decimal_sub(base_qty_ordered, base_qty_executed);
|
||||||
let rest_base_qty_fee_adjusted =
|
let rest_base_qty_fee_adjusted =
|
||||||
decimal_mul(rest_base_qty, decimal_sub(dec!(1), trade_fee));
|
decimal_mul(rest_base_qty, decimal_sub(dec!(1), trade_fee));
|
||||||
let mut insert_values: Vec<Vec<String>> = Vec::new();
|
let mut insert_values: Vec<Vec<String>> = Vec::new();
|
||||||
|
|
@ -1668,7 +1697,9 @@ pub async fn query_buy_order(
|
||||||
|
|
||||||
if trade_fee_option.is_some() && base_asset_precision_option.is_some() {
|
if trade_fee_option.is_some() && base_asset_precision_option.is_some() {
|
||||||
let trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
let trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
||||||
let base_asset_precision = exchange_info_vec[base_asset_precision_option.unwrap()].base_asset_precision;
|
let base_asset_precision = exchange_info_vec
|
||||||
|
[base_asset_precision_option.unwrap()]
|
||||||
|
.base_asset_precision;
|
||||||
let base_qty_ordered = rust_decimal::prelude::FromStr::from_str(
|
let base_qty_ordered = rust_decimal::prelude::FromStr::from_str(
|
||||||
T.get("executedQty").unwrap().as_str().unwrap(),
|
T.get("executedQty").unwrap().as_str().unwrap(),
|
||||||
)
|
)
|
||||||
|
|
@ -1709,13 +1740,18 @@ pub async fn query_buy_order(
|
||||||
println!("buy {}", order.symbol);
|
println!("buy {}", order.symbol);
|
||||||
// update available_usdt
|
// update available_usdt
|
||||||
if order.used_usdt > cummulative_quote_qty {
|
if order.used_usdt > cummulative_quote_qty {
|
||||||
add_available_usdt(decimal_sub(order.used_usdt, cummulative_quote_qty));
|
add_available_usdt(decimal_sub(
|
||||||
|
order.used_usdt,
|
||||||
|
cummulative_quote_qty,
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
sub_available_usdt(decimal_sub(cummulative_quote_qty, order.used_usdt));
|
sub_available_usdt(decimal_sub(
|
||||||
|
cummulative_quote_qty,
|
||||||
|
order.used_usdt,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if T.get("status").unwrap().as_str().unwrap() == "CANCELED" {
|
} else if T.get("status").unwrap().as_str().unwrap() == "CANCELED" {
|
||||||
let update_table_name = String::from("suggested_coin_list");
|
let update_table_name = String::from("suggested_coin_list");
|
||||||
let update_condition = vec![
|
let update_condition = vec![
|
||||||
|
|
@ -1790,14 +1826,20 @@ pub async fn query_sell_order(
|
||||||
.position(|trade_fee| trade_fee.symbol == order.symbol);
|
.position(|trade_fee| trade_fee.symbol == order.symbol);
|
||||||
|
|
||||||
if quote_asset_precision_option.is_some() && trade_fee_option.is_some() {
|
if quote_asset_precision_option.is_some() && trade_fee_option.is_some() {
|
||||||
let quote_asset_precision = exchange_info_vec[quote_asset_precision_option.unwrap()].quote_asset_precision;
|
let quote_asset_precision = exchange_info_vec
|
||||||
|
[quote_asset_precision_option.unwrap()]
|
||||||
|
.quote_asset_precision;
|
||||||
let trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
let trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
||||||
let get_usdt = rust_decimal::prelude::FromStr::from_str(
|
let get_usdt = rust_decimal::prelude::FromStr::from_str(
|
||||||
T.get("cummulativeQuoteQty").unwrap().as_str().unwrap(),
|
T.get("cummulativeQuoteQty").unwrap().as_str().unwrap(),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let get_usdt_fee_adjusted = decimal_mul(get_usdt, decimal_sub(dec!(1), trade_fee))
|
let get_usdt_fee_adjusted =
|
||||||
.round_dp_with_strategy(quote_asset_precision, RoundingStrategy::ToZero);
|
decimal_mul(get_usdt, decimal_sub(dec!(1), trade_fee))
|
||||||
|
.round_dp_with_strategy(
|
||||||
|
quote_asset_precision,
|
||||||
|
RoundingStrategy::ToZero,
|
||||||
|
);
|
||||||
let ordered_base_qty = rust_decimal::prelude::FromStr::from_str(
|
let ordered_base_qty = rust_decimal::prelude::FromStr::from_str(
|
||||||
T.get("executedQty").unwrap().as_str().unwrap(),
|
T.get("executedQty").unwrap().as_str().unwrap(),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ pub mod strategy_manager;
|
||||||
use crate::coex::order_team::{limit_order_sell, select_filled_buy_orders};
|
use crate::coex::order_team::{limit_order_sell, select_filled_buy_orders};
|
||||||
use crate::coin_health_check_team::request_others::{ExchangeInfo, TradeFee};
|
use crate::coin_health_check_team::request_others::{ExchangeInfo, TradeFee};
|
||||||
use crate::database_control::*;
|
use crate::database_control::*;
|
||||||
|
use crate::decimal_funcs::*;
|
||||||
use crate::value_estimation_team::datapoints::price_data::RealtimePriceData;
|
use crate::value_estimation_team::datapoints::price_data::RealtimePriceData;
|
||||||
use crate::value_estimation_team::indicators::bollingerband::{bollingerband, BollingerBandData};
|
use crate::value_estimation_team::indicators::bollingerband::{bollingerband, BollingerBandData};
|
||||||
use crate::value_estimation_team::indicators::ema::{ema, EmaData};
|
use crate::value_estimation_team::indicators::ema::{ema, EmaData};
|
||||||
|
|
@ -24,11 +25,10 @@ use futures::future::try_join_all;
|
||||||
use reqwest::{Client, ClientBuilder};
|
use reqwest::{Client, ClientBuilder};
|
||||||
use rust_decimal::{prelude::FromPrimitive, prelude::ToPrimitive, Decimal, RoundingStrategy};
|
use rust_decimal::{prelude::FromPrimitive, prelude::ToPrimitive, Decimal, RoundingStrategy};
|
||||||
use rust_decimal_macros::dec;
|
use rust_decimal_macros::dec;
|
||||||
use crate::decimal_funcs::*;
|
|
||||||
use sqlx::FromRow;
|
use sqlx::FromRow;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::Mutex;
|
|
||||||
use strategy_manager::insert_pre_suggested_coins;
|
use strategy_manager::insert_pre_suggested_coins;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct AllData {
|
pub struct AllData {
|
||||||
|
|
|
||||||
|
|
@ -1,442 +1,495 @@
|
||||||
use super::{
|
// use super::{
|
||||||
dec, ema, exists_record, limit_order_sell, rsi, select_filled_buy_orders, stoch_rsi,
|
// dec, ema, exists_record, limit_order_sell, rsi, select_filled_buy_orders, stoch_rsi,
|
||||||
supertrend, try_join_all, AllData, Arc, Client, ClientBuilder, EmaData, ExchangeInfo, Mutex,
|
// supertrend, try_join_all, AllData, Arc, Client, ClientBuilder, EmaData, ExchangeInfo, Mutex,
|
||||||
RealtimePriceData, RoundingStrategy, RsiData, StochRsiData, SupertrendData, TradeFee,
|
// RealtimePriceData, RoundingStrategy, RsiData, StochRsiData, SupertrendData, TradeFee,
|
||||||
};
|
// insert_pre_suggested_coins, Decimal, FilteredData, decimal_add, decimal_sub
|
||||||
|
// };
|
||||||
|
|
||||||
pub async fn list_up_for_buy(
|
// pub async fn list_up_for_buy(
|
||||||
alldata: &AllData,
|
// alldata: AllData,
|
||||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
// ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
// print rt_price for debugging
|
// // print rt_price for debugging
|
||||||
// let a = alldata.rt_price_30m_vec.iter().position(|a| a.0 == "BTCUSDT");
|
// // 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());
|
// // println!("BTCUSDT: {:?}", alldata.rt_price_30m_vec[a.unwrap()].1.last().unwrap());
|
||||||
|
|
||||||
// 1st filtering: supertrend(ATR period 100, multiplier: 6.0, 30m close price), the area should be in UP area.
|
// // 1st filtering: lookup tables if the tradepair is already there
|
||||||
let mut filtered_2nd_symbols: Vec<(String, i64)> = Vec::new();
|
// let inspect_table_name_1 = String::from("buy_ordered_coin_list");
|
||||||
let mut filtered_2nd_symbols_arc: Arc<Mutex<Vec<(String, i64)>>> =
|
// let inspect_table_name_2 = String::from("sell_ordered_coin_list");
|
||||||
Arc::new(Mutex::new(filtered_2nd_symbols)); // (symbol, closetime)
|
// let inspect_table_name_3 = String::from("pre_suggested_coin_list");
|
||||||
let mut task_vec = Vec::new();
|
// let inspect_table_name_4 = String::from("suggested_coin_list");
|
||||||
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, 100, 6.0, true).await;
|
|
||||||
|
|
||||||
if opclo_30m_option.is_some() && supertrend_option_30m.is_some() {
|
// let mut filtered_data_1st: Vec<FilteredData> = Vec::new();
|
||||||
opclo_30m_vec = rt_price_30m_vec_c[opclo_30m_option.unwrap()].1.clone();
|
// let mut filtered_data_1st_arc: Arc<Mutex<Vec<FilteredData>>> =
|
||||||
supertrend_vec = supertrend_option_30m.unwrap();
|
// Arc::new(Mutex::new(filtered_data_1st));
|
||||||
|
// let mut task_vec = Vec::new();
|
||||||
|
|
||||||
if opclo_30m_vec.len() >= 3 && supertrend_vec.len() >= 3 {
|
// for symbol in &alldata.valid_symbol_vec {
|
||||||
let supertrend_search_result = supertrend_vec.binary_search_by_key(
|
// let mut exists_condition_build = String::from("symbol=\'");
|
||||||
&opclo_30m_vec.last().unwrap().close_time,
|
// exists_condition_build.push_str(symbol.as_str());
|
||||||
|SupertrendData {
|
// exists_condition_build.push_str("\' AND registerer=");
|
||||||
band_value,
|
// exists_condition_build.push_str(3.to_string().as_str());
|
||||||
signal,
|
// // exists_condition_build.push_str("\' AND close_time=");
|
||||||
area,
|
// // exists_condition_build.push_str(element.1.to_string().as_str());
|
||||||
close_time,
|
// let exists_condition = Some(exists_condition_build);
|
||||||
}| *close_time,
|
// let exists_condition_c = exists_condition.clone();
|
||||||
);
|
// let inspect_table_name_1_c = inspect_table_name_1.clone();
|
||||||
if supertrend_search_result.is_ok() {
|
// let inspect_table_name_2_c = inspect_table_name_2.clone();
|
||||||
if supertrend_vec[supertrend_search_result.unwrap()]
|
// let inspect_table_name_3_c = inspect_table_name_3.clone();
|
||||||
.area
|
// let inspect_table_name_4_c = inspect_table_name_4.clone();
|
||||||
.contains("UP")
|
// let symbol_c = symbol.clone();
|
||||||
{
|
// let filtered_data_1st_arc_c = Arc::clone(&filtered_data_1st_arc);
|
||||||
let mut filtered_2nd_symbols_lock =
|
// task_vec.push(tokio::spawn(async move {
|
||||||
filtered_2nd_symbols_arc_c.lock().await;
|
// let mut filtered_data = FilteredData::new();
|
||||||
filtered_2nd_symbols_lock
|
// let inspect_result_1 =
|
||||||
.push((symbol.clone(), opclo_30m_vec.last().unwrap().close_time));
|
// 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;
|
||||||
try_join_all(task_vec).await?;
|
|
||||||
|
|
||||||
// 2nd filtering: lookup tables if the tradepair is already there
|
// if inspect_result_1 == false
|
||||||
let inspect_table_name_1 = String::from("buy_ordered_coin_list");
|
// && inspect_result_2 == false
|
||||||
let inspect_table_name_2 = String::from("sell_ordered_coin_list");
|
// && inspect_result_3 == false
|
||||||
let inspect_table_name_3 = String::from("pre_suggested_coin_list");
|
// && inspect_result_4 == false
|
||||||
let inspect_table_name_4 = String::from("suggested_coin_list");
|
// {
|
||||||
|
// let mut filtered_data_1st_lock = filtered_data_1st_arc_c.lock().await;
|
||||||
|
|
||||||
let mut filtered_3rd_symbols: Vec<(String, i64)> = Vec::new();
|
// filtered_data.symbol = symbol_c;
|
||||||
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();
|
|
||||||
|
|
||||||
let filtered_2nd_iter = filtered_2nd_symbols_arc.lock().await.clone().into_iter();
|
// filtered_data_1st_lock.push(filtered_data);
|
||||||
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=");
|
// try_join_all(task_vec).await?;
|
||||||
exists_condition_build.push_str(3.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;
|
|
||||||
|
|
||||||
if inspect_result_1 == false
|
// // 1st filtering: supertrend(ATR period 100, multiplier: 6.0, 30m close price), the area should be in UP area.
|
||||||
&& inspect_result_2 == false
|
// let mut filtered_2nd_symbols: Vec<(String, i64)> = Vec::new();
|
||||||
&& inspect_result_3 == false
|
// let mut filtered_2nd_symbols_arc: Arc<Mutex<Vec<(String, i64)>>> =
|
||||||
&& inspect_result_4 == false
|
// Arc::new(Mutex::new(filtered_2nd_symbols)); // (symbol, closetime)
|
||||||
{
|
// let mut task_vec = Vec::new();
|
||||||
let mut filtered_3rd_symbols_lock = filtered_3rd_symbols_arc_c.lock().await;
|
// let valid_symbol_vec_c = alldata.valid_symbol_vec.clone();
|
||||||
filtered_3rd_symbols_lock.push(element_c);
|
// for symbol in valid_symbol_vec_c {
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
try_join_all(task_vec).await?;
|
|
||||||
|
|
||||||
// 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?;
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
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,
|
|
||||||
);
|
|
||||||
|
|
||||||
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?;
|
|
||||||
|
|
||||||
// 4th filtering: StochRSI (RSI length: 10, Stoch length: 10, smooth k: 3, smooth d: 3) 20 > k > kn-1 > kn-2 > kn-3,
|
|
||||||
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);
|
|
||||||
|
|
||||||
if stoch_rsi10_30m_option.is_some() {
|
|
||||||
stoch_rsi10_30m_vec = stoch_rsi_data[stoch_rsi10_30m_option.unwrap()].1.clone();
|
|
||||||
|
|
||||||
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 10.0 > stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap()].k
|
|
||||||
&& stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap()].k
|
|
||||||
> stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap() - 1].k
|
|
||||||
&& stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap() - 1].k
|
|
||||||
<= stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap() - 2].k
|
|
||||||
&& stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap() - 2].k
|
|
||||||
<= stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap() - 3].k
|
|
||||||
{
|
|
||||||
filtered_5th_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_5th_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?;
|
|
||||||
|
|
||||||
// TODO: abnormal price filtering (too high current price)
|
|
||||||
|
|
||||||
// 6th filtering condition: MACD
|
|
||||||
// let mut opclo_30m_vec: Vec<RealtimePriceData> = Vec::new();
|
// let mut opclo_30m_vec: Vec<RealtimePriceData> = Vec::new();
|
||||||
// let mut ema3_1d_vec: &Vec<EmaData> = &Vec::new();
|
// let mut supertrend_vec: Vec<SupertrendData> = Vec::new();
|
||||||
// let mut ema10_1d_vec: &Vec<EmaData> = &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, 100, 6.0, true).await;
|
||||||
|
|
||||||
// let mut filtered_7th_symbols: Vec<(String, i64)> = Vec::new(); // (symbol, closetime)
|
// if opclo_30m_option.is_some() && supertrend_option_30m.is_some() {
|
||||||
// for element in filtered_6th_symbols {
|
// opclo_30m_vec = rt_price_30m_vec_c[opclo_30m_option.unwrap()].1.clone();
|
||||||
// let ema3_1d_option = alldata.ema3_1d_data.iter().position(|x| *x.0 == *element.0);
|
// supertrend_vec = supertrend_option_30m.unwrap();
|
||||||
// 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() {
|
// if opclo_30m_vec.len() >= 3 && supertrend_vec.len() >= 3 {
|
||||||
// ema3_1d_vec = &alldata.ema3_1d_data[ema3_1d_option.unwrap()].1;
|
// let supertrend_search_result = supertrend_vec.binary_search_by_key(
|
||||||
// ema10_1d_vec = &alldata.ema10_1d_data[ema10_1d_option.unwrap()].1;
|
// &opclo_30m_vec.last().unwrap().close_time,
|
||||||
|
// |SupertrendData {
|
||||||
// if ema3_1d_vec.len() > 20 && ema10_1d_vec.len() > 20 {
|
// band_value,
|
||||||
// let macd_vec = ema_macd(&ema3_1d_vec, &ema10_1d_vec, 10).await?;
|
// signal,
|
||||||
// // let macd_search_result = macd_vec.binary_search_by_key(&element.1, |&EmaMacd{macd_value, close_time}|close_time);
|
// area,
|
||||||
|
// close_time,
|
||||||
// // if macd_search_result.is_ok() {
|
// }| *close_time,
|
||||||
// if macd_vec.last().unwrap().macd_value - macd_vec[macd_vec.len() -2].macd_value >= 0.0 {
|
// );
|
||||||
// filtered_7th_symbols.push(element);
|
// if supertrend_search_result.is_ok() {
|
||||||
|
// if supertrend_vec[supertrend_search_result.unwrap()]
|
||||||
|
// .area
|
||||||
|
// .contains("UP")
|
||||||
|
// {
|
||||||
|
// 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?;
|
||||||
|
|
||||||
|
// // 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");
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
// 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(3.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;
|
||||||
|
|
||||||
|
// 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?;
|
||||||
|
|
||||||
|
// // 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?;
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
// 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,
|
||||||
|
// );
|
||||||
|
|
||||||
|
// 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?;
|
||||||
|
|
||||||
|
// // 4th filtering: StochRSI (RSI length: 10, Stoch length: 10, smooth k: 3, smooth d: 3) 20 > k > kn-1 > kn-2 > kn-3,
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// if stoch_rsi10_30m_option.is_some() {
|
||||||
|
// stoch_rsi10_30m_vec = stoch_rsi_data[stoch_rsi10_30m_option.unwrap()].1.clone();
|
||||||
|
|
||||||
|
// 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 10.0 > stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap()].k
|
||||||
|
// && stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap()].k
|
||||||
|
// > stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap() - 1].k
|
||||||
|
// && stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap() - 1].k
|
||||||
|
// <= stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap() - 2].k
|
||||||
|
// && stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap() - 2].k
|
||||||
|
// <= stoch_rsi10_30m_vec[stoch_rsi_search_result.unwrap() - 3].k
|
||||||
|
// {
|
||||||
|
// filtered_5th_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_5th_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?;
|
||||||
|
|
||||||
|
// // TODO: abnormal price filtering (too high current price)
|
||||||
|
|
||||||
|
// // 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(3, false, &a, &alldata).await;
|
||||||
|
|
||||||
|
// Ok(())
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 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(3).await?;
|
||||||
|
|
||||||
|
// if !filled_buy_orders.is_empty() {
|
||||||
|
// let client = ClientBuilder::new()
|
||||||
|
// .timeout(tokio::time::Duration::from_millis(5000))
|
||||||
|
// .build()
|
||||||
|
// .unwrap();
|
||||||
|
|
||||||
|
// let mut sell_order_count = 0;
|
||||||
|
// for element in filled_buy_orders {
|
||||||
|
// if element.used_usdt >= dec!(10.0) {
|
||||||
|
// // ignore coins having 10 usdt below because of not traded
|
||||||
|
// let sell_percent_for_uptrend = |z: f64| 0.94 * z - 0.5;
|
||||||
|
|
||||||
|
// let lot_step_size_option = exchange_info_vec
|
||||||
|
// .iter()
|
||||||
|
// .find(|exchange_info| exchange_info.symbol == element.symbol);
|
||||||
|
// let quote_commission_precision_option = exchange_info_vec
|
||||||
|
// .iter()
|
||||||
|
// .find(|exchange_info| exchange_info.symbol == element.symbol);
|
||||||
|
|
||||||
|
// let opclo_30m_option = all_data
|
||||||
|
// .rt_price_30m_vec
|
||||||
|
// .iter()
|
||||||
|
// .position(|x| *x.0 == element.symbol);
|
||||||
|
|
||||||
|
// if lot_step_size_option.is_some()
|
||||||
|
// && quote_commission_precision_option.is_some()
|
||||||
|
// && opclo_30m_option.is_some()
|
||||||
|
// {
|
||||||
|
// let lot_step_size = lot_step_size_option.unwrap().stepsize;
|
||||||
|
// let quote_commission_precision = quote_commission_precision_option
|
||||||
|
// .unwrap()
|
||||||
|
// .quote_commission_precision;
|
||||||
|
// let base_qty_to_be_ordered =
|
||||||
|
// element.base_qty_fee_adjusted.round_dp_with_strategy(
|
||||||
|
// lot_step_size.normalize().scale(),
|
||||||
|
// RoundingStrategy::ToZero,
|
||||||
|
// );
|
||||||
|
|
||||||
|
// let mut opclo_30m_vec = all_data.rt_price_30m_vec[opclo_30m_option.unwrap()]
|
||||||
|
// .1
|
||||||
|
// .clone();
|
||||||
|
|
||||||
|
// opclo_30m_vec.pop();
|
||||||
|
// opclo_30m_vec.reverse();
|
||||||
|
// let mut opclo_sample_length: usize = 50; // 50 candle samsples
|
||||||
|
// let nbr_of_exclusive: usize = 5;
|
||||||
|
// opclo_30m_vec.truncate(opclo_sample_length);
|
||||||
|
// opclo_30m_vec.sort_by(|a, b| {
|
||||||
|
// (a.high_price - a.low_price).total_cmp(&(b.high_price - b.low_price))
|
||||||
|
// });
|
||||||
|
// opclo_30m_vec.truncate(opclo_sample_length - nbr_of_exclusive);
|
||||||
|
// opclo_sample_length -= nbr_of_exclusive;
|
||||||
|
|
||||||
|
// let mut sum_amplitude_candles = 0.0;
|
||||||
|
// let mut sum_ratio_amp_body = 0.0;
|
||||||
|
// for element in &opclo_30m_vec {
|
||||||
|
// sum_amplitude_candles +=
|
||||||
|
// ((element.high_price / element.low_price) - 1.0) * 100.0;
|
||||||
|
// sum_ratio_amp_body += (element.close_price - element.open_price).abs()
|
||||||
|
// / (element.high_price - element.low_price);
|
||||||
|
// }
|
||||||
|
// let average_amplitude = sum_amplitude_candles / opclo_sample_length as f64; // percent unit
|
||||||
|
// let average_ratio_amp_body = sum_ratio_amp_body / opclo_sample_length as f64;
|
||||||
|
|
||||||
|
// let mut amplitude_variance = 0.0;
|
||||||
|
// for element in &opclo_30m_vec {
|
||||||
|
// amplitude_variance += ((((element.high_price / element.low_price) - 1.0)
|
||||||
|
// * 100.0)
|
||||||
|
// - average_amplitude)
|
||||||
|
// .powi(2);
|
||||||
|
// }
|
||||||
|
// amplitude_variance = amplitude_variance / (opclo_sample_length - 1) as f64;
|
||||||
|
// let standard_deviation_amplitude = amplitude_variance.sqrt();
|
||||||
|
|
||||||
|
// // let target_profit_percent = average_amplitude + (standard_deviation_amplitude * (average_ratio_amp_body));
|
||||||
|
// let target_profit_percent = |multiplier: f64| -> f64 {
|
||||||
|
// if multiplier < 0.0 {
|
||||||
|
// ((average_amplitude) * multiplier)
|
||||||
|
// - (standard_deviation_amplitude * 2.0) // 2.0 sigma (recommand: 0.5 ~ 2.0(patient & greedy))
|
||||||
|
// } else {
|
||||||
|
// ((average_amplitude) * multiplier)
|
||||||
|
// + (standard_deviation_amplitude * 2.0) // 2.0 sigma (recommand: 0.5 ~ 2.0(patient & greedy))
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
|
||||||
|
// if element.is_long == 0 || element.is_long == 1 {
|
||||||
|
// if element.pure_profit_percent >= 0.0 {
|
||||||
|
// let mut is_sell = false;
|
||||||
|
// if element.maximum_profit_percent >= target_profit_percent(5.0) + 0.2
|
||||||
|
// && element.pure_profit_percent >= target_profit_percent(5.0) + 0.2
|
||||||
|
// {
|
||||||
|
// println!(
|
||||||
|
// "Selling {} 500% target_profit_percent: {:.3}",
|
||||||
|
// element.symbol, element.pure_profit_percent
|
||||||
|
// );
|
||||||
|
// is_sell = true;
|
||||||
|
// } else if element.pure_profit_percent >= 7.0 {
|
||||||
|
// println!("Selling {} 7% profit_percent", element.symbol);
|
||||||
|
// is_sell = true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if is_sell == true {
|
||||||
|
// // let mut sell_price_ahead: Decimal = Decimal::new(14, 8);
|
||||||
|
// // sell_price_ahead = decimal_mul(decimal_add(decimal_mul(decimal_mul(rust_decimal::Decimal::from_f64(element.pure_profit_percent).unwrap(), dec!(0.01)), dec!(0.97)), dec!(1)), element.buy_price).round_dp_with_strategy(2, RoundingStrategy::ToZero);
|
||||||
|
// limit_order_sell(
|
||||||
|
// &element,
|
||||||
|
// element.current_price,
|
||||||
|
// base_qty_to_be_ordered,
|
||||||
|
// &client,
|
||||||
|
// &exchange_info_vec,
|
||||||
|
// &trade_fee_vec,
|
||||||
|
// )
|
||||||
|
// .await;
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// let mut is_sell = false;
|
||||||
|
// if element.pure_profit_percent <= target_profit_percent(-2.5) - 0.2
|
||||||
|
// // -0.2 means about total trade fees.
|
||||||
|
// {
|
||||||
|
// println!(
|
||||||
|
// "Selling {} -250% target_profit_percent: {:.3}",
|
||||||
|
// element.symbol, element.pure_profit_percent
|
||||||
|
// );
|
||||||
|
// is_sell = true;
|
||||||
|
// } else if element.pure_profit_percent <= -5.0 {
|
||||||
|
// println!("selling {} -5.0% profit", element.symbol);
|
||||||
|
// is_sell = true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if is_sell == true {
|
||||||
|
// 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();
|
|
||||||
super::strategy_manager::insert_pre_suggested_coins(3, false, &a, alldata, dec!(0)).await;
|
|
||||||
|
|
||||||
Ok(())
|
// Ok(())
|
||||||
}
|
// }
|
||||||
|
|
||||||
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(3).await?;
|
|
||||||
|
|
||||||
if !filled_buy_orders.is_empty() {
|
|
||||||
let client = ClientBuilder::new()
|
|
||||||
.timeout(tokio::time::Duration::from_millis(5000))
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut sell_order_count = 0;
|
|
||||||
for element in filled_buy_orders {
|
|
||||||
if element.used_usdt >= dec!(10.0) {
|
|
||||||
// ignore coins having 10 usdt below because of not traded
|
|
||||||
let sell_percent_for_uptrend = |z: f64| 0.94 * z - 0.5;
|
|
||||||
|
|
||||||
let lot_step_size_option = exchange_info_vec
|
|
||||||
.iter()
|
|
||||||
.find(|exchange_info| exchange_info.symbol == element.symbol);
|
|
||||||
let quote_commission_precision_option = exchange_info_vec
|
|
||||||
.iter()
|
|
||||||
.find(|exchange_info| exchange_info.symbol == element.symbol);
|
|
||||||
|
|
||||||
let opclo_30m_option = all_data
|
|
||||||
.rt_price_30m_vec
|
|
||||||
.iter()
|
|
||||||
.position(|x| *x.0 == element.symbol);
|
|
||||||
|
|
||||||
if lot_step_size_option.is_some()
|
|
||||||
&& quote_commission_precision_option.is_some()
|
|
||||||
&& opclo_30m_option.is_some()
|
|
||||||
{
|
|
||||||
let lot_step_size = lot_step_size_option.unwrap().stepsize;
|
|
||||||
let quote_commission_precision = quote_commission_precision_option
|
|
||||||
.unwrap()
|
|
||||||
.quote_commission_precision;
|
|
||||||
let base_qty_to_be_ordered =
|
|
||||||
element.base_qty_fee_adjusted.round_dp_with_strategy(
|
|
||||||
lot_step_size.normalize().scale(),
|
|
||||||
RoundingStrategy::ToZero,
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut opclo_30m_vec = all_data.rt_price_30m_vec[opclo_30m_option.unwrap()]
|
|
||||||
.1
|
|
||||||
.clone();
|
|
||||||
|
|
||||||
opclo_30m_vec.pop();
|
|
||||||
opclo_30m_vec.reverse();
|
|
||||||
let mut opclo_sample_length: usize = 50; // 50 candle samsples
|
|
||||||
let nbr_of_exclusive: usize = 5;
|
|
||||||
opclo_30m_vec.truncate(opclo_sample_length);
|
|
||||||
opclo_30m_vec.sort_by(|a, b| {
|
|
||||||
(a.high_price - a.low_price).total_cmp(&(b.high_price - b.low_price))
|
|
||||||
});
|
|
||||||
opclo_30m_vec.truncate(opclo_sample_length - nbr_of_exclusive);
|
|
||||||
opclo_sample_length -= nbr_of_exclusive;
|
|
||||||
|
|
||||||
let mut sum_amplitude_candles = 0.0;
|
|
||||||
let mut sum_ratio_amp_body = 0.0;
|
|
||||||
for element in &opclo_30m_vec {
|
|
||||||
sum_amplitude_candles +=
|
|
||||||
((element.high_price / element.low_price) - 1.0) * 100.0;
|
|
||||||
sum_ratio_amp_body += (element.close_price - element.open_price).abs()
|
|
||||||
/ (element.high_price - element.low_price);
|
|
||||||
}
|
|
||||||
let average_amplitude = sum_amplitude_candles / opclo_sample_length as f64; // percent unit
|
|
||||||
let average_ratio_amp_body = sum_ratio_amp_body / opclo_sample_length as f64;
|
|
||||||
|
|
||||||
let mut amplitude_variance = 0.0;
|
|
||||||
for element in &opclo_30m_vec {
|
|
||||||
amplitude_variance += ((((element.high_price / element.low_price) - 1.0)
|
|
||||||
* 100.0)
|
|
||||||
- average_amplitude)
|
|
||||||
.powi(2);
|
|
||||||
}
|
|
||||||
amplitude_variance = amplitude_variance / (opclo_sample_length - 1) as f64;
|
|
||||||
let standard_deviation_amplitude = amplitude_variance.sqrt();
|
|
||||||
|
|
||||||
// let target_profit_percent = average_amplitude + (standard_deviation_amplitude * (average_ratio_amp_body));
|
|
||||||
let target_profit_percent = |multiplier: f64| -> f64 {
|
|
||||||
if multiplier < 0.0 {
|
|
||||||
((average_amplitude) * multiplier)
|
|
||||||
- (standard_deviation_amplitude * 2.0) // 2.0 sigma (recommand: 0.5 ~ 2.0(patient & greedy))
|
|
||||||
} else {
|
|
||||||
((average_amplitude) * multiplier)
|
|
||||||
+ (standard_deviation_amplitude * 2.0) // 2.0 sigma (recommand: 0.5 ~ 2.0(patient & greedy))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if element.is_long == 0 || element.is_long == 1 {
|
|
||||||
if element.pure_profit_percent >= 0.0 {
|
|
||||||
let mut is_sell = false;
|
|
||||||
if element.maximum_profit_percent >= target_profit_percent(5.0) + 0.2
|
|
||||||
&& element.pure_profit_percent >= target_profit_percent(5.0) + 0.2
|
|
||||||
{
|
|
||||||
println!(
|
|
||||||
"Selling {} 500% target_profit_percent: {:.3}",
|
|
||||||
element.symbol, element.pure_profit_percent
|
|
||||||
);
|
|
||||||
is_sell = true;
|
|
||||||
} else if element.pure_profit_percent >= 7.0 {
|
|
||||||
println!("Selling {} 7% profit_percent", element.symbol);
|
|
||||||
is_sell = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if is_sell == true {
|
|
||||||
// let mut sell_price_ahead: Decimal = Decimal::new(14, 8);
|
|
||||||
// sell_price_ahead = decimal_mul(decimal_add(decimal_mul(decimal_mul(rust_decimal::Decimal::from_f64(element.pure_profit_percent).unwrap(), dec!(0.01)), dec!(0.97)), dec!(1)), element.buy_price).round_dp_with_strategy(2, RoundingStrategy::ToZero);
|
|
||||||
limit_order_sell(
|
|
||||||
&element,
|
|
||||||
element.current_price,
|
|
||||||
base_qty_to_be_ordered,
|
|
||||||
&client,
|
|
||||||
&exchange_info_vec,
|
|
||||||
&trade_fee_vec,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let mut is_sell = false;
|
|
||||||
if element.pure_profit_percent <= target_profit_percent(-2.5) - 0.2
|
|
||||||
// -0.2 means about total trade fees.
|
|
||||||
{
|
|
||||||
println!(
|
|
||||||
"Selling {} -250% target_profit_percent: {:.3}",
|
|
||||||
element.symbol, element.pure_profit_percent
|
|
||||||
);
|
|
||||||
is_sell = true;
|
|
||||||
} else if element.pure_profit_percent <= -5.0 {
|
|
||||||
println!("selling {} -5.0% profit", element.symbol);
|
|
||||||
is_sell = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if is_sell == true {
|
|
||||||
limit_order_sell(
|
|
||||||
&element,
|
|
||||||
element.current_price,
|
|
||||||
base_qty_to_be_ordered,
|
|
||||||
&client,
|
|
||||||
&exchange_info_vec,
|
|
||||||
&trade_fee_vec,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
use super::{
|
use super::{
|
||||||
dec, ema, exists_record, limit_order_sell, rsi, select_filled_buy_orders, stoch_rsi,
|
dec, decimal_add, decimal_sub, ema, exists_record, insert_pre_suggested_coins,
|
||||||
supertrend, try_join_all, AllData, Arc, Client, ClientBuilder, EmaData, ExchangeInfo, Mutex,
|
limit_order_sell, rsi, select_filled_buy_orders, stoch_rsi, supertrend, try_join_all, AllData,
|
||||||
RealtimePriceData, RoundingStrategy, RsiData, StochRsiData, SupertrendData, TradeFee,
|
Arc, Client, ClientBuilder, Decimal, EmaData, ExchangeInfo, FilteredData, Mutex,
|
||||||
insert_pre_suggested_coins, Decimal, FilteredData, decimal_add, decimal_sub
|
RealtimePriceData, RoundingStrategy, RsiData, StochRsiData, SupertrendData, TradeFee, update_record3
|
||||||
};
|
};
|
||||||
|
|
||||||
// Triple SuperTrend strategy
|
// Triple SuperTrend strategy
|
||||||
// SuperTrend length: 10, multiplier: 5, already up area
|
|
||||||
// SuperTrend length: 10, multiplier: 3, already up area
|
// SuperTrend length: 10, multiplier: 3, already up area
|
||||||
|
// SuperTrend length: 10, multiplier: 2, already up area
|
||||||
// SuperTrend length: 10, multiplier: 1.2, buy signal
|
// SuperTrend length: 10, multiplier: 1.2, buy signal
|
||||||
pub async fn list_up_for_buy(
|
pub async fn list_up_for_buy(
|
||||||
alldata: AllData,
|
alldata: AllData,
|
||||||
|
|
@ -68,7 +68,7 @@ pub async fn list_up_for_buy(
|
||||||
}
|
}
|
||||||
try_join_all(task_vec).await?;
|
try_join_all(task_vec).await?;
|
||||||
|
|
||||||
// 2nd filtering: supertrend(ATR period 10, multiplier: 5.0, 30m close price), the area should be in UP area.
|
// 2nd filtering: supertrend(ATR period 10, multiplier: 1.5, 30m close price), signal should be BUY
|
||||||
let filtered_data_1st = filtered_data_1st_arc.lock().await.clone();
|
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: Vec<FilteredData> = Vec::new();
|
||||||
let mut filtered_data_2nd_arc: Arc<Mutex<Vec<FilteredData>>> =
|
let mut filtered_data_2nd_arc: Arc<Mutex<Vec<FilteredData>>> =
|
||||||
|
|
@ -81,10 +81,11 @@ pub async fn list_up_for_buy(
|
||||||
let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
|
let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
|
||||||
let filtered_data_2nd_arc_c = Arc::clone(&filtered_data_2nd_arc);
|
let filtered_data_2nd_arc_c = Arc::clone(&filtered_data_2nd_arc);
|
||||||
task_vec.push(tokio::spawn(async move {
|
task_vec.push(tokio::spawn(async move {
|
||||||
let mut filtered_data = FilteredData::new();
|
let rt_30m_option = rt_price_30m_vec_c
|
||||||
let rt_30m_option = rt_price_30m_vec_c.iter().position(|x| *x.0 == element.symbol);
|
.iter()
|
||||||
|
.position(|x| *x.0 == element.symbol);
|
||||||
let supertrend_option_30m =
|
let supertrend_option_30m =
|
||||||
supertrend(&element.symbol, &rt_price_30m_vec_c, 10, 5.0, true).await;
|
supertrend(&element.symbol, &rt_price_30m_vec_c, 10, 1.5, true).await;
|
||||||
|
|
||||||
if rt_30m_option.is_some() && supertrend_option_30m.is_some() {
|
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();
|
rt_30m_vec = rt_price_30m_vec_c[rt_30m_option.unwrap()].1.clone();
|
||||||
|
|
@ -101,18 +102,29 @@ pub async fn list_up_for_buy(
|
||||||
}| *close_time,
|
}| *close_time,
|
||||||
);
|
);
|
||||||
if supertrend_search_result.is_ok() {
|
if supertrend_search_result.is_ok() {
|
||||||
if supertrend_vec[supertrend_search_result.unwrap()]
|
let current_price: Decimal = rust_decimal::prelude::FromPrimitive::from_f64(rt_30m_vec.last().unwrap().close_price).unwrap();
|
||||||
.area
|
|
||||||
.contains("UP")
|
|
||||||
{
|
|
||||||
let mut filtered_data_2nd_lock =
|
|
||||||
filtered_data_2nd_arc_c.lock().await;
|
|
||||||
|
|
||||||
|
if supertrend_vec[supertrend_search_result.unwrap()].signal.as_ref().is_some_and(|x| x.contains("BUY"))
|
||||||
|
&& current_price
|
||||||
|
< rust_decimal::prelude::FromPrimitive::from_f64(
|
||||||
|
supertrend_vec[supertrend_search_result.unwrap()-1].band_value * 1.002,
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
&& supertrend_vec[supertrend_search_result.unwrap()-1].band_value > supertrend_vec[supertrend_search_result.unwrap()].band_value
|
||||||
|
{
|
||||||
|
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.symbol = element.symbol.clone();
|
||||||
filtered_data.closetime = rt_30m_vec.last().unwrap().close_time;
|
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.current_price = current_price;
|
||||||
filtered_data_2nd_lock
|
filtered_data.stoploss = rust_decimal::prelude::FromPrimitive::from_f64(supertrend_vec[supertrend_search_result.unwrap()].band_value).unwrap();
|
||||||
.push(filtered_data);
|
let target_price = decimal_add(
|
||||||
|
filtered_data.current_price,
|
||||||
|
decimal_sub(filtered_data.current_price, filtered_data.stoploss),
|
||||||
|
);
|
||||||
|
filtered_data.target_price = target_price;
|
||||||
|
|
||||||
|
filtered_data_2nd_lock.push(filtered_data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -121,7 +133,7 @@ pub async fn list_up_for_buy(
|
||||||
}
|
}
|
||||||
try_join_all(task_vec).await?;
|
try_join_all(task_vec).await?;
|
||||||
|
|
||||||
// 3rd filtering: supertrend(ATR period 10, multiplier: 3.0, 30m close price), the area should be in UP area.
|
// 3rd filtering: supertrend(ATR period 50, multiplier: 4.0, 30m close price), the area should be in UP area.
|
||||||
let filtered_data_2nd = filtered_data_2nd_arc.lock().await.clone();
|
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: Vec<FilteredData> = Vec::new();
|
||||||
let mut filtered_data_3rd_arc: Arc<Mutex<Vec<FilteredData>>> =
|
let mut filtered_data_3rd_arc: Arc<Mutex<Vec<FilteredData>>> =
|
||||||
|
|
@ -134,9 +146,8 @@ pub async fn list_up_for_buy(
|
||||||
let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
|
let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
|
||||||
let filtered_data_3rd_arc_c = Arc::clone(&filtered_data_3rd_arc);
|
let filtered_data_3rd_arc_c = Arc::clone(&filtered_data_3rd_arc);
|
||||||
task_vec.push(tokio::spawn(async move {
|
task_vec.push(tokio::spawn(async move {
|
||||||
let mut filtered_data = FilteredData::new();
|
|
||||||
let supertrend_option_30m =
|
let supertrend_option_30m =
|
||||||
supertrend(&element.symbol, &rt_price_30m_vec_c, 10, 3.0, true).await;
|
supertrend(&element.symbol, &rt_price_30m_vec_c, 50, 4.0, true).await;
|
||||||
|
|
||||||
if supertrend_option_30m.is_some() {
|
if supertrend_option_30m.is_some() {
|
||||||
supertrend_vec = supertrend_option_30m.unwrap();
|
supertrend_vec = supertrend_option_30m.unwrap();
|
||||||
|
|
@ -158,13 +169,14 @@ pub async fn list_up_for_buy(
|
||||||
{
|
{
|
||||||
let mut filtered_3rd_symbols_lock =
|
let mut filtered_3rd_symbols_lock =
|
||||||
filtered_data_3rd_arc_c.lock().await;
|
filtered_data_3rd_arc_c.lock().await;
|
||||||
|
let mut filtered_data = FilteredData::new();
|
||||||
filtered_data.symbol = element.symbol.clone();
|
filtered_data.symbol = element.symbol.clone();
|
||||||
filtered_data.closetime = element.closetime;
|
filtered_data.closetime = element.closetime;
|
||||||
filtered_data.current_price = element.current_price;
|
filtered_data.current_price = element.current_price;
|
||||||
|
filtered_data.stoploss = element.stoploss;
|
||||||
|
filtered_data.target_price = element.target_price;
|
||||||
|
|
||||||
filtered_3rd_symbols_lock
|
filtered_3rd_symbols_lock.push(filtered_data);
|
||||||
.push(filtered_data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -173,7 +185,7 @@ pub async fn list_up_for_buy(
|
||||||
}
|
}
|
||||||
try_join_all(task_vec).await?;
|
try_join_all(task_vec).await?;
|
||||||
|
|
||||||
// 4th filtering: supertrend(ATR period 10, multiplier: 1.2, 30m close price), the area should be in UP area.
|
// 5th filtering: supertrend(ATR period 10, multiplier: 1.2, 30m close price), the area should be UP
|
||||||
// set stoploss and target_price
|
// set stoploss and target_price
|
||||||
let filtered_data_3rd_c = filtered_data_3rd_arc.lock().await.clone();
|
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: Vec<FilteredData> = Vec::new();
|
||||||
|
|
@ -188,7 +200,7 @@ pub async fn list_up_for_buy(
|
||||||
task_vec.push(tokio::spawn(async move {
|
task_vec.push(tokio::spawn(async move {
|
||||||
let supertrend_option_30m =
|
let supertrend_option_30m =
|
||||||
supertrend(&element.symbol, &rt_price_30m_vec_c, 10, 1.2, true).await;
|
supertrend(&element.symbol, &rt_price_30m_vec_c, 10, 1.2, true).await;
|
||||||
let mut filtered_data = FilteredData::new();
|
|
||||||
if supertrend_option_30m.is_some() {
|
if supertrend_option_30m.is_some() {
|
||||||
supertrend_vec = supertrend_option_30m.unwrap();
|
supertrend_vec = supertrend_option_30m.unwrap();
|
||||||
|
|
||||||
|
|
@ -203,23 +215,19 @@ pub async fn list_up_for_buy(
|
||||||
}| *close_time,
|
}| *close_time,
|
||||||
);
|
);
|
||||||
if supertrend_search_result.is_ok() {
|
if supertrend_search_result.is_ok() {
|
||||||
let supertrend_prev_element = supertrend_vec[supertrend_search_result.unwrap()-1].clone();
|
if supertrend_vec[supertrend_search_result.unwrap()]
|
||||||
let supertrend_element = supertrend_vec[supertrend_search_result.unwrap()].clone();
|
.area
|
||||||
if supertrend_element.signal.is_some_and(|x| x.contains("BUY"))
|
.contains("UP")
|
||||||
&& element.current_price < rust_decimal::prelude::FromPrimitive::from_f64(supertrend_prev_element.band_value * 1.003).unwrap()
|
|
||||||
{
|
{
|
||||||
let mut filtered_data_4th_lock =
|
let mut filtered_data_4th_lock = filtered_data_4th_arc_c.lock().await;
|
||||||
filtered_data_4th_arc_c.lock().await;
|
let mut filtered_data = FilteredData::new();
|
||||||
|
|
||||||
filtered_data.symbol = element.symbol.clone();
|
filtered_data.symbol = element.symbol.clone();
|
||||||
filtered_data.closetime = element.closetime;
|
filtered_data.closetime = element.closetime;
|
||||||
filtered_data.current_price = element.current_price;
|
filtered_data.current_price = element.current_price;
|
||||||
filtered_data.stoploss = rust_decimal::prelude::FromPrimitive::from_f64(supertrend_element.band_value).unwrap();
|
filtered_data.stoploss = element.stoploss;
|
||||||
let target_price = decimal_add(element.current_price, decimal_sub(element.current_price, filtered_data.stoploss));
|
filtered_data.target_price = element.target_price;
|
||||||
filtered_data.target_price = target_price;
|
|
||||||
|
|
||||||
filtered_data_4th_lock
|
filtered_data_4th_lock.push(filtered_data);
|
||||||
.push(filtered_data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -228,7 +236,89 @@ pub async fn list_up_for_buy(
|
||||||
}
|
}
|
||||||
try_join_all(task_vec).await?;
|
try_join_all(task_vec).await?;
|
||||||
|
|
||||||
let final_filtered_data = filtered_data_4th_arc.lock().await.clone();
|
// 4th filtering: StochRSI (RSI length: 3, Stoch length: 3, smooth k: 3, smooth d: 3) 80 > k > kn-1
|
||||||
|
let filtered_4th_symbol_c = filtered_data_4th_arc.lock().await.clone();
|
||||||
|
let mut rsi10_1d_data: Vec<(String, Vec<RsiData>)> =
|
||||||
|
rsi(3, &alldata.rt_price_1d_vec, &filtered_4th_symbol_c).await?;
|
||||||
|
let stoch_rsi_data = stoch_rsi(&rsi10_1d_data, 3, 3, 3).await?;
|
||||||
|
let mut stoch_rsi3_1d_vec: Vec<StochRsiData> = Vec::new();
|
||||||
|
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_4th_symbol_c {
|
||||||
|
let stoch_rsi3_1d_option = stoch_rsi_data.iter().position(|x| *x.0 == element.symbol);
|
||||||
|
let filtered_data_5th_arc_c = Arc::clone(&filtered_data_5th_arc);
|
||||||
|
if stoch_rsi3_1d_option.is_some() {
|
||||||
|
stoch_rsi3_1d_vec = stoch_rsi_data[stoch_rsi3_1d_option.unwrap()].1.clone();
|
||||||
|
|
||||||
|
if stoch_rsi3_1d_vec.len() >= 3 {
|
||||||
|
task_vec.push(tokio::spawn(async move {
|
||||||
|
let stoch_rsi_search_result = stoch_rsi3_1d_vec
|
||||||
|
.binary_search_by_key(&element.closetime, |&StochRsiData { k, d, close_time }| {
|
||||||
|
close_time
|
||||||
|
});
|
||||||
|
if stoch_rsi_search_result.is_ok() {
|
||||||
|
if 95.0 >= stoch_rsi3_1d_vec[stoch_rsi_search_result.unwrap()].k
|
||||||
|
&& stoch_rsi3_1d_vec[stoch_rsi_search_result.unwrap()].k
|
||||||
|
> stoch_rsi3_1d_vec[stoch_rsi_search_result.unwrap() - 1].k
|
||||||
|
&& stoch_rsi3_1d_vec[stoch_rsi_search_result.unwrap()].k
|
||||||
|
> stoch_rsi3_1d_vec[stoch_rsi_search_result.unwrap()].d
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6th filtertering: coefficient-of-variation 0.5<= CV <= 1.0
|
||||||
|
// 20개 amplitude 기준 CV 계산
|
||||||
|
|
||||||
|
|
||||||
|
// // 6th filtering: Avoid high volatility
|
||||||
|
// let filtered_data_5th_c = filtered_data_5th_arc.lock().await.clone();
|
||||||
|
// let mut filtered_data_6th: Vec<FilteredData> = Vec::new();
|
||||||
|
// let mut filtered_data_6th_arc: Arc<Mutex<Vec<FilteredData>>> =
|
||||||
|
// Arc::new(Mutex::new(filtered_data_6th));
|
||||||
|
// let mut task_vec = Vec::new();
|
||||||
|
// for element in filtered_data_5th_c {
|
||||||
|
// let mut supertrend_vec: Vec<SupertrendData> = Vec::new();
|
||||||
|
// let rt_price_30m_vec_c = alldata.rt_price_30m_vec.clone();
|
||||||
|
// let filtered_data_6th_arc_c = Arc::clone(&filtered_data_5th_arc);
|
||||||
|
|
||||||
|
// task_vec.push(tokio::spawn(async move {
|
||||||
|
// let mut filtered_data = FilteredData::new();
|
||||||
|
// if rt_price_30m_vec_c.len() >= 50 {
|
||||||
|
|
||||||
|
|
||||||
|
// let mut filtered_data_5th_lock = filtered_data_6th_arc_c.lock().await;
|
||||||
|
|
||||||
|
// 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?;
|
||||||
|
|
||||||
|
let final_filtered_data = filtered_data_5th_arc.lock().await.clone();
|
||||||
insert_pre_suggested_coins(4, false, &final_filtered_data, &alldata).await;
|
insert_pre_suggested_coins(4, false, &final_filtered_data, &alldata).await;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -246,10 +336,9 @@ pub async fn list_up_for_sell(
|
||||||
.timeout(tokio::time::Duration::from_millis(5000))
|
.timeout(tokio::time::Duration::from_millis(5000))
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
let mut supertrend_vec: Vec<SupertrendData> = Vec::new();
|
||||||
for element in filled_buy_orders {
|
for element in filled_buy_orders {
|
||||||
if element.used_usdt >= dec!(10.0) {
|
if element.used_usdt >= dec!(10.0) {
|
||||||
|
|
||||||
let lot_step_size_option = exchange_info_vec
|
let lot_step_size_option = exchange_info_vec
|
||||||
.iter()
|
.iter()
|
||||||
.position(|exchange_info| exchange_info.symbol == element.symbol);
|
.position(|exchange_info| exchange_info.symbol == element.symbol);
|
||||||
|
|
@ -261,24 +350,49 @@ pub async fn list_up_for_sell(
|
||||||
.rt_price_30m_vec
|
.rt_price_30m_vec
|
||||||
.iter()
|
.iter()
|
||||||
.position(|x| *x.0 == element.symbol);
|
.position(|x| *x.0 == element.symbol);
|
||||||
|
let supertrend_option_30m =
|
||||||
|
supertrend(&element.symbol, &all_data.rt_price_30m_vec, 10, 1.5, true).await;
|
||||||
|
|
||||||
if lot_step_size_option.is_some()
|
if lot_step_size_option.is_some()
|
||||||
&& quote_commission_precision_option.is_some()
|
&& quote_commission_precision_option.is_some()
|
||||||
&& opclo_30m_option.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 lot_step_size = exchange_info_vec[lot_step_size_option.unwrap()].stepsize;
|
||||||
let quote_commission_precision = exchange_info_vec[quote_commission_precision_option
|
let quote_commission_precision = exchange_info_vec
|
||||||
.unwrap()]
|
[quote_commission_precision_option.unwrap()]
|
||||||
.quote_commission_precision;
|
.quote_commission_precision;
|
||||||
let base_qty_to_be_ordered =
|
let base_qty_to_be_ordered =
|
||||||
element.base_qty_fee_adjusted.round_dp_with_strategy(
|
element.base_qty_ordered.round_dp_with_strategy(
|
||||||
lot_step_size.normalize().scale(),
|
lot_step_size.normalize().scale(),
|
||||||
RoundingStrategy::ToZero,
|
RoundingStrategy::ToZero,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (element.is_long == 0 || element.is_long == 1) && !element.current_price.is_zero() {
|
if (element.is_long == 0 || element.is_long == 1)
|
||||||
if element.current_price >= element.target_price && element.pure_profit_percent >= 0.1 {
|
&& !element.current_price.is_zero()
|
||||||
println!("target selling {} {:.2}", element.symbol, element.pure_profit_percent);
|
{
|
||||||
|
if element.current_price >= element.target_price
|
||||||
|
&& element.pure_profit_percent >= 0.1
|
||||||
|
{
|
||||||
|
println!(
|
||||||
|
"target selling {} {:.2}",
|
||||||
|
element.symbol, element.pure_profit_percent
|
||||||
|
);
|
||||||
limit_order_sell(
|
limit_order_sell(
|
||||||
&element,
|
&element,
|
||||||
element.current_price,
|
element.current_price,
|
||||||
|
|
@ -289,7 +403,10 @@ pub async fn list_up_for_sell(
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
} else if element.current_price <= element.stoploss {
|
} else if element.current_price <= element.stoploss {
|
||||||
println!("stoploss selling {} {:.2}", element.symbol, element.pure_profit_percent);
|
println!(
|
||||||
|
"stoploss selling {} {:.2}",
|
||||||
|
element.symbol, element.pure_profit_percent
|
||||||
|
);
|
||||||
limit_order_sell(
|
limit_order_sell(
|
||||||
&element,
|
&element,
|
||||||
element.current_price,
|
element.current_price,
|
||||||
|
|
@ -300,6 +417,30 @@ pub async fn list_up_for_sell(
|
||||||
)
|
)
|
||||||
.await;
|
.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;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,8 @@ use rust_decimal::Decimal;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
exists_record, insert_one_record, try_select_record, AllData, ExchangeInfo, FromRow,
|
exists_record, insert_one_record, try_join_all, try_select_record, AllData, ExchangeInfo,
|
||||||
RealtimePriceData, TradeFee, FilteredData, try_join_all
|
FilteredData, FromRow, RealtimePriceData, TradeFee,
|
||||||
};
|
};
|
||||||
use crate::signal_association::signal_decision::*;
|
use crate::signal_association::signal_decision::*;
|
||||||
use tokio::time::{sleep, Duration, Instant};
|
use tokio::time::{sleep, Duration, Instant};
|
||||||
|
|
@ -55,7 +55,6 @@ pub async fn execute_list_up_for_sell(
|
||||||
exchange_info_vec: &Vec<ExchangeInfo>,
|
exchange_info_vec: &Vec<ExchangeInfo>,
|
||||||
trade_fee_vec: &Vec<TradeFee>,
|
trade_fee_vec: &Vec<TradeFee>,
|
||||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
|
||||||
crate::strategy_team::strategy_004::list_up_for_sell(
|
crate::strategy_team::strategy_004::list_up_for_sell(
|
||||||
all_data,
|
all_data,
|
||||||
exchange_info_vec,
|
exchange_info_vec,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user