MarketBookGet HOW
Example from file: examples/demos/lowlevel/01_general_operations.goΒΆ
The
MarketBookGet()method makes a one-time request to retrieve a market depth snapshot (Depth of Market, DOM) for a specified symbol. If the broker supports DOM, price levels and order volumes are returned.
π§© Code exampleΒΆ
fmt.Println("\n6.2. MarketBookGet() - Get DOM snapshot")
fmt.Println(" Testing if data is actually available...")
bookGetCtx, bookGetCancel := context.WithTimeout(context.Background(), 3*time.Second)
defer bookGetCancel()
marketBookGetReq := &pb.MarketBookGetRequest{
Symbol: cfg.TestSymbol, // symbol from config.json (test_symbol parameter)
}
marketBookGetData, err := account.MarketBookGet(bookGetCtx, marketBookGetReq)
if err != nil {
fmt.Println(" β No data available (timeout)")
fmt.Println(" β This is EXPECTED for forex pairs on demo accounts")
fmt.Println(" β Not an error - just a limitation of forex market structure")
} else {
if len(marketBookGetData.Book) > 0 {
fmt.Printf(" β
SUCCESS! Received %d price levels\n", len(marketBookGetData.Book))
fmt.Println(" β This is UNUSUAL for forex - broker provides limited DOM")
fmt.Println()
maxShow := 5
if len(marketBookGetData.Book) < maxShow {
maxShow = len(marketBookGetData.Book)
}
fmt.Println(" First few levels:")
for i := 0; i < maxShow; i++ {
level := marketBookGetData.Book[i]
bookType := "SELL"
if level.Type == pb.BookType_BOOK_TYPE_BUY {
bookType = "BUY"
}
fmt.Printf(" [%d] %s Price: %.5f Volume: %.2f\n", i+1, bookType, level.Price, level.VolumeDouble)
}
} else {
fmt.Println(" β οΈ Call succeeded but no data returned")
fmt.Println(" β Broker has no DOM data to show")
}
}
π’ Detailed Code ExplanationΒΆ
- Create context with timeout:
bookGetCtx, bookGetCancel := context.WithTimeout(context.Background(), 3*time.Second)
defer bookGetCancel()
A 3-second timeout is needed to avoid hanging if the server doesn't provide data.
- Form the request:
The symbol is taken from the config.json configuration file (test_symbol field).
- Send the request:
Returns the Book array β a list of price levels with volumes.
- Error (no data):
Means the broker doesn't provide DOM (common on demo or forex pairs).
- Data available:
if len(marketBookGetData.Book) > 0 {
fmt.Printf(" β
SUCCESS! Received %d price levels\n", len(marketBookGetData.Book))
}
Displays the number of order book levels. This is rare for forex.
- Display first levels:
for i := 0; i < maxShow; i++ {
level := marketBookGetData.Book[i]
fmt.Printf("[%d] %s Price: %.5f Volume: %.2f\n", i+1, bookType, level.Price, level.VolumeDouble)
}
Shows the type (BUY or SELL), price, and volume for the first 5 levels.
π¦ What the Server ReturnsΒΆ
message MarketBookGetData {
repeated BookStruct Book = 1;
}
message BookStruct {
BookType Type = 1; // BUY or SELL
double Price = 2; // price level
int64 Volume = 3; // volume at level (deprecated)
double VolumeDouble = 4; // volume at level with extended precision
}
Example Successful OutputΒΆ
β
SUCCESS! Received 5 price levels
β This is UNUSUAL for forex - broker provides limited DOM
First few levels:
[1] SELL Price: 1.08610 Volume: 50000.00
[2] SELL Price: 1.08608 Volume: 30000.00
[3] BUY Price: 1.08595 Volume: 40000.00
[4] BUY Price: 1.08592 Volume: 20000.00
If No Data AvailableΒΆ
This is normal: on forex and demo accounts, brokers usually don't form an order book.
π§ What It's Used ForΒΆ
The MarketBookGet() method is used for:
- one-time retrieval of current DOM snapshot;
- checking if the broker supports market depth;
- liquidity analysis or API testing.