Errors and empty data
Handle invalid configs, failed requests, empty datasets, rate limits and refused access when downloading market data with dukascopy-node.
dukascopy-node depends on Dukascopy’s servers, which occasionally fail a request or return no data. By default a failed request stops the download with an error and an empty response is accepted as-is. The options below let you decide what should happen instead.
Retry failed requests
Section titled “Retry failed requests”Set retryCount (--retries) to retry a request that fails, for example with a network error or a non-200 status. pauseBetweenRetriesMs (--retry-pause, 500 ms by default) sets the wait between attempts. A refused request (HTTP 403) is never retried: see access refused.
npx dukascopy-node -i usdcad -from 2023-05-11 -to 2023-05-12 -t tick -f csv --retries 5const data = await getHistoricalRates({ instrument: 'usdcad', dates: { from: '2023-05-11', to: '2023-05-12' }, timeframe: 'tick', retryCount: 5, pauseBetweenRetriesMs: 1000});Retry empty data
Section titled “Retry empty data”Sometimes a request succeeds but contains no data. That is normal for weekends, holidays and periods before an instrument started trading, but it can also be a temporary glitch. With retryOnEmpty (--retry-on-empty), an empty dataset counts as a failed attempt and is retried like any other failure:
npx dukascopy-node -i usdcad -from 2023-05-11 -to 2023-05-12 -t tick -f csv --retries 5 --retry-on-emptyCaution
With retryOnEmpty, a period that is genuinely empty fails after its last attempt. Combine it
with failAfterRetryCount: false (below) if your range can include weekends or holidays.
Keep going after failures
Section titled “Keep going after failures”By default, a request that still fails after all attempts stops the download. Set failAfterRetryCount to false (--no-fail-after-retries in the CLI) to skip that period and continue. This also applies when retryCount is 0:
npx dukascopy-node -i usdcad -from 2023-05-11 -to 2023-05-12 -t tick -f csv \ --retries 5 --retry-on-empty --no-fail-after-retriesThe result then simply has no rows for the skipped periods, so check for gaps if completeness matters.
Rate limits
Section titled “Rate limits”A RateLimitError has its own recovery and is not retried again by retryCount: Dukascopy kept answering HTTP 429 after rateLimitRetryCount retries. See rate limiting.
Access refused
Section titled “Access refused”If Dukascopy refuses a request with HTTP 403, the download stops right away: no further requests are sent and nothing is retried, whatever retryCount is. It fails with a DukascopyAccessDeniedError, which has code: 'DUKASCOPY_ACCESS_DENIED', status: 403 and the refused url.
Check that Dukascopy’s historical data export page works in a regular browser, and update dukascopy-node to the latest version. The next download starts fresh.
Caution
With failAfterRetryCount: false, this error is not thrown: the periods after the refusal come
back empty and the download still completes. Keep the default true to see the error.
Handling errors in Node.js
Section titled “Handling errors in Node.js”The error classes are exported, so you can react to each case:
import { DukascopyAccessDeniedError, getHistoricalRates, RateLimitError } from 'dukascopy-node';
try { const data = await getHistoricalRates({ instrument: 'eurusd', dates: { from: '2024-01-01', to: '2024-02-01' }, timeframe: 'm1', retryCount: 3 });} catch (error) { if (error instanceof RateLimitError) { console.error(`Rate limited, retry after ${error.retryAfterMs ?? 'a while'} ms`); } else if (error instanceof DukascopyAccessDeniedError) { console.error(`Access refused (HTTP ${error.status}): ${error.url}`); } else if (error?.validationErrors) { console.error('Invalid config:', error.validationErrors); } else { throw error; }}Invalid config
Section titled “Invalid config”The config is validated before anything is downloaded. If it is invalid, the promise rejects with an object (not an Error) listing every problem:
{ validationErrors: [ { message: "The 'instrument' field does not match any of the allowed values.", actual: 'abcdxxx', expected: '0005hkhkd, 0027hkhkd, 0175hkhkd, …' } ];}In the CLI, the same messages are printed under Search config invalid: and the process exits with code 1. Every other error also exits with code 1, which makes failures easy to detect in scripts.