Home » Ticker Functions » Understanding the request.currency_rate() Function in Pine Script

Understanding the request.currency_rate() Function in Pine Script

Photo of author
Published on

Pine Script, particularly in its Version 5, offers a powerful function named request.currency_rate(). This function is instrumental in fetching daily exchange rates, allowing scriptwriters to convert values expressed in one currency into another seamlessly. Let’s delve into how to effectively utilize this function in your Pine Script projects.

What is request.currency_rate()?

request.currency_rate() is a built-in function in Pine Script Version 5 that requests the current exchange rate between two specified currencies. It’s particularly useful in trading scripts where currency conversion is required.

Example

Let’s consider an example where we want to convert an amount from Euros (EUR) to US Dollars (USD). We’ll use a variable, exchangeRateEURtoUSD, to store the exchange rate.

//@version=5
indicator("Currency Rate Example", overlay=true)

exchangeRateEURtoUSD = request.currency_rate("EUR", "USD")
plot(exchangeRateEURtoUSD, title="EUR to USD Exchange Rate")
Example

Explanation of the Code

  • Version Declaration: //@version=5 specifies that we are using Pine Script Version 5.
  • Indicator Declaration: indicator("Currency Rate Example", overlay=true) declares a new indicator named “Currency Rate Example” that will overlay on the price chart.
  • Exchange Rate Request: exchangeRateEURtoUSD = request.currency_rate("EUR", "USD") requests the current exchange rate from EUR to USD and stores it in exchangeRateEURtoUSD.
  • Plotting the Exchange Rate: plot(exchangeRateEURtoUSD, title="EUR to USD Exchange Rate") plots the exchange rate on the chart for visualization.

Key Features of request.currency_rate()

  • Function Usability: This function is easy to use and requires two arguments: the base currency and the quote currency.
  • Syntax: request.currency_rate(base_currency, quote_currency)
  • Application: Ideal for scripts that need real-time currency conversion, such as in multi-currency trading strategies or risk management tools.

Takeaways

  • request.currency_rate() is a versatile function in Pine Script Version 5 for fetching real-time currency exchange rates.
  • It simplifies the process of converting values between currencies in trading scripts.
  • The function is straightforward, requiring only the base and quote currencies as arguments.

Leave a Comment