Skip to content

Instantly share code, notes, and snippets.

View jayhuang75's full-sized avatar

jayhuang75 jayhuang75

View GitHub Profile
@jayhuang75
jayhuang75 / tradingbot.mod.rs
Created November 6, 2020 19:57
tradingbot.mod.rs
pub struct TradingBot {
pub trading_config: TradingConfig,
pub market: Box<dyn Market>,
}
@jayhuang75
jayhuang75 / tradingbot.market.rs
Created November 6, 2020 19:58
tradingbot.market.rs
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>>;
}
@jayhuang75
jayhuang75 / tradingbot_impl_pub.rs
Last active November 30, 2022 07:27
tradingbot_impl_pub
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;
@jayhuang75
jayhuang75 / place_buy.rs
Created November 8, 2020 05:28
place_buy.rs
// 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
);
@jayhuang75
jayhuang75 / coinbase_impl.rs
Last active November 8, 2020 15:14
coinbase_impl.rs
#[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))
@jayhuang75
jayhuang75 / tradingbot_main.rs
Created November 8, 2020 15:17
tradingbot_main.rs
// 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
@jayhuang75
jayhuang75 / go-train-delay-oauth.go
Created October 25, 2021 15:53
go-train-delay-oauth.go
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 {
@jayhuang75
jayhuang75 / go-train-delay-run.go
Last active October 25, 2021 16:01
go-train-delay-run.go
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
@jayhuang75
jayhuang75 / go-train-delay-inline-processor.go
Last active October 25, 2021 17:43
go-train-delay-inline-processor.go
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
@jayhuang75
jayhuang75 / go-train-delay-db-insert.go
Last active October 25, 2021 17:45
go-train-delay-db-insert.go
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,