This file contains hidden or 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
| pub struct TradingBot { | |
| pub trading_config: TradingConfig, | |
| pub market: Box<dyn Market>, | |
| } |
This file contains hidden or 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
| pub trait Market { | |
| async fn get_balances(&self) -> Result<f32, Box<dyn Error>>; | |
| async fn get_market_price(&self) -> Result<f32, Box<dyn Error>>; | |
| async fn place_sell_order(&self, amount: f32) -> Result<f32, Box<dyn Error>>; | |
| async fn place_buy_order(&self, amount: f32) -> Result<f32, Box<dyn Error>>; | |
| } |
This file contains hidden or 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; |
This file contains hidden or 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
| // get the buy point | |
| // buy action | |
| async fn try_to_buy(&mut self, diff: f32) -> Result<f32, Box<dyn Error>> { | |
| if diff >= self.trading_config.upward_trend_threshold | |
| || diff <= self.trading_config.dip_threshold { | |
| let current_balance = self.get_balances().await?; | |
| info!( | |
| "[BALANCE] current account balance {:?} $ USD", | |
| current_balance | |
| ); |
This file contains hidden or 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
| #[async_trait(?Send)] | |
| impl Market for Coinbase { | |
| async fn get_balances(&self) -> Result<f32, Box<dyn Error>> { | |
| // coinbase API call | |
| Ok(1.0) | |
| } | |
| async fn get_market_price(&self) -> Result<f32, Box<dyn Error>> { | |
| // coinbase API call | |
| let mut rng = rand::thread_rng(); | |
| Ok(rng.gen_range(0.0, 10.0)) |
This file contains hidden or 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
| // inital the TradingBot for coinbase context | |
| let mut coinbase_bot = bot::TradingBot::new(new_config, Box::new(bot::coinbase::Coinbase {})); | |
| // set the interval for every 20s | |
| let trading_cadence = env::var("TRADING_CADENCE").unwrap().parse::<u64>().unwrap(); | |
| let mut interval = time::interval(time::Duration::from_secs(trading_cadence)); | |
| loop { | |
| // wait every 20s |
This file contains hidden or 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
| type App struct { | |
| srv *gmail.Service | |
| userEmail string | |
| googleAPIVersion string | |
| db *sqlx.DB | |
| } | |
| func NewApp(userEmail, credential string) *App { | |
| srv, err := newGmailClient(userEmail, credential) | |
| if err != nil { |
This file contains hidden or 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
| func (a *App) Run(total int64) ([]DelayDataSet, error) { | |
| // get the User email list | |
| email_list, err := a.getUserEmailList(total) | |
| if err != nil { | |
| return nil, err | |
| } | |
| // processing the email get dataset | |
| delayDataSet, err := a.processEmail(email_list, total) | |
| if err != nil { | |
| return nil, err |
This file contains hidden or 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
| func (a *App) processEmail(email_list *gmail.ListMessagesResponse, total int64) ([]DelayDataSet, error) { | |
| delayDataSet := []DelayDataSet{} | |
| bar := processBar(total, "[email] processing...") | |
| for _, l := range email_list.Messages { | |
| log.Debugf("[processEmail] processing email id: %s\n", l.Id) | |
| msg, err := a.getEmail(l.Id) | |
| if err != nil { | |
| log.Fatalf("[processEmail] Unable to retrieve msg: %v", err) | |
| } | |
| // reduce part |
This file contains hidden or 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
| func (db *DB) Insert(data []DelayDataSet) { | |
| tx := db.Pool.MustBegin() | |
| bar := processBar(int64(len(data)), "[db] inserting...") | |
| for _, d := range data { | |
| tx.MustExec( | |
| "INSERT INTO delays (date, departure, depart_scheduled, destination, arrival_scheduled, delay) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (date) DO NOTHING", | |
| d.Date, | |
| d.Departure.Station, |