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

Download, save and manipulate Binance data #128

Merged
merged 44 commits into from
Nov 22, 2023
Merged

Download, save and manipulate Binance data #128

merged 44 commits into from
Nov 22, 2023

Conversation

AlexTheLion123
Copy link
Contributor

@AlexTheLion123 AlexTheLion123 commented Nov 14, 2023

Goal

Primarily, allow the use of easily downloading and using Binance data in a strategy. This allows for backtesting much longer time periods; around 3 years in total for shorting, even longer for long only

  • Easily download candlestick and lending data from Binance API (no API keys needed)
  • convert lending data to supply data
  • Use Binance data in long and shorting strategies
  • Fix get_candles_by_pair method
  • Fix resample_candles() method
  • Add resample_series method

Related to tradingstrategy-ai/trade-executor#664

@AlexTheLion123 AlexTheLion123 changed the title Download, save and manipulate Binance Data Download, save and manipulate Binance data Nov 16, 2023
logger = logging.getLogger(__name__)


def get_binance_candlestick_data(
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename this to fetch_ so it is aligned with other function names in this package.

  • get: read from memory
  • fetch: read over HTTP

return df


def get_binance_data_parquet(
Copy link
Contributor

Choose a reason for hiding this comment

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

fetch_

tradingstrategy/binance/candlestick.py Outdated Show resolved Hide resolved
tradingstrategy/binance/lending_supply_rates.py Outdated Show resolved Hide resolved
tradingstrategy/binance/lending_supply_rates.py Outdated Show resolved Hide resolved
@@ -34,7 +34,7 @@ def test_generate_candle_data(synthetic_candles):
assert universe.get_pair_count() == 1
assert universe.get_candle_count() == 4

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add an integration test.

Test cases needed

  • Read fresh data
  • Read cached data
  • Purge cache
  • Fetch prices
  • Fetch lending rates
  • Use a specific folder under /tmp folder for tests.

tradingstrategy/binance/candlestick.py Outdated Show resolved Hide resolved
#pandas_time_delta = new_bucket.to_pandas_timedelta()
# https://stackoverflow.com/questions/21140630/resampling-trade-data-into-ohlcv-with-pandas
candles = df.resample(new_timedelta).mean(numeric_only=True)
candles = df.resample(new_timedelta).mean()
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure if this a correct way to resample, and it might be that open/high/etc. gets incorrectly resampled. I am not sure if this code path was ever correctly tested.

We are using a different method here:

https://github.com/tradingstrategy-ai/trade-executor/blob/6068ff60f9c0572ed1dee1b56e6d8c7e31e40f87/tradeexecutor/strategy/pandas_trader/alternative_market_data.py#L27

Can you confirm the resamping works by adding a simple test case that checks whether the given data was correctly resampled?

[],
)

for i in range(0, len(timestamps) - 1):
Copy link
Contributor

Choose a reason for hiding this comment

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

For user friendliness we can add a tqdm progress bar that gives a good time estimator in Jupyter Notebooks if data is being downloaded.

An example here:

if not progress_bar_start and progress_bar_description:

def test_read_fresh_candle_data(candle_downloader: BinanceDownloader):
"""Test reading fresh candle data. Will be downloaded from Binance API."""

if os.environ.get("GITHUB_ACTIONS", None) == "true":
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment why this is done like this - there is no context

@miohtama miohtama merged commit c2fd672 into master Nov 22, 2023
2 checks passed
miohtama pushed a commit to tradingstrategy-ai/trade-executor that referenced this pull request Dec 6, 2023
### Goal

Primarily, allow the use of easily downloading and using Binance data in a strategy. This allows for backtesting much longer time periods; around 3 years in total for shorting, even longer for long only

- Easily download candlestick and lending data from Binance API (no API keys needed)
- convert lending data to supply data
- Use Binance data in long and shorting strategies

See related prerequisite PR: tradingstrategy-ai/trading-strategy#128

### Summary

- Adds notebook demonstrating how to use `create_binance_universe`
- Bump tradingstrategy to 0.22.3
- make loading functions in `alternative_market_data.py` allow for multipair
- Add utils file for `load_binance_dataset` and `create_binance_universe`

#### Unrelated

- remove incorrect warning in `position_manager.py`
- Add `hover_text` bool argument for `visualise_single_pair` so user can hide hover_text if needs be. This is because the hover_text can sometimes be very large and cover much of the chart.
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.

2 participants