MarketBookAdd HOW
Example from file: examples/demos/lowlevel/01_general_operations.goΒΆ
The
MarketBookAdd()method subscribes to Depth of Market (DOM) β market depth for a specified symbol. DOM shows real order levels (Bid/Ask levels, volumes, and prices), if the broker provides them.
π§© Code exampleΒΆ
// Use short timeout
bookAddCtx, bookAddCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer bookAddCancel()
marketBookAddReq := &pb.MarketBookAddRequest{
Symbol: cfg.TestSymbol, // symbol from config.json (test_symbol parameter)
}
marketBookAddData, err := account.MarketBookAdd(bookAddCtx, marketBookAddReq)
if err != nil {
fmt.Printf(" β Subscription failed: %v\n", err)
fmt.Println(" β Broker does not support DOM for this symbol")
} else {
if marketBookAddData.Success {
fmt.Printf(" β Subscription accepted for '%s'\n", cfg.TestSymbol)
fmt.Println(" β This doesn't mean data will be available")
} else {
fmt.Println(" β Subscription rejected by broker")
}
}
π’ Detailed Code ExplanationΒΆ
bookAddCtx, bookAddCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer bookAddCancel()
A context with a 5-second timeout is created to prevent the request from hanging if the server doesn't respond.
A DOM subscription request is formed for the test symbol (e.g., EURUSD).
The cfg.TestSymbol field is taken from the config.json configuration file.
A gRPC request is sent to MetaTrader. The server returns a flag indicating whether the DOM subscription for the specified instrument was successful.
if err != nil {
fmt.Printf(" β Subscription failed: %v\n", err)
fmt.Println(" β Broker does not support DOM for this symbol")
} else {
if marketBookAddData.Success {
fmt.Printf(" β Subscription accepted for '%s'\n", cfg.TestSymbol)
fmt.Println(" β This doesn't mean data will be available")
} else {
fmt.Println(" β Subscription rejected by broker")
}
}
Handling possible scenarios:
- Error (
err) β broker or server does not support DOM. - Subscription accepted (
Success = true) β broker allowed subscription, but doesn't guarantee data availability. - Subscription rejected β broker explicitly refused (common on demo or forex accounts).
π What is Depth of Market (DOM)ΒΆ
DOM (market depth) β a table of all available Bid and Ask levels, reflecting current limit orders from participants. It shows not only current buy/sell prices, but also volumes at each level.
DOM is usually available for:
- exchange instruments (futures, stocks, CFDs);
- instruments with real order book from liquidity providers.
β οΈ Why DOM is Often Unavailable on Forex and DemoΒΆ
- Forex is an OTC market, there is no centralized order book. Therefore, brokers usually don't publish order levels.
- On demo accounts, brokers often don't form real DOM to reduce server load.
- Not all brokers support Level II data β some provide only Bid/Ask.
π‘ What to Do if DOM is UnavailableΒΆ
- Try another instrument (e.g., stock CFDs or futures).
- Check on a real account, not demo.
- Ask your broker if the server supports Depth of Market.
π¬ In Simple TermsΒΆ
MarketBookAdd()attempts to connect to the market depth data stream. If the broker doesn't support DOM for the selected symbol, the server will return an error or empty subscription. This is normal behavior for forex and demo servers.