[PYTHON] FX data collection using OANDA REST API

motivation

When creating an automatic trading bot for Forex, it is necessary to consider what kind of strategy should be used for automatic trading. And the strategy must be evaluated for how well it performs. This article also serves as a reminder how to collect FX data using the OANDA REST API to check the performance of your strategy.

How to do

1. OANDA registration

OANDA REST API refers to a group of APIs published by a Forex trader called Oanda. There are very few companies that publish the Forex API, and considering the amount of information on the net, I think it is better to use the OANDA REST API for the Forex API. To use the API, you need to register with OANDA and get an API key issued. You can register in about 5 minutes from the link below

OANDA JAPAN

2. Install oandapy V20

Register with OANDA, and once the API key has been issued, let's create an environment where you can hit the API. It is convenient to put the API key in a separate file.


pip install oandapyV20

oanda_access_key.py


ACCOUNT_ID = "xxxxxxxxxxx"
PERSONAL_ACCESS_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

instruments_candles.py


from oandapyV20.endpoints.instruments import InstrumentsCandles
import oandapyV20
import oanda_access_key as oak

account_id = oak.ACCOUNT_ID
access_token = oak.PERSONAL_ACCESS_TOKEN

api = oandapyV20.API(access_token = access_token, environment = "practice")

3. Data acquisition

Use Instruments Candles to get historical data. Official Document Documents for oandapy V20 Roughly, it is a sample that acquires 15-minute data and plunges into the data frame ↓ After that, you can cook freely by plunging into the DB or using the data as it is.


ic = InstrumentsCandles(
		instrument="USD_JPY", #Select a currency pair
		params={
			"granularity": "M15", #Select the type of candlestick
			"alignmentTimezone": "Japan", #Time zone
			# "count": 5000 #Number of data to be acquired, from-If to is specified, count is not specified
			"from": start_datetime.strftime(datetime_format),
			"to": (start_datetime + relativedelta(days=date_window) - relativedelta(seconds=1)).strftime(datetime_format)
		}
	)
api.request(ic)
data = []
for candle in ic.response["candles"]:
	data.append(
		[
			candle['time'],
			candle['volume'],
			candle['mid']['o'],
			candle['mid']['h'],
			candle['mid']['l'],
			candle['mid']['c']
		]
	)
df = pd.DataFrame(data)

Recommended Posts

FX data collection using OANDA REST API
Get Salesforce data using REST API
Get LEAD data using Marketo's REST API in Python
Data acquisition using python googlemap api
Get Amazon data using Keep API # 1 Get data
Data acquisition memo using Backlog API
I tried using YOUTUBE Data API V3
Get Youtube data in Python using Youtube Data API
Image collection using Google Custom Search API
Creating Google Spreadsheet using Python / Google Data API
Awareness of using Aurora Severless Data API
SBI FX API
Let's create a REST API using SpringBoot + MongoDB
How to get article data using Qiita API
Create a data collection bot in Python using Selenium
[Python] Get insight data using Google My Business API
I tried APN (remote notification) using Parse.com REST API
Create a pseudo REST API server using GitHub Pages
I tried using the API of the salmon data project
Data analysis using xarray
Data analysis parts collection
Data analysis using Python 0
Data cleansing 2 Data cleansing using DataFrame
Data cleaning using Python
I tried to search videos using Youtube Data API (beginner)
Get data using Ministry of Internal Affairs and Communications API
How to reset password via API using Django rest framework
Obtain vulnerability information using the REST API published by NVD
[Python] I tried collecting data using the API of wikipedia
Play with YouTube Data API v3 using Google API Python Client