how to get Top Gainers and Top Loser
Thank you for this wonderful package . Please how can i get the top Gainer & top losers using this package ?
Thank you
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);
@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 , 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 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.