Longbridge Developers
Get Started

Estimate Maximum Purchase Quantity

This API is used for estimating the maximum purchase quantity for Hong Kong and US stocks, warrants, and options.

>_ CLI
longbridge max-qty TSLA.US

Request

HTTP MethodGET
HTTP URL/v1/trade/estimate/buy_limit

Parameters

Content-Type: application/json; charset=utf-8

NameTypeRequiredDescription
symbolstringYESStock code, using ticker.region format, for example: AAPL.US
order_typestringYESOrder Type
pricestringNOEstimated order price, for example: 388.5
sidestringYESOrder side

Enum Value
Buy - Buy
Sell - Sell (Short selling is only supported for US stocks)
currencystringNOSettlement currency
order_idstringNOOrder ID, required when estimating the maximum purchase quantity for a modified order

Request Example

from longbridge.openapi import TradeContext, Config, OrderType, OrderSide, OAuthBuilder

oauth = OAuthBuilder("your-client-id").build(lambda url: print("Visit:", url))
config = Config.from_oauth(oauth)
ctx = TradeContext(config)

resp = ctx.estimate_max_purchase_quantity(
    symbol = "700.HK",
    order_type = OrderType.LO,
    side = OrderSide.Buy,
)
print(resp)
import asyncio
from longbridge.openapi import AsyncTradeContext, Config, OrderType, OrderSide, OAuthBuilder

async def main() -> None:
    oauth = await OAuthBuilder("your-client-id").build_async(lambda url: print("Visit:", url))
    config = Config.from_oauth(oauth)
    ctx = AsyncTradeContext.create(config)

    resp = await ctx.estimate_max_purchase_quantity(
        symbol = "700.HK",
        order_type = OrderType.LO,
        side = OrderSide.Buy,
    )
    print(resp)

if __name__ == "__main__":
    asyncio.run(main())
const { Config, TradeContext, OAuth, OrderType, OrderSide, Decimal } = require('longbridge')

async function main() {
  const oauth = await OAuth.build('your-client-id', (_, url) => {
    console.log('Open this URL to authorize: ' + url)
  })
  const config = Config.fromOAuth(oauth)
  const ctx = TradeContext.new(config)
  const resp = await ctx.estimateMaxPurchaseQuantity({
    symbol: '700.HK',
    orderType: OrderType.LO,
    side: OrderSide.Buy,
    price: new Decimal('400'),
    fractionalShares: false,
  })
  console.log(resp)
}
main().catch(console.error)
import com.longbridge.*;
import com.longbridge.trade.*;
import java.math.BigDecimal;
class Main {
    public static void main(String[] args) throws Exception {
        try (OAuth oauth = new OAuthBuilder("your-client-id").build(url -> System.out.println("Open to authorize: " + url)).get();
             Config config = Config.fromOAuth(oauth);
             TradeContext ctx = TradeContext.create(config)) {
            EstimateMaxPurchaseQuantityResponse resp = ctx.getEstimateMaxPurchaseQuantity(new EstimateMaxPurchaseQuantityOptions("700.HK", OrderType.LO, OrderSide.Buy).setPrice(new BigDecimal("400"))).get();
            System.out.println(resp);
        }
    }
}
use std::sync::Arc;
use longbridge::{oauth::OAuthBuilder, trade::{TradeContext, EstimateMaxPurchaseQuantityOptions, OrderType, OrderSide}, Config};
use rust_decimal::Decimal;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let oauth = OAuthBuilder::new("your-client-id").build(|url| println!("Open this URL to authorize: {url}")).await?;
    let config = Arc::new(Config::from_oauth(oauth));
    let (ctx, _) = TradeContext::new(config);
    let resp = ctx.estimate_max_purchase_quantity(
        EstimateMaxPurchaseQuantityOptions::new("700.HK", OrderType::LO, OrderSide::Buy)
            .price(Decimal::from(400))
    ).await?;
    println!("{:?}", resp);
    Ok(())
}
#include &lt;iostream&gt;
#include <longbridge.hpp>

#ifdef WIN32
#include <windows.h>
#endif

using namespace longbridge;
using namespace longbridge::trade;

static void
run(const OAuth& oauth)
{
    Config config = Config::from_oauth(oauth);
    TradeContext ctx = TradeContext::create(config);

    EstimateMaxPurchaseQuantityOptions opts{"700.HK", OrderType::LO, OrderSide::Buy, Decimal(400.0), 100};
    ctx.estimate_max_purchase_quantity(opts, [](auto res) {
        if (!res) { std::cout << "failed" << std::endl; return; }
        std::cout << "max_cash_buy: " << res->max_cash_buy << std::endl;
    });
}

int main(int argc, char const* argv[]) {
#ifdef WIN32
    SetConsoleOutputCP(CP_UTF8);
#endif

    const std::string client_id = "your-client-id";
    OAuthBuilder(client_id).build(
    [](const std::string& url) {
        std::cout << "Open this URL to authorize: " << url << std::endl;
    },
    [](auto res) {
        if (!res) {
            std::cout << "authorization failed: " << *res.status().message() << std::endl;
            return;
        }
        run(*res);
    });

    std::cin.get();
    return 0;
}
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/longbridge/openapi-go/config"
	"github.com/longbridge/openapi-go/oauth"
	"github.com/longbridge/openapi-go/trade"
	"github.com/shopspring/decimal"
)

func main() {
	o := oauth.New("your-client-id").
		OnOpenURL(func(url string) { fmt.Println("Open this URL to authorize:", url) })
	if err := o.Build(context.Background()); err != nil {
		log.Fatal(err)
	}
	conf, err := config.New(config.WithOAuthClient(o))
	if err != nil {
		log.Fatal(err)
	}
	tctx, err := trade.NewFromCfg(conf)
	if err != nil {
		log.Fatal(err)
	}
	defer tctx.Close()
	resp, err := tctx.EstimateMaxPurchaseQuantity(context.Background(), &trade.GetEstimateMaxPurchaseQuantity{
		Symbol:    "AAPL.US",
		OrderType: trade.OrderTypeLO,
		Price:     decimal.NewFromFloat(175.62),
		Currency:  "USD",
		Side:      trade.OrderSideBuy,
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("max_cash_buy:", resp.MaxCashBuy)
}

Response

Response Headers

  • Content-Type: application/json

Response Example

{
  "code": 0,
  "message": "success",
  "data": {
    "cash_max_qty": "100",
    "margin_max_qty": "100"
  }
}

Response Status

StatusDescriptionSchema
200Estimate Maximum Purchase Quantity Successestimate_available_buy_limit_rsp
400The query failed with an error in the request parameter.None

Schemas

estimate_available_buy_limit_rsp

Estimated Maximum Purchase Quantity

NameTypeRequiredDescription
cash_max_qtystringtrueCash available quantity, default value is empty string.
margin_max_qtystringtrueMargin available quantity, default value is empty string.