Martingale Strategy

The gambling trading strategy.

Photo Creds: Unsplash


  • Difficulty:★☆☆☆☆
  • Introduce commonly used trading strategy in the foreign exchange market.


「Martingale Strategy」is a popular way of gambling since 18th century. The main method is to double the next bet when we lose this time. By this strategy, if we win the next bet, not only recover previous losses, but also get rewards for the first bet. It sound like a unbeatable strategy, but it has a tremendous risk behind.

The Editing Environment and Modules Required

Mac OS and Jupyter Notebook

import tejapi
import pandas as pd
import numpy as np

Note: To install module tejapi : pip install tejapi

tejapi.ApiConfig.api_key = 'Your Key'
tejapi.ApiConfig.ignoretz = True

Database Used

  • Multinational Database: The code of database is ‘GLOBAL/GCURR’. To obtain quotations in the New York and Asian markets exchange rate. The time range is from 2019 to 2020.
fx = tejapi.get('GLOBAL/GCURR',
coid = 'EUR',
mdate={'gte': '2019-01-01', 'lte':'2020-12-31'},
opts = {'columns':['mdate','tuse2']},
chinese_column_name = True,
paginate = True)

Foreign Exchange Margin & Profit


When the profit is positive, execute liquidation and buy 0.01 lots; if it is negative, continue to increase (2 times the lot) to buy.

Step 1. Initial Value

account  = 100000   # 100k USD
lot = 0.01
lev = 1/500
lot_record = [].
account_record = []
cum_profit = 0

Step 2. Execute Strategy

for i in range(len(fx)):

# 第一筆買入
if i < 1:
cum_profit -= (100000*lot*lev*fx.loc[i, '原幣兌美元 (美元)'])
# 初始買0.01手需要的保證金為歐元,所以按照當時匯率換成美元
account += cum_profit
# 累計損益,乘以100000是為了換成布數
cum_profit += (fx.loc[i, '原幣兌美元 (美元)'] - fx.loc[i-1,
'原幣兌美元 (美元)'])*100000*lot # 帳戶餘額
account += (fx.loc[i, '原幣兌美元 (美元)'] - fx.loc[i-1, '原幣
兌美元 (美元)'])*100000*lot # 如果累計損益獲利,平倉後買入0.01手
if cum_profit >= 0:
#買入0.01手,故重置lot & cum_profit
lot = 0.01
cum_profit = -(100000*lot*lev*fx.loc[i, '原幣兌美元 (美
account += cum_profit
lot_record.append(lot) # 若累計損益為負,則加碼買進
lot = lot *2
cum_profit -= (100000*(lot-lot/2)*lev*fx.loc[i, '原幣兌美
元 (美元)'])
account -= (100000*(lot-lot/2)*lev*fx.loc[i, '原幣兌美元

Step 3. Review

fx['價值'] = account_record


Here we can see the big drawdown in the middle of our profit line. That is because when we face loss, we will increase our position. But if the price did not go up, we would double the loss. The biggest problem of this strategy is the risk could eat all the profit we earned. Therefore, the risk control is the Martingale users continually dedicate to improve.

If readers are interested in certain databases, welcome to E-shop to choose the optimal plan for yourself !

The content of this webpage is not an investment device and does not constitute any offer or solicitation to offer or recommendation of any investment product. It is for learning purposes only and does not take into account your individual needs, investment objectives and specific financial circumstances. Investment involves risk. Past performance is not indicative of future performance. Readers are requested to use their personal independent thinking skills to make investment decisions on their own. If losses are incurred due to relevant suggestions, it will not be involved with author.

Source code

Extended Reading

Related Link