Add Stocks and FX to a default Freqtrade

To make a custom exchange.

sudo apt-get update

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

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": "USD", //CHANGE THIS FROM USDT TO USD

"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": "alpacastocks",

"key": "",

"secret": "",

"pair_whitelist": [ // REMOVE THE USDT, REPLACE WITH USD

"TSLA/USD",

"AAPL/USD",

"MSFT/USD"

],

"pair_blacklist": []

},

"pairlists": [

{

"method": "StaticPairList",

"pairs": [ // REMOVE THE USDT, REPLACE WITH USD

"TSLA/USD",

"AAPL/USD",

"MSFT/USD"

]

}

]

}

Forex (extract)

"exchange": {

"name": "oanda",

"key": "",

"secret": "",

"pair_whitelist": [ // REMOVE THE USDT, REPLACE WITH USD

"EUR/USD",

"AUD/USD",

"GBP/USD"

],

"pair_blacklist": []

},

"pairlists": [

{ // REMOVE THE USDT, REPLACE WITH USD

"method": "StaticPairList",

"pairs": [

"EUR/USD",

"AUD/USD",

"GBP/USD"

]

}

],

The default config.json files need to be edited, they use volume pair lists, for this example it needs to be static pairs. Run these commands to see the situation, find a freqtrade strategy and run it, sample_strategy is by default supplied with Freqtrade...

pip install alpaca-py

pip install oandapyV20

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

and

freqtrade download-data --exchange alpacastocks

and

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

***Forbidden - Alpaca stocks requires an API key to be setup with Alpaca.

***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 bypass 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/alpacastocks.py
  2. freqtrade/exchange/oanda.py

Alpaca for stocks and Oanda for foreign exchange or another, in these we define out alpacastocks class and oanda class

This is an identical replication with what freqtrade does with crypto. Note: ccxt has Alpaca crypto routines so we name it alpacastocks.

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

The file is freqtrade/exchange/__init__.py and add

from freqtrade.exchange.stockexchange import Exchange

from freqtrade.exchange.foreignexchange import Exchange

from freqtrade.exchange.alpacastocks import Alpacastocks

from freqtrade.exchange.oanda import Oanda

We must also register the new exchanges with freqtrade in freqtrade/exchange/common.py

SUPPORTED_EXCHANGES = [

"binance",

"bingx",

...

"alpacastocks",

"oanda",

]

from freqtrade.exchange.stockexchange import Exchange

class Alpacastocks(Exchange):

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

@property

def name(self):

return "alpacastocks"

The check_exchange.py routine is the bypass point, edit it to accept all exchanges listed in SUPPORTED_EXCHANGES, 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(f"\nThe {exchange.capitalize()} exchange has been recognized!")

print(f"The {exchange.capitalize()} exchange has been recognized!")

print(f"The {exchange.capitalize()} exchange has been recognized!\n")

return True

Make sure the __pycache__ is deleted.

Run the 3 test commands again, ccxt is bypassed and the handler is registered. Now all the logic for foreign exchange and stock exchange needs to be found in the new files, such as API calls to the exchange and so on as freqtrade queries this file for the objects definitions.

2024-12-14 09:57:39,498 - freqtrade - ERROR - No pair in whitelist.

Make sure the config file has USD and NOT USDT , GBP/USD and stake current is also USD.

Files edited...

  1. freqtrade/exchange/__init__.py
  2. freqtrade/exchange/common.py
  3. freqtrade/exchange/check_exchange.py

File added...

  1. freqtrade/exchange/stockexchnage.py
  2. freqtrade/exchange/foreignexchange.py
  3. freqtrade/exchange/alpacastocks.py
  4. freqtrade/exchange/oanda.py

Pips...

  1. pip install alpaca-py

Implement the required functions. Atleast make it exit gracefully.

Part 2: Building stockexhange.py and alpacastocks.py for Backtesting stocks.

...and foreignexchange.py and oanda.py for currency trading.

Rather than, all the changed file are in the download. Extract and replace and to a default Freqtrade installation and run the 3 commands again.

All the alterations in a zip file freqtrade_stocks.tar.gz

The new classes need work.

  

📝 📜 ⏱️ ⬆️