Add Stocks and FX to a default Freqtrade
This revision is from 2024/12/13 18:21. 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": "alpacastocks",
"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": "oanda",
"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 SampleStrategy --timerange=20240101-20240201
and
freqtrade download-data --exchange alpacastocks
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.
- freqtrade/exchange/stockexchange.py
- freqtrade/exchange/foreignexchange.py
We also make per exchange files, we want...
- freqtrade/exchange/alpacastocks.py
- freqtrade/exchange/oanda.py
Alpaca for stocks and Oanda for foreign exchange, 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.
Implement the required functions. Atleast make it exit gracefully.
Part 2: Adding Alpaca stocks and Oanda directly to ccxt for Freqtrade.
In the previous section if we made fresh new classes, ccxt would never be called, we then need to code all the interactions between Freqtrade and the exchange. For backtesting it is a pair verifications, that the pairs exist and a whole bunch of parameter functions. Let add alpacastocks and oanda to ccxt and run the 3 commands. In this case we copy freqtrade exchange.py and make the two new files stockexchange.py and foreignexchange.py and we take for example ihex.py in the exchnage and rename it to alpacastocks.py and oanda.py and change the class names in the files.
Now we go to ccxt and copy the existing alpaca.py file and copy and rename the file and class to alpacastocks.py and oanda.py and also in the async directory and add them to the __init__.py files.
Then we run the 3 command again. Alpaca requires an API key and password.
"exchange": {
"name": "alpacastocks",
"key": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
"password": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"use_sandbox": true,
"ccxt_config": {
"apiKey": "xxxxxxxxxxxxxxxxxxxxxxx",
"secret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"pair_whitelist": [
"TSLA/USDT",
"AAPL/USDT",
"MSFT/USDT"
],
"pair_blacklist": []
},
"pairlists": [
{
"method": "StaticPairList",
"pairs": [
"TSLA/USDT",
"AAPL/USDT",
"MSFT/USDT"
]
}
],
The code in copied files are Alpaca crypto API calls, you will have to change them to stocks communication. The new files can be used to implement stocks and foreign exchange.