Add target_price column

This commit is contained in:
Sik Yoon 2023-11-19 13:38:51 +09:00
parent 5b7c4a9acb
commit efd0bd5b10
6 changed files with 103 additions and 71 deletions

View File

@ -22,6 +22,7 @@ struct PreSuggestedCoin {
close_time: i64,
suggested_price: Decimal,
current_price: Decimal,
target_price: Decimal,
registered_server_epoch: i64,
profit_percent: f64,
minimum_profit_percent: f64,
@ -38,6 +39,7 @@ impl DBlist for PreSuggestedCoin {
close_time: 0,
suggested_price: Decimal::new(0, 8),
current_price: Decimal::new(0, 8),
target_price: Decimal::new(0, 8),
registered_server_epoch: 0,
profit_percent: 0.0,
minimum_profit_percent: 0.0,
@ -54,6 +56,7 @@ pub struct SuggestedCoin {
pub id: u64,
pub symbol: String,
pub suggested_price: Decimal,
pub target_price: Decimal,
pub close_time: i64,
pub registered_server_epoch: i64,
pub registerer: u16,
@ -67,6 +70,7 @@ impl DBlist for SuggestedCoin {
id: 0,
symbol: String::new(),
suggested_price: Decimal::new(0, 8),
target_price: Decimal::new(0, 8),
close_time: 0,
registered_server_epoch: 0,
registerer: 0,
@ -249,11 +253,11 @@ pub async fn buy_coin(
if is_tradable == true {
let exchange_info_result = exchange_info_vec
.iter()
.position(|ExchangeInfo| ExchangeInfo.symbol == element.symbol);
.position(|exchange_info| exchange_info.symbol == element.symbol);
let trade_fee_result = trade_fee_vec
.iter()
.position(|TradeFee| TradeFee.symbol == element.symbol);
.position(|trade_fee| trade_fee.symbol == element.symbol);
if exchange_info_result.is_some() && trade_fee_result.is_some() {
let lot_step_size =
@ -379,22 +383,22 @@ pub async fn buy_coin_for_test(
for element in &filtered_suggested_coin_vec {
let lot_step_size = exchange_info_vec
.iter()
.find(|ExchangeInfo| ExchangeInfo.symbol == element.symbol)
.find(|exchange_info| exchange_info.symbol == element.symbol)
.unwrap()
.stepsize;
let tick_size = exchange_info_vec
.iter()
.find(|ExchangeInfo| ExchangeInfo.symbol == element.symbol)
.find(|exchange_info| exchange_info.symbol == element.symbol)
.unwrap()
.ticksize;
let base_commission_precision = exchange_info_vec
.iter()
.find(|ExchangeInfo| ExchangeInfo.symbol == element.symbol)
.find(|exchange_info| exchange_info.symbol == element.symbol)
.unwrap()
.base_commission_precision;
let trade_fee = trade_fee_vec
.iter()
.find(|TradeFee| TradeFee.symbol == element.symbol)
.find(|trade_fee| trade_fee.symbol == element.symbol)
.unwrap()
.takercommission;
@ -517,6 +521,7 @@ pub async fn monitoring_pre_suggested_coins(
let mut insert_columns = vec![
"symbol",
"suggested_price",
"target_price",
"close_time",
"registered_server_epoch",
"registerer",
@ -548,6 +553,7 @@ pub async fn monitoring_pre_suggested_coins(
insert_values.clear();
insert_values.push(element.symbol.clone()); // symbol
insert_values.push(element.current_price.to_string()); // suggested_price
insert_values.push(element.target_price.to_string()); // target_price
insert_values.push(element.close_time.to_string()); // close_time
insert_values.push(server_epoch().await.to_string()); // registered_server_epoch
insert_values.push(element.registerer.to_string()); // registerer

View File

@ -59,6 +59,7 @@ pub struct BuyOrderedCoinList {
pub expected_usdt_profit: f64,
pub buy_price: Decimal,
pub current_price: Decimal,
pub target_price: Decimal,
pub base_qty_ordered: Decimal,
pub base_qty_fee_adjusted: Decimal,
pub pure_profit_percent: f64,
@ -82,6 +83,7 @@ pub struct SellOrderedCoinList {
pub get_usdt_fee_adjusted: Decimal,
pub buy_price: Decimal,
pub sell_price: Decimal,
pub target_price: Decimal,
pub base_qty_ordered: Decimal,
pub pure_profit_percent: Decimal,
pub maximum_profit_percent: f64,
@ -123,6 +125,7 @@ impl DBlist for SellOrderedCoinList {
get_usdt_fee_adjusted: Decimal::new(0, 8),
buy_price: Decimal::new(0, 8),
sell_price: Decimal::new(0, 8),
target_price: Decimal::new(0, 8),
base_qty_ordered: Decimal::new(0, 8),
pure_profit_percent: Decimal::new(0, 8),
maximum_profit_percent: 0.0,
@ -147,6 +150,7 @@ impl DBlist for BuyOrderedCoinList {
expected_usdt_profit: 0.0,
buy_price: Decimal::new(0, 8),
current_price: Decimal::new(0, 8),
target_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,
@ -311,6 +315,7 @@ pub async fn limit_order_buy(
"expected_usdt_profit",
"buy_price",
"current_price",
"target_price",
"base_qty_ordered",
"base_qty_fee_adjusted",
"pure_profit_percent",
@ -333,6 +338,7 @@ pub async fn limit_order_buy(
insert_value_container.push(0.0.to_string()); // expected_usdt_profit
insert_value_container.push(order_price.to_string()); // buy_price
insert_value_container.push(0.0.to_string()); // current_price
insert_value_container.push(element.target_price.to_string()); // target_price
insert_value_container.push(order_quantity.to_string()); // base_qty_ordered
insert_value_container.push(simul_base_qty_fee_adjusted.to_string()); // base_qty_fee_adjusted
insert_value_container.push(0.0.to_string()); // pure_profit_percent
@ -429,7 +435,7 @@ pub async fn limit_order_buy(
.unwrap();
let base_asset_precision = exchange_info_vec
.iter()
.find(|ExchangeInfo| ExchangeInfo.symbol == element.symbol)
.find(|exchange_info| exchange_info.symbol == element.symbol)
.unwrap()
.base_asset_precision;
let buy_price = decimal_div(cummulative_quote_qty, base_qty_ordered)
@ -440,6 +446,7 @@ pub async fn limit_order_buy(
insert_value_container.push(0.0.to_string()); // expected_usdt_profit
insert_value_container.push(buy_price.to_string()); // buy_price
insert_value_container.push(0.0.to_string()); // current_price
insert_value_container.push(element.target_price.to_string()); // target_price
insert_value_container.push(base_qty_ordered.to_string()); // base_qty_ordered
insert_value_container.push(base_qty_fee_adjusted.to_string()); // base_qty_fee_adjusted
@ -453,6 +460,7 @@ pub async fn limit_order_buy(
insert_value_container.push(0.0.to_string()); // expected_usdt_profit
insert_value_container.push(0.0.to_string()); // buy_price
insert_value_container.push(0.0.to_string()); // current_price
insert_value_container.push(element.target_price.to_string()); // target_price
insert_value_container
.push(T.get("origQty").unwrap().as_str().unwrap().to_string()); // base_qty_ordered
insert_value_container.push(0.0.to_string()); // base_qty_fee_adjusted
@ -582,13 +590,13 @@ async fn update_repeat_task(
.position(|x| *x.symbol == element.symbol);
let lot_step_size_result = exchange_info_vec
.iter()
.position(|ExchangeInfo| ExchangeInfo.symbol == element.symbol);
.position(|exchange_info| exchange_info.symbol == element.symbol);
let quote_commission_precision_result = exchange_info_vec
.iter()
.position(|ExchangeInfo| ExchangeInfo.symbol == element.symbol);
.position(|exchange_info| exchange_info.symbol == element.symbol);
let trade_fee_result = trade_fee_vec
.iter()
.position(|TradeFee| TradeFee.symbol == element.symbol);
.position(|trade_fee| trade_fee.symbol == element.symbol);
if price_index_option.is_some()
&& lot_step_size_result.is_some()
@ -703,13 +711,13 @@ pub async fn limit_order_sell(
let quote_asset_precision = exchange_info_vec
.iter()
// FIXME: find() should be position()
.find(|ExchangeInfo| ExchangeInfo.symbol == buy_ordered_coin.symbol)
.find(|exchange_info| exchange_info.symbol == buy_ordered_coin.symbol)
.unwrap()
.quote_asset_precision;
let trade_fee = trade_fee_vec
.iter()
// FIXME: find() should be position()
.find(|TradeFee| TradeFee.symbol == buy_ordered_coin.symbol)
.find(|trade_fee| trade_fee.symbol == buy_ordered_coin.symbol)
.unwrap()
.takercommission;
let get_usdt = decimal_mul(sell_base_quantity, sell_base_price)
@ -814,13 +822,13 @@ pub async fn limit_order_sell(
let quote_asset_precision = exchange_info_vec
.iter()
// FIXME: find() should be position()
.find(|ExchangeInfo| ExchangeInfo.symbol == buy_ordered_coin.symbol)
.find(|exchange_info| exchange_info.symbol == buy_ordered_coin.symbol)
.unwrap()
.quote_asset_precision;
let trade_fee = trade_fee_vec
.iter()
// FIXME: find() should be position()
.find(|TradeFee| TradeFee.symbol == buy_ordered_coin.symbol)
.find(|trade_fee| trade_fee.symbol == buy_ordered_coin.symbol)
.unwrap()
.takercommission;
let get_usdt = rust_decimal::prelude::FromStr::from_str(
@ -1169,7 +1177,7 @@ pub async fn cancel_buy_order(
let trade_fee = trade_fee_vec
.iter()
// FIXME: find() should be position()
.find(|TradeFee| TradeFee.symbol == order.symbol)
.find(|trade_fee| trade_fee.symbol == order.symbol)
.unwrap()
.takercommission;
@ -1183,7 +1191,7 @@ pub async fn cancel_buy_order(
let base_asset_precision = exchange_info_vec
.iter()
// FIXME: find() should be position()
.find(|ExchangeInfo| ExchangeInfo.symbol == order.symbol)
.find(|exchange_info| exchange_info.symbol == order.symbol)
.unwrap()
.base_asset_precision;
let buy_price = decimal_div(cummulative_quote_qty, base_qty_ordered)
@ -1292,6 +1300,7 @@ pub async fn cancel_sell_order(
"expected_usdt_profit",
"buy_price",
"current_price",
"target_price",
"base_qty_ordered",
"base_qty_fee_adjusted",
"pure_profit_percent",
@ -1334,6 +1343,7 @@ pub async fn cancel_sell_order(
insert_value_container.push(0.0.to_string()); // expected_usdt_profit
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(order.target_price.to_string()); // current_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_fee_adjusted
insert_value_container.push(0.0.to_string()); // pure_profit_percent
@ -1351,13 +1361,13 @@ pub async fn cancel_sell_order(
let quote_asset_precision = exchange_info_vec
.iter()
// FIXME: find() should be position()
.find(|ExchangeInfo| ExchangeInfo.symbol == order.symbol)
.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(|TradeFee| TradeFee.symbol == order.symbol)
.find(|trade_fee| trade_fee.symbol == order.symbol)
.unwrap()
.takercommission;
@ -1412,13 +1422,13 @@ pub async fn cancel_sell_order(
let quote_asset_precision = exchange_info_vec
.iter()
// FIXME: find() should be position()
.find(|ExchangeInfo| ExchangeInfo.symbol == order.symbol)
.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(|TradeFee| TradeFee.symbol == order.symbol)
.find(|trade_fee| trade_fee.symbol == order.symbol)
.unwrap()
.takercommission;
let used_usdt = decimal_mul(base_qty_executed, order.buy_price);
@ -1488,6 +1498,7 @@ pub async fn cancel_sell_order(
insert_value_container.push(0.0.to_string()); // expected_usdt_profit
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(order.target_price.to_string()); // target_price
insert_value_container.push(rest_base_qty.to_string()); // base_qty_ordered
insert_value_container.push(rest_base_qty_fee_adjusted.to_string()); // base_qty_fee_adjusted
insert_value_container.push(0.0.to_string()); // pure_profit_percent
@ -1650,7 +1661,7 @@ pub async fn query_buy_order(
let trade_fee = trade_fee_vec
.iter()
// FIXME: find() should be position()
.find(|TradeFee| TradeFee.symbol == order.symbol)
.find(|trade_fee| trade_fee.symbol == order.symbol)
.unwrap()
.takercommission;
@ -1668,7 +1679,7 @@ pub async fn query_buy_order(
let base_asset_precision = exchange_info_vec
.iter()
// FIXME: find() should be position()
.find(|ExchangeInfo| ExchangeInfo.symbol == order.symbol)
.find(|exchange_info| exchange_info.symbol == order.symbol)
.unwrap()
.base_asset_precision;
let buy_price = decimal_div(cummulative_quote_qty, base_qty_ordered)
@ -1773,13 +1784,13 @@ pub async fn query_sell_order(
let quote_asset_precision = exchange_info_vec
.iter()
// FIXME: find() should be position()
.find(|ExchangeInfo| ExchangeInfo.symbol == order.symbol)
.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(|TradeFee| TradeFee.symbol == order.symbol)
.find(|trade_fee| trade_fee.symbol == order.symbol)
.unwrap()
.takercommission;
let get_usdt = rust_decimal::prelude::FromStr::from_str(

View File

@ -144,7 +144,7 @@ pub async fn collect_valid_usde_trade(
// build update values
let step_size_result = exchange_info_vec
.iter()
.position(|ExchangeInfo| ExchangeInfo.symbol == usdt_trade.symbol);
.position(|exchange_info| exchange_info.symbol == usdt_trade.symbol);
if step_size_result.is_some() {
let avg_price: Decimal =

View File

@ -722,6 +722,7 @@ async fn initialize_database() {
("id", "integer", Some("PK, AI, UN")),
("symbol", "char(20)", None),
("suggested_price", "decimal(16,8)", None),
("target_price", "decimal(16,8)", None),
("close_time", "bigint", None),
("registered_server_epoch", "bigint", None),
("registerer", "smallint", Some("UN")),
@ -918,6 +919,48 @@ async fn initialize_database() {
println!("Ok");
}
{
// sell_history
print!("table 'sell_history'...");
io::stdout().flush();
let table_name = String::from("sell_history");
let exists_result = exists_table(&table_name).await;
let initial_table = vec![
("id", "integer", Some("PK, AI, UN")),
("symbol", "char(20)", None),
("soldtime", "bigint", None),
("used_usdt", "decimal(16,8)", None),
("get_usdt", "decimal(16,8)", None),
("buy_price", "decimal(16,8)", None),
("sell_price", "decimal(16,8)", None),
("base_qty", "decimal(16,8)", None),
("pure_profit_percent", "double", None),
("pure_profit_usdt", "decimal(16,8)", None),
("registerer", "smallint", Some("UN")),
];
let table_condition = None;
if exists_result == false {
let mut result = new_table(&table_name, &initial_table, &table_condition).await;
if result.is_err() {
loop {
result = new_table(&table_name, &initial_table, &table_condition).await;
if result.is_ok() {
break;
}
sleep(Duration::from_millis(10)).await;
}
}
}
// else {
// delete_all_rows(&table_name)
// .await
// .expect("Failed to delete rows!");
// }
println!("Ok");
}
{
// asset_manage_announcement
print!("table 'asset_manage_announcement'...");
@ -982,7 +1025,7 @@ async fn initialize_database() {
}
if RUNNING_MODE == SIMUL {
assets_managing_team::add_extra_usdt(dec!(10000000000.0)).await;
assets_managing_team::add_extra_usdt(dec!(10000000.0)).await;
assets_managing_team::update_current_total_usdt().await;
} else {
let mut table_name = String::new();
@ -1092,6 +1135,7 @@ async fn initialize_database() {
("close_time", "bigint", None),
("suggested_price", "decimal(16,8)", None),
("current_price", "decimal(16,8)", None),
("target_price", "decimal(16,8)", None),
("registered_server_epoch", "bigint", None),
("profit_percent", "double", None),
("minimum_profit_percent", "double", None),
@ -1140,6 +1184,7 @@ async fn initialize_database() {
("expected_usdt_profit", "double", None),
("buy_price", "decimal(16,8)", None),
("current_price", "decimal(16,8)", None),
("target_price", "decimal(16,8)", None),
("base_qty_ordered", "decimal(16,8)", None),
("base_qty_fee_adjusted", "decimal(16,8)", None),
("pure_profit_percent", "double", None),
@ -1221,6 +1266,7 @@ async fn initialize_database() {
("get_usdt_fee_adjusted", "decimal(16,8)", None),
("buy_price", "decimal(16,8)", None),
("sell_price", "decimal(16,8)", None),
("target_price", "decimal(16,8)", None),
("base_qty_ordered", "decimal(16,8)", None),
("pure_profit_percent", "decimal(16,8)", None),
("maximum_profit_percent", "double", None),
@ -1260,6 +1306,7 @@ async fn initialize_database() {
get_usdt_fee_adjusted: Decimal::new(0, 8),
buy_price: Decimal::new(0, 8),
sell_price: Decimal::new(0, 8),
target_price: Decimal::new(0, 8),
base_qty_ordered: Decimal::new(0, 8),
pure_profit_percent: Decimal::new(0, 8),
maximum_profit_percent: 0.0,
@ -1393,46 +1440,4 @@ async fn initialize_database() {
}
println!("Ok");
}
{
// sell_history
print!("table 'sell_history'...");
io::stdout().flush();
let table_name = String::from("sell_history");
let exists_result = exists_table(&table_name).await;
let initial_table = vec![
("id", "integer", Some("PK, AI, UN")),
("symbol", "char(20)", None),
("soldtime", "bigint", None),
("used_usdt", "decimal(16,8)", None),
("get_usdt", "decimal(16,8)", None),
("buy_price", "decimal(16,8)", None),
("sell_price", "decimal(16,8)", None),
("base_qty", "decimal(16,8)", None),
("pure_profit_percent", "double", None),
("pure_profit_usdt", "decimal(16,8)", None),
("registerer", "smallint", Some("UN")),
];
let table_condition = None;
if exists_result == false {
let mut result = new_table(&table_name, &initial_table, &table_condition).await;
if result.is_err() {
loop {
result = new_table(&table_name, &initial_table, &table_condition).await;
if result.is_ok() {
break;
}
sleep(Duration::from_millis(10)).await;
}
}
}
// else {
// delete_all_rows(&table_name)
// .await
// .expect("Failed to delete rows!");
// }
println!("Ok");
}
}

View File

@ -279,7 +279,7 @@ pub async fn list_up_for_buy(
// }
// }
let a = filtered_symbols_arc.lock().await.clone();
super::strategy_manager::insert_pre_suggested_coins(3, false, &a, alldata).await;
super::strategy_manager::insert_pre_suggested_coins(3, false, &a, alldata, dec!(0)).await;
Ok(())
}
@ -305,10 +305,10 @@ pub async fn list_up_for_sell(
let lot_step_size_option = exchange_info_vec
.iter()
.find(|ExchangeInfo| ExchangeInfo.symbol == element.symbol);
.find(|exchange_info| exchange_info.symbol == element.symbol);
let quote_commission_precision_option = exchange_info_vec
.iter()
.find(|ExchangeInfo| ExchangeInfo.symbol == element.symbol);
.find(|exchange_info| exchange_info.symbol == element.symbol);
let opclo_30m_option = all_data
.rt_price_30m_vec

View File

@ -79,6 +79,7 @@ pub async fn insert_pre_suggested_coins(
is_long: bool,
filtered_symbols: &Vec<(String, i64, f64)>,
alldata: &AllData,
target_price: Decimal
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// Check the existance of record that is registered by this strategist
let mut dest_table_name = String::from("suggested_coin_list");
@ -96,6 +97,7 @@ pub async fn insert_pre_suggested_coins(
"close_time",
"suggested_price",
"current_price",
"target_price",
"registered_server_epoch",
"profit_percent",
"minimum_profit_percent",
@ -188,6 +190,7 @@ pub async fn insert_pre_suggested_coins(
filtered_element.1.to_string(), // close_time
filtered_element.2.to_string(), // suggested_price
filtered_element.2.to_string(), // current_price
target_price.to_string(), // target_price
server_epoch().await.to_string(), // registered_server_epoch
0.0.to_string(), // profit_percent
0.0.to_string(), // minimum_profit_percent
@ -272,6 +275,7 @@ pub async fn insert_pre_suggested_coins(
filtered_element.1.to_string(), // close_time
filtered_element.2.to_string(), // suggested_price
filtered_element.2.to_string(), // current_price
target_price.to_string(), // target_price
server_epoch().await.to_string(), // registered_server_epoch
0.0.to_string(), // profit_percent
0.0.to_string(), // minimum_profit_percent
@ -346,6 +350,7 @@ pub async fn insert_pre_suggested_coins(
filtered_element.1.to_string(), // close_time
filtered_element.2.to_string(), // suggested_price
filtered_element.2.to_string(), // current_price
target_price.to_string(), // target_price
server_epoch().await.to_string(), // registered_server_epoch
0.0.to_string(), // profit_percent
0.0.to_string(), // minimum_profit_percent
@ -404,6 +409,7 @@ pub async fn insert_pre_suggested_coins(
filtered_element.1.to_string(), // close_time
filtered_element.2.to_string(), // suggested_price
filtered_element.2.to_string(), // current_price
target_price.to_string(), // target_price
server_epoch().await.to_string(), // registered_server_epoch
0.0.to_string(), // profit_percent
0.0.to_string(), // minimum_profit_percent
@ -488,6 +494,7 @@ pub async fn insert_pre_suggested_coins(
filtered_element.1.to_string(), // close_time
filtered_element.2.to_string(), // suggested_price
filtered_element.2.to_string(), // current_price
target_price.to_string(), // target_price
server_epoch().await.to_string(), // registered_server_epoch
0.0.to_string(), // profit_percent
0.0.to_string(), // minimum_profit_percent
@ -556,6 +563,7 @@ pub async fn insert_pre_suggested_coins(
filtered_element.1.to_string(), // close_time
filtered_element.2.to_string(), // suggested_price
filtered_element.2.to_string(), // current_price
target_price.to_string(), // target_price
server_epoch().await.to_string(), // registered_server_epoch
0.0.to_string(), // profit_percent
0.0.to_string(), // minimum_profit_percent
@ -614,6 +622,7 @@ pub async fn insert_pre_suggested_coins(
filtered_element.1.to_string(), // close_time
filtered_element.2.to_string(), // suggested_price
filtered_element.2.to_string(), // current_price
target_price.to_string(), // target_price
server_epoch().await.to_string(), // registered_server_epoch
0.0.to_string(), // profit_percent
0.0.to_string(), // minimum_profit_percent
@ -637,6 +646,7 @@ pub async fn insert_pre_suggested_coins(
filtered_element.1.to_string(), // close_time
filtered_element.2.to_string(), // suggested_price
filtered_element.2.to_string(), // current_price
target_price.to_string(), // target_price
server_epoch().await.to_string(), // registered_server_epoch
0.0.to_string(), // profit_percent
0.0.to_string(), // minimum_profit_percent