Last active
November 30, 2022 07:27
-
-
Save jayhuang75/e658c4959cbfdd5c18d9bbcd02159189 to your computer and use it in GitHub Desktop.
tradingbot_impl_pub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl TradingBot { | |
// main trading logic | |
// high sell, low buy | |
pub async fn start(&mut self) -> Result<(), Box<dyn Error>> { | |
let current_price = self.get_market_price().await?; | |
info!("[PRICE] current market price: {:?} $", current_price); | |
let percentage_diff = (current_price - self.trading_config.last_operation_price) | |
/ self.trading_config.last_operation_price | |
* 100 as f32; | |
info!("[PRICE] percentage_diff: {:?} $", percentage_diff); | |
// based on operation state for the buy and sell action | |
match self.trading_config.next_operation { | |
State::BUY => { | |
self.trading_config.last_operation_price = self.try_to_buy(percentage_diff).await?; | |
} | |
State::SELL => { | |
self.trading_config.last_operation_price = | |
self.try_to_sell(percentage_diff).await?; | |
} | |
} | |
Ok(()) | |
} | |
pub fn new(trading_config: TradingConfig, market: Box<dyn Market>) -> Self { | |
TradingBot { | |
trading_config, | |
market, | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment