Refactor code
This commit is contained in:
parent
39b8c31e30
commit
fdd2623fba
|
|
@ -29,6 +29,24 @@ struct PreSuggestedCoin {
|
||||||
registerer: u16,
|
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)]
|
#[derive(Debug, FromRow)]
|
||||||
pub struct SuggestedCoin {
|
pub struct SuggestedCoin {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
|
|
@ -41,6 +59,22 @@ pub struct SuggestedCoin {
|
||||||
pub already_buy: u8,
|
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)]
|
#[derive(Debug, FromRow)]
|
||||||
pub struct ServerEpoch {
|
pub struct ServerEpoch {
|
||||||
server_epoch: u64,
|
server_epoch: u64,
|
||||||
|
|
@ -73,6 +107,27 @@ pub struct ScoreboardList {
|
||||||
pub neg_liquidation_signal: u32,
|
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)]
|
#[derive(Debug, FromRow)]
|
||||||
pub struct EvaluationList {
|
pub struct EvaluationList {
|
||||||
id: u64,
|
id: u64,
|
||||||
|
|
@ -90,6 +145,18 @@ pub struct IndexList {
|
||||||
pub total_price_down_dist_index: f64,
|
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)]
|
#[derive(Debug, FromRow)]
|
||||||
pub struct FilteredIndexList {
|
pub struct FilteredIndexList {
|
||||||
pub server_epoch: u64,
|
pub server_epoch: u64,
|
||||||
|
|
@ -100,6 +167,20 @@ pub struct FilteredIndexList {
|
||||||
pub total_price_down_dist_index_flag: i32,
|
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)]
|
#[derive(Debug, FromRow, Clone)]
|
||||||
pub struct MarketCapIndex {
|
pub struct MarketCapIndex {
|
||||||
pub market_cap_index: f64,
|
pub market_cap_index: f64,
|
||||||
|
|
@ -111,6 +192,21 @@ pub struct MarketCapIndex {
|
||||||
pub transition_buy_signal: i8,
|
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
|
// buy coin
|
||||||
pub async fn buy_coin(
|
pub async fn buy_coin(
|
||||||
exchange_info_vec: &Vec<ExchangeInfo>,
|
exchange_info_vec: &Vec<ExchangeInfo>,
|
||||||
|
|
@ -118,6 +214,14 @@ pub async fn buy_coin(
|
||||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
let mut suggested_coin = get_suggested_coin_list().await;
|
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 {
|
if !suggested_coin.is_empty() && exchange_info_vec.len() != 0 && trade_fee_vec.len() != 0 {
|
||||||
let server_epoch = server_epoch().await;
|
let server_epoch = server_epoch().await;
|
||||||
let mut filtered_suggested_coin_vec: Vec<&SuggestedCoin> = Vec::new();
|
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() {
|
if is_tradable == true && !filtered_suggested_coin_vec.is_empty() {
|
||||||
let unit_trade_usdt = get_unit_trade_usdt().await;
|
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()
|
let client = ClientBuilder::new()
|
||||||
.timeout(tokio::time::Duration::from_millis(3000))
|
.timeout(tokio::time::Duration::from_millis(3000))
|
||||||
.build()
|
.build()
|
||||||
|
|
@ -184,23 +280,28 @@ pub async fn buy_coin(
|
||||||
let trade_fee = trade_fee_result.unwrap().takercommission;
|
let trade_fee = trade_fee_result.unwrap().takercommission;
|
||||||
|
|
||||||
// buy the suggested coin and transfer it into [buy_ordered_coin_list]
|
// 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)
|
base_qty_ordered = decimal_div(unit_trade_usdt, element.suggested_price)
|
||||||
.round_dp_with_strategy(
|
.round_dp_with_strategy(
|
||||||
lot_step_size.normalize().scale(),
|
lot_step_size.normalize().scale(),
|
||||||
RoundingStrategy::ToZero,
|
RoundingStrategy::ToZero,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let mut base_qty_fee_adjusted = Decimal::new(0, 8);
|
||||||
base_qty_fee_adjusted =
|
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))
|
||||||
.round_dp_with_strategy(
|
.round_dp_with_strategy(
|
||||||
base_commission_precision,
|
base_commission_precision,
|
||||||
RoundingStrategy::ToZero,
|
RoundingStrategy::ToZero,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let mut used_usdt = Decimal::new(0, 8);
|
||||||
used_usdt = decimal_mul(base_qty_ordered, element.suggested_price)
|
used_usdt = decimal_mul(base_qty_ordered, element.suggested_price)
|
||||||
.round_dp_with_strategy(
|
.round_dp_with_strategy(
|
||||||
tick_size.normalize().scale(),
|
tick_size.normalize().scale(),
|
||||||
RoundingStrategy::ToZero,
|
RoundingStrategy::ToZero,
|
||||||
);
|
);
|
||||||
expected_pure_profit_percent = decimal_sub(
|
let expected_pure_profit_percen: f64 = decimal_sub(
|
||||||
decimal_div(
|
decimal_div(
|
||||||
decimal_mul(base_qty_fee_adjusted, element.suggested_price)
|
decimal_mul(base_qty_fee_adjusted, element.suggested_price)
|
||||||
.round_dp_with_strategy(
|
.round_dp_with_strategy(
|
||||||
|
|
@ -252,9 +353,11 @@ pub async fn buy_coin(
|
||||||
delete_condition.pop();
|
delete_condition.pop();
|
||||||
delete_condition.push(';');
|
delete_condition.push(';');
|
||||||
let suggested_coin_list_table_name = String::from("suggested_coin_list");
|
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)
|
delete_record(&suggested_coin_list_table_name, &delete_condition)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.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_condition: Vec<(String, String)> = Vec::new();
|
||||||
let mut update_condition2: 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() {
|
if filled_buy_orders.is_empty() {
|
||||||
// initialization of table
|
// 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_table_name = String::from("pre_suggested_coin_list");
|
||||||
let select_columns = String::from("*");
|
let select_columns = String::from("*");
|
||||||
let select_condition = None;
|
let select_condition = None;
|
||||||
let select_data_structure = PreSuggestedCoin {
|
let select_data_structure = PreSuggestedCoin::new();
|
||||||
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 pre_suggested_coin_list = try_select_record(
|
let pre_suggested_coin_list = try_select_record(
|
||||||
&select_table_name,
|
&select_table_name,
|
||||||
&select_columns,
|
&select_columns,
|
||||||
|
|
@ -852,16 +944,7 @@ async fn get_suggested_coin_list() -> Vec<SuggestedCoin> {
|
||||||
let table_name = String::from("suggested_coin_list");
|
let table_name = String::from("suggested_coin_list");
|
||||||
let columns = String::from("*");
|
let columns = String::from("*");
|
||||||
let condition = None;
|
let condition = None;
|
||||||
let mut suggested_coin = SuggestedCoin {
|
let mut suggested_coin = SuggestedCoin::new();
|
||||||
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 select_result = try_select_record(&table_name, &columns, &condition, &suggested_coin)
|
let select_result = try_select_record(&table_name, &columns, &condition, &suggested_coin)
|
||||||
.await
|
.await
|
||||||
|
|
@ -887,26 +970,7 @@ async fn select_buy_ordered_coins() -> Vec<BuyOrderedCoinList> {
|
||||||
let table_name = String::from("buy_ordered_coin_list");
|
let table_name = String::from("buy_ordered_coin_list");
|
||||||
let columns = String::from("*");
|
let columns = String::from("*");
|
||||||
let condition = None;
|
let condition = None;
|
||||||
let mut ordered_coin = BuyOrderedCoinList {
|
let mut ordered_coin = BuyOrderedCoinList::new();
|
||||||
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 select_result = select_record(&table_name, &columns, &condition, &ordered_coin)
|
let select_result = select_record(&table_name, &columns, &condition, &ordered_coin)
|
||||||
.await
|
.await
|
||||||
|
|
@ -915,12 +979,7 @@ async fn select_buy_ordered_coins() -> Vec<BuyOrderedCoinList> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn select_indices() -> IndexList {
|
pub async fn select_indices() -> IndexList {
|
||||||
let mut index_list = IndexList {
|
let mut index_list = IndexList::new();
|
||||||
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 select_table_name = String::from("indices");
|
let select_table_name = String::from("indices");
|
||||||
let select_columns_name = String::from("*");
|
let select_columns_name = String::from("*");
|
||||||
let select_condition = None;
|
let select_condition = None;
|
||||||
|
|
@ -942,14 +1001,7 @@ pub async fn select_indices() -> IndexList {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn select_filtered_indices() -> FilteredIndexList {
|
pub async fn select_filtered_indices() -> FilteredIndexList {
|
||||||
let mut index_list = FilteredIndexList {
|
let mut index_list = FilteredIndexList::new();
|
||||||
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 select_table_name = String::from("filtered_indices");
|
let select_table_name = String::from("filtered_indices");
|
||||||
let select_columns_name = String::from("*");
|
let select_columns_name = String::from("*");
|
||||||
let select_condition = None;
|
let select_condition = None;
|
||||||
|
|
@ -973,15 +1025,7 @@ pub async fn select_filtered_indices() -> FilteredIndexList {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn select_marketcap() -> Vec<MarketCapIndex> {
|
pub async fn select_marketcap() -> Vec<MarketCapIndex> {
|
||||||
let mut market_cap_list = MarketCapIndex {
|
let mut market_cap_list = MarketCapIndex::new();
|
||||||
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 select_table_name = String::from("market_cap_index");
|
let select_table_name = String::from("market_cap_index");
|
||||||
let select_columns_name = String::from("*");
|
let select_columns_name = String::from("*");
|
||||||
let select_condition = None;
|
let select_condition = None;
|
||||||
|
|
@ -1001,21 +1045,7 @@ pub async fn select_scoreboard() -> Vec<ScoreboardList> {
|
||||||
let select_table_name = String::from("scoreboard");
|
let select_table_name = String::from("scoreboard");
|
||||||
let select_columns = String::from("*");
|
let select_columns = String::from("*");
|
||||||
let select_condition = None;
|
let select_condition = None;
|
||||||
let mut select_data_structure = ScoreboardList {
|
let mut select_data_structure = ScoreboardList::new();
|
||||||
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 scoreboard_list = select_record(
|
let scoreboard_list = select_record(
|
||||||
&select_table_name,
|
&select_table_name,
|
||||||
&select_columns,
|
&select_columns,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user