Forex oanda 9

ichimoku: The OANDA fxTrade API.


ichimoku provides an R language interface to the OANDA fxTrade API.


OANDA is a primary source of foreign exchange data used by investors, corporations and governments globally. The API can be used for retrieving historical and live streaming price data for major currencies, metals, commodities, government bonds and stock indices.


As an example of available financial data: OHLC prices for major forex pairs can be obtained from the start of 2005 with granularity ranging from 5 seconds to monthly. For the total list of over 120 covered instruments please refer to the Available Instruments section below.


Please note that ‘OANDA’ and ‘fxTrade’ are trademarks owned by OANDA Corporation, an entity unaffiliated with the ichimoku package, its authors or copyright holders.


Registering with OANDA.


The OANDA fxTrade API, as distinct to other OANDA data sources, is based upon its retail / professional trading offering of the same name. As such, access to the API requires an fxTrade account with OANDA and agreement to their API terms of use.


Registering for a ‘demo’ or ‘practice’ account is entirely free at the time of writing and provides full access to the API. For registering an account, the following URL may be used: https://www.oanda.com/forex-trading/.


After successful registration, a link can be found on your OANDA fxTrade account profile page ‘Manage API Access’ (My Account -> My Services -> Manage API Access). From there, a personal access token to use with the OANDA API can be generated, as well as revoked. This access token will enable all the OANDA functions in this package.


Setting your OANDA API Key.


If the ‘keyring’ package is installed, the OANDA API key (personal access token) can be set once and saved in your system credential store by using oanda set key() . This will enable it to be retrieved automatically for all subsequent sessions.


Separate keys can be set for practice and live accounts; please choose the correct account type when prompted.


If an access token is revoked and re-generated, please remember to set the API key again through the oanda set key() function.


Using a Live Account.


If you are using an access token for a live (real money) account, you will want to call oanda switch() at the beginning of each session; this switches the default server from the practice server to the live server. Settings will only persist until the end of the session.


library (ichimoku) oanda switch () #> Default OANDA server switched to 'live'


Note: this package only contains functions for retrieving available instruments and price data. It does not contain code to implement any trading capability whatsoever. Please be aware however, that a personal access token can be used (outside of this package) for all operations on your account including placing orders and trades, and appropriate precautions should be taken when using your live account token.


The OANDA Functions.


All of the OANDA functions in the package are prefixed by ‘oanda’ for clarity.


Functions may be called interactively without any arguments, e.g. oanda stream() or oanda view() , in which case you will be further prompted to enter required arguments; as opposed to when specifying such arguments normally, these should be entered unquoted i.e. without any surrounding ““.


For all functions, the ‘instrument’ argument is case-insensitive and the delimiter can be either ’ ’ or ‘-’, such that both oanda("USD JPY") and oanda("usd-jpy") would be acceptable.


oanda()


Retrieve pricing data from the OANDA fxTrade API.


The arguments to the function are phrased identically to that of the API itself where relevant:


instrument string containing the base currency and quote currency delimited by ’ ’ or ‘-’ (e.g. “USD JPY” or “usd-jpy”). Use the oanda instruments() function to return a list of all valid instruments granularity [default “D”] the granularity of the price data to fetch, one of “M”, “W”, “D”, “H12”, “H8”, “H6”, “H4”, “H3”, “H2”, “H1”, “M30”, “M15”, “M10”, “M5”, “M4”, “M2”, “M1”, “S30”, “S15”, “S10”, “S5” count (optional) the number of periods to return. The API supports a maximum of 5000 for each individual request, and defaults to 500 if not specified. If both ‘from’ and ‘to’ are specified, ‘count’ is ignored, as the time range combined with ‘granularity’ will determine the number of periods to return from (optional) the start of the time range for which to fetch price data, for example “2022-02-01” to (optional) the end of the time range for which to fetch price data, for example “2022-06-30” price [default “M”] the pricing component, one of “M” (midpoint), “B” (bid) or “A” (ask) server (optional) Specify the “practice” or “live” server according to the account type held with OANDA. If not specified, will default to “practice”, unless this has been changed by oanda switch() . apikey (optional) string containing the OANDA fxTrade API key (personal access token). Does not need to be specified if already stored by oanda set key() . This argument is designed for specifying a function that returns such a string rather than the string itself, which could be potentially unsecure. This allows other packages that provide secure storage of secrets to be used if preferred over ‘keyring’ quietly if set to TRUE, will suppress printing of output to the console and return quietly.


Note: if both from and to are specified and the request would result in over 5000 periods being returned, the function prompts the user to confirm if multiple requests should be submitted. Unless the user response is “n” or “no”, the data will proceed to be downloaded in batches and merged together into a single dataframe. The function is safe for use in non-interactive settings as in such cases the download will proceed automatically without prompting.


oanda ( "USD JPY" , count = 3 , from = "2010-11-01" , price = "B" ) #> time open high low close volume complete #> 1 2010-11-01 21:00:00 80.275 81.504 80.225 80.501 22291 TRUE #> 2 2010-11-02 21:00:00 80.504 80.956 80.460 80.617 13795 TRUE #> 3 2010-11-03 21:00:00 80.632 81.585 80.594 81.069 22204 TRUE.


The returned object is a dataframe with the following columns: ‘time’, ‘open’, ‘high’, ‘low’, ‘close’, ‘volume’ and ‘complete’.


volume should be used with caution as this reflects the trading volume registered by OANDA and may not be reflective of the broader market complete indicates whether the data period is complete or not. If TRUE, the values are historical and not subject to change. If FALSE, the period is ongoing and the values reflect the current snapshot whilst final values may or may not differ.


Important implementation notes:


Incorporates automatic adjustment of the timestamps to end of period, as is customary for financial data. This means that, for example, today’s pricing data (incomplete trading period) will have today’s date as would be expected. In contrast, the raw data from the API has a timestamp of the start of the period The API data sometimes includes data for non-trading days with very low volumes. This is more common for older data and less so with more recent data. These may represent OTC trades that occurred over the OANDA platform, but nevertheless do not reflect market rates. These are automatically excluded from the data returned as they would otherwise produce spurious results when used with ichimoku()


oanda chart()


Create an updating real-time Ichimoku cloud chart. The refresh rate can be set for the chart to be updated down to every second. A plot of the ichimoku chart for the price data requested is output to the graphical device at each refresh interval.


The arguments are identical to oanda() above with the addition of:


refresh [default 5] set the refresh rate of the chart in seconds, with a minimum of 1 limit (optional) specify a time in seconds by which to limit the session. The session will end with data returned automatically after the specified time has elapsed . additional parameters passed onto ichimoku() for calculating the ichimoku cloud or autoplot() to set chart parameters periods [default c(9L, 26L, 52L)] a vector defining the length of periods used for the cloud. This parameter should not normally be modified as using other values would be invalid in the context of traditional Ichimoku analysis.


The arguments ‘from’ and ‘to’ are excluded as they are not relevant in this context.


# For a live Palladium price chart: oanda chart ( "XPD USD" )


In RStudio, please switch to the plots plane to view the chart if not already in focus.


Note that the periods displayed on the chart will be fewer than the ‘count’ specified - using default cloud periods, the first 77 datapoints will be excluded. This is by design to ensure a full cloud is displayed (i.e. there are no missing values for any cloud lines at the start of the data). Please take this into account when setting the ‘count’ parameter.


On exit from the function, the ichimoku object underlying the chart will be returned (invisibly). This allows the data to be saved simply by assigning to an object.


# To access the data, simply assign the return value to an object: cloud oanda chart ( "XPD USD" )


oanda studio()


Create an interactive R Shiny session for working with real-time Ichimoku cloud charts. Provides a live analysis environment where parameters may be set interactively as required. The cursor infotip provides ready access to the data directly from the chart.


As all parameters can be set interactively in-app, oanda studio() may be called without specifying any arguments. Otherwise, parameters may be set beforehand in the same way as oanda chart() .


oanda studio ()


The following additional parameters can be used to customise the behaviour of the Shiny environment:


new.process [default FALSE] if TRUE, will start the shiny session in a new R process, unblocking the current process and allowing continued use of the R console. . additional arguments passed to ichimoku() for calculating the ichimoku cloud, autoplot() to set chart parameters, or the ‘options’ argument of shiny::shinyApp() launch.browser [default TRUE] If TRUE, the system’s default web browser will be launched automatically after the app is started. The value of this argument can also be a function to call with the application’s URL. To use the default Shiny viewer in RStudio, please specify getOption("shiny.launch.browser")


Downloading Data.


The ichimoku object underlying the live cloud chart can be saved by selecting the Data Archive button.


This button uses archive() to save the ichimoku object to disk. The filename will be of the form: instrument granularity price.rda , e.g. SUGAR USD D A.rda . If confirmed, the file is saved to the browser’s default download location. Note that files with the same name are not overwritten but instead the filename is made unique.


To read back the file, use archive() :


# Supply the quoted file path / name, for example: cloud archive ( "~/Downloads/SUGAR USD D A.rda" ) # Or alternatively, choose the saved file interactively using a system dialog: cloud archive ()


oanda stream()


Connect to the OANDA fxTrade streaming API and stream a list of prices generated by OANDA.


The arguments are as follows:


instrument the instrument to stream data for display [default 8L] specify an integer to customise the number of rows to display in the console at any one time limit (optional) specify a time in seconds by which to limit the streaming session. The session will end with data returned automatically after the specified time has elapsed.


The ‘server’ and ‘apikey’ arguments are also available if required.


# Streaming session will end with data automatically returned after 600 secs (10 mins): data oanda stream ( "UK10YB GBP" , limit = 600 ) #> Streaming data. Press 'Esc' to return #> time bids asks closeoutBid closeoutAsk status tradeable instrument #> 1 2022-11-15 11:04:00 105.049, 10000 105.079, 10000 105.049 105.079 tradeable TRUE UK10YB GBP #> 2 2022-11-15 11:04:01 105.039, 10000 105.079, 10000 105.039 105.079 tradeable TRUE UK10YB GBP #> 3 2022-11-15 11:04:01 105.049, 10000 105.079, 10000 105.049 105.079 tradeable TRUE UK10YB GBP #> 4 2022-11-15 11:04:04 105.059, 10000 105.089, 10000 105.059 105.089 tradeable TRUE UK10YB GBP #> 5 2022-11-15 11:04:10 105.059, 10000 105.089, 10000 105.059 105.089 tradeable TRUE UK10YB GBP #> 6 2022-11-15 11:04:11 105.059, 10000 105.089, 10000 105.059 105.089 tradeable TRUE UK10YB GBP #> 7 2022-11-15 11:04:15 105.049, 10000 105.089, 10000 105.049 105.089 tradeable TRUE UK10YB GBP #> 8 2022-11-15 11:04:15 105.049, 10000 105.079, 10000 105.049 105.079 tradeable TRUE UK10YB GBP.


Use the ‘Esc’ key to stop the stream and return the session data. Assign the return value to an object if you wish to retain the data.


Note: only messages of type ‘PRICE’ are processed. Messages of type ‘HEARTBEAT’ consisting of only a timestamp are discarded.


The following should be noted from the streaming API documentation:


Pricing stream does not include every single price created for the Account At most 4 prices are sent per second (every 250 milliseconds) for each instrument If more than one price is created during the 250 millisecond window, only the price in effect at the end of the window is sent This means that during periods of rapid price movement, not every price is sent Pricing windows for different connections to the stream are not all aligned in the same way (e.g. to the top of the second) This means that during periods of rapid price movement, different prices may be observed depending on the alignment for the connection.


oanda quote()


A convenience function that outputs to the console the latest one-line price quote for an instrument.


The output format is as follows: instrument, timestamp, daily open, high, low and last prices, daily percentage change (from the open), and the pricing component (M for mid, B for bid, A for ask).


oanda quote ( "USD JPY" ) #> USD JPY 2021-10-05 16:29:44 open: 110.931 high: 111.564 low: 110.871 last: 111.398 %chg: 0.421 M.


oanda view()


Prints a snapshot overview of the selected market, showing the relative performance of all constituents.


The available markets are : ‘allfx’ for all available currencies, ‘bonds’ for government bonds, ‘commodities’ for commodities, ‘fx’ for major currency pairs, ‘metals’ for metals and ‘stocks’ for global stock markets. As partial matching is enabled, it is possible to type just the first letter e.g. ‘c’ for commodities.


The first retrieved timestamp and the pricing component are printed, followed by the dataframe showing similar data to oanda quote() for all instruments in the selected market. The data is ordered by the daily percentage change, from the most positive to the most negative.


oanda view () Enter market [a]llfx [b]onds [c]ommodities [f]x [m]etals [s]tocks : c #> Retrieving commodities [. ] #> 2021-12-11 01:58:21 / M #> open high low last %chg #> NATGAS USD 3.76200 3.9260 3.72400 3.85200 2.3923 #> BCO USD 74.52600 76.2310 74.32600 75.88000 1.8168 #> WTICO USD 70.91200 72.5540 70.55400 72.19000 1.8022 #> WHEAT USD 7.67200 7.7710 7.57100 7.72700 0.7169 #> XPT USD 930.20220 939.6000 924.29400 936.43800 0.6704 #> SOYBN USD 12.63500 12.7580 12.58800 12.67800 0.3403 #> SUGAR USD 0.19274 0.1948 0.19176 0.19334 0.3113 #> CORN USD 5.89800 5.9410 5.86800 5.90200 0.0678 #> XCU USD 4.30292 4.3479 4.26536 4.28361 -0.4488 #> XPD USD 1805.14800 1815.1260 1729.61100 1752.81200 -2.8993.


oanda orders() / oanda positions()


Provides a summary of the aggregate orders/positions held by OANDA fxTrade clients at each price level.


‘instrument’ is a required argument. The latest order/position book is retrieved unless the optional argument ‘time’ is used to specify a time for which to retrieve the order/position book.


The data is returned as a dataframe, with parameters saved as attributes which can be viewed with look() . A chart showing the percentage long and short orders/positions at each price level is output to the graphical device.


df oanda positions ( "GBP JPY" )


Available Instruments.


The list of financial instruments available for your account may be retrieved by oanda instruments() . The available instruments are also automatically populated as an input in oanda studio() .


The list below serves as a reference only and may differ from those actually available for your account type and/or country/region.


oanda instruments ()


#> name displayName type #> 1 AU200 AUD Australia 200 CFD #> 2 AUD CAD AUD/CAD CURRENCY #> 3 AUD CHF AUD/CHF CURRENCY #> 4 AUD HKD AUD/HKD CURRENCY #> 5 AUD JPY AUD/JPY CURRENCY #> 6 AUD NZD AUD/NZD CURRENCY #> 7 AUD SGD AUD/SGD CURRENCY #> 8 AUD USD AUD/USD CURRENCY #> 9 BCO USD Brent Crude Oil CFD #> 10 CAD CHF CAD/CHF CURRENCY #> 11 CAD HKD CAD/HKD CURRENCY #> 12 CAD JPY CAD/JPY CURRENCY #> 13 CAD SGD CAD/SGD CURRENCY #> 14 CH20 CHF Switzerland 20 CFD #> 15 CHF HKD CHF/HKD CURRENCY #> 16 CHF JPY CHF/JPY CURRENCY #> 17 CHF ZAR CHF/ZAR CURRENCY #> 18 CHINAH HKD China H Shares CFD #> 19 CN50 USD China A50 CFD #> 20 CORN USD Corn CFD #> 21 DE10YB EUR Bund CFD #> 22 DE30 EUR Germany 30 CFD #> 23 ESPIX EUR Spain 35 CFD #> 24 EU50 EUR Europe 50 CFD #> 25 EUR AUD EUR/AUD CURRENCY #> 26 EUR CAD EUR/CAD CURRENCY #> 27 EUR CHF EUR/CHF CURRENCY #> 28 EUR CZK EUR/CZK CURRENCY #> 29 EUR DKK EUR/DKK CURRENCY #> 30 EUR GBP EUR/GBP CURRENCY #> 31 EUR HKD EUR/HKD CURRENCY #> 32 EUR HUF EUR/HUF CURRENCY #> 33 EUR JPY EUR/JPY CURRENCY #> 34 EUR NOK EUR/NOK CURRENCY #> 35 EUR NZD EUR/NZD CURRENCY #> 36 EUR PLN EUR/PLN CURRENCY #> 37 EUR SEK EUR/SEK CURRENCY #> 38 EUR SGD EUR/SGD CURRENCY #> 39 EUR TRY EUR/TRY CURRENCY #> 40 EUR USD EUR/USD CURRENCY #> 41 EUR ZAR EUR/ZAR CURRENCY #> 42 FR40 EUR France 40 CFD #> 43 GBP AUD GBP/AUD CURRENCY #> 44 GBP CAD GBP/CAD CURRENCY #> 45 GBP CHF GBP/CHF CURRENCY #> 46 GBP HKD GBP/HKD CURRENCY #> 47 GBP JPY GBP/JPY CURRENCY #> 48 GBP NZD GBP/NZD CURRENCY #> 49 GBP PLN GBP/PLN CURRENCY #> 50 GBP SGD GBP/SGD CURRENCY #> 51 GBP USD GBP/USD CURRENCY #> 52 GBP ZAR GBP/ZAR CURRENCY #> 53 HK33 HKD Hong Kong 33 CFD #> 54 HKD JPY HKD/JPY CURRENCY #> 55 JP225 USD Japan 225 CFD #> 56 JP225Y JPY Japan 225 (JPY) CFD #> 57 NAS100 USD US Nas 100 CFD #> 58 NATGAS USD Natural Gas CFD #> 59 NL25 EUR Netherlands 25 CFD #> 60 NZD CAD NZD/CAD CURRENCY #> 61 NZD CHF NZD/CHF CURRENCY #> 62 NZD HKD NZD/HKD CURRENCY #> 63 NZD JPY NZD/JPY CURRENCY #> 64 NZD SGD NZD/SGD CURRENCY #> 65 NZD USD NZD/USD CURRENCY #> 66 SG30 SGD Singapore 30 CFD #> 67 SGD CHF SGD/CHF CURRENCY #> 68 SGD JPY SGD/JPY CURRENCY #> 69 SOYBN USD Soybeans CFD #> 70 SPX500 USD US SPX 500 CFD #> 71 SUGAR USD Sugar CFD #> 72 TRY JPY TRY/JPY CURRENCY #> 73 TWIX USD Taiwan Index CFD #> 74 UK100 GBP UK 100 CFD #> 75 UK10YB GBP UK 10Y Gilt CFD #> 76 US2000 USD US Russ 2000 CFD #> 77 US30 USD US Wall St 30 CFD #> 78 USB02Y USD US 2Y T-Note CFD #> 79 USB05Y USD US 5Y T-Note CFD #> 80 USB10Y USD US 10Y T-Note CFD #> 81 USB30Y USD US T-Bond CFD #> 82 USD CAD USD/CAD CURRENCY #> 83 USD CHF USD/CHF CURRENCY #> 84 USD CNH USD/CNH CURRENCY #> 85 USD CZK USD/CZK CURRENCY #> 86 USD DKK USD/DKK CURRENCY #> 87 USD HKD USD/HKD CURRENCY #> 88 USD HUF USD/HUF CURRENCY #> 89 USD JPY USD/JPY CURRENCY #> 90 USD MXN USD/MXN CURRENCY #> 91 USD NOK USD/NOK CURRENCY #> 92 USD PLN USD/PLN CURRENCY #> 93 USD SEK USD/SEK CURRENCY #> 94 USD SGD USD/SGD CURRENCY #> 95 USD THB USD/THB CURRENCY #> 96 USD TRY USD/TRY CURRENCY #> 97 USD ZAR USD/ZAR CURRENCY #> 98 WHEAT USD Wheat CFD #> 99 WTICO USD West Texas Oil CFD #> 100 XAG AUD Silver/AUD METAL #> 101 XAG CAD Silver/CAD METAL #> 102 XAG CHF Silver/CHF METAL #> 103 XAG EUR Silver/EUR METAL #> 104 XAG GBP Silver/GBP METAL #> 105 XAG HKD Silver/HKD METAL #> 106 XAG JPY Silver/JPY METAL #> 107 XAG NZD Silver/NZD METAL #> 108 XAG SGD Silver/SGD METAL #> 109 XAG USD Silver METAL #> 110 XAU AUD Gold/AUD METAL #> 111 XAU CAD Gold/CAD METAL #> 112 XAU CHF Gold/CHF METAL #> 113 XAU EUR Gold/EUR METAL #> 114 XAU GBP Gold/GBP METAL #> 115 XAU HKD Gold/HKD METAL #> 116 XAU JPY Gold/JPY METAL #> 117 XAU NZD Gold/NZD METAL #> 118 XAU SGD Gold/SGD METAL #> 119 XAU USD Gold METAL #> 120 XAU XAG Gold/Silver METAL #> 121 XCU USD Copper CFD #> 122 XPD USD Palladium CFD #> 123 XPT USD Platinum CFD #> 124 ZAR JPY ZAR/JPY CURRENCY.


Note: the above table is saved as internal package data and used as a fallback if the instruments API call fails. In such a case, a warning is issued once per session as subsequent calls will return the cached list.


Caching Implementation.


The package retrieves the following once only during a session (the first time they are used) and caches the variables for further use:


OANDA fxTrade API key OANDA fxTrade account associated with an fxTrade API key OANDA list of available instruments.


If for any reason these become invalid or outdated, please re-load the package (or restart your R session) to enable these variables to be refreshed.


Calling oanda switch() will also clear all cached variables.


References.


‘OANDA’ and ‘fxTrade’ are trademarks owned by OANDA Corporation, an entity unaffiliated with the ichimoku package, its authors or copyright holders.