I am trying to get my trade history from the Bybit api V5, but it keeps returning an empty list although getting my wallet balance works.
I wrote this code below and although the "getBybitWalletBalance" function works perfectly, the "getBybitTradeHistory" function keeps returning an empty list. My last trade on BTCUSDT perpetuals was a month ago. I also tried the other categories, but they all give the same output.
bybit.php
<?php
$apiKey = '...';
$apiSecret = '...';
$window = "5000";
// Global
function bybitSignature($method, $path, $query, $body) {
global $apiKey, $apiSecret, $window;
$timestamp = round(microtime(true) * 1000);
if($method === "GET") {
return hash_hmac("sha256", $timestamp . $apiKey . $window . $query, $apiSecret);
} else {
return hash_hmac("sha256", $timestamp . $apiKey . $window . $body, $apiSecret);
}
}
function bybitCurl($url, $signature) {
global $apiKey, $window;
$timestamp = round(microtime(true) * 1000);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json",
"X-BAPI-API-KEY: " . $apiKey,
"X-BAPI-TIMESTAMP: " . $timestamp,
"X-BAPI-SIGN: " . $signature,
"X-BAPI-RECV-WINDOW: " . $window
));
if(curl_errno($ch)) {
return curl_error($ch);
} else {
return curl_exec($ch);
}
curl_close($ch);
}
// Trade
function getBybitTradeHistory($category) {
return bybitCurl("https://api.bybit.com/v5/execution/list?category=" . $category, bybitSignature("GET", "/v5/execution/list", "category=" . $category, ""));
}
// Account
function getBybitWalletBalance($accountType) {
return bybitCurl("https://api.bybit.com/v5/account/wallet-balance?accountType=" . $accountType, bybitSignature("GET", "/v5/account/wallet-balance", "accountType=" . $accountType, ""));
}
var_dump(json_decode(getBybitTradeHistory("linear"), true));
?>
output
array(5) { ["retCode"]=> int(0) ["retMsg"]=> string(2) "OK" ["result"]=> array(3) { ["nextPageCursor"]=> string(0) "" ["category"]=> string(6) "linear" ["list"]=> array(0) { } } ["retExtInfo"]=> array(0) { } ["time"]=> int(1751725711326) }
I have no experience with ByBit API, but the documentation is easy to find and pretty clear. It describes request parameters startTime
and endTime
, and that if:
startTime
andendTime
are not passed, return 7 days by default
Your code does not pass either parameter, so you're only getting the last 7 days' history. You mention your last trade was a month ago, so it won't show up.
The docs describe how to use the parameters, and that you can retrieve at most a 7-day window of history. Depending on what you're trying to do you could either set up a single window including your last trade or maybe do some kind of pagination, where you can scroll back week by week.
Here's a quick and dirty way to at least see your last trade, assuming your last trade was 30 days ago.
// The docs show that using only startTime means we will get a window
// between startTime and startTime+7 days, so if we pick a start day
// say 33 days ago, that window will include your last trade 30 days
// ago. This is likely not a good real solution for whatever your
// app is trying to do but will at least get you a result and
// familiarity with using the timestamps.
$now = new DateTime();
$startTime = $now->modify('-33 days')->format('Uv');
$query = "category=" . $category . "&startTime=" . $startTime;
echo bybitCurl("https://api.bybit.com/v5/execution/list?" . $query,
bybitSignature("GET", "/v5/execution/list", $query));