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.
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
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")
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