Change find() with position()
This commit is contained in:
parent
689a3cd8a7
commit
f45d5da3c3
|
|
@ -439,11 +439,11 @@ pub async fn limit_order_buy(
|
|||
T.get("cummulativeQuoteQty").unwrap().as_str().unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let base_asset_precision = exchange_info_vec
|
||||
.iter()
|
||||
.find(|exchange_info| exchange_info.symbol == element.symbol)
|
||||
.unwrap()
|
||||
.base_asset_precision;
|
||||
// let base_asset_precision = exchange_info_vec
|
||||
// .iter()
|
||||
// .find(|exchange_info| exchange_info.symbol == element.symbol)
|
||||
// .unwrap()
|
||||
// .base_asset_precision;
|
||||
let buy_price = decimal_div(cummulative_quote_qty, base_qty_ordered)
|
||||
.round_dp_with_strategy(8, RoundingStrategy::ToZero);
|
||||
|
||||
|
|
@ -718,18 +718,16 @@ pub async fn limit_order_sell(
|
|||
let mut insert_value_container: Vec<String> = Vec::new();
|
||||
let server_epoch = server_epoch().await;
|
||||
if RUNNING_MODE == SIMUL && buy_ordered_coin.status == "SIMUL" {
|
||||
let quote_asset_precision = exchange_info_vec
|
||||
let quote_asset_precision_option = exchange_info_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|exchange_info| exchange_info.symbol == buy_ordered_coin.symbol)
|
||||
.unwrap()
|
||||
.quote_asset_precision;
|
||||
let trade_fee = trade_fee_vec
|
||||
.position(|exchange_info| exchange_info.symbol == buy_ordered_coin.symbol);
|
||||
let trade_fee_option = trade_fee_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|trade_fee| trade_fee.symbol == buy_ordered_coin.symbol)
|
||||
.unwrap()
|
||||
.takercommission;
|
||||
.position(|trade_fee| trade_fee.symbol == buy_ordered_coin.symbol);
|
||||
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 trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
||||
|
||||
let get_usdt = decimal_mul(sell_base_quantity, sell_base_price)
|
||||
.round_dp_with_strategy(quote_asset_precision, RoundingStrategy::ToZero);
|
||||
let get_usdt_fee_adjusted = decimal_mul(get_usdt, decimal_sub(dec!(1), trade_fee))
|
||||
|
|
@ -770,6 +768,7 @@ pub async fn limit_order_sell(
|
|||
let mut delete_condition = String::from("WHERE id = ");
|
||||
delete_condition.push_str(buy_ordered_coin.id.to_string().as_str());
|
||||
delete_record(&delete_table_name, &delete_condition).await;
|
||||
}
|
||||
} else if RUNNING_MODE == REAL || RUNNING_MODE == TEST {
|
||||
// building URL and API-keys
|
||||
let mut url = String::new();
|
||||
|
|
@ -831,18 +830,15 @@ pub async fn limit_order_sell(
|
|||
.push(T.get("status").unwrap().as_str().unwrap().to_string()); // status
|
||||
insert_value_container.push(buy_ordered_coin.used_usdt.to_string()); // used_usdt
|
||||
|
||||
let quote_asset_precision = exchange_info_vec
|
||||
let quote_asset_precision_option = exchange_info_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|exchange_info| exchange_info.symbol == buy_ordered_coin.symbol)
|
||||
.unwrap()
|
||||
.quote_asset_precision;
|
||||
let trade_fee = trade_fee_vec
|
||||
.position(|exchange_info| exchange_info.symbol == buy_ordered_coin.symbol);
|
||||
let trade_fee_option = trade_fee_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|trade_fee| trade_fee.symbol == buy_ordered_coin.symbol)
|
||||
.unwrap()
|
||||
.takercommission;
|
||||
.position(|trade_fee| trade_fee.symbol == buy_ordered_coin.symbol);
|
||||
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 trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
||||
let get_usdt = rust_decimal::prelude::FromStr::from_str(
|
||||
T.get("cummulativeQuoteQty").unwrap().as_str().unwrap(),
|
||||
)
|
||||
|
|
@ -913,6 +909,7 @@ pub async fn limit_order_sell(
|
|||
delete_record(&delete_table_name, &delete_condition).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("sell order failed!: {}", body);
|
||||
}
|
||||
|
|
@ -1188,26 +1185,26 @@ pub async fn cancel_buy_order(
|
|||
status_value_build.push('\'');
|
||||
|
||||
// calculate values to be updated
|
||||
let trade_fee = trade_fee_vec
|
||||
let trade_fee_option = trade_fee_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|trade_fee| trade_fee.symbol == order.symbol)
|
||||
.unwrap()
|
||||
.takercommission;
|
||||
.position(|trade_fee| trade_fee.symbol == order.symbol);
|
||||
|
||||
let base_asset_precision_option = exchange_info_vec
|
||||
.iter()
|
||||
.position(|exchange_info| exchange_info.symbol == order.symbol);
|
||||
|
||||
if trade_fee_option.is_some() && base_asset_precision_option.is_some() {
|
||||
let trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
||||
|
||||
let base_qty_ordered = rust_decimal::prelude::FromStr::from_str(
|
||||
T.get("executedQty").unwrap().as_str().unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let base_qty_fee_adjusted =
|
||||
decimal_mul(base_qty_ordered, decimal_sub(dec!(1), trade_fee));
|
||||
|
||||
let base_asset_precision = exchange_info_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|exchange_info| exchange_info.symbol == order.symbol)
|
||||
.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)
|
||||
.round_dp_with_strategy(8, RoundingStrategy::ToZero);
|
||||
|
||||
|
|
@ -1241,6 +1238,7 @@ pub async fn cancel_buy_order(
|
|||
println!("partially buy {}", order.symbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if T.get("code").is_some() {
|
||||
// case that the order isn't canceled because the order completes while canceling
|
||||
// update record in ordered_coin_list
|
||||
|
|
@ -1371,22 +1369,19 @@ pub async fn cancel_sell_order(
|
|||
insert_values.push(insert_value_container.clone());
|
||||
insert_records(&insert_table_name, &insert_columns, &insert_values).await;
|
||||
} else {
|
||||
let quote_asset_precision_option = exchange_info_vec
|
||||
.iter()
|
||||
.position(|exchange_info| exchange_info.symbol == order.symbol);
|
||||
let trade_fee_option = trade_fee_vec
|
||||
.iter()
|
||||
.position(|trade_fee| trade_fee.symbol == order.symbol);
|
||||
|
||||
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 trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
||||
if base_qty_executed == base_qty_ordered {
|
||||
// FILLED case
|
||||
// update status FILLED
|
||||
let quote_asset_precision = exchange_info_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|exchange_info| exchange_info.symbol == order.symbol)
|
||||
.unwrap()
|
||||
.quote_asset_precision;
|
||||
let trade_fee = trade_fee_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|trade_fee| trade_fee.symbol == order.symbol)
|
||||
.unwrap()
|
||||
.takercommission;
|
||||
|
||||
let get_usdt = rust_decimal::prelude::FromStr::from_str(
|
||||
T.get("cummulativeQuoteQty").unwrap().as_str().unwrap(),
|
||||
)
|
||||
|
|
@ -1435,18 +1430,6 @@ pub async fn cancel_sell_order(
|
|||
} else {
|
||||
// PARTIALLY FILLED case
|
||||
// reflect partially filled information and update status with FILLED
|
||||
let quote_asset_precision = exchange_info_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|exchange_info| exchange_info.symbol == order.symbol)
|
||||
.unwrap()
|
||||
.quote_asset_precision;
|
||||
let trade_fee = trade_fee_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|trade_fee| trade_fee.symbol == order.symbol)
|
||||
.unwrap()
|
||||
.takercommission;
|
||||
let used_usdt = decimal_mul(base_qty_executed, order.buy_price);
|
||||
let get_usdt = rust_decimal::prelude::FromStr::from_str(
|
||||
T.get("cummulativeQuoteQty").unwrap().as_str().unwrap(),
|
||||
|
|
@ -1529,6 +1512,7 @@ pub async fn cancel_sell_order(
|
|||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
query_sell_order(&order, &client, exchange_info_vec, trade_fee_vec).await;
|
||||
}
|
||||
|
|
@ -1675,13 +1659,16 @@ pub async fn query_buy_order(
|
|||
value_build.push('\'');
|
||||
|
||||
// calculate values to be updated
|
||||
let trade_fee = trade_fee_vec
|
||||
let trade_fee_option = trade_fee_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|trade_fee| trade_fee.symbol == order.symbol)
|
||||
.unwrap()
|
||||
.takercommission;
|
||||
.position(|trade_fee| trade_fee.symbol == order.symbol);
|
||||
let base_asset_precision_option = exchange_info_vec
|
||||
.iter()
|
||||
.position(|exchange_info| exchange_info.symbol == order.symbol);
|
||||
|
||||
if trade_fee_option.is_some() && base_asset_precision_option.is_some() {
|
||||
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_qty_ordered = rust_decimal::prelude::FromStr::from_str(
|
||||
T.get("executedQty").unwrap().as_str().unwrap(),
|
||||
)
|
||||
|
|
@ -1693,12 +1680,7 @@ pub async fn query_buy_order(
|
|||
T.get("cummulativeQuoteQty").unwrap().as_str().unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let base_asset_precision = exchange_info_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|exchange_info| exchange_info.symbol == order.symbol)
|
||||
.unwrap()
|
||||
.base_asset_precision;
|
||||
|
||||
let buy_price = decimal_div(cummulative_quote_qty, base_qty_ordered)
|
||||
.round_dp_with_strategy(8, RoundingStrategy::ToZero);
|
||||
|
||||
|
|
@ -1732,6 +1714,8 @@ pub async fn query_buy_order(
|
|||
sub_available_usdt(decimal_sub(cummulative_quote_qty, order.used_usdt));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else if T.get("status").unwrap().as_str().unwrap() == "CANCELED" {
|
||||
let update_table_name = String::from("suggested_coin_list");
|
||||
let update_condition = vec![
|
||||
|
|
@ -1798,18 +1782,16 @@ pub async fn query_sell_order(
|
|||
if T.get("status").unwrap().as_str().unwrap() == "FILLED"
|
||||
|| T.get("status").unwrap().as_str().unwrap() == "PARTIALLY_FILLED"
|
||||
{
|
||||
let quote_asset_precision = exchange_info_vec
|
||||
let quote_asset_precision_option = exchange_info_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|exchange_info| exchange_info.symbol == order.symbol)
|
||||
.unwrap()
|
||||
.quote_asset_precision;
|
||||
let trade_fee = trade_fee_vec
|
||||
.position(|exchange_info| exchange_info.symbol == order.symbol);
|
||||
let trade_fee_option = trade_fee_vec
|
||||
.iter()
|
||||
// FIXME: find() should be position()
|
||||
.find(|trade_fee| trade_fee.symbol == order.symbol)
|
||||
.unwrap()
|
||||
.takercommission;
|
||||
.position(|trade_fee| trade_fee.symbol == order.symbol);
|
||||
|
||||
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 trade_fee = trade_fee_vec[trade_fee_option.unwrap()].takercommission;
|
||||
let get_usdt = rust_decimal::prelude::FromStr::from_str(
|
||||
T.get("cummulativeQuoteQty").unwrap().as_str().unwrap(),
|
||||
)
|
||||
|
|
@ -1851,6 +1833,7 @@ pub async fn query_sell_order(
|
|||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("query sell order failed!: {}", body);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -252,10 +252,10 @@ pub async fn list_up_for_sell(
|
|||
|
||||
let lot_step_size_option = exchange_info_vec
|
||||
.iter()
|
||||
.find(|exchange_info| exchange_info.symbol == element.symbol);
|
||||
.position(|exchange_info| exchange_info.symbol == element.symbol);
|
||||
let quote_commission_precision_option = exchange_info_vec
|
||||
.iter()
|
||||
.find(|exchange_info| exchange_info.symbol == element.symbol);
|
||||
.position(|exchange_info| exchange_info.symbol == element.symbol);
|
||||
|
||||
let opclo_30m_option = all_data
|
||||
.rt_price_30m_vec
|
||||
|
|
@ -266,9 +266,9 @@ pub async fn list_up_for_sell(
|
|||
&& 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()
|
||||
let lot_step_size = exchange_info_vec[lot_step_size_option.unwrap()].stepsize;
|
||||
let quote_commission_precision = exchange_info_vec[quote_commission_precision_option
|
||||
.unwrap()]
|
||||
.quote_commission_precision;
|
||||
let base_qty_to_be_ordered =
|
||||
element.base_qty_fee_adjusted.round_dp_with_strategy(
|
||||
|
|
@ -277,8 +277,8 @@ pub async fn list_up_for_sell(
|
|||
);
|
||||
|
||||
if (element.is_long == 0 || element.is_long == 1) && !element.current_price.is_zero() {
|
||||
if element.current_price >= element.target_price {
|
||||
println!("target selling {} {}", 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(
|
||||
&element,
|
||||
element.current_price,
|
||||
|
|
@ -289,7 +289,7 @@ pub async fn list_up_for_sell(
|
|||
)
|
||||
.await;
|
||||
} else if element.current_price <= element.stoploss {
|
||||
println!("stoploss selling {} {}", element.symbol, element.pure_profit_percent);
|
||||
println!("stoploss selling {} {:.2}", element.symbol, element.pure_profit_percent);
|
||||
limit_order_sell(
|
||||
&element,
|
||||
element.current_price,
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user