OnSymbolTick (RECOMMENDED)¶
Real-time bid/ask/last ticks for one or more symbols, pushed as they change.
API Information:
- Low-level API:
MT4Account.OnSymbolTick(ctx, symbols)(defined inmt4/MT4Account.go) - gRPC service:
mt4_term_api.Subscriptions - RPC:
OnSymbolTick(OnSymbolTickRequest) โ stream OnSymbolTickReply - Payload:
OnSymbolTickDataโGetSymbolTick()โOnSymbolMqlTickInfo
๐ฌ Just the essentials¶
- What it is. A live tick feed โ one frame per price change.
- Why you need it. Drive pricing, trailing, dashboards without polling.
- When to use. Any time you need the current market as it moves.
๐ฏ Purpose¶
- Stream live bid/ask/last for a set of symbols
- React to price changes in real time
- Feed trailing/breakeven and live charts
- Auto-reconnects on transient transport errors
Method Signature¶
func (a *MT4Account) OnSymbolTick(ctx context.Context, symbols []string) (<-chan *pb.OnSymbolTickData, <-chan error)
Request message:
message OnSymbolTickRequest { repeated string symbol_names = 1; }
Payload message:
message OnSymbolTickData { OnSymbolMqlTickInfo symbol_tick = 1; }
message OnSymbolMqlTickInfo {
string symbol = ...;
double bid = ...; double ask = ...; double last = ...;
uint64 volume = ...;
Timestamp time = ...; Timestamp time_utc = ...;
}
๐ฝ Input¶
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx |
context.Context |
yes | Cancel it to unsubscribe and close the channels. |
symbols |
[]string |
yes | Symbols to stream, e.g. []string{"EURUSD","USDJPY"}. |
โฌ๏ธ Frame โ *pb.OnSymbolTickData¶
GetSymbolTick() returns *pb.OnSymbolMqlTickInfo:
| Field | Proto Type | Go getter | Description |
|---|---|---|---|
symbol |
string | GetSymbol() string |
Symbol |
bid / ask |
double | GetBid() / GetAsk() |
Best bid / ask |
last |
double | GetLast() float64 |
Last trade price |
volume |
uint64 | GetVolume() uint64 |
Tick volume |
time / time_utc |
Timestamp | GetTime() / GetTimeUtc() |
Tick time (server / UTC) |
A non-nil value on the error channel ends the stream. Both channels close when
ctxis cancelled.
๐งฉ Notes & Tips¶
- Subscribe once per symbol set โ pass every symbol in the one slice.
cancel()unsubscribes โ the returned channels close.- Auto-reconnects on
codes.Unavailable; you keep reading the same channel. - Guard the tick โ
GetSymbolTick()can benilon a keep-alive frame.
๐ Usage Examples¶
1) Stream ticks for 10 seconds¶
func streamTicks(ctx context.Context, acc *mt4.MT4Account, symbols []string) error {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
data, errs := acc.OnSymbolTick(ctx, symbols)
for {
select {
case d := <-data:
t := d.GetSymbolTick()
if t == nil { continue } // keep-alive
fmt.Printf("%s %.5f / %.5f\n", t.GetSymbol(), t.GetBid(), t.GetAsk())
case err := <-errs:
if err != nil { return err }
case <-ctx.Done():
return nil
}
}
}
2) Take the first N ticks, then stop¶
func firstN(ctx context.Context, acc *mt4.MT4Account, symbol string, n int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
data, errs := acc.OnSymbolTick(ctx, []string{symbol})
for got := 0; got < n; {
select {
case d := <-data:
if t := d.GetSymbolTick(); t != nil {
fmt.Printf("tick %d: %.5f\n", got+1, t.GetBid())
got++
}
case err := <-errs:
if err != nil { return err }
}
}
return nil // cancel() unsubscribes
}
3) Keep a latest-price cache¶
func priceCache(ctx context.Context, acc *mt4.MT4Account, symbols []string, cache map[string]float64) error {
data, errs := acc.OnSymbolTick(ctx, symbols)
for {
select {
case d := <-data:
if t := d.GetSymbolTick(); t != nil {
cache[t.GetSymbol()] = (t.GetBid() + t.GetAsk()) / 2
}
case err := <-errs:
if err != nil { return err }
case <-ctx.Done():
return nil
}
}
}
โ ๏ธ Pitfalls¶
- Nil tick on keep-alive frames โ guard before use.
- High frequency. Don't block the
select; offload heavy work to another goroutine. - Leaking the context leaks the stream goroutine โ always
defer cancel().
๐งช Testing Suggestions¶
- Happy path (demo, market open). Frames arrive within seconds;
GetBid()/GetAsk()are positive. - Cancel. Cancelling the context ends the loop and closes the channels.
- Idle market. On a closed market, expect few/no frames (not an error).