Refactor code

This commit is contained in:
Sik Yoon 2023-10-04 20:13:22 +09:00
parent 39b8c31e30
commit fdd2623fba

View File

@ -29,6 +29,24 @@ struct PreSuggestedCoin {
registerer: u16,
}
impl DBlist for PreSuggestedCoin {
fn new() -> PreSuggestedCoin {
let a = PreSuggestedCoin {
id: 0,
symbol: String::new(),
close_time: 0,
suggested_price: Decimal::new(0, 8),
current_price: Decimal::new(0, 8),
registered_server_epoch: 0,
profit_percent: 0.0,
minimum_profit_percent: 0.0,
maximum_profit_percent: 0.0,
registerer: 0,
};
a
}
}
#[derive(Debug, FromRow)]
pub struct SuggestedCoin {
pub id: u64,
@ -41,6 +59,22 @@ pub struct SuggestedCoin {
pub already_buy: u8,
}
impl DBlist for SuggestedCoin {
fn new() -> SuggestedCoin {
let a = SuggestedCoin {
id: 0,
symbol: String::new(),
suggested_price: Decimal::new(0, 8),
close_time: 0,
registered_server_epoch: 0,
registerer: 0,
is_long: 0,
already_buy: 0,
};
a
}
}
#[derive(Debug, FromRow)]
pub struct ServerEpoch {
server_epoch: u64,
@ -73,6 +107,27 @@ pub struct ScoreboardList {
pub neg_liquidation_signal: u32,
}
impl DBlist for ScoreboardList {
fn new() -> ScoreboardList {
let a = ScoreboardList {
id: 0,
total_number_of_coin: 0,
pos_profit_number: 0,
neg_profit_number: 0,
total_used_usdt: 0.0,
pos_used_usdt: 0.0,
neg_used_usdt: 0.0,
total_pos_profit_usdt: 0.0,
total_neg_profit_usdt: 0.0,
maximum_total_pos_profit_usdt: 0.0,
avg_neg_profit_percent: 0.0,
pos_liquidation_signal: 0,
neg_liquidation_signal: 0,
};
a
}
}
#[derive(Debug, FromRow)]
pub struct EvaluationList {
id: u64,
@ -90,6 +145,18 @@ pub struct IndexList {
pub total_price_down_dist_index: f64,
}
impl DBlist for IndexList {
fn new() -> IndexList {
let a = IndexList {
server_epoch: 0,
total_24h_change_profit_index: 0.0,
usdt_24h_change_profit_index: 0.0,
total_price_down_dist_index: 0.0,
};
a
}
}
#[derive(Debug, FromRow)]
pub struct FilteredIndexList {
pub server_epoch: u64,
@ -100,6 +167,20 @@ pub struct FilteredIndexList {
pub total_price_down_dist_index_flag: i32,
}
impl DBlist for FilteredIndexList {
fn new() -> FilteredIndexList {
let a = FilteredIndexList {
server_epoch: 0,
total_24h_change_profit_index: 0.0,
usdt_24h_change_profit_index: 0.0,
total_price_down_dist_index: 0.0,
total_price_down_dist_index_maximum: 0.0,
total_price_down_dist_index_flag: 0,
};
a
}
}
#[derive(Debug, FromRow, Clone)]
pub struct MarketCapIndex {
pub market_cap_index: f64,
@ -111,6 +192,21 @@ pub struct MarketCapIndex {
pub transition_buy_signal: i8,
}
impl DBlist for MarketCapIndex {
fn new() -> MarketCapIndex {
let a = MarketCapIndex {
market_cap_index: 0.0,
minimum: 0.0,
maximum: 0.0,
transition_point: 0.0,
liquidation_signal: 0,
negative_buy_signal: 0,
transition_buy_signal: 0,
};
a
}
}
// buy coin
pub async fn buy_coin(
exchange_info_vec: &Vec<ExchangeInfo>,
@ -118,6 +214,14 @@ pub async fn buy_coin(
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let mut suggested_coin = get_suggested_coin_list().await;
if exchange_info_vec.len() == 0 {
println!("exchange_info_vec length is zero in buy coin");
}
if trade_fee_vec.len() == 0 {
println!("trade_fee_vec length is zero in buy coin");
}
if !suggested_coin.is_empty() && exchange_info_vec.len() != 0 && trade_fee_vec.len() != 0 {
let server_epoch = server_epoch().await;
let mut filtered_suggested_coin_vec: Vec<&SuggestedCoin> = Vec::new();
@ -144,14 +248,6 @@ pub async fn buy_coin(
if is_tradable == true && !filtered_suggested_coin_vec.is_empty() {
let unit_trade_usdt = get_unit_trade_usdt().await;
let available_usdt = get_available_usdt().await;
let vec_len = Decimal::new(filtered_suggested_coin_vec.len() as i64, 0);
let mut used_usdt = Decimal::new(0, 8);
let mut base_qty_ordered = Decimal::new(0, 8);
let mut base_qty_fee_adjusted = Decimal::new(0, 8);
let mut expected_pure_profit_percent = 0.0;
let insert_table_name = String::from("buy_ordered_coin_list");
let client = ClientBuilder::new()
.timeout(tokio::time::Duration::from_millis(3000))
.build()
@ -184,23 +280,28 @@ pub async fn buy_coin(
let trade_fee = trade_fee_result.unwrap().takercommission;
// buy the suggested coin and transfer it into [buy_ordered_coin_list]
let mut base_qty_ordered = Decimal::new(0, 8);
base_qty_ordered = decimal_div(unit_trade_usdt, element.suggested_price)
.round_dp_with_strategy(
lot_step_size.normalize().scale(),
RoundingStrategy::ToZero,
);
let mut base_qty_fee_adjusted = Decimal::new(0, 8);
base_qty_fee_adjusted =
decimal_mul(base_qty_ordered, decimal_sub(dec!(1), trade_fee))
.round_dp_with_strategy(
base_commission_precision,
RoundingStrategy::ToZero,
);
let mut used_usdt = Decimal::new(0, 8);
used_usdt = decimal_mul(base_qty_ordered, element.suggested_price)
.round_dp_with_strategy(
tick_size.normalize().scale(),
RoundingStrategy::ToZero,
);
expected_pure_profit_percent = decimal_sub(
let expected_pure_profit_percen: f64 = decimal_sub(
decimal_div(
decimal_mul(base_qty_fee_adjusted, element.suggested_price)
.round_dp_with_strategy(
@ -252,9 +353,11 @@ pub async fn buy_coin(
delete_condition.pop();
delete_condition.push(';');
let suggested_coin_list_table_name = String::from("suggested_coin_list");
println!("delete suggested coin list");
delete_record(&suggested_coin_list_table_name, &delete_condition)
.await
.unwrap();
println!("deletion done");
}
}
@ -489,7 +592,7 @@ pub async fn monitoring_scoreboard(
let mut update_condition: Vec<(String, String)> = Vec::new();
let mut update_condition2: Vec<(String, String)> = Vec::new();
let filled_buy_orders = select_filled_buy_orders().await;
let filled_buy_orders = select_filled_buy_orders().await?;
if filled_buy_orders.is_empty() {
// initialization of table
@ -824,18 +927,7 @@ async fn select_pre_suggested_coin_list() -> Vec<PreSuggestedCoin> {
let select_table_name = String::from("pre_suggested_coin_list");
let select_columns = String::from("*");
let select_condition = None;
let select_data_structure = PreSuggestedCoin {
id: 0,
symbol: String::new(),
close_time: 0,
suggested_price: Decimal::new(0, 8),
current_price: Decimal::new(0, 8),
registered_server_epoch: 0,
profit_percent: 0.0,
minimum_profit_percent: 0.0,
maximum_profit_percent: 0.0,
registerer: 0,
};
let select_data_structure = PreSuggestedCoin::new();
let pre_suggested_coin_list = try_select_record(
&select_table_name,
&select_columns,
@ -852,16 +944,7 @@ async fn get_suggested_coin_list() -> Vec<SuggestedCoin> {
let table_name = String::from("suggested_coin_list");
let columns = String::from("*");
let condition = None;
let mut suggested_coin = SuggestedCoin {
id: 0,
symbol: String::new(),
suggested_price: Decimal::new(0, 8),
close_time: 0,
registered_server_epoch: 0,
registerer: 0,
is_long: 0,
already_buy: 0,
};
let mut suggested_coin = SuggestedCoin::new();
let select_result = try_select_record(&table_name, &columns, &condition, &suggested_coin)
.await
@ -887,26 +970,7 @@ async fn select_buy_ordered_coins() -> Vec<BuyOrderedCoinList> {
let table_name = String::from("buy_ordered_coin_list");
let columns = String::from("*");
let condition = None;
let mut ordered_coin = BuyOrderedCoinList {
id: 0,
symbol: String::new(),
order_id: 0,
transact_time: 0,
close_time: 0,
status: String::new(),
used_usdt: Decimal::new(0, 8),
expected_get_usdt: 0.0,
expected_usdt_profit: 0.0,
buy_price: Decimal::new(0, 8),
current_price: Decimal::new(0, 8),
base_qty_ordered: Decimal::new(0, 8),
base_qty_fee_adjusted: Decimal::new(0, 8),
pure_profit_percent: 0.0,
minimum_profit_percent: 0.0,
maximum_profit_percent: 0.0,
registerer: 0,
is_long: 0,
};
let mut ordered_coin = BuyOrderedCoinList::new();
let select_result = select_record(&table_name, &columns, &condition, &ordered_coin)
.await
@ -915,12 +979,7 @@ async fn select_buy_ordered_coins() -> Vec<BuyOrderedCoinList> {
}
pub async fn select_indices() -> IndexList {
let mut index_list = IndexList {
server_epoch: 0,
total_24h_change_profit_index: 0.0,
usdt_24h_change_profit_index: 0.0,
total_price_down_dist_index: 0.0,
};
let mut index_list = IndexList::new();
let select_table_name = String::from("indices");
let select_columns_name = String::from("*");
let select_condition = None;
@ -942,14 +1001,7 @@ pub async fn select_indices() -> IndexList {
}
pub async fn select_filtered_indices() -> FilteredIndexList {
let mut index_list = FilteredIndexList {
server_epoch: 0,
total_24h_change_profit_index: 0.0,
usdt_24h_change_profit_index: 0.0,
total_price_down_dist_index: 0.0,
total_price_down_dist_index_maximum: 0.0,
total_price_down_dist_index_flag: 0,
};
let mut index_list = FilteredIndexList::new();
let select_table_name = String::from("filtered_indices");
let select_columns_name = String::from("*");
let select_condition = None;
@ -973,15 +1025,7 @@ pub async fn select_filtered_indices() -> FilteredIndexList {
}
pub async fn select_marketcap() -> Vec<MarketCapIndex> {
let mut market_cap_list = MarketCapIndex {
market_cap_index: 0.0,
minimum: 0.0,
maximum: 0.0,
transition_point: 0.0,
liquidation_signal: 0,
negative_buy_signal: 0,
transition_buy_signal: 0,
};
let mut market_cap_list = MarketCapIndex::new();
let select_table_name = String::from("market_cap_index");
let select_columns_name = String::from("*");
let select_condition = None;
@ -1001,21 +1045,7 @@ pub async fn select_scoreboard() -> Vec<ScoreboardList> {
let select_table_name = String::from("scoreboard");
let select_columns = String::from("*");
let select_condition = None;
let mut select_data_structure = ScoreboardList {
id: 0,
total_number_of_coin: 0,
pos_profit_number: 0,
neg_profit_number: 0,
total_used_usdt: 0.0,
pos_used_usdt: 0.0,
neg_used_usdt: 0.0,
total_pos_profit_usdt: 0.0,
total_neg_profit_usdt: 0.0,
maximum_total_pos_profit_usdt: 0.0,
avg_neg_profit_percent: 0.0,
pos_liquidation_signal: 0,
neg_liquidation_signal: 0,
};
let mut select_data_structure = ScoreboardList::new();
let scoreboard_list = select_record(
&select_table_name,
&select_columns,