Add Stocks and FX to a default Freqtrade

This revision is from 2024/12/12 21:29. You can Restore it.

To make a custom exchange.

sudo apt-get update

sudo apt install -y python3-pip python3-venv python3-dev python3-pandas git curl

git clone https://github.com/freqtrade/freqtrade.git

cd freqtrade

python -m .venv venv

source .venv/bin/activate

deactivate

./setup.sh -i

source .venv/bin/activate

freqtrade create-userdir --userdir user_data

freqtrade new-config --config user_data/crypto_config.json

freqtrade new-config --config user_data/stocks_config.json

freqtrade new-config --config user_data/forex_config.json

{

"max_open_trades": 100,

"stake_currency": "USDT",

"stake_amount": 1000,

"dry_run": true,

"dry_run_wallet": 100000,

"cache": "none", //avoid lookahead bias

"enable_protections": false, //avoid lookahead bias

"exit_pricing":{

"price_side": "other"

},

"entry_pricing": {

"price_side": "other"

},

"exchange": {

"name": "stocks",

"key": "",

"secret": "",

"pair_whitelist": [

"TSLA/USDT",

"AAPL/USDT",

"MSFT/USDT"

],

"pair_blacklist": []

},

"pairlists": [

{

"method": "StaticPairList",

"pairs": [

"TSLA/USDT",

"AAPL/USDT",

"MSFT/USDT"

]

}

]

}

Forex (extract)

"exchange": {

"name": "forex",

"key": "",

"secret": "",

"pair_whitelist": [

"EUR/USDT",

"AUD/USDT",

"GBP/USDT"

],

"pair_blacklist": []

},

"pairlists": [

{

"method": "StaticPairList",

"pairs": [

"EUR/USDT",

"AUD/USDT",

"GBP/USDT"

]

}

],

Run these commands to see the situation, find a freqtrade strategy and run it...

freqtrade backtesting -c user_data/stocks_config.json -s SMA --timerange=20240101-20240201

and

freqtrade download-data --exchange stocks

and

freqtrade download-data --config user_data/stocks_config.json --timeframes 1m --timerange 20240101-

***freqtrade - ERROR - Exchange "stocks" is not known to the ccxt library and therefore not available for the bot.

There are three file to set this up.

Step 1: Create a Custom Exchange Class

We need to replace and replicate the exchange/exchange.py file.

Create a new file stockexchange.py in the freqtrade/exchange directory. In this file, define our exchange class.

  1. freqtrade/exchange/stockexchange.py
  2. freqtrade/exchange/foreignexchange.py

We also make per exchange files, we want...

  1. freqtrade/exchange/stocksalpaca.py
  2. freqtrade/exchange/fxoanda.py

Alpaca for stocks and Oanda for foreign exchange, in these we define out alpaca class and oanda class

This is an identical replication with what freqtrade does with crypto.

The freqtrade.exchange.stockexchange.exchange is imported into stocksalpaca.py and stocksalpaca in stocksalpaca.py is registered as a new exchange within freqtrade and called in user_data/stocks_config.json

from freqtrade.exchange import Exchange

class Custardtart(Exchange):

def __init__(self, config, validate=True, exchange_config=None, load_leverage_tiers=False):

@property

def name(self):

return "custardtart"

Register it with freqtrade/exchange/__init__.py

from freqtrade.exchange.custardtart import Custardtart

Add it to /freqtrade/exchange/common.py

SUPPORTED_EXCHANGES = [

"custardtart",

]

freqtrade/exchange/check_exchange.py

from freqtrade.exchange.common import MAP_EXCHANGE_CHILDCLASS, SUPPORTED_EXCHANGES

....

exchange = config.get("exchange", {}).get("name", "").lower()

if not exchange:

raise OperationalException(

f"This command requires a configured exchange. You should either use "

f"`--exchange <exchange_name>` or specify a configuration file via `--config`.\n"

f"The following exchanges are available for Freqtrade: "

f'{", ".join(available_exchanges())}'

)

if exchange in SUPPORTED_EXCHANGES:

print("Custardtart exchange recognized")

return True

Make sure the __pycache__ is deleted.

Run the test commands, ccxt is bypassed and the handler is registered, custardtart.py. Now all the logic needs to be found in custardtart.py, API calls to the exchange and so on as freqtrade queries this file for the objects definitions.

Make it so custardtart.py exits gracefully.

  

📝 📜 ⏱️ ⬆️