From b5c8da8e623de210ac30c8c2ee230a2bfef591f1 Mon Sep 17 00:00:00 2001 From: "T.Todua" <7117978+ttodua@users.noreply.github.com> Date: Mon, 26 Dec 2022 17:44:42 +0400 Subject: [PATCH 1/2] duplicated ticker examples remove --- .../async-multiple-tickers-one-exchange.php | 28 -------- examples/php/coinbasepro-fetch-ticker.php | 28 -------- ...coinbasepro-sandbox-fetch-ticker-async.php | 71 ------------------- .../php/coinbasepro-sandbox-fetch-ticker.php | 62 ---------------- examples/php/coinone-fetch-tickers.php | 23 ------ examples/php/fetch-ticker.php | 19 ++++- examples/php/fetch-tickers.php | 29 ++++++-- examples/php/gdax-fetch-ticker.php | 28 -------- examples/php/gdax-sandbox-fetch-ticker.php | 64 ----------------- 9 files changed, 40 insertions(+), 312 deletions(-) delete mode 100644 examples/php/async-multiple-tickers-one-exchange.php delete mode 100644 examples/php/coinbasepro-fetch-ticker.php delete mode 100644 examples/php/coinbasepro-sandbox-fetch-ticker-async.php delete mode 100644 examples/php/coinbasepro-sandbox-fetch-ticker.php delete mode 100644 examples/php/coinone-fetch-tickers.php delete mode 100644 examples/php/gdax-fetch-ticker.php delete mode 100644 examples/php/gdax-sandbox-fetch-ticker.php diff --git a/examples/php/async-multiple-tickers-one-exchange.php b/examples/php/async-multiple-tickers-one-exchange.php deleted file mode 100644 index ad97bdace97f..000000000000 --- a/examples/php/async-multiple-tickers-one-exchange.php +++ /dev/null @@ -1,28 +0,0 @@ - true, -]); - -$exchange::$kernel->execute(function () use ($exchange) { - $symbols = array('ETH/BTC', 'LTC/BTC', 'BCH/BTC'); - try { - $yields = []; - foreach ($symbols as $symbol) { - $yields[] = $exchange->fetch_ticker($symbol); - } - $tickers = yield $yields; - foreach ($tickers as $ticker) { - echo $ticker['symbol'] . ' bid price is ' . $ticker['bid'] . PHP_EOL; - } - } catch (Exception $e) { - var_dump($e); - } -}); -$exchange::$kernel->run(); \ No newline at end of file diff --git a/examples/php/coinbasepro-fetch-ticker.php b/examples/php/coinbasepro-fetch-ticker.php deleted file mode 100644 index d74fb8cad911..000000000000 --- a/examples/php/coinbasepro-fetch-ticker.php +++ /dev/null @@ -1,28 +0,0 @@ -fetch_ticker($symbol); - - var_dump($result); - -} catch (\ccxt\NetworkError $e) { - echo '[Network Error] ' . $e->getMessage() . "\n"; -} catch (\ccxt\ExchangeError $e) { - echo '[Exchange Error] ' . $e->getMessage() . "\n"; -} catch (Exception $e) { - echo '[Error] ' . $e->getMessage() . "\n"; -} - -?> diff --git a/examples/php/coinbasepro-sandbox-fetch-ticker-async.php b/examples/php/coinbasepro-sandbox-fetch-ticker-async.php deleted file mode 100644 index 085398425dbc..000000000000 --- a/examples/php/coinbasepro-sandbox-fetch-ticker-async.php +++ /dev/null @@ -1,71 +0,0 @@ -execute(function() use ($loop, $kernel) { - - $exchange = new \ccxt_async\coinbasepro(array( - 'loop' => $loop, - 'kernel' => $kernel, - )); - - $exchange->urls['api'] = $exchange->urls['test']; - - // preload the markets first - - try { - - yield $exchange->load_markets(); - - } catch (\ccxt\BaseError $e) { - echo 'Failed to load markets: ' . $e->getMessage() . "\n"; - } catch (Exception $e) { - echo '[Error] ' . $e->getMessage() . "\n"; - } - - $symbol = 'ETH/BTC'; - $market = null; - - // check if the market in question is available - - try { - $market = $exchange->market($symbol); - } catch (\ccxt\BaseError $e) { - echo $exchange->id . ' does not have market symbol ' . $symbol . "!\n"; - echo 'Markets symbols supported by ' . $exchange->id . ":\n"; - echo print_r($exchange->symbols, true) . "\n"; - exit (); - } - - if ($market['active']) { - - try { - - $result = yield $exchange->fetch_ticker($symbol); - var_dump($result); - - } catch (\ccxt\NetworkError $e) { - echo '[Network Error] ' . $e->getMessage() . "\n"; - } catch (\ccxt\ExchangeError $e) { - echo '[Exchange Error] ' . $e->getMessage() . "\n"; - } catch (Exception $e) { - echo '[Error] ' . $e->getMessage() . "\n"; - } - - } else { - - echo $exchange->id . ' market ' . $symbol . " is inactive!\n"; - exit(); - } -}, $loop); - -$kernel->run(); diff --git a/examples/php/coinbasepro-sandbox-fetch-ticker.php b/examples/php/coinbasepro-sandbox-fetch-ticker.php deleted file mode 100644 index 5a951aea0cbb..000000000000 --- a/examples/php/coinbasepro-sandbox-fetch-ticker.php +++ /dev/null @@ -1,62 +0,0 @@ -set_sandbox_mode(true); - -// preload the markets first - -try { - - $exchange->load_markets(); - -} catch (\ccxt\BaseError $e) { - echo 'Failed to load markets: ' . $e->getMessage() . "\n"; -} catch (Exception $e) { - echo '[Error] ' . $e->getMessage() . "\n"; -} - -$symbol = 'ETH/BTC'; -$market = null; - -// check if the market in question is available - -try { - $market = $exchange->market($symbol); -} catch (\ccxt\BaseError $e) { - echo $exchange->id . ' does not have market symbol ' . $symbol . "!\n"; - echo 'Markets symbols supported by ' . $exchange->id . ":\n"; - echo print_r($exchange->symbols, true) . "\n"; - exit(); -} - -if($market['active']) { - - try { - - $result = $exchange->fetch_ticker($symbol); - var_dump($result); - - } catch (\ccxt\NetworkError $e) { - echo '[Network Error] ' . $e->getMessage() . "\n"; - } catch (\ccxt\ExchangeError $e) { - echo '[Exchange Error] ' . $e->getMessage() . "\n"; - } catch (Exception $e) { - echo '[Error] ' . $e->getMessage() . "\n"; - } - -} else { - - echo $exchange->id . ' market ' . $symbol . " is inactive!\n"; - exit(); -} - -?> diff --git a/examples/php/coinone-fetch-tickers.php b/examples/php/coinone-fetch-tickers.php deleted file mode 100644 index ddeb8ce6758c..000000000000 --- a/examples/php/coinone-fetch-tickers.php +++ /dev/null @@ -1,23 +0,0 @@ - true, // uncomment for verbose output -)); - -// fetch all -$tickers = $exchange->fetch_tickers(); -var_dump ($tickers); - -echo "\n"; - -// fetch one by one -$markets = $exchange->load_markets(); -foreach ($markets as $symbol => $m) { - var_dump($exchange->fetch_ticker($symbol)); -} - -?> diff --git a/examples/php/fetch-ticker.php b/examples/php/fetch-ticker.php index f57df633286b..ee87871abc82 100644 --- a/examples/php/fetch-ticker.php +++ b/examples/php/fetch-ticker.php @@ -10,11 +10,26 @@ // 'verbose' => true, // for debugging 'timeout' => 30000, )); + +// If you want to use test-mode (a.k.a. sandbox), uncomment the following line: +// $exchange->set_sandbox_mode(true); + $symbol = 'ETH/USDT'; try { - $result = $exchange->fetch_ticker($symbol); - echo "Ticker: " . $result['symbol'] . ', 24hr high: '. $result['high']. "\n"; + if (array_key_exists($symbol, $exchange->markets)) { + $market = $exchange->market($symbol); + } else { + echo $exchange->id . ' does not have market symbol ' . $symbol . "; Supported symbols:\n"; + echo print_r($exchange->symbols, true) . "\n"; + exit(); + } + if($market['active']) { + $result = $exchange->fetch_ticker($symbol); + echo "Ticker: " . $result['symbol'] . ', 24hr high: '. $result['high']. "\n"; + } else { + echo $exchange->id . ' market ' . $symbol . " is not active!\n"; + } } catch (Exception $e) { if ($e instanceof \ccxt\NetworkError) { echo '[Network Error] ' . $e->getMessage() . "\n"; diff --git a/examples/php/fetch-tickers.php b/examples/php/fetch-tickers.php index 4416fb6d0223..fe28254f8ec1 100644 --- a/examples/php/fetch-tickers.php +++ b/examples/php/fetch-tickers.php @@ -11,13 +11,30 @@ 'timeout' => 30000, )); +$markets = $exchange->load_markets(); + try { - $result = $exchange->fetch_tickers (); // note, don't call it for binance more than once in every few seconds. - print_r ($result); -} catch (\ccxt\NetworkError $e) { - echo '[Network Error] ' . $e->getMessage() . "\n"; + if ($exchange->has['fetchTickers']) { + // one API call for all tickers (preferred way) + $result = $exchange->fetch_tickers (); // note, don't call it for specifically binance more than once in every few seconds. + echo "Called fetchTickers() for all tickers at once. Results count: " . count($result) . "\n"; + } else if ($exchange->has['fetchTicker']) { + // Individual API calls for all tickers one by one (non-preferred way) + echo "fetchTickers() is not supported by " . $exchange->id . ", calling individual fetchTicker() for each symbol instead.\n"; + // fetch one by one (not recommended) + $i = 0; + $test_symbols_amount = 4; + foreach ($markets as $symbol => $m) { + if ($i++ && $i > $test_symbols_amount) { + echo "Stopping after getting " . $test_symbols_amount . " test symbols.\n"; + break; + } + $result = $exchange->fetch_ticker($symbol); + echo "Fetched ticker for " . $result['symbol'] . ", 24hr high: " . $result['high'] . "\n"; + } + } else { + echo "fetchTicker/s() not supported by " . $exchange->id . ", skipping.\n"; + } } catch (Exception $e) { echo '[Error] ' . $e->getMessage() . "\n"; } - -?> \ No newline at end of file diff --git a/examples/php/gdax-fetch-ticker.php b/examples/php/gdax-fetch-ticker.php deleted file mode 100644 index a45026de269a..000000000000 --- a/examples/php/gdax-fetch-ticker.php +++ /dev/null @@ -1,28 +0,0 @@ -fetch_ticker($symbol); - - var_dump($result); - -} catch (\ccxt\NetworkError $e) { - echo '[Network Error] ' . $e->getMessage() . "\n"; -} catch (\ccxt\ExchangeError $e) { - echo '[Exchange Error] ' . $e->getMessage() . "\n"; -} catch (Exception $e) { - echo '[Error] ' . $e->getMessage() . "\n"; -} - -?> diff --git a/examples/php/gdax-sandbox-fetch-ticker.php b/examples/php/gdax-sandbox-fetch-ticker.php deleted file mode 100644 index ad0a6190dd3b..000000000000 --- a/examples/php/gdax-sandbox-fetch-ticker.php +++ /dev/null @@ -1,64 +0,0 @@ - array ( - 'api' => 'https://api-public.sandbox.gdax.com', - ), -)); - -// preload the markets first - -try { - - $exchange->load_markets(); - -} catch (\ccxt\BaseError $e) { - echo 'Failed to load markets: ' . $e->getMessage() . "\n"; -} catch (Exception $e) { - echo '[Error] ' . $e->getMessage() . "\n"; -} - -$symbol = 'ETH/BTC'; -$market = null; - -// check if the market in question is available - -try { - $market = $exchange->market($symbol); -} catch (\ccxt\BaseError $e) { - echo $exchange->id . ' does not have market symbol ' . $symbol . "!\n"; - echo 'Markets symbols supported by ' . $exchange->id . ":\n"; - echo print_r($exchange->symbols, true) . "\n"; - exit (); -} - -if ($market['active']) { - - try { - - $result = $exchange->fetch_ticker($symbol); - var_dump($result); - - } catch (\ccxt\NetworkError $e) { - echo '[Network Error] ' . $e->getMessage() . "\n"; - } catch (\ccxt\ExchangeError $e) { - echo '[Exchange Error] ' . $e->getMessage() . "\n"; - } catch (Exception $e) { - echo '[Error] ' . $e->getMessage() . "\n"; - } - -} else { - - echo $exchange->id . ' market ' . $symbol . " is inactive!\n"; - exit(); -} - -?> From 0db962e2a42f7cb85a159c2e68a9d1607abdcef9 Mon Sep 17 00:00:00 2001 From: "T.Todua" <7117978+ttodua@users.noreply.github.com> Date: Mon, 26 Dec 2022 18:03:07 +0400 Subject: [PATCH 2/2] fix tickers --- examples/php/async-await-fetch-multiple.php | 22 +++++++++++++++++++ ...{async-await.php => async-await-fetch.php} | 0 2 files changed, 22 insertions(+) create mode 100644 examples/php/async-await-fetch-multiple.php rename examples/php/{async-await.php => async-await-fetch.php} (100%) diff --git a/examples/php/async-await-fetch-multiple.php b/examples/php/async-await-fetch-multiple.php new file mode 100644 index 000000000000..49abb40cce0b --- /dev/null +++ b/examples/php/async-await-fetch-multiple.php @@ -0,0 +1,22 @@ +load_markets()); +$symbols = array('BTC/USDT', 'ETH/USDT', 'DOGE/USDT'); + + +echo "########### Combined await ###########\n"; +$promises = []; +foreach ($symbols as $symbol) { + $promises[] = $exchange->fetch_ticker($symbol); +} +$tickers = await(all($promises)); + +echo "{$tickers[0]['symbol']} {$tickers[0]['close']} | {$tickers[1]['symbol']} {$tickers[1]['close']} | {$tickers[21]['symbol']} {$tickers[2]['close']}\n"; diff --git a/examples/php/async-await.php b/examples/php/async-await-fetch.php similarity index 100% rename from examples/php/async-await.php rename to examples/php/async-await-fetch.php