Skip to content

MT4Service — 2. Orders & History

Open orders and closed history — with wrapper payloads unwrapped to Go slices where it's trivial, and the sort enum passed directly.


OpenedOrders(ctx)

func (s *MT4Service) OpenedOrders(ctx context.Context) ([]*pb.OpenedOrderInfo, error)

Open market + pending orders, already unwrapped to []*pb.OpenedOrderInfo (the service drops the wrapper OpenedOrdersData and calls GetOrderInfos() for you).

Returns: []*pb.OpenedOrderInfo — getters GetTicket() (int32), GetSymbol(), GetOrderType() (OpenedOrderType 0 Buy…5 SellStop), GetLots(), GetOpenPrice(), GetStopLoss(), GetTakeProfit(), GetProfit(), GetSwap(), GetCommision(), GetMagicNumber(), GetComment().

import pb "github.com/MetaRPC/GoMT4/package"

open, _ := svc.OpenedOrders(ctx)
positions := 0
for _, o := range open {
    if o.GetOrderType() == pb.OpenedOrderType_OO_OP_BUY || o.GetOrderType() == pb.OpenedOrderType_OO_OP_SELL {
        positions++
    }
}
fmt.Printf("%d orders, %d positions\n", len(open), positions)

OpenedOrdersTickets(ctx)

func (s *MT4Service) OpenedOrdersTickets(ctx context.Context) ([]int32, error)

Just the ticket numbers of open/pending orders — a lightweight "what's open?" check. Unwrapped to []int32.

tickets, _ := svc.OpenedOrdersTickets(ctx)
fmt.Printf("open tickets: %v\n", tickets)

OrdersHistory(ctx, sortType, from, to)

func (s *MT4Service) OrdersHistory(ctx context.Context, sortType pb.EnumOrderHistorySortType, from, to *time.Time) (*pb.OrdersHistoryData, error)

Closed / cancelled orders over a time window. The service fills the paging defaults; you pass the sort enum + bounds.

Args: sortType — pb.EnumOrderHistorySortType (e.g. …_HISTORY_SORT_BY_CLOSE_TIME_DESC); from / to — *time.Time server-time bounds (nil = open-ended).

Returns: *pb.OrdersHistoryData → GetOrdersInfo() ([]*HistoryOrderInfo), GetTotalCount(), GetNextPageToken().

import (
    "time"
    pb "github.com/MetaRPC/GoMT4/package"
)

now := time.Now()
from := now.Add(-7 * 24 * time.Hour)
h, _ := svc.OrdersHistory(ctx, pb.EnumOrderHistorySortType_HISTORY_SORT_BY_CLOSE_TIME_DESC, &from, &now)

var realized float64
for _, o := range h.GetOrdersInfo() {
    realized += o.GetProfit() + o.GetSwap() + o.GetCommision()
}
fmt.Printf("%d closed, realized %+.2f\n", h.GetTotalCount(), realized)

Paging: if GetNextPageToken() is non-empty there are more rows — fetch the next page via the raw MT4Account layer.


See also: MT4Service — Overview · 3. Symbols & Quotes · MT4Account overview