Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement forex python #225

Open
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

macanudo527
Copy link
Collaborator

@macanudo527 macanudo527 commented Mar 4, 2024

What?

Fixes #224
Dependent on #223
This allows users to make use of CSV files for Forex pricing instead of rely on exchangerate.host

Testing

I have not tested this with real world data at all. This is curently WIP.

@macanudo527
Copy link
Collaborator Author

@eprbell this reads in missing forex data from CSV files. But, I did finally find this API that can basically do the same thing, but via a REST API. Should I keep both and use the CSV files as a fallback?

@eprbell
Copy link
Owner

eprbell commented Mar 22, 2024

@eprbell this reads in missing forex data from CSV files. But, I did finally find this API that can basically do the same thing, but via a REST API. Should I keep both and use the CSV files as a fallback?

Sounds good, but should it be a separate plugin, so that the user can decide which to use and with what priority?

@macanudo527
Copy link
Collaborator Author

Sounds good, but should it be a separate plugin, so that the user can decide which to use and with what priority?

I'm wondering about how to implement that. Maybe have two different CCXT pair converters one with the REST API, and one with the importing of CSV?

@eprbell
Copy link
Owner

eprbell commented Mar 25, 2024

Sounds good, but should it be a separate plugin, so that the user can decide which to use and with what priority?

I'm wondering about how to implement that. Maybe have two different CCXT pair converters one with the REST API, and one with the importing of CSV?

Yes, that's what I was thinking: that should give the user the most flexibility.

@macanudo527
Copy link
Collaborator Author

@eprbell btw, I'm going to try to cherry pick and splice the code from your PR. Hopefully, I can get it all put together here soon.

Should I implement this with inheritance, or by parameter that will load a particular class to handle forex processing? I'm thinking making ccxt pair converter abstract and the forex processing has to inherit from it. OR, make the REST API default, and then have another CSV class inherit and override the defaults.

@eprbell
Copy link
Owner

eprbell commented Mar 26, 2024

@eprbell btw, I'm going to try to cherry pick and splice the code from your PR. Hopefully, I can get it all put together here soon.

Sounds good, no worries.

Should I implement this with inheritance, or by parameter that will load a particular class to handle forex processing? I'm thinking making ccxt pair converter abstract and the forex processing has to inherit from it. OR, make the REST API default, and then have another CSV class inherit and override the defaults.

I'm leaning toward making the ccxt-based converter abstract and have the two forex processing plugins inherit from it. In theory we could do something similar to the input plugins and have csv and rest subdirectories, but that would not be backward compatible, would require changes in users config files, and would make this change more complex: let's leave it as is. If needed, we'll tackle that issue as a separate change later.

@macanudo527 macanudo527 force-pushed the implement_forex_python branch 6 times, most recently from 8b754be to 9a57efb Compare April 17, 2024 00:43
@macanudo527 macanudo527 force-pushed the implement_forex_python branch 2 times, most recently from 0b2ea73 to 8aebb3c Compare April 19, 2024 00:48
@macanudo527 macanudo527 force-pushed the implement_forex_python branch 6 times, most recently from 6286ef4 to 6414ff7 Compare April 27, 2024 00:21
@macanudo527 macanudo527 mentioned this pull request May 13, 2024
@macanudo527 macanudo527 marked this pull request as ready for review May 13, 2024 05:29
@macanudo527
Copy link
Collaborator Author

@eprbell This should fix most Forex Issues. It is lacking docs, but I thought I would let you get a peek at it.

@eprbell
Copy link
Owner

eprbell commented May 13, 2024

@eprbell This should fix most Forex Issues. It is lacking docs, but I thought I would let you get a peek at it.

Sounds good: I'll take a look (the diff in Github is large so it may take some time for me to go through it).

@macanudo527
Copy link
Collaborator Author

macanudo527 commented May 13, 2024

Sounds good: I'll take a look (the diff in Github is large so it may take some time for me to go through it).

I figured it would take a while. I tried to figure out a way to break down into simpler parts, but kind of just had to be done all at once. Sorry.

@macanudo527 macanudo527 changed the title [WIP] Implement forex python Implement forex python May 14, 2024
Copy link
Owner

@eprbell eprbell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, here's a first review.

@@ -485,7 +484,7 @@ aliases = <em>&lt;untradeable_assets&gt;</em>
Where:
* `<historical_price_type>` is one of `open`, `high`, `low`, `close`, `nearest`. When DaLi downloads historical market data, it captures a `bar` of data surrounding the timestamp of the transaction. Each bar has a starting timestamp, an ending timestamp, and OHLC prices. You can choose which price to select for price lookups. The open, high, low, and close prices are self-explanatory. The `nearest` price is either the open price or the close price of the bar depending on whether the transaction time is nearer the bar starting time or the bar ending time.
* `default_exchange` is an optional string for the name of an exchange to use if the exchange listed in a transaction is not currently supported by the CCXT plugin. If no default is set, Kraken(US) is used. If you would like an exchange added please open an issue. The current available exchanges are "Binance.com", "Gate", "Huobi" and "Kraken". Please note that the Binance.com API is not accessible from the US, Canada, Netherlands, and other [prohibited countries](https://www.binance.com/en/legal/list-of-prohibited-countries).
* `fiat_access_key` is an optional access key that can be obtained from [Exchangerate.host](https://exchangerate.host/). It is required for any fiat conversions, which are typically required if the base fiat is other than USD.
* The CCXT plugin makes use of the default forex exchange API, Frankfurter, which provides daily rates from the European Central Bank. Rates for bank holidays and weekends are taken from the previous trading day, so if a rate is requested for Saturday, the Friday rate will be used. Two other plugins are also available for forex - one that makes use of Exchangerate.host free plan and requires a fiat_access_key and another plugin that makes use of a CSV file for forex rates.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a spurious bullet in this bullet list (which is about ccxt pair converter plugin parameters): should it be moved elsewhere?

-------------------|------|------|------|------|------
2008-02-22 00:00:00|210.45|215.45|211.56|213.45|8900

Historical CSV Files are available from several places on the web for free. [Forexsb.com](https://forexsb.com/historical-forex-data) is a good source. Currently only 1 day candles are supported. One row per day a rate is needed is required. For example, if you want to use the [official yearly rates from the IRS](https://www.irs.gov/individuals/international-taxpayers/yearly-average-currency-exchange-rates), you will need to copy and paste the same rate for all days of the calendar year. CSV files can contain multiple years of rates.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "is needed is required"


#### Note on Forex CSV file

If you do not want to use Exchangerate.host or the Frankfurter API for Forex prices, you can use CSV files by saving them to the folder `.dali_cache/forex`. The files must be in the format of quote_asset_base_asset.csv (e.g. USD_JPY.csv). The format should be Time, high, low, open, close, volume. Like the below example:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you do not want to use Exchangerate.host or the Frankfurter API for Forex prices, you can use CSV files by saving them to the folder `.dali_cache/forex`. The files must be in the format of quote_asset_base_asset.csv (e.g. USD_JPY.csv). The format should be Time, high, low, open, close, volume. Like the below example:
If you do not want to use Exchangerate.host or the Frankfurter API for Forex prices, you can use CSV files by saving them to the folder `.dali_cache/forex`. The files must be in the format of <quote_asset>_<base_asset>.csv (e.g. USD_JPY.csv). The format should be Time, high, low, open, close, volume. Like the below example:

# It appears Kraken public API is limited to around 12 calls per minute.
# There also appears to be a limit of how many calls per 2 hour time period.
# Being authenticated lowers this limit.
_REQUEST_DELAYDICT: Dict[str, float] = {_KRAKEN: 5.1, _BITFINEX: 5.0}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion
_REQUEST_DELAY_DICT


}

# If Exchangerates.host is not available or the user does not have an access key, we can use this list
DEFAULT_FIAT_LIST: List[str] = ["AUD", "CAD", "CHF", "EUR", "GBP", "JPY", "NZD", "USD"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this list have the same number of elements as FIAT_PRIORITY?

@@ -1,4 +1,4 @@
# Copyright 2022 Neal Chambers
# Copyright 2024 Neal Chambers
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file is not new, so the date should not change.


for day in range((end_of_year - beginning_of_year).days + 1):
current_day: datetime = beginning_of_year + timedelta(days=day)
print(f"current_day: {current_day}, timestamp: {key.timestamp}")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the logger module instead of print.

@@ -0,0 +1,120 @@
# Copyright 2024 Neal Chambers
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this file live in the csv subdirectory?

@@ -62,7 +62,7 @@
_GOOGLE_DRIVE_DOWNLOAD_URL: str = "https://drive.usercontent.google.com/download"

# File ID for the unified CSV file ID. This will need to be replaced every quarter.
_UNIFIED_CSV_FILE_ID: str = "16YKyFkYlvawCHv3W7WuTFzM8RYgMRWMt"
_UNIFIED_CSV_FILE_ID: str = "11WtjXA9kvVYV9KDoebGV5U75dmcA3bJa"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the full URL to the file into a comment, so that people can download it manually and inspect it, if they wish to do so?

@@ -1,4 +1,4 @@
# Copyright 2022 Neal Chambers
# Copyright 2024 Neal Chambers
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to change the date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Forex Services Unavailable
2 participants