php-binance-api icon indicating copy to clipboard operation
php-binance-api copied to clipboard

how to get Top Gainers and Top Loser

Open trimmytech opened this issue 4 years ago • 4 comments

Thank you for this wonderful package . Please how can i get the top Gainer & top losers using this package ?

Thank you

trimmytech avatar Mar 19 '21 10:03 trimmytech

I'm not sure if it's the best way, but this works. I'm getting all tickers and sorting on 24h changes. Losers come at the beginning, gainers at the end.

$changes = [];
$prices = $this->api->prevDay();
foreach($prices as $item) {
  $symbol = $item['symbol'];
  $change_24h_percent = (float) $item['priceChangePercent'];
  $changes[$symbol] = $change_24h_percent;
}

// Sort numeric on percentage change
asort($changes, SORT_NUMERIC);

$top_loser = array_key_first($changes);
$top_gainer = array_key_last($changes);

var_dump($top_loser, $top_gainer);

royarisse avatar Mar 20 '21 15:03 royarisse

@trimmytech Were you looking for what @royarisse gave as an answer, so on the best and worse symbols? Or were you looking for https://coinmarketcap.com/gainers-losers/ ?

ePascalC avatar Mar 23 '21 11:03 ePascalC

@ePascalC , yes something like what he posted but i cant seems to get it for the last maybe 15mins, 20mins or any time i want , i managed to to one by. combining his code with ticker but to get ticker details for each symbol (usually 1000+) remotely takes a lot of time, i am hoping there is a better solution for this

trimmytech avatar Mar 23 '21 11:03 trimmytech

@trimmytech From what I see, the Binance API is only providing the 24h changes in one block for all symbols, so the above code is the only one that exist. If you want them in a shorter period you will have create a routine yourself looping the symbols of your interest. Of course this takes time AND you might quickly run out of allowed actions per second/minute towards Binance.

ePascalC avatar Mar 23 '21 12:03 ePascalC