[FX] Hit oanda-API in Python using Docker

Overview

Speaking of Forex trading, it is basically a manual operation. It would be great fun to be able to settle this automatically by programming using Python. You can use machine learning to predict future prices, and even set up automatic trading at fixed times and fixed logic. Here, as the first step for that, we aim to be able to make payments using Python by hitting api from Docker.

Prerequisite environment

--Create an account (a demo account is fine) in oanda and issue an API token --Environment where docker and docker-compose can be used

oanda settings

You have to hit the API to buy and sell Forex using a programming language such as Python. There are many companies that can do Forex such as SBI SECURITIES and DMM FX, but since only oanda provides APIs for Forex in Japan, first open an account with oanda. If there are any bugs, it will be difficult, so a demo account is fine at first. You can do it in about 15 minutes. Regarding Forex trading using the oanda API, the following articles are easy to understand with Qiita.

Find a nice sample code

If you look at the above article, you can hit the API in one shot, but considering that you will implement various things in the future, it is easier to do it as a class or function. I thought it would be annoying to implement it myself, but when I searched for it, I found a nice sample code on Github.

You can git clone this one and do it exactly as it is written. You can see how to do it in detail by looking at REAMDE in the above repository, but the main points are summarized below.

--Set up the environment using Python's virtualenv --A mechanism to read information such as token and account ID of oanda API required for payment in ~ / .v20.conf --The entry point is specified in setup.py, and you can buy or sell it just by typing v20-order-entry or v20-order-cancel and specifying the argument.

It looks convenient. You can try it on your own PC as it is, but since it's a big deal, I'll add the following modifications.

--In the original repository, virtualenv is used to create something like Pyhon's virtual environment, but if you use Docker, it can be used in any environment, so it will be more convenient? --This time, for studying, create a Dockerfile on the AWS server and try to buy and sell on the container without using the virtualenv. --It is not good for security to put the account information in ~ / .v20.conf like the original specification, so use docker-compose and pass it as secrets (There are many other good methods) maybe)

AWS settings

I don't care if I can use docker, but I have never used ʻAWS, so I tried it. This time, I created an instance from ʻAWS's ʻAmazon EC2 with an appropriate OS (I made it ʻubuntu), and tried using docker in it. Since it is a free trial frame, the specifications such as memory are the worst ones. If it is the default, nothing is included, so check it appropriately I put docker.

Write a Dockerfile

Looking at the original repository, it says that setup requires the following:


user@host: ~/v20-python-samples$ make bootstrap #do virtualenv from makefile
user@host: ~/v20-python-samples$ source env/bin/activate
(env)user@host: ~/v20-python-samples$ python setup.py develop #entry point settings

In the makefile above


env/bin/pip install -r requirements/base.txt

I have installed the necessary packages. Let docker do all the settings for this. Write with the following feeling.

Dockerfile


FROM centos:7 #Appropriate version

#Pull the relevant repository from github (master branch)
ARG BRANCH="master"
RUN yum install git -y \ 
    && git clone --depth 1 --single-branch -b ${BRANCH} https://github.com/oanda/v20-python-samples.git 

#Put in the necessary packages
RUN yum install python3 -y \ 
    && yum install python3-devel -y \
    && yum install gcc -y \
    && cd v20-python-samples/ \
    && pip3 install -r requirements/base.txt \
    && python3 setup.py develop

Around authentication

Every time you make a payment, read the authentication information such as API token from .v20.conf. Write as follows.

bash:.v20.conf


hostname: api-fxpractice.oanda.com #Because it is a demo, fxfractice
streaming_hostname: stream-fxpractice.oanda.com
port: 443
ssl: true
token: e6ab562b039325f12a026c6fdb7b71bb-b3d8721445817159410f01514acd19hbc #Example of token issued by oanda
username: user
accounts:
- 101-001-100000-001
- 101-001-100000-002
active_account: 101-001-100000-002 #Oanda account ID example

The problem is the location of this one, if you want to move it from your local environment as originally expected, you can just put it in ~ / .v20.conf, but copy this file as it is in docker image Is not very good for security, so use docker-compose. To use docker-compose, write docker-compose.yaml as follows.

docker-compose.yaml



services:
  oanda: #
    image: oanda #image name
    build: #./oanda #with this./oanda/Built using Dockerfile
    tty: true #Without this, it will exit immediately after execution, so attach it
    secrets:
    - .v20.conf #File you want to pass
secrets: #secrets settings
  .v20.conf: 
    file: .v20.conf

The structure of the file is as follows

$ tree -a
.
├── .v20.conf
├── docker-compose.yaml
└── oanda
    └── Dockerfile

The mechanism of docker-compose is not touched on here, but it can handle multiple containers at once. This time, a docker image is created based on ./oanda/Dockerfile. The secrets information is mounted under / run / secrets in the container. I referred to the following

There seem to be several other ways to pass PASS information etc. as environment variables, but this time I used this method because I wanted to use the specification of reading .20.conf as it is.

Run docker-compose

Now that we are ready, let's create a container.


$ sudo docker-compose up -d #I want to run it in the background-Add d
Creating network "test_default" with the default driver
Creating test_oanda_1 ... #This time~/test/It seems that the naming rule is like this because it was executed below
Creating test_oanda_1 ... done
Attaching to test_oanda_1
#You can check various things with the following command
$ sudo docker-compose ps 
    Name        Command    State   Ports
----------------------------------------
test_oanda_1   /bin/bash   Up    #Since the State is Up, it seems that the container is working properly
$ sudo docker-compose images #Confirm image
 Container     Repository    Tag       Image Id      Size 
----------------------------------------------------------
test_oanda_1   oanda        latest   c84xxxxxxxxx   489 MB   
$ sudo docker ps #You can also check by using docker commands normally
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d56xxxxxxxxx        oanda               "/bin/bash"         2 minutes ago       Up 2 minutes                            test_oanda_1     

This time I just passed the secrets information, but it seems that docker-compose can be used in the same way when linking nginx and mysql. By the way, you can delete the running container with docker-compose down.

Enter the container and try running

Enter the created docker image and execute the command


$ sudo docker-compose exec oanda bash #oanda is the name of the service set in the Dockerfile

If you put it in safely, you should be able to see the cloned repository from github, so Change DEFAULT_PATH =" ~ / .v20.conf " in v20-python-samples / src / common / config.py to"/ run / secrets". (Because the authentication file is mounted under / run / secrets)

After that, you can buy and sell just by executing the command. Let's try to display your account information.


# cd v20-python-samples/
# v20-account-details 
===============================  ==============================
Account ID                       100-xxx-xxxxxxxx-xxx
Alias                            xxxxxxx
Home Currency                    JPY
Balance                          2999999.978 #Balance. It ’s a demo account, so it ’s about 3 million.
Created by User ID               xxxxxxxx 
Create Time                      2020-06-03T12:45:17.366012787Z
Guaranteed Stop Loss Order Mode  DISABLED
Profit/Loss                      -0.022
Resettable Profit/Loss           -0.022
Profit/Loss Reset Time           0
Financing                        0.0
Commission                       0.0
Guaranteed Execution Fees        0.0
Margin Rate                      0.04
Open Trade Count                 0
Open Position Count              0
Pending Order Count              0
Hedging Enabled                  True
Unrealized Profit/Loss           0.0
Net Asset Value                  2999999.978
Margin Used                      0.0
Margin Available                 2999999.978
Position Value                   0.0
Closeout UPL                     0.0
Closeout NAV                     2999999.978
Closeout Margin Used             0.0
Margin Closeout Percentage       0.0
Margin Closeout Position Value   0.0
Withdrawal Limit                 2999999.978
Last Transaction ID              10
===============================  ==============================

did it!

Summary

--I was able to hit oanda-api using docker! ――After that, if you improve this to your liking, you can do various things such as automatic trading and machine learning. ――I might write an article that follows this one

Other digressions

--Other usage of buy / sell commands will be omitted in the repository README, as it will appear when you check how to write the code to hit oanda-api and the implementation. --At first, I was trying to copy .v20.conf to ~ / under Docker image due to brain death without using docker-compose. You have to think about how to pass the authentication information safely. ..

Recommended Posts

[FX] Hit oanda-API in Python using Docker
Using venv in Windows + Docker environment [Python]
Hit Mastodon's API in Python
Translate using googletrans in Python
Backtesting FX Systre in Python (1)
Using Python mode in Processing
GUI programming in Python using Appjar
Precautions when using pit in Python
Hit a command in Python (Windows)
FX Systre Parameter Optimization in Python
Try using LevelDB in Python (plyvel)
Using global variables in python functions
Hit the web API in Python
Let's see using input in python
Infinite product in Python (using functools)
Edit videos in Python using MoviePy
Handwriting recognition using KNN in Python
Try using Leap Motion in Python
Depth-first search using stack in Python
When using regular expressions in Python
GUI creation in python using tkinter 2
Build and try an OpenCV & Python environment in minutes using Docker
Mouse operation using Windows API in Python
Notes using cChardet and python3-chardet in Python 3.3.1.
Try using the Wunderlist API in Python
GUI creation in python using tkinter part 1
Get Suica balance in Python (using libpafe)
Slowly hash passwords using bcrypt in Python
Try using the Kraken API in Python
Behind the flyer: Using Docker with Python
Tweet using the Twitter API in Python
[Python] [Windows] Serial communication in Python using DLL
I tried using Bayesian Optimization in Python
Log in to Slack using requests in Python
Get Youtube data in Python using Youtube Data API
Using physical constants in Python scipy.constants ~ constants e ~
Scraping a website using JavaScript in Python
Develop slack bot in python using chat.postMessage
Write python modules in fortran using f2py
Draw a tree in Python 3 using graphviz
Notes for using python (pydev) in eclipse
Disease classification in Random Forest using Python
Download files in any format using Python
Parallel task execution using concurrent.futures in Python
Notes on using code formatter in Python
Meaning of using DI framework in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Create a GIF file using Pillow in Python
Meta-analysis in Python
Email attachments using your gmail account in python.
Unittest in python
Develop and deploy Python APIs using Kubernetes and Docker
Python development flow using Poetry, Git and Docker
Creating numbering process using python in DynamoDB Local Numbering process
Try using the BitFlyer Ligntning API in Python