-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Informative Pairs annotated
# --- Do not remove these libs ---
from freqtrade.strategy.interface import IStrategy
from typing import Dict, List
from functools import reduce
from pandas import DataFrame
# --------------------------------
import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib
class InformativeSample(IStrategy):
"""
Sample strategy implementing Informative Pairs - compares stake_currency with USDT.
Not performing very well - but should serve as an example how to use a referential pair against USDT.
author@: xmatthias
github@: https://github.com/freqtrade/freqtrade-strategies
How to use it?
> python3 freqtrade -s InformativeSample
"""
# Minimal ROI designed for the strategy.
# This attribute will be overridden if the config file contains "minimal_roi"
minimal_roi = {
"60": 0.01,
"30": 0.03,
"20": 0.04,
"0": 0.05
}
# Optimal stoploss designed for the strategy
# This attribute will be overridden if the config file contains "stoploss"
stoploss = -0.10
# Optimal ticker interval for the strategy
ticker_interval = '5m'
# trailing stoploss
trailing_stop = False
trailing_stop_positive = 0.01
trailing_stop_positive_offset = 0.02
# run "populate_indicators" only for new candle
ta_on_candle = False
# Experimental settings (configuration will overide these if set)
use_sell_signal = True
sell_profit_only = True
ignore_roi_if_buy_signal = False
# Optional order type mapping
order_types = {
'buy': 'limit',
'sell': 'limit',
'stoploss': 'market',
'stoploss_on_exchange': False
}
def informative_pairs(self): ### (1)
"""
Define additional, informative pair/interval combinations to be cached from the exchange.
These pair/interval combinations are non-tradeable, unless they are part
of the whitelist as well.
For more information, please consult the documentation
:return: List of tuples in the format (pair, interval)
Sample: return [("ETH/USDT", "5m"),
("BTC/USDT", "15m"),
]
"""
return [(f"{self.config['stake_currency']}/USDT", self.ticker_interval)] ### (2)
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Adds several different TA indicators to the given DataFrame
Performance Note: For the best performance be frugal on the number of indicators
you are using. Let uncomment only the indicator you are using in your strategies
or your hyperopt configuration, otherwise you will waste your memory and CPU usage.
"""
dataframe['ema20'] = ta.EMA(dataframe, timeperiod=20) ### (3)
dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50)
dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100)
if self.dp:
# Get ohlcv data for informative pair.
data = self.dp.get_pair_dataframe(pair=f"{self.stake_currency}/USDT", ### (4)
timeframe=self.ticker_interval)
# Combine the 2 dataframes using 'close'.
# This will result in a column named 'closeETH' or 'closeBTC' - depending on stake_currency.
dataframe = dataframe.merge(data[["date", "close"]], on="date", how="left", ### (5)
suffixes=("", self.config['stake_currency']))
# Calculate SMA20 on 'close' data for stake_currency/USDT. Resulting column is named as 'smaETH20' (if stake_currency is ETH)
dataframe[f"sma{self.config['stake_currency']}20"] = ### (6)
dataframe[f'close{self.stake_currency}'].rolling(20).mean()
return dataframe
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Based on TA indicators, populates the buy signal for the given dataframe
:param dataframe: DataFrame
:return: DataFrame with buy column
"""
dataframe.loc[
(
(dataframe['ema20'] > dataframe['ema50']) &
# stake/USDT above sma(stake/USDT, 20)
(dataframe[f'close{self.stake_currency}'] > dataframe[f'sma{self.stake_currency}20']) ### (7)
),
'buy'] = 1
return dataframe
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Based on TA indicators, populates the sell signal for the given dataframe
:param dataframe: DataFrame
:return: DataFrame with buy column
"""
dataframe.loc[
(
(dataframe['ema20'] < dataframe['ema50']) &
# stake/USDT below sma(stake/USDT, 20)
(dataframe[f'close{self.stake_currency}'] < dataframe[f'sma{self.stake_currency}20']) ### (8)
),
'sell'] = 1
return dataframe
(1) Informative pairs are non-tradable pairs, the bot downloads ohlcv data for that in the same manner as ohlcv data is updated for tradable pairs in the whitelist. But they are not traded by the bot. The data may be of same timeframe as ticker_interval set in the config, or of another timeframe. The dataframe with data for these pairs is available via so-called dataprovider, implemented in the bot, which supplies the custom strategy and other parts of the bot with various data, including ohclv for informative pairs, defined in this strategy attribute.
See more on informative pairs here: https://www.freqtrade.io/en/latest/strategy-customization/#get-data-for-non-tradeable-pairs.
(2) This line declares informative pairs with the name "{stake_currency}/USDT"
, where {stake_currency}
is the stake currency as it's defined in the config, and, in this case, with the same timeframe as defined in the config for trading.
(3) Ordinal EMAs, which can be found in many strategies
(4) This line fetches the ohlcv data for the informative pair from the dataprovider. Live data (as for tradable pairs from whitelist) for Dry-run/live trade modes and historical data for backtesting.
During backtesting, the historical data for this pair and appropriate timeframe and backtesting timerange should exist, i.e. should be downloaded first for the whole backtesting timerange to produce correct results.
(5) This merges the 'close'
column from the dataframe with data fetched for the informative pair to the "main" dataframe with ohlcv data and indicators for the tradable pair. The resulting column is assigned the name "close{stake_currency}"
, i.e. "closeETH"
or "closeBTC"
for stake currency ETH and BTC respectively.
(6) This calculates SMA20 on the "close{stake_currency}"
column, i.e. on data for informative pair. The resulting column for this indicator is called "sma{stake_currency}20"
, i.e. like "smaETH20"
or "smaBTC20"
, depending on the stake currency.
(7) This implements a condition for buy signals, calculated on the informative pair data: actually, it can be read as closeETH > smaBTC20
.
(8) Similar condition for the sell signals. You can combine both data for the traded pair and for the informative pair(s) since latter are just columns in the "main" dataframe after their merge.