fx
Foreign exchange services.
convert(*, value, of, to, on, using='European Central Bank')
cached
Convert a value from one currency to another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Decimal
|
The value to convert. |
required |
of
|
ISO4217
|
Currency code to convert from. |
required |
to
|
ISO4217
|
Currency code to convert to. |
required |
on
|
PastDate
|
Date to get the rate for. |
required |
using
|
FxProviderStr
|
The FX provider to use. Defaults to "European Central Bank" |
'European Central Bank'
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
When the given FX provider is not supported. |
Returns:
| Name | Type | Description |
|---|---|---|
Decimal |
Decimal
|
The converted value. |
Examples:
>>> from pycountant.services.fx import convert
>>> print(
... convert(value=Decimal("12340"), of="USD", to="EUR", on=date(2023, 1, 5))
... )
11640.41128195453259126497500
Warning
If the on PastDate does not fall on a weekday (Monday to Friday), then it will not be possible to obtain a rate for conversion.
Failure
If there is no internet connection, it will not be possible to make the relevant API call to obtain the rate for conversion.
Source code in src/pycountant/services/fx.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
get_conversion_strategy(*, using)
Get the conversion strategy for a particular provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
using
|
FxProviderStr
|
The provider. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Error raised if no strategy exists for that provider. |
Returns:
| Type | Description |
|---|---|
Callable[[ISO4217, ISO4217, date], Decimal]
|
Callable[[ISO4217, ISO4217, date], Decimal]: A strategy to obtain the rate for conversion with. |
Source code in src/pycountant/services/fx.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
get_rate(*, of, to, on, using='European Central Bank')
cached
Get the rate to convert one currency to another on a past date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
of
|
ISO4217
|
Currency code to convert from. |
required |
to
|
ISO4217
|
Currency code to convert to. |
required |
on
|
PastDate
|
Date to get the rate for. |
required |
using
|
FxProviderStr
|
The FX provider to use. Defaults to "European Central Bank" |
'European Central Bank'
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
When the given FX provider is not supported. |
Returns:
| Name | Type | Description |
|---|---|---|
Decimal |
The rate |
Warning
If the on PastDate does not fall on a weekday (Monday to Friday), then it will not be possible to obtain a rate for conversion.
Failure
If there is no internet connection, it will not be possible to make the relevant API call to obtain the rate for conversion.
Source code in src/pycountant/services/fx.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
get_rate_from_base(of_rate, to_rate)
cached
Gets the rate when it is known that the of and to rate are relative to a single currency in the base rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
of_rate
|
Decimal
|
The of rate. |
required |
to_rate
|
Decimal
|
The to rate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Decimal |
Decimal
|
The rate to use for conversion. |
Source code in src/pycountant/services/fx.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
get_rate_via_ecb(of, to, on)
cached
Get FX rate via European Central Bank.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
of
|
ISO4217
|
Currency code to convert from. |
required |
to
|
ISO4217
|
Currency code to convert to. |
required |
on
|
PastDate
|
Date to get the rate for. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
When no rate is found for the given currencies on the given date. |
Returns:
| Name | Type | Description |
|---|---|---|
Decimal |
Decimal
|
The FX rate. |
Source code in src/pycountant/services/fx.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
get_rate_via_hmrc(of, to, on)
cached
Get FX rate via HMRC.
Note
The rates obtained are the rate for that month and are not specific to the particular day.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
of
|
ISO4217
|
Currency code to convert from. |
required |
to
|
ISO4217
|
Currency code to convert to. |
required |
on
|
PastDate
|
Date to get the rate for. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
When no rate is found for the given currencies on the given date. |
Returns:
| Name | Type | Description |
|---|---|---|
Decimal |
Decimal
|
The FX rate. |
Source code in src/pycountant/services/fx.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |