use std::env;
use webull_unofficial::{models::*, Result, WebullClient};
#[tokio::main]
async fn main() -> Result<()> {
dotenv::dotenv().ok();
env_logger::init();
let username = env::var("WEBULL_USERNAME").expect("WEBULL_USERNAME not set");
let password = env::var("WEBULL_PASSWORD").expect("WEBULL_PASSWORD not set");
let mut client = WebullClient::new_paper(Some(6))?;
client
.login_with()
.username(&username)
.password(&password)
.await?;
println!("✓ Logged in to paper account");
let tickers = client.find_ticker("AAPL").await?;
if let Some(ticker) = tickers.first() {
let ticker_id = ticker.ticker_id;
println!("\n📊 Ticker found: {} (ID: {})", ticker.symbol, ticker_id);
let quote = client.get_quotes(&ticker_id.to_string()).await?;
println!("Current price: ${:.2}", quote.close);
println!("\n🎯 Demonstrating automatic order type detection:");
println!("\n1. Market Order (no prices):");
println!(" client.place_order_with()");
println!(" .ticker_id(ticker_id)");
println!(" .buy()");
println!(" .quantity(1.0)");
println!(" .await?");
println!(" → Detects: MARKET order");
println!("\n2. Limit Order (limit price only):");
println!(" client.place_order_with()");
println!(" .ticker_id(ticker_id)");
println!(" .limit(150.0) // Sets limit price");
println!(" .buy()");
println!(" .quantity(1.0)");
println!(" .await?");
println!(" → Detects: LIMIT order at $150.00");
println!("\n3. Stop Order (stop price only):");
println!(" client.place_order_with()");
println!(" .ticker_id(ticker_id)");
println!(" .stop(140.0) // Sets stop price");
println!(" .sell()");
println!(" .quantity(1.0)");
println!(" .await?");
println!(" → Detects: STOP order at $140.00");
println!("\n4. Stop-Limit Order (both prices):");
println!(" client.place_order_with()");
println!(" .ticker_id(ticker_id)");
println!(" .stop(140.0) // Sets stop price");
println!(" .limit(139.0) // Sets limit price");
println!(" .sell()");
println!(" .quantity(1.0)");
println!(" .await?");
println!(" → Detects: STOP-LIMIT order (stop: $140.00, limit: $139.00)");
println!("\n📝 Placing a test limit order (paper trading)...");
match client
.place_order_with()
.ticker_id(ticker_id)
.limit(quote.close * 0.95) .buy()
.quantity(1.0)
.time_in_force(TimeInForce::Day)
.await
{
Ok(order_id) => {
println!("✅ Order placed successfully!");
println!(" Order ID: {}", order_id);
println!(" Type: AUTO-DETECTED as LIMIT");
println!(" Price: ${:.2}", quote.close * 0.95);
}
Err(e) => {
println!("❌ Failed to place order: {}", e);
}
}
println!("\n💡 Note: You can still use explicit order type methods:");
println!(" • client.place_market_order_with()");
println!(" • client.place_limit_order_with(price)");
println!(" • client.place_stop_order_with(price)");
println!(" • client.place_stop_limit_order_with(stop, limit)");
} else {
println!("❌ Ticker not found");
}
Ok(())
}