-
Notifications
You must be signed in to change notification settings - Fork 33
/
fxrates.py
55 lines (34 loc) · 1.64 KB
/
fxrates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Python UK trading tax calculator
Copyright (C) 2015 Robert Carver
You may copy, modify and redistribute this file as allowed in the license agreement
but you must retain this header
See README.txt
"""
import pandas as pd
from databasefxrates import get_fx_data
def FXDict(all_currencies, source):
"""
Return pd dataframe of currencies
all_currencies is list of currencies to get
fx source can be: 'FIXED' uses fixed rates for whole year, 'QUANDL' downloads rates from www.quandl.com
'DATABASE' this is my function for accessing my own database. It won't work for you, need to roll your own
"""
if source=="CSV":
fx_dict=dict([(currency, get_fx_data(currency)) for currency in all_currencies])
elif source=="FIXED":
fx_dict=dict([(currency, get_fixed_fx_data(currency)) for currency in all_currencies])
else:
raise Exception("Source %s for fx data unknown. Use DATABASE or FIXED" % source)
return fx_dict
RATE_DICT=dict(GBP=1.0, USD=0.60, KRW=0.00078, JPY=0.0038, EUR=0.66, CHF=0.66, AUD=0.55)
def get_fixed_fx_data(currency):
"""
Use this if you don't have proper FX data and are happy to use fixed values
Rate starts in 2008 (before that old CGT rules applied) and will be forward filled as required
"""
if currency not in RATE_DICT:
raise Exception("Don't have an fx rate for %s " % currency)
rate_value=RATE_DICT[currency]
print("Warning using approximate rate of %f for %s" % (rate_value, currency))
return pd.Series([rate_value], index=[pd.datetime(2008,1,1)])