MT4Sugar — 9. Order Info¶
Query open orders and positions, filter them, and summarize profit/exposure.
Reads return []*pb.OpenedOrderInfo (key getters: GetTicket(), GetSymbol(), GetOrderType(), GetLots(), GetOpenPrice(), GetProfit(), GetStopLoss(), GetTakeProfit(), GetMagicNumber(), GetSwap(), GetCommision()). Orders = everything; Positions = open market orders only.
Orders(ctx) · Positions(ctx)¶
func (s *MT4Sugar) Orders(ctx context.Context) ([]*pb.OpenedOrderInfo, error)
func (s *MT4Sugar) Positions(ctx context.Context) ([]*pb.OpenedOrderInfo, error)
Orders returns all open market + pending orders; Positions keeps only the market ones (MT4's "positions").
Example — sum floating P/L across positions:
var pl float64
for _, o := range must(sugar.Positions(ctx)) {
pl += o.GetProfit() + o.GetSwap() + o.GetCommision()
}
fmt.Printf("floating P/L: %+.2f\n", pl)
Example — list everything open:
for _, o := range must(sugar.Orders(ctx)) {
fmt.Printf("#%d %s %s %.2f\n", o.GetTicket(), o.GetSymbol(), o.GetOrderType(), o.GetLots())
}
OrdersForSymbol(ctx, symbol) · OrdersByMagic(ctx, magic)¶
func (s *MT4Sugar) OrdersForSymbol(ctx context.Context, symbol string) ([]*pb.OpenedOrderInfo, error)
func (s *MT4Sugar) OrdersByMagic(ctx context.Context, magic int32) ([]*pb.OpenedOrderInfo, error)
Open orders filtered by symbol, or by magic number.
Example — count my bot's EURUSD orders:
mine, _ := sugar.OrdersByMagic(ctx, 777)
fmt.Printf("%d orders tagged 777\n", len(mine))
FindTicket(ctx, ticket)¶
func (s *MT4Sugar) FindTicket(ctx context.Context, ticket int32) (*pb.OpenedOrderInfo, error)
One open order by ticket, or nil if it isn't open (used by the trailing/waiter helpers).
Example — is the order still open?
if o, _ := sugar.FindTicket(ctx, ticket); o == nil {
fmt.Println("closed")
}
TotalProfit(ctx, symbol) · PositionsValue(ctx, symbol, magic)¶
func (s *MT4Sugar) TotalProfit(ctx context.Context, symbol string) (float64, error)
func (s *MT4Sugar) PositionsValue(ctx context.Context, symbol string, magic int32) (float64, error)
Floating value across open positions, including swap and commission. TotalProfit optionally scopes to a symbol ("" = all); PositionsValue filters by symbol and/or magic ("" / magic < 0 = no filter on that axis).
Example — P/L for one symbol:
pl, _ := sugar.TotalProfit(ctx, "EURUSD")
Example — value of my bot's EURUSD positions:
v, _ := sugar.PositionsValue(ctx, "EURUSD", 777)
ExposureSummary(ctx)¶
func (s *MT4Sugar) ExposureSummary(ctx context.Context) (map[string]float64, error)
Net lots per symbol across open market positions — buys positive, sells negative (a hedged symbol nets toward zero).
Example — print net exposure per symbol:
for sym, net := range must(sugar.ExposureSummary(ctx)) {
fmt.Printf("%s: net %+.2f lots\n", sym, net)
}
GetAccountInfo(ctx)¶
func (s *MT4Sugar) GetAccountInfo(ctx context.Context) (*pb.AccountSummaryData, error)
The account snapshot (AccountSummary) — GetAccountBalance(), GetAccountEquity(), GetAccountCurrency(), … See MT4Account · AccountSummary.
Example — balance and equity:
a, _ := sugar.GetAccountInfo(ctx)
fmt.Printf("balance %.2f equity %.2f %s\n",
a.GetAccountBalance(), a.GetAccountEquity(), a.GetAccountCurrency())
See also: 8. Watchers & Waiters · MT4Sugar — Overview