LoginSignup
5
10

More than 3 years have passed since last update.

OANDA REST APIを使用したFXデータ収集

Last updated at Posted at 2020-03-21

モチベーション

FXを対象とした自動売買botを作成する際に、どのような戦略で自動売買をしようか考える必要があります。
そしてその戦略はどの程度のパフォーマンスであるかを評価されなければいけません。
この記事では戦略のパフォーマンスチェックのためにOANDA REST APIを使用したFXデータの収集方法について、備忘も兼ねて記載します。

やりかた

1. OANDAの登録

OANDA REST APIはOandaというFX業者が公開しているAPI群のことを指します。
FXのAPIを公開している業者は非常に少なく、またネット上にある情報量を考慮するとFXに関するAPIはOANDA REST APIを使用するのが良いと思います。
APIを使用するにはOANDAに登録し、APIキーを発行して貰う必要があります。
下記のリンクから5分くらいで登録できます

OANDA JAPAN

2. oandapyV20のインストール

OANDAに登録し、APIキーが払い出されたら次はAPIを叩く環境を作りましょう。
APIキーは別ファイルに出しておくと便利です。


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. データ取得

過去データを取得するにはInstrumentsCandlesを使用します。
公式ドキュメント
oandapyV20のドキュメント
ざっくり、15分足のデータを取得してデータフレームに突っ込むサンプルです↓
あとはDBに突っ込むなりそのままデータを使用するなり、自由に調理できます。


ic = InstrumentsCandles(
        instrument="USD_JPY", # 通貨ペアを選択
        params={
            "granularity": "M15", # ロウソク足の種類を選択
            "alignmentTimezone": "Japan", # タイムゾーン
            # "count": 5000 # 取得するデータ件数、from-toを指定する場合はcountは指定しない
            "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)
5
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
10