📒
Knowledge-Base
  • Knowledge Base
  • Tutorials
    • Python
      • Introduction
      • Important basic syntax
      • Awesome Python
      • Python 101
      • Python Cheat sheet
      • āđ‚āļ„āļĢāļ‡āļŠāļĢāđ‰āļēāļ‡āļ‚āļ­āļ‡āļ āļēāļĐāļē
      • Library & Package
      • Variable & Data Types
      • Lists
      • Dictionary
      • Function
      • Built-in Function
        • enumerate()
      • Modules
      • Classes & Objects
      • Inheritance
      • Date & Time
      • āļāļēāļĢāđƒāļŠāđ‰āļ‡āļēāļ™ Virtualenv
    • Pandas
      • Learning Pandas Second Edition
        • 2. Running with pandas
        • 3. Data with the Series
        • 4. Create DataFrame
        • 5. Manipulating DataFrame Structure
  • e-Book
    • Tech
      • Automate the Boring Stuff
      • A Whirlwind Tour of Python
  • Innovation & Tech
    • Python
    • Pandas
      • 10 Pandas tips
    • Web Scraping
      • Web Scraping 101
      • Requests and BeautifulSoup
  • Industry
    • 20 āđāļ™āļ§āļ„āļīāļ”āļ‚āļēāļĒāļ‚āļ­āļ‡āļ­āļ­āļ™āđ„āļĨāļ™āđŒ
    • āđāļœāļ™āļĢāļ°āļĒāļ°āļĒāļēāļ§āļ‚āļ­āļ‡ Toyota
    • āđ‚āļĨāļāļŦāļĨāļąāļ‡āļĒāļļāļ„āđ‚āļĨāļāļēāļ āļīāļ§āļąāļ’āļ™āđŒ
  • Opinion
    • āļšāļĢāļĢāļĒāļ‡ āļžāļ‡āļĐāđŒāļžāļēāļ™āļīāļŠ: āļāļąāļšāļ”āļąāļāļĢāļąāļāļĢāļēāļŠāļāļēāļĢ 4.0
    • āļ›āļąāļāļāļēāļ āļīāļāđ‚āļ āļ“ Wongnai WeShare
    • āļ›āļĢāļ°āđ€āļ—āļĻāđ„āļ—āļĒāđƒāļ™āļ„āļ§āļēāļĄāļ„āļīāļ” āļ„āļ§āļēāļĄāļ„āļīāļ”āđƒāļ™āļ›āļĢāļ°āđ€āļ—āļĻāđ„āļ—āļĒ
    • āļāļēāļĢāļŠāļ–āļēāļ›āļ™āļē ‘āļĢāļąāļāļšāļĢāļĢāļĐāļąāļ—āļ­āļģāļ™āļēāļˆāļ™āļīāļĒāļĄâ€™ āđƒāļ™āļŠāļąāļ‡āļ„āļĄāđ„āļ—āļĒ
  • People
    • “āļ„āļ§āļēāļĄāļŠāļģāđ€āļĢāđ‡āļˆ āļ”āļĩāđƒāļˆāđ„āļ”āđ‰āļ§āļąāļ™āđ€āļ”āļĩāļĒāļ§â€
    • āđ‚āļ‹āđ€āļŠāļĩāļĒāļĨāļĄāļĩāđ€āļ”āļĩāļĒ āđƒāļ™āļĄāļļāļĄāļĄāļ­āļ‡āļ‚āļ­āļ‡ āļĄāļēāļĢāđŒāļ āļ‹āļąāļāđ€āļ„āļ­āļĢāđŒāđ€āļšāļīāļĢāđŒāļ
  • Parent
    • ADULTIFICTION
    • āļ„āļ§āļēāļĄāļ‰āļĨāļēāļ”āļŠāļĢāđ‰āļēāļ‡āđ„āļ”āđ‰
    • āļāļēāļĢāđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļ‚āļ­āļ‡āļĨāļđāļāđƒāļ™āļ§āļąāļ™āļ™āļĩāđ‰
    • CODING āļ„āļ·āļ­āļ­āļ°āđ„āļĢ
    • āļŠāļ­āļ™ CODING āļ­āļĒāđˆāļēāļ‡āđ„āļĢāđƒāļŦāđ‰āļ‡āđˆāļēāļĒ
    • 8 āļ‚āđ‰āļ­āļ„āļĢāļđāļ„āļ§āļĢāļĢāļđāđ‰ āđ€āļĄāļ·āđˆāļ­āļˆāļąāļ”āļāļēāļĢāđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļœāđˆāļēāļ™āļ›āļĢāļ°āļŠāļšāļāļēāļĢāļ“āđŒ
  • Lift
    • āļ„āļļāļ“āļĢāļđāđ‰āļŠāļķāļāļ§āđˆāļē āđ‚āļĨāļāļ—āļļāļāļ§āļąāļ™āļ™āļĩāđ‰āļŦāļĄāļļāļ™āđ€āļĢāđ‡āļ§āđāļĨāļ°āđāļ„āļšāļĨāļ‡āļŦāļĢāļ·āļ­āđ€āļ›āļĨāđˆāļē?
    • āļ›āļąāļˆāļˆāļļāļšāļąāļ™āđ€āļĢāļēāļ•āđ‰āļ­āļ‡āđ€āļœāļŠāļīāļāļāļąāļšāļ„āļ§āļēāļĄāļ—āđ‰āļēāļ—āļēāļĒāļ­āļ°āđ„āļĢāļšāđ‰āļēāļ‡
    • āļāļŽ 40% āļ‚āļ­āļ‡āļŦāļ™āđˆāļ§āļĒ SEAL
    • e-Book
      • Sapiens – A Brief History of Humankind
        • [āļŠāļĢāļļāļ›] āđ‚āļŪāđ‚āļĄ āđ€āļ‹āđ€āļ›āļĩāļĒāļ™āļŠāđŒ āļŠāļąāļ•āļ§āđŒāļĄāļŦāļąāļĻāļˆāļĢāļĢāļĒāđŒāđāļĨāļ°āļ–āļīāđˆāļ™āļ—āļĩāđˆāļ­āļĒāļđāđˆ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 1- āļāļģāđ€āļ™āļīāļ” Homo Sapiens
        • āļ•āļ­āļ™āļ—āļĩāđˆ 2 – āļŠāļīāđˆāļ‡āļ—āļĩāđˆāļ—āļģāđƒāļŦāđ‰āđ€āļĢāļēāļ„āļĢāļ­āļ‡āđ‚āļĨāļ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 3 – āļĒāļļāļ„āđāļŦāđˆāļ‡āļāļēāļĢāļĨāđˆāļēāļŠāļąāļ•āļ§āđŒāđ€āļāđ‡āļšāļžāļ·āļŠāļœāļĨ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 4 – āļāļēāļĢāļŦāļĨāļ­āļāļĨāļ§āļ‡āļ„āļĢāļąāđ‰āļ‡āļĒāļīāđˆāļ‡āđƒāļŦāļāđˆ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 5 – āļ„āļļāļāļ—āļĩāđˆāļĄāļ­āļ‡āđ„āļĄāđˆāđ€āļŦāđ‡āļ™
        • āļ•āļ­āļ™āļ—āļĩāđˆ 6 – āļāļģāđ€āļ™āļīāļ”āļ āļēāļĐāļēāđ€āļ‚āļĩāļĒāļ™
        • āļ•āļ­āļ™āļ—āļĩāđˆ 7 – āļ„āļ§āļēāļĄāđ€āļŦāļĨāļ·āđˆāļ­āļĄāļĨāđ‰āļģ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 8 – āđ‚āļĨāļāļ—āļĩāđˆāļ–āļđāļāļŦāļĨāļ­āļĄāļĢāļ§āļĄ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 9 – āļĄāļ™āļ•āļĢāļēāļ‚āļ­āļ‡āđ€āļ‡āļīāļ™āļ•āļĢāļē
        • āļ•āļ­āļ™āļ—āļĩāđˆ 10 – āļˆāļąāļāļĢāļ§āļĢāļĢāļ”āļī
        • āļ•āļ­āļ™āļ—āļĩāđˆ 11 – āļšāļ—āļšāļēāļ—āļ‚āļ­āļ‡āļĻāļēāļŠāļ™āļē
        • āļ•āļ­āļ™āļ—āļĩāđˆ 12 – āļĻāļēāļŠāļ™āļēāđ„āļĢāđ‰āļžāļĢāļ°āđ€āļˆāđ‰āļē
        • āļ•āļ­āļ™āļ—āļĩāđˆ 13 – āļĒāļļāļ„āđāļŦāđˆāļ‡āļ„āļ§āļēāļĄāđ„āļĄāđˆāļĢāļđāđ‰
        • āļ•āļ­āļ™āļ—āļĩāđˆ 14 – 500 āļ›āļĩāđāļŦāđˆāļ‡āļ„āļ§āļēāļĄāļāđ‰āļēāļ§āļŦāļ™āđ‰āļē
        • āļ•āļ­āļ™āļ—āļĩāđˆ 15 – āđ€āļĄāļ·āđˆāļ­āļĒāļļāđ‚āļĢāļ›āļ„āļĢāļ­āļ‡āđ‚āļĨāļ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 16 – āļŠāļ§āļąāļŠāļ”āļĩāļ—āļļāļ™āļ™āļīāļĒāļĄ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 17 – āļˆāļēāļ™āļ­āļĨāļđāļĄāļīāđ€āļ™āļĩāļĒāļĄāļ‚āļ­āļ‡āļ™āđ‚āļ›āđ€āļĨāļĩāļĒāļ™
        • āļ•āļ­āļ™āļ—āļĩāđˆ 18 – āļ„āļĢāļ­āļšāļ„āļĢāļąāļ§āļĨāđˆāļĄāļŠāļĨāļēāļĒ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 19 – āļŠāļļāļ‚āļŠāļĄāļšāđˆāļĄāļīāļŠāļĄ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 20 – āļ­āļ§āļŠāļēāļ™ Sapiens
      • Homo Deus
        • [āļŠāļĢāļļāļ›āļŦāļ™āļąāļ‡āļŠāļ·āļ­] Homo Deus
        • āļ•āļ­āļ™āļ—āļĩāđˆ 1: āļŠāļēāļĄāļ§āļēāļĢāļ°āđƒāļŦāļĄāđˆāđāļŦāđˆāļ‡āļ­āļ™āļēāļ„āļ•
        • āļ•āļ­āļ™āļ—āļĩāđˆ 2: āļ„āļģāļŠāļēāļ›āđ€āļĢāļ·āđˆāļ­āļ‡āļ”āļĩāļ­āļļāļŠ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 3: āđ€āļ‹āđ€āļ›āļĩāļĒāļ™āļŠāđŒāļ„āļĢāļ­āļ‡āđ‚āļĨāļāđ„āļ”āđ‰āļ­āļĒāđˆāļēāļ‡āđ„āļĢ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 4: āļžāļĨāļąāļ‡āļ‚āļ­āļ‡āļˆāļīāļ•āļ§āļīāļŠāļąāļĒāļĢāđˆāļ§āļĄ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 5: āļ‚āđ‰āļ­āļ•āļāļĨāļ‡āđ€āļĢāļ·āđˆāļ­āļ‡āļ„āļ§āļēāļĄāļ—āļąāļ™āļŠāļĄāļąāļĒāļāļąāļšāđ€āļ—āļ§āļ—āļąāļ“āļ‘āđŒ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 6: āļ›āļĨāļēāļĒāļ—āļēāļ‡āļ‚āļ­āļ‡āļāļēāļĢāļ›āļāļīāļ§āļąāļ•āļīāļĄāļ™āļļāļĐāļĒāđŒāļ™āļīāļĒāļĄāļ„āļ·āļ­āļ­āļ āļīāļĄāļ™āļļāļĐāļĒāđŒ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 7: āđ„āļĄāđˆāļĄāļĩāļ—āļąāđ‰āļ‡āđ€āļˆāļ•āļˆāļģāļ™āļ‡āđ€āļŠāļĢāļĩāđāļĨāļ°āļ§āļīāļāļāļēāļ“āđƒāļ™āđ‚āļĨāļāļ‚āļ­āļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāļ™āļīāļĒāļĄ (dataism)
        • āļ•āļ­āļ™āļ—āļĩāđˆ 8: āđ€āļ‹āđ€āļ›āļĩāļĒāļ™āļŠāđŒāļāļĨāļēāļĒāđ€āļ›āđ‡āļ™āļŠāļīāđˆāļ‡āļŠāļģāļĢāļļāļ”āļ—āļēāļ‡āļ›āļĢāļ°āļ§āļąāļ•āļīāļĻāļēāļŠāļ•āļĢāđŒāđ„āļ”āđ‰āļ­āļĒāđˆāļēāļ‡āđ„āļĢ
        • āļ•āļ­āļ™āļ—āļĩāđˆ 9: āļĄāļīāļˆāļ‰āļēāļ—āļīāļāļīāļ—āļĩāđˆāļĢāđ‰āļēāļĒāđāļĢāļ‡āļ—āļĩāđˆāļŠāļļāļ”āđƒāļ™āļĒāļļāļ„āļ‚āļąāļ­āļĄāļđāļĨāļ™āļīāļĒāļĄ (dataism)
        • āļ•āļ­āļ™āļ—āļĩāđˆ 10: āļžāļĨāļąāļ‡āļāļļāļ“āļ‘āļēāļĨāļīāļ™āļĩ āļ„āļ·āļ­āđ€āļŠāđ‰āļ™āļ—āļēāļ‡āļŠāļđāđˆāļ”āđ‰āļēāļ™āļŠāļ§āđˆāļēāļ‡āļ‚āļ­āļ‡ Homo Deus
        • āļ•āļ­āļ™āļ—āļĩāđˆ 11: āļ—āļĪāļĐāļŽāļĩāđāļŦāđˆāļ‡āļŠāļĢāļĢāļžāļŠāļīāđˆāļ‡â€‹(Theory​ of​ Everything)​āļ‚āļ­āļ‡āļĨāļąāļ—āļ˜āļīāļ‚āđ‰āļ­āļĄāļđāļĨāļ™āļīāļĒāļĄāļāļąāļšâ€‹āļ§āļīāļ–āļĩāđāļŦāđˆāļ‡āļ•āļąāļ§āļ•āļ™
        • āļ•āļ­āļ™āļ—āļĩāđˆ 12: āđ€āļĢāļēāļ•āđ‰āļ­āļ‡āļāđ‰āļēāļ§āļ‚āđ‰āļēāļĄāđāļ•āđˆāļŦāļĨāļ­āļĄāļĢāļ§āļĄāļĨāļąāļ—āļ˜āļīāļ‚āđ‰āļ­āļĄāļđāļĨāļ™āļīāļĒāļĄ
  • See Behind the FX rate
  • Obtaining Stock Prices
  • Monte Carlo Simulation in Finance Python Part-2
  • The Easiest Data Cleaning Method using Python & Pandas
  • How to use iloc and loc for Indexing and Slicing Pandas Dataframes
  • Converting HTML to a Jupyter Notebook
  • Top 50 Tips & Tricks
Powered by GitBook
On this page

Was this helpful?

See Behind the FX rate

Previousāļ•āļ­āļ™āļ—āļĩāđˆ 12: āđ€āļĢāļēāļ•āđ‰āļ­āļ‡āļāđ‰āļēāļ§āļ‚āđ‰āļēāļĄāđāļ•āđˆāļŦāļĨāļ­āļĄāļĢāļ§āļĄāļĨāļąāļ—āļ˜āļīāļ‚āđ‰āļ­āļĄāļđāļĨāļ™āļīāļĒāļĄNextObtaining Stock Prices

Last updated 5 years ago

Was this helpful?

· 4 min read

As a beginner in Python, I am eager to find interesting projects to play with. In this article, I will introduce a Python project that I found quite intriguing and practical with the finance terms explained in plain language.

The aforementioned loss incurred by the currency fluctuations is jargoned ‘FX exposure’ in finance.

Let's start the project with a question — If you’d like to convert some money to another currency on a future date, how could you monitor your FX exposure?

Well, you could try to track the live FX rates and exchange the money when the rate dropped. But with so many agents in the market, how could we know which one offers the most competitive rates? And is there any more could we learn from the provided FX rates?

Thus this Python project will focus on: 1. Tracking the live FX rates provided by various agents using html requests 2. Comparing and analysing the rates offered using trend plot First, let start with a FX trading site that you like.

Below are the site content for the two Banks I choose.

Bank Of China — FX rate

China Merchant Bank — FX rate

If you were familiar with CSS/HTML (don’t panic if not), then you would notice — they are both in the form of tables.

In short, an html table element on any page could be converted to a Pandas dataframe using pd.read_html().

As one of the banks only provides real-time data, i have to make a request to the website every 15 mins and store the table data into excel. I used the below code to do the job but i am sure there are many ways to get the data as well!

import time
import requests
import xlsxwriter
import pandas as pd
from lxml import etree
from datetime import datetime


config = {
    'BankOfChina': {
        'link' : 'https://www.bankofchina.com/sourcedb/whpj/enindex_1619.html',
    },
    'CMBChina' : {
        'link': 'http://english.cmbchina.com/Rate/ForexRates.aspx',
        'xpath' : '//*[@id="rightpart"]/div[3]/div[2]/div[1]/text()',
    }
}


def requestFxTable(bankLink):
    # make a request to the website and asking for the table
    # Bank of China have different format and i have to clean it using loc
    if bankLink == config['BankOfChina']['link'] :
        df = pd.read_html(bankLink)[4].loc[4:31,:6]
        return df
    elif bankLink == config['CMBChina']['link'] :
        df = pd.read_html(bankLink)[0]
        return df
    
def enrich_time_prefix(df , bankLink):
    page = requests.get(bankLink)
    tree = etree.HTML(page.text)
    time_prefix = tree.xpath(config['CMBChina']['xpath'])
    time_prefix = time_prefix[1][:10]
    df['Time'] = time_prefix +' '+ df['Time']

def requestFxRate(bankLink):
    df = requestFxTable(bankLink)
    #change columns and rows to make the table more neat
    df.columns = df.iloc[0]
    df = df[1:]
    #add a request time to keep track 
    df['RequestTime_UK'] = datetime.now()
    if bankLink == config['CMBChina']['link'] :
        #CMB have slightly different time format thus need to add a prefix before time
        enrich_time_prefix(df, bankLink)
        return df
    return df

def dataWriter(bank):
    df_All = pd.read_excel('/Users/Ivy_li/Fx_Rate_{}.xlsx'.format(bank))
    df = requestFxRate(config[bank]['link'])
    df_All = df_All.append(df)
    df_All.to_excel('/Users/Ivy_li/Fx_Rate_{}.xlsx'.format(bank))

#Once you have got the data, you could store it into excel
count = 0
#I let the file run for 2 days
while count < 15*4*24*2 :
    count += 1
    #request every 15 mins , you could change the frequency 
    time.sleep(60.0*15)
    #open excel and write the data
    for bank in config.keys():
        dataWriter( bank )

Leave the code to run for a day or two to collect the data, afterwards you could start the analysis .

By plotting line chart as per below, you could check which banks offer a better rates — in my case, it is the China Merchant Bank. As it consistently offered a lower rate throughout the time (lower rates means more RMB could be bought with a same amount of GBP).

Also by looking at this graph, it seems that the best time to convert GBP to RMB are 26th Dec 0:00(with CMB) when the rates is at the lowest. But this decision is made retrospectively when we already the full information.

With the FX data at hand, there are more that we could do , for example : 1. We could verify if China Merchant Bank is offering the more competitive rates across the currencies for both selling and buying 2. there are two dips in the plotted graph, are them being driven by certain financial event? etc.

Since I am a beginner as well, I welcome your feedback !

Living in the UK for the past year, it has been difficult for me to not become tired about the Brexit. Be it the higher overseas traveling expense or the 2% more that I have to spend on groceries(), the weakening pound driven by Brexit has taken its toll on my life.

I went with two major Chinese banks — and .

Simply speaking , if you open the (click me!) in Chrome, right-click and choose ‘inspect’. You would see the below which shows the website code. Hovering your mouse over the FX rate section on the screen, you would have uncovered an html table!

In real life, we would not have any knowledge if the pound is going to drop further after 26th Dec . A stop-loss could help us to mitigate the FX exposure — where you place an order with an broker to sell when a certain price is reached ().

It also seems like that both banks are charging relatively consistent (the difference between selling and buying rate which could be seems as profit margin for agents) across the days. But in comparison, Bank of China are charging higher spreads than China Merchant Bank as calculated below.

Guardian
Bank Of China
China Merchant Bank
link
full definition
Dec 29, 2019
Source :
spreads