FX automatic trading system made with python and genetic algorithm Part 1

Things I made

I tried to implement the following functions

  1. A program that keeps you up to date with the latest exchange rates
  2. A program that operates AI to buy and sell automatically
  3. A program that uses a genetic algorithm to generate new AI based on the latest data
  4. A program that measures AI performance and manages retirement and transaction currency units

background

The trading API provided by OANDA felt pretty good, so I was able to realize it. In particular, since you can buy and sell in units of one currency (in units of one dollar), even if you move 100 AIs and repeat transactions, the loss will be within a few tens of yen a day. It is safe even if the trading system runs out of control due to a bug during the test. I think it's a good time because I couldn't realize it by myself without this API. http://developer.oanda.com/rest-live/development-guide/

Characteristics of genetic algorithm

The semi-optimal solution of the optimization problem can be solved in a short time. If you're wondering about optimization problems or sub-optimal solutions, you might be able to find out by watching a video that captures Mario with a genetic algorithm. → [Nico Douga] I tried to learn Super Mario Bros.

Get the rate

While trying Oanda API with curl, I will write it with python when I finish trying

request


curl -X GET "https://api-sandbox.oanda.com/v1/candles?instrument=EUR_USD&count=100&candleFormat=midpoint&granularity=D&dailyAlignment=0&alignmentTimezone=America%2FNew_York"

response


{
	"instrument" : "EUR_USD",
	"granularity" : "D",
	"candles" : [
		{
			"time" : "2015-06-22T04:00:00.000000Z",
			"openMid" : 1.24121,
			"highMid" : 1.241535,
			"lowMid" : 1.240145,
			"closeMid" : 1.24113,
			"volume" : 24144,
			"complete" : true
		}
	]
}

Rate acquisition python code


base_domain = MODE.get('production')
url_base = 'https://{}/v1/candles?'.format(base_domain)
url = url_base + 'instrument={}&'.format(currency_pair.name) + \
    'count=5000&' +\
    'candleFormat=midpoint&' +\
    'granularity={}&'.format(granularity.name) +\
    'dailyAlignment=0&' +\
    'alignmentTimezone=Asia%2FTokyo&' +\
    'start={}T00%3A00%3A00Z'.format(start)

response = requests_api(url)

def requests_api(url, payload=None):
    auth = 'Bearer {}'.format(get_password('OandaRestAPIToken'))
    headers = {'Accept-Encoding': 'identity, deflate, compress, gzip',
               'Accept': '*/*', 'User-Agent': 'python-requests/1.2.0',
               'Content-type': 'application/json; charset=utf-8',
               'Authorization': auth}
    if payload:
        requests.adapters.DEFAULT_RETRIES = 2
        response = requests.post(url, headers=headers, data=payload, timeout=10)
    else:
        requests.adapters.DEFAULT_RETRIES = 2
        response = requests.get(url, headers=headers, timeout=10)
    print 'REQUEST_API: {}'.format(url)
    return response

Let's make a simple trading AI

■ Specifications Compare the current rate with the 24-hour average rate, and if there is a deviation of 30 pip or more, set the profit margin and loss cut at 30 pip and buy and sell.

■ Operation example Currency: Dollar Yen Current time: October 1, 2015 10:00:00 Current rate: 120.00 yen 24-hour average rate: 119.40 yen Purchase currency unit: 100 Order details: 120 yen for 100 units BUY, profit taking 120.30 yen, loss cut 119.70 yen

code


# -*- coding: utf-8 -*-
import datetime
from enum import Enum


class OrderType(Enum):
    Buy = 1
    Wait = 0
    Sell = -1


class Rate(object):
    start_at = datetime.datetime(2015, 10, 1, 10, 0, 0)
    bid = float(120.00)
    h24 = float(119.40)
    base_tick = float(0.01)

    @classmethod
    def get(cls, currency=None, start_at=None):
        return Rate()

    def get_rate(self, tick, is_add):
        if is_add:
            return self.bid + float(tick * self.base_tick)
        else:
            return self.bid - float(tick * self.base_tick)


class AIOrder(object):
    def __init__(self, order_type, limit, stop_limit):
        self.order_type = order_type
        self.limit = limit
        self.stop_limit = stop_limit


class AI(object):
    DIFF = 30
    LIMIT = 30
    STOP_LIMIT = 30

    def __init__(self, rate):
        self.rate = rate

    def order(self):
        """
Order.
        """
        if self._can_order():
            order(self._get_order())

    def _can_order(self):
        """
Can you order or return
        rtype: bool
        """
        point = (self.rate.bid - self.rate.h24) / self.rate.base_tick
        return int(point / self.DIFF) not in [0]

    def _get_order(self):
        """
Return order class
        rtype: AIOrder
        """
        limit_rate = self.rate.get_rate(AI.LIMIT, True)
        stop_limit_rate = self.rate.get_rate(AI.STOP_LIMIT, False)
        return AIOrder(self._get_order_type(), limit_rate, stop_limit_rate)

    def _get_order_type(self):
        if self.rate.bid > self.rate.h24:
            return OrderType.Buy
        if self.rate.h24 > self.rate.bid:
            return OrderType.Sell
        return OrderType.Wait


def order(ai_order):
    _base = "OrderDone:{}, Limit:{}, Stop-Limit:{}"
    print _base.format(ai_order.order_type,
                       ai_order.limit,
                       ai_order.stop_limit)

#Get rate
rate = Rate.get(currency='USDJPY', start_at=datetime.datetime.now())

#AI generation
ai = AI(rate)

#Order
ai.order()

>>> OrderDone:True, Limit:120.3, Stop-Limit:119.7

Benchmark buying and selling AI

Let's benchmark with 5-minute data to see if this AI is a good and profitable AI. I tried to define the definition of excellent AI as follows

  1. The number of transactions has been 100 or more in the past 10 years.
  2. Profit of 1 million yen or more in the past 10 years
  3. The maximum loss in the past 10 years is 500,000 yen or less

Benchmarks that go back in time are called backtests. In the case of program trading, condition 3 is important. Because it's basically left unattended, it makes a lot of money, but I don't like programs that make a lot of losses. 3 is called the maximum drawdown.

Optimize trading AI with genetic algorithms

Trading AI consists of three parameters. Let's optimize this value. In other words, buying and selling AI was defined as an optimization problem.

class AI(object):
    #The factor that determines the order, the difference from the 24h average is more than this value.
    DIFF = 30
    #Factors that determine the profit taking rate at the time of ordering
    LIMIT = 30
    #Factors that determine the loss cut rate at the time of ordering
    STOP_LIMIT = 30

Assuming that the upper and lower limits of these three parameters are 400 and 10, the number of calculations for all patterns is 390 ^ 3 = 59,319,000 times. If you do a 10-year backtest on a 5-minute bar, it will take about 10 seconds because python is slow to calculate. CPU: The calculation time required for parallel calculation on an 8-core personal computer is as follows. 59,319,000 / 8cpu * 10 seconds / 3600 seconds / 24 hours = 858 days

The calculation time is 858 days. It takes 9 days even if the distribution calculation is performed on 100 PCs. Calculation of all patterns is called linear search.

Well, it's not the number one, but is the AI with the 30th highest profit out of 59,319,000 AIs meaningless? Genetic algorithms work well to find suboptimal solutions.

Preparation before calculation by genetic algorithm

Determines the rules when calculating with a genetic algorithm.

  1. Randomly determine the initial parameters between 10-400
  2. Calculate up to 200 generations.
  3. Improve trading profit as a score.
  4. Set the number of individuals in one generation to 20
  5. Set the mutation rate to 1% ... etc

Calculation efficiency varies depending on the number of individuals and the mutation rate. There are also papers from the perspective of what values should be used for efficiency, so if you are interested, you may want to read them.

Common Failures in Genetic Algorithms Partial Optimization

If you do not finish the calculation in the 200th generation and evolve it endlessly, it will be partially optimized and the efficiency will drop. スクリーンショット 2015-09-30 14.57.54.png

Calculation result by genetic algorithm

Finally, data like this will be collected. スクリーンショット 2015-09-30 14.58.53.png

Implementation with a genetic algorithm

How to implement crossover / mutation in the genetic algorithm How to generate the initial population Backtest algorithm Actual trading system construction

I haven't finished writing it yet, so I'd like to introduce it in Part 2 at a later date.

I wrote the next article

Forex automatic trading with genetic algorithm Part 2 Evolving trading AI implementation

Recommended Posts

FX automatic trading system made with python and genetic algorithm Part 1
Forex automatic trading with genetic algorithm Part 2 Evolving trading AI implementation
Forex automatic trading with genetic algorithm Part 3 Actual trading with Oanda API
System trading starting with Python 3: Investment and risk
[Python] Try optimizing FX systole parameters with a genetic algorithm
System trading starting with Python3: long-term investment
FM modulation and demodulation with Python Part 3
FM modulation and demodulation with Python Part 2
[Python] Python and security-② Port scanning tool made with Python
Dynamic HTML pages made with AWS Lambda and Python
Genetic algorithm in python
Automatic follow on Twitter with python and selenium! (RPA)
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
I made a LINE BOT with Python and Heroku
Web application made with Python3.4 + Django (Part.1 Environment construction)
System Trade Beginning with Python 3: Hedge Funds and CTA
Python sample to learn XOR with genetic algorithm with neural network
I evaluated the strategy of stock system trading with Python.
Solve the spiral book (algorithm and data structure) with python!
Image processing with Python (Part 2)
Programming with Python and Tkinter
Encryption and decryption with Python
Studying Python with freeCodeCamp part1
Bordering images with python Part 1
Python and hardware-Using RS232C with Python-
Scraping with Selenium + Python Part 1
Studying Python with freeCodeCamp part2
Image processing with Python (Part 1)
Solving Sudoku with Python (Part 2)
Backtesting FX Systre with Python (2)
[Python3] Dijkstra's algorithm with 14 lines
Image processing with Python (Part 3)
Scraping with Selenium + Python Part 2
python with pyenv and venv
I made blackjack with Python.
Presentation Support System with Python3
Othello made with python (GUI-like)
I made wordcloud with Python.
Works with Python and R
Create an automatic grade management app for Tenhou private room with LINE bot and Python Part 1
Create an automatic grade management app for Tenhou private room with LINE bot and Python Part 2
Create an automatic grade management app for Tenhou private room with LINE bot and Python Part ③
I made a simple circuit with Python (AND, OR, NOR, etc.)
IPynb scoring system made with TA of Introduction to Programming (Python)
Automatic image interpolation with OpenCV and Python (Fast Marching Method, Navier-Stokes)
Find the optimal value of a function with a genetic algorithm (Part 2)
I made a Nyanko tweet form with Python, Flask and Heroku
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
How to use Python with Jw_cad (Part 2 Command explanation and operation)
LINE BOT (Messaging API) development with API Gateway and Lambda (Python) [Part 2]
Communicate with FX-5204PS with Python and PyUSB
Playing handwritten numbers with python Part 1
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
SNS Python basics made with Flask
perl objects and python class part 2.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib