Skip to content

πŸ“œ Get Orders History (Last N Days)ΒΆ

History method: retrieves orders history for the last N days. Simplified alternative to manual timestamp calculation.

API Information:

  • Sugar method: MT5Sugar.getOrdersHistoryLastDays(int days, String symbol)
  • Returns: OrdersHistoryData - Proto message with historical orders
  • Underlying: MT5Service.orderHistory() with auto-calculated timestamps

πŸ”½ InputΒΆ

Parameter Type Required Description
days int βœ… Number of days back to retrieve
symbol String βšͺ Symbol filter (null = all symbols)

⬆️ OutputΒΆ

Returns: Mt5TermApiAccountHelper.OrdersHistoryData - Historical orders data

Throws: ApiExceptionMT5 if request fails

Execution: - Automatically calculates time range: (now - days) to now - Sorts by close time descending (newest first) - No pagination (offset=0, limit=0 = all results)


πŸ’¬ Just the essentialsΒΆ

  • What it is. Get order history for last N days without manual timestamps.
  • Why you need it. Simpler than calculating Unix timestamps manually.
  • Use case. Daily reports, performance analysis, trade review.

πŸ”— Usage ExamplesΒΆ

1) Last 7 days historyΒΆ

var history = sugar.getOrdersHistoryLastDays(7, null);

System.out.printf("Orders from last 7 days: %d%n",
    history.getOpenedOrderHistoriesCount());

for (var order : history.getOpenedOrderHistoriesList()) {
    System.out.printf("  #%d %s %.2f lots @ %.5f%n",
        order.getTicket(),
        order.getSymbol(),
        order.getVolume(),
        order.getClosePrice());
}

2) Today's orders for symbolΒΆ

String symbol = "EURUSD";
var history = sugar.getOrdersHistoryLastDays(1, symbol);

System.out.printf("Today's %s orders: %d%n",
    symbol, history.getOpenedOrderHistoriesCount());

3) Last 30 days performanceΒΆ

var history = sugar.getOrdersHistoryLastDays(30, null);

double totalProfit = 0.0;
int wins = 0;
int losses = 0;

for (var order : history.getOpenedOrderHistoriesList()) {
    double profit = order.getProfit();
    totalProfit += profit;

    if (profit > 0) wins++;
    else if (profit < 0) losses++;
}

System.out.printf("Last 30 days:%n");
System.out.printf("  Total P/L: $%.2f%n", totalProfit);
System.out.printf("  Wins: %d | Losses: %d%n", wins, losses);
System.out.printf("  Win rate: %.1f%%%n", (wins * 100.0) / (wins + losses));

πŸ“Œ Important NotesΒΆ

  • Time range: From (now - days) to now
  • Symbol filter: Pass null for all symbols
  • Sort order: Newest first (close time descending)
  • All results: No pagination applied

See alsoΒΆ