Skip to content

TradeStats

Aggregated trading performance โ€” profit factor, win rates, drawdowns, expectancy and more, in one call.

API Information:

  • Low-level API: MT4Account.TradeStats(ctx, from, fromUtc) (defined in mt4/MT4Account.go)
  • gRPC service: mt4_term_api.AccountHelper
  • RPC: TradeStats(...) โ†’ ...Reply
  • Payload: StatsData

๐Ÿ’ฌ Just the essentials

  • What it is. A performance report computed from the account's closed trades.
  • Why you need it. KPIs (profit factor, expectancy, drawdown, win %) without crunching history yourself.
  • When to use. Dashboards, periodic reports, strategy health checks.

๐ŸŽฏ Purpose

  • Profitability KPIs: profit factor, GHPR, expectancy, Sharpe
  • Win rates for longs and shorts
  • Balance / equity drawdown (raw and relative)
  • Totals: trades, lots, pips, deposits/withdrawals
  • Best / worst trade

Method Signature

func (a *MT4Account) TradeStats(ctx context.Context, from, fromUtc *string) (*pb.StatsData, error)

Request message:

message TradeStatsRequest {
  optional string from = 1;       // server-time "yyyy-MM-ddTHH:mm:ss"; nil = since account start
  optional string from_utc = 2;   // same moment in UTC โ€” send one of from / from_utc
}

Payload message (excerpt):

message StatsData {
  int64  trades = ...;
  double profit_factor = ...;
  double sharpe_ratio = ...;
  double growth_percent = ...;
  double max_balance_drawdown_relative = ...;
  double max_equity_drawdown_relative = ...;
  ExpectancyData expectancy = ...;
  WonData longs_won = ...;
  WonData shorts_won = ...;
  ProfitData best_trade = ...;
  ProfitData worst_trade = ...;
  repeated MarketTradeCountData markets = ...;
  // โ€ฆ and more (see getters below)
}

๐Ÿ”ฝ Input

Parameter Type Required Description
ctx context.Context yes Cancellation / timeout.
from *string no Server-time start "yyyy-MM-ddTHH:mm:ss"; nil = since account start.
fromUtc *string no Same start in UTC. Send one of from / fromUtc, not both.

โฌ†๏ธ Output โ€” *pb.StatsData

Field Proto Type Go getter Description
trades int64 GetTrades() int64 Number of closed trades
profit_factor double GetProfitFactor() float64 Gross profit / gross loss
sharpe_ratio double GetSharpeRatio() float64 Risk-adjusted return
growth_percent double GetGrowthPercent() float64 Account growth %
ghpr double GetGhpr() float64 Geometric holding-period return
pips / lots double GetPips() / GetLots() Totals
max_balance_drawdown_relative double GetMaxBalanceDrawdownRelative() float64 Worst balance drawdown (%)
max_equity_drawdown_relative double GetMaxEquityDrawdownRelative() float64 Worst equity drawdown (%)
expectancy ExpectancyData GetExpectancy() โ†’ GetDollar(), GetPips()
longs_won / shorts_won WonData GetLongsWon() / GetShortsWon() โ†’ GetAll(), GetWonCount(), GetWonPersent()
best_trade / worst_trade ProfitData GetBestTrade() / GetWorstTrade() โ†’ GetProfit(), GetDate()
markets repeated MarketTradeCountData GetMarkets() Per-symbol trade counts

On failure the call returns a non-nil error (text "API error (code=โ€ฆ): โ€ฆ") โ€” see the Return Codes Reference.


๐Ÿงฉ Notes & Tips

  • from is server time, formatted "yyyy-MM-ddTHH:mm:ss"; use fromUtc to pass UTC instead.
  • Nested getters are nillable. GetExpectancy() / GetBestTrade() can be nil on an empty history โ€” guard before dereferencing.
  • Note the spelling โ€” WonData.GetWonPersent() and ProfitData.GetTiket() are as generated.

๐Ÿ”— Usage Examples

1) Performance summary

func perfSummary(ctx context.Context, acc *mt4.MT4Account) error {
    st, err := acc.TradeStats(ctx, nil, nil)
    if err != nil { return err }
    fmt.Printf("trades=%d  PF=%.2f  growth=%.1f%%  maxDD=%.1f%%\n",
        st.GetTrades(), st.GetProfitFactor(), st.GetGrowthPercent(),
        st.GetMaxEquityDrawdownRelative())
    if e := st.GetExpectancy(); e != nil {
        fmt.Printf("expectancy: %.2f$ / %.1f pips per trade\n", e.GetDollar(), e.GetPips())
    }
    return nil
}

2) Which markets get traded most?

func topMarkets(ctx context.Context, acc *mt4.MT4Account) error {
    st, err := acc.TradeStats(ctx, nil, nil)
    if err != nil { return err }
    for _, m := range st.GetMarkets() {
        fmt.Printf("%s: %d trades  win %.0f%%  profit %+.2f\n",
            m.GetMarketName(), m.GetCount(), m.GetWinRate(), m.GetProfit())
    }
    return nil
}

3) Month-to-date health check

func monthToDate(ctx context.Context, acc *mt4.MT4Account) (bool, error) {
    from := time.Now().Format("2006-01") + "-01T00:00:00"
    st, err := acc.TradeStats(ctx, &from, nil)
    if err != nil { return false, err }
    healthy := st.GetProfitFactor() >= 1.0 && st.GetMaxEquityDrawdownRelative() < 20.0
    return healthy, nil
}

โš ๏ธ Pitfalls

  • Empty history. With no closed trades the ratios are 0 and nested messages may be nil โ€” guard.
  • Two time axes. Don't send both from and fromUtc.
  • Server vs UTC. A naive from is interpreted as server time; mind the broker offset.

๐Ÿงช Testing Suggestions

  • Happy path (demo with history). GetTrades() > 0; GetProfitFactor() finite; nested getters non-nil.
  • Windowed. Pass a recent from and confirm the counts shrink vs the all-time call.
  • Empty window. A future from yields GetTrades() == 0 and no panic on nested getters.

๐Ÿ“š See also