freqtrade / freqtrade

Free, open source crypto trading bot
https://www.freqtrade.io
GNU General Public License v3.0
28.73k stars 6.16k forks source link

Can load_pair_history handle futures data? #7073

Closed konradsemsch closed 2 years ago

konradsemsch commented 2 years ago

Describe your environment

Your question

I'm working with a strategy that can buy long and short trades, for which it uses futures data. It seems to be working correctly when I run hyperopt or backtesting through the freqtrade cli. However, when I wanted to load my data for further analysis in a jupyter notebook the load_pair_history function wasn't able to load it.

Or is simply my understanding of this wrong and I should use regular spot data as input for fine-tuning my strategy and the configuration is more intended for actually running it?

Here's a snippet of the code I try to execute (it worked with regular spot data):

import os
import pandas as pd
import numpy as np

import plotly.express as px
from pathlib import Path
from freqtrade.configuration import Configuration

os.chdir("../../") # based on: https://github.com/freqtrade/freqtrade/issues/5914

config = Configuration.from_files(["user_data/strategies/rf_naive/config.json"])

# Define some constants
STRATEGY = "RfNaiveStrategy"
TIMEFRAME = "15m"
PAIR = "BTC/USDT"
DATA_LOCATION = Path(config['user_data_dir'], 'data', 'binance')

# Overwrite certain config values
config["strategy"] = STRATEGY
config["timeframe"] = TIMEFRAME
from freqtrade.data.history import load_pair_history
from freqtrade.resolvers import StrategyResolver
from freqtrade.data.dataprovider import DataProvider

# Load pair candle data
df_candles = load_pair_history(
    datadir=DATA_LOCATION,
    timeframe=TIMEFRAME,
    pair=PAIR,
    data_format = "json",
    candle_type = "futures",
    )

strategy = StrategyResolver.load_strategy(config)
strategy.dp = DataProvider(config, None, None)

# Generate buy/sell signals using strategy and set index
# df = strategy.analyze_ticker(candles, {"pair": PAIR}) - use this to generate indicators from Strategy
df = df_candles.set_index("date", drop=False)
df.tail()

# Report results
print_dates(df)
print("------------------------------")
print(f"Available columns: {df.columns}")

Changing this line also didn't resolve the problem:

data_location = Path(config['user_data_dir'], 'data', 'binance', 'futures')

Here's my package structure:

image

Here's the respective config file:


{
    "max_open_trades": 10,
    "stake_currency": "USDT", // crypto-currency used for trading
    "stake_amount": "unlimited", // amount used for each trade, by default the current balance is divided by `max_open_trades`
    "tradable_balance_ratio": 0.99, // share of balance available to the bot for trading, 1% is reserved for fees
    // "available_capital": ... - use when running multiple bots
    "fiat_display_currency": "EUR",
    "timeframe": "15m",
    "dry_run": true, // set to false when you want to run in full production mode
    "dry_run_wallet": 1000,
    "cancel_open_orders_on_exit": false, // TODO: should this be true
    // "process_only_new_candles": ... - might be useful to tap into tick data and not only full candles
    "trading_mode": "futures", // change to `futures` to short trade, normally `spot
    "margin_mode": "isolated", // only relevant for trading with leverage or short, set to `isolated` for short trades, normally ""
    "unfilledtimeout": {
        "entry": 10,
        "exit": 10,
        "exit_timeout_count": 0,
        "unit": "minutes"
    },
    // configuration of prices based on the orderbook when buying
    "entry_pricing": {
        "price_side": "same", // keep for consistency
        "use_order_book": true,
        "order_book_top": 1,
        "price_last_balance": 0.0, // will use the `side` price based on orderbook, don't change
        "check_depth_of_market": {
            // TODO: look into this later
            "enabled": false,
            "bids_to_ask_delta": 1
        }
    },
    // configuration of prices based on the orderbook when selling
    "exit_pricing":{
        "price_side": "same", // keep for consistency
        "use_order_book": true,
        "order_book_top": 1
    },
    "recursive_strategy_search": true, // allows to group strategies and their models in subfolders in strategies folder
    "exchange": {
        "name": "binance",
        //"sandbox": true, - can be activated when I want to test binance integration before running the bot live
        "key": "",
        "secret": "",
        "ccxt_config": {},
        "ccxt_async_config": {},
        "pair_whitelist": [ // list of pairs to trade; set in combination with `pairlists` parameter
            "BTC/USDT"
        ],
        "pair_blacklist": [
            "BNB/.*"
        ]
    },
    "pairlists": [
        {"method": "StaticPairList"}
    ],
    "edge": { // TODO: look into this later in order to adjust the position amount depending on model's probability
        "enabled": false,
        "process_throttle_secs": 3600,
        "calculate_since_number_of_days": 7,
        "allowed_risk": 0.01,
        "stoploss_range_min": -0.01,
        "stoploss_range_max": -0.1,
        "stoploss_range_step": -0.01,
        "minimum_winrate": 0.60,
        "minimum_expectancy": 0.20,
        "min_trade_number": 10,
        "max_trade_duration_minute": 1440,
        "remove_pumps": false
    }, // TODO: look into this later
    "telegram": {
        "enabled": false,
        "token": "",
        "chat_id": ""
    },
    "api_server": {
        "enabled": true,
        "listen_ip_address": "0.0.0.0",
        "listen_port": 8080,
        "verbosity": "error",
        "enable_openapi": false,
        "jwt_secret_key": "...",
        "CORS_origins": [],
        "username": "...", // this will have to be defined separately when the bot will run in production, in e.g. AWS Secrets Manager
        "password": "..."
    },
    "bot_name": "freqtrade_bbrsi",
    "initial_state": "running",
    "force_entry_enable": false, // allows to force the bot to enter trades via the API or Telegram
    "internals": {
        "process_throttle_secs": 5
    }
}
xmatthias commented 2 years ago

Please don't use screenshots when posting code. It disables searching for other users, as well as disables copy/paste if we'd like to try something out.

The path should NOT contain the /futures folder. The configuration is also irrelevant to load_pair_history() - which is a function in it's own.

load_pair_history has an additional argument in candle_type which you don't seem to be using. It's defaulting to spot - changing this to CandleType.FUTURES should load futures data. Using 'futures' (as string) should also work, but that's untested - but based on experience with other methods.

konradsemsch commented 2 years ago

Oh, thank you - I missed that! I will check the method you mentioned and change my print screen to pure code.

konradsemsch commented 2 years ago

I can confirm that your resolution worked - hence closing this.