pub struct Margin { /* private fields */ }Expand description
Margin Trading API client.
Provides access to Binance Margin SAPI endpoints for margin trading, loans, transfers, and account management.
§Example
let client = Binance::new("api_key", "secret_key")?;
// Get cross-margin account details
let account = client.margin().account().await?;
println!("Margin level: {}", account.margin_level);
// Check max borrowable
let max = client.margin().max_borrowable("BTC", None).await?;
println!("Max borrowable BTC: {}", max.amount);Implementations§
Source§impl Margin
impl Margin
Sourcepub async fn account(&self) -> Result<MarginAccountDetails>
pub async fn account(&self) -> Result<MarginAccountDetails>
Get cross-margin account details.
Returns margin account information including balances, margin level, and trading status.
§Example
let account = client.margin().account().await?;
println!("Margin level: {}", account.margin_level);
println!("Total assets (BTC): {}", account.total_asset_of_btc);
for asset in account.user_assets {
if asset.net_asset > 0.0 {
println!("{}: free={}, borrowed={}", asset.asset, asset.free, asset.borrowed);
}
}Sourcepub async fn isolated_account(
&self,
symbols: Option<&str>,
) -> Result<IsolatedMarginAccountDetails>
pub async fn isolated_account( &self, symbols: Option<&str>, ) -> Result<IsolatedMarginAccountDetails>
Get isolated margin account details.
§Arguments
symbols- Optional list of symbols to filter (comma-separated if multiple)
§Example
let account = client.margin().isolated_account(Some("BTCUSDT")).await?;
for asset in account.assets {
println!("{}: margin_level={}", asset.symbol, asset.margin_level);
}Sourcepub async fn max_borrowable(
&self,
asset: &str,
isolated_symbol: Option<&str>,
) -> Result<MaxBorrowableAmount>
pub async fn max_borrowable( &self, asset: &str, isolated_symbol: Option<&str>, ) -> Result<MaxBorrowableAmount>
Sourcepub async fn max_transferable(
&self,
asset: &str,
isolated_symbol: Option<&str>,
) -> Result<MaxTransferableAmount>
pub async fn max_transferable( &self, asset: &str, isolated_symbol: Option<&str>, ) -> Result<MaxTransferableAmount>
Sourcepub async fn isolated_account_limit(&self) -> Result<IsolatedAccountLimit>
pub async fn isolated_account_limit(&self) -> Result<IsolatedAccountLimit>
Sourcepub async fn transfer(
&self,
asset: &str,
amount: &str,
transfer_type: MarginTransferType,
) -> Result<TransactionId>
pub async fn transfer( &self, asset: &str, amount: &str, transfer_type: MarginTransferType, ) -> Result<TransactionId>
Execute a cross-margin transfer between spot and margin accounts.
§Arguments
asset- Asset to transferamount- Amount to transfertransfer_type- Direction of transfer
§Example
use binance_api_client::MarginTransferType;
// Transfer 100 USDT from spot to margin
let result = client.margin()
.transfer("USDT", "100.0", MarginTransferType::MainToMargin)
.await?;
println!("Transfer ID: {}", result.tran_id);Sourcepub async fn isolated_transfer(
&self,
asset: &str,
symbol: &str,
amount: &str,
trans_from: IsolatedMarginTransferType,
trans_to: IsolatedMarginTransferType,
) -> Result<TransactionId>
pub async fn isolated_transfer( &self, asset: &str, symbol: &str, amount: &str, trans_from: IsolatedMarginTransferType, trans_to: IsolatedMarginTransferType, ) -> Result<TransactionId>
Execute an isolated margin transfer.
§Arguments
asset- Asset to transfersymbol- Isolated margin symbolamount- Amount to transfertrans_from- Source account typetrans_to- Destination account type
§Example
use binance_api_client::IsolatedMarginTransferType;
// Transfer 100 USDT from spot to BTCUSDT isolated margin
let result = client.margin()
.isolated_transfer(
"USDT",
"BTCUSDT",
"100.0",
IsolatedMarginTransferType::Spot,
IsolatedMarginTransferType::IsolatedMargin,
)
.await?;Sourcepub async fn loan(
&self,
asset: &str,
amount: &str,
is_isolated: bool,
symbol: Option<&str>,
) -> Result<TransactionId>
pub async fn loan( &self, asset: &str, amount: &str, is_isolated: bool, symbol: Option<&str>, ) -> Result<TransactionId>
Apply for a margin loan.
§Arguments
asset- Asset to borrowamount- Amount to borrowis_isolated- Whether this is isolated marginsymbol- Symbol for isolated margin (required if is_isolated is true)
§Example
// Borrow 0.1 BTC on cross margin
let result = client.margin().loan("BTC", "0.1", false, None).await?;
println!("Loan transaction ID: {}", result.tran_id);Sourcepub async fn repay(
&self,
asset: &str,
amount: &str,
is_isolated: bool,
symbol: Option<&str>,
) -> Result<TransactionId>
pub async fn repay( &self, asset: &str, amount: &str, is_isolated: bool, symbol: Option<&str>, ) -> Result<TransactionId>
Repay a margin loan.
§Arguments
asset- Asset to repayamount- Amount to repayis_isolated- Whether this is isolated marginsymbol- Symbol for isolated margin (required if is_isolated is true)
§Example
// Repay 0.1 BTC on cross margin
let result = client.margin().repay("BTC", "0.1", false, None).await?;
println!("Repay transaction ID: {}", result.tran_id);Sourcepub async fn loan_records(
&self,
asset: &str,
isolated_symbol: Option<&str>,
start_time: Option<u64>,
end_time: Option<u64>,
current: Option<u32>,
size: Option<u32>,
) -> Result<RecordsQueryResult<LoanRecord>>
pub async fn loan_records( &self, asset: &str, isolated_symbol: Option<&str>, start_time: Option<u64>, end_time: Option<u64>, current: Option<u32>, size: Option<u32>, ) -> Result<RecordsQueryResult<LoanRecord>>
Get loan records.
§Arguments
asset- Asset to queryisolated_symbol- Isolated margin symbol (optional)start_time- Start timestamp (optional)end_time- End timestamp (optional)current- Page number (default 1)size- Page size (default 10, max 100)
§Example
let records = client.margin()
.loan_records("BTC", None, None, None, None, Some(20))
.await?;
for record in records.rows {
println!("Loan: {} {} at {}", record.principal, record.asset, record.timestamp);
}Sourcepub async fn repay_records(
&self,
asset: &str,
isolated_symbol: Option<&str>,
start_time: Option<u64>,
end_time: Option<u64>,
current: Option<u32>,
size: Option<u32>,
) -> Result<RecordsQueryResult<RepayRecord>>
pub async fn repay_records( &self, asset: &str, isolated_symbol: Option<&str>, start_time: Option<u64>, end_time: Option<u64>, current: Option<u32>, size: Option<u32>, ) -> Result<RecordsQueryResult<RepayRecord>>
Get repay records.
§Arguments
asset- Asset to queryisolated_symbol- Isolated margin symbol (optional)start_time- Start timestamp (optional)end_time- End timestamp (optional)current- Page number (default 1)size- Page size (default 10, max 100)
Sourcepub async fn create_order(
&self,
symbol: &str,
side: OrderSide,
order_type: OrderType,
quantity: Option<&str>,
quote_order_qty: Option<&str>,
price: Option<&str>,
stop_price: Option<&str>,
time_in_force: Option<TimeInForce>,
new_client_order_id: Option<&str>,
side_effect_type: Option<SideEffectType>,
is_isolated: Option<bool>,
) -> Result<MarginOrderResult>
pub async fn create_order( &self, symbol: &str, side: OrderSide, order_type: OrderType, quantity: Option<&str>, quote_order_qty: Option<&str>, price: Option<&str>, stop_price: Option<&str>, time_in_force: Option<TimeInForce>, new_client_order_id: Option<&str>, side_effect_type: Option<SideEffectType>, is_isolated: Option<bool>, ) -> Result<MarginOrderResult>
Create a new margin order.
§Arguments
symbol- Trading pair symbolside- Order side (Buy/Sell)order_type- Order typequantity- Order quantity (optional for some order types)quote_order_qty- Quote order quantity (optional)price- Price (required for limit orders)stop_price- Stop price (for stop orders)time_in_force- Time in force (optional)new_client_order_id- Client order ID (optional)side_effect_type- Side effect type (optional)is_isolated- Whether isolated margin (optional)
§Example
use binance_api_client::{OrderSide, OrderType, TimeInForce, SideEffectType};
let order = client.margin().create_order(
"BTCUSDT",
OrderSide::Buy,
OrderType::Limit,
Some("0.001"),
None,
Some("50000.00"),
None,
Some(TimeInForce::GTC),
None,
Some(SideEffectType::MarginBuy),
None,
).await?;Sourcepub async fn cancel_order(
&self,
symbol: &str,
order_id: Option<u64>,
orig_client_order_id: Option<&str>,
is_isolated: Option<bool>,
) -> Result<MarginOrderCancellation>
pub async fn cancel_order( &self, symbol: &str, order_id: Option<u64>, orig_client_order_id: Option<&str>, is_isolated: Option<bool>, ) -> Result<MarginOrderCancellation>
Cancel a margin order.
§Arguments
symbol- Trading pair symbolorder_id- Order ID (optional if orig_client_order_id provided)orig_client_order_id- Original client order ID (optional if order_id provided)is_isolated- Whether isolated margin
§Example
let result = client.margin()
.cancel_order("BTCUSDT", Some(12345), None, None)
.await?;Sourcepub async fn cancel_all_orders(
&self,
symbol: &str,
is_isolated: Option<bool>,
) -> Result<Vec<MarginOrderCancellation>>
pub async fn cancel_all_orders( &self, symbol: &str, is_isolated: Option<bool>, ) -> Result<Vec<MarginOrderCancellation>>
Sourcepub async fn get_order(
&self,
symbol: &str,
order_id: Option<u64>,
orig_client_order_id: Option<&str>,
is_isolated: Option<bool>,
) -> Result<MarginOrderState>
pub async fn get_order( &self, symbol: &str, order_id: Option<u64>, orig_client_order_id: Option<&str>, is_isolated: Option<bool>, ) -> Result<MarginOrderState>
Query a margin order.
§Arguments
symbol- Trading pair symbolorder_id- Order ID (optional)orig_client_order_id- Original client order ID (optional)is_isolated- Whether isolated margin
Sourcepub async fn open_orders(
&self,
symbol: Option<&str>,
is_isolated: Option<bool>,
) -> Result<Vec<MarginOrderState>>
pub async fn open_orders( &self, symbol: Option<&str>, is_isolated: Option<bool>, ) -> Result<Vec<MarginOrderState>>
Get all open margin orders.
§Arguments
symbol- Trading pair symbol (optional)is_isolated- Whether isolated margin
Sourcepub async fn all_orders(
&self,
symbol: &str,
order_id: Option<u64>,
start_time: Option<u64>,
end_time: Option<u64>,
limit: Option<u32>,
is_isolated: Option<bool>,
) -> Result<Vec<MarginOrderState>>
pub async fn all_orders( &self, symbol: &str, order_id: Option<u64>, start_time: Option<u64>, end_time: Option<u64>, limit: Option<u32>, is_isolated: Option<bool>, ) -> Result<Vec<MarginOrderState>>
Get all margin orders (active, cancelled, or filled).
§Arguments
symbol- Trading pair symbolorder_id- Order ID to start from (optional)start_time- Start timestamp (optional)end_time- End timestamp (optional)limit- Number of records (default 500, max 500)is_isolated- Whether isolated margin
Sourcepub async fn my_trades(
&self,
symbol: &str,
order_id: Option<u64>,
start_time: Option<u64>,
end_time: Option<u64>,
from_id: Option<u64>,
limit: Option<u32>,
is_isolated: Option<bool>,
) -> Result<Vec<MarginTrade>>
pub async fn my_trades( &self, symbol: &str, order_id: Option<u64>, start_time: Option<u64>, end_time: Option<u64>, from_id: Option<u64>, limit: Option<u32>, is_isolated: Option<bool>, ) -> Result<Vec<MarginTrade>>
Get margin trades.
§Arguments
symbol- Trading pair symbolorder_id- Filter by order ID (optional)start_time- Start timestamp (optional)end_time- End timestamp (optional)from_id- Trade ID to start from (optional)limit- Number of records (default 500, max 1000)is_isolated- Whether isolated margin
Sourcepub async fn interest_history(
&self,
asset: Option<&str>,
isolated_symbol: Option<&str>,
start_time: Option<u64>,
end_time: Option<u64>,
current: Option<u32>,
size: Option<u32>,
) -> Result<RecordsQueryResult<InterestHistoryRecord>>
pub async fn interest_history( &self, asset: Option<&str>, isolated_symbol: Option<&str>, start_time: Option<u64>, end_time: Option<u64>, current: Option<u32>, size: Option<u32>, ) -> Result<RecordsQueryResult<InterestHistoryRecord>>
Get interest history.
§Arguments
asset- Asset to query (optional)isolated_symbol- Isolated margin symbol (optional)start_time- Start timestamp (optional)end_time- End timestamp (optional)current- Page number (default 1)size- Page size (default 10, max 100)
Sourcepub async fn interest_rate_history(
&self,
asset: &str,
vip_level: Option<u32>,
start_time: Option<u64>,
end_time: Option<u64>,
limit: Option<u32>,
) -> Result<Vec<InterestRateRecord>>
pub async fn interest_rate_history( &self, asset: &str, vip_level: Option<u32>, start_time: Option<u64>, end_time: Option<u64>, limit: Option<u32>, ) -> Result<Vec<InterestRateRecord>>
Get interest rate history.
§Arguments
asset- Asset to queryvip_level- VIP level (optional, default uses user’s vip level)start_time- Start timestamp (optional)end_time- End timestamp (optional)limit- Number of records (default 20, max 100)
Sourcepub async fn pair(&self, symbol: &str) -> Result<MarginPairDetails>
pub async fn pair(&self, symbol: &str) -> Result<MarginPairDetails>
Sourcepub async fn all_pairs(&self) -> Result<Vec<MarginPairDetails>>
pub async fn all_pairs(&self) -> Result<Vec<MarginPairDetails>>
Get all cross margin pairs.
Sourcepub async fn asset(&self, asset: &str) -> Result<MarginAssetInfo>
pub async fn asset(&self, asset: &str) -> Result<MarginAssetInfo>
Sourcepub async fn all_assets(&self) -> Result<Vec<MarginAssetInfo>>
pub async fn all_assets(&self) -> Result<Vec<MarginAssetInfo>>
Get all margin assets info.
Sourcepub async fn price_index(&self, symbol: &str) -> Result<MarginPriceIndex>
pub async fn price_index(&self, symbol: &str) -> Result<MarginPriceIndex>
Sourcepub async fn bnb_burn_status(&self) -> Result<BnbBurnStatus>
pub async fn bnb_burn_status(&self) -> Result<BnbBurnStatus>
Get BNB burn status for spot trading and margin interest.
Sourcepub async fn toggle_bnb_burn(
&self,
spot_bnb_burn: Option<bool>,
interest_bnb_burn: Option<bool>,
) -> Result<BnbBurnStatus>
pub async fn toggle_bnb_burn( &self, spot_bnb_burn: Option<bool>, interest_bnb_burn: Option<bool>, ) -> Result<BnbBurnStatus>
Toggle BNB burn on spot trade and margin interest.
§Arguments
spot_bnb_burn- Enable BNB for spot trading fees (optional)interest_bnb_burn- Enable BNB for margin interest (optional)