stable/docs/strategy-customization.md

664 lines
28 KiB
Markdown
Raw Normal View History

2019-12-03 09:51:52 +00:00
# Strategy Customization
2020-02-21 19:54:21 +00:00
This page explains where to customize your strategies, and add new indicators.
2018-01-02 02:17:10 +00:00
2018-01-18 07:06:37 +00:00
## Install a custom strategy file
This is very simple. Copy paste your strategy file into the directory `user_data/strategies`.
2018-01-18 07:06:37 +00:00
2019-11-17 09:31:21 +00:00
Let assume you have a class called `AwesomeStrategy` in the file `AwesomeStrategy.py`:
2019-11-17 09:31:21 +00:00
1. Move your file into `user_data/strategies` (you should have `user_data/strategies/AwesomeStrategy.py`
2018-03-25 14:42:20 +00:00
2. Start the bot with the param `--strategy AwesomeStrategy` (the parameter is the class name)
2018-01-18 07:06:37 +00:00
```bash
freqtrade trade --strategy AwesomeStrategy
2018-01-18 07:06:37 +00:00
```
2018-01-02 02:17:10 +00:00
2019-11-17 09:31:21 +00:00
## Develop your own strategy
2019-11-17 09:31:21 +00:00
The bot includes a default strategy file.
Also, several other strategies are available in the [strategy repository](https://github.com/freqtrade/freqtrade-strategies).
2018-01-02 02:17:10 +00:00
2019-11-17 09:31:21 +00:00
You will however most likely have your own idea for a strategy.
2019-11-24 08:55:34 +00:00
This document intends to help you develop one for yourself.
2019-11-17 09:31:21 +00:00
To get started, use `freqtrade new-strategy --strategy AwesomeStrategy`.
This will create a new strategy file from a template, which will be located under `user_data/strategies/AwesomeStrategy.py`.
!!! Note
This is just a template file, which will most likely not be profitable out of the box.
### Anatomy of a strategy
2018-01-18 07:06:37 +00:00
A strategy file contains all the information needed to build a good strategy:
- Indicators
2018-01-18 07:06:37 +00:00
- Buy strategy rules
- Sell strategy rules
- Minimal ROI recommended
2018-12-26 12:06:25 +00:00
- Stoploss strongly recommended
2018-01-02 02:17:10 +00:00
2019-08-27 04:41:07 +00:00
The bot also include a sample strategy called `SampleStrategy` you can update: `user_data/strategies/sample_strategy.py`.
You can test it with the parameter: `--strategy SampleStrategy`
Additionally, there is an attribute called `INTERFACE_VERSION`, which defines the version of the strategy interface the bot should use.
The current version is 2 - which is also the default when it's not set explicitly in the strategy.
Future versions will require this to be set.
2018-12-30 16:14:03 +00:00
```bash
freqtrade trade --strategy AwesomeStrategy
2018-01-18 07:06:37 +00:00
```
**For the following section we will use the [user_data/strategies/sample_strategy.py](https://github.com/freqtrade/freqtrade/blob/develop/freqtrade/templates/sample_strategy.py)
2018-12-27 06:03:28 +00:00
file as reference.**
!!! Note "Strategies and Backtesting"
2019-01-21 19:03:19 +00:00
To avoid problems and unexpected differences between Backtesting and dry/live modes, please be aware
that during backtesting the full time-interval is passed to the `populate_*()` methods at once.
It is therefore best to use vectorized operations (across the whole dataframe, not loops) and
avoid index referencing (`df.iloc[-1]`), but instead use `df.shift()` to get to the previous candle.
!!! Warning "Warning: Using future data"
Since backtesting passes the full time interval to the `populate_*()` methods, the strategy author
needs to take care to avoid having the strategy utilize data from the future.
Some common patterns for this are listed in the [Common Mistakes](#common-mistakes-when-developing-strategies) section of this document.
### Customize Indicators
Buy and sell strategies need indicators. You can add more indicators by extending the list contained in the method `populate_indicators()` from your strategy file.
You should only add the indicators used in either `populate_buy_trend()`, `populate_sell_trend()`, or to populate another indicator, otherwise performance may suffer.
It's important to always return the dataframe without removing/modifying the columns `"open", "high", "low", "close", "volume"`, otherwise these fields would contain something unexpected.
Sample:
```python
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.
:param dataframe: Dataframe with data from the exchange
:param metadata: Additional information, like the currently traded pair
:return: a Dataframe with all mandatory indicators for the strategies
"""
dataframe['sar'] = ta.SAR(dataframe)
dataframe['adx'] = ta.ADX(dataframe)
stoch = ta.STOCHF(dataframe)
dataframe['fastd'] = stoch['fastd']
dataframe['fastk'] = stoch['fastk']
dataframe['blower'] = ta.BBANDS(dataframe, nbdevup=2, nbdevdn=2)['lowerband']
dataframe['sma'] = ta.SMA(dataframe, timeperiod=40)
dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9)
dataframe['mfi'] = ta.MFI(dataframe)
dataframe['rsi'] = ta.RSI(dataframe)
dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5)
dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10)
dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50)
dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100)
dataframe['ao'] = awesome_oscillator(dataframe)
macd = ta.MACD(dataframe)
dataframe['macd'] = macd['macd']
dataframe['macdsignal'] = macd['macdsignal']
dataframe['macdhist'] = macd['macdhist']
hilbert = ta.HT_SINE(dataframe)
dataframe['htsine'] = hilbert['sine']
dataframe['htleadsine'] = hilbert['leadsine']
dataframe['plus_dm'] = ta.PLUS_DM(dataframe)
dataframe['plus_di'] = ta.PLUS_DI(dataframe)
dataframe['minus_dm'] = ta.MINUS_DM(dataframe)
dataframe['minus_di'] = ta.MINUS_DI(dataframe)
return dataframe
```
2018-12-30 16:14:03 +00:00
!!! Note "Want more indicator examples?"
Look into the [user_data/strategies/sample_strategy.py](https://github.com/freqtrade/freqtrade/blob/develop/freqtrade/templates/sample_strategy.py).
2018-12-30 16:14:03 +00:00
Then uncomment indicators you need.
2018-12-26 12:32:35 +00:00
### Strategy startup period
2019-10-28 12:05:54 +00:00
Most indicators have an instable startup period, in which they are either not available, or the calculation is incorrect. This can lead to inconsistencies, since Freqtrade does not know how long this instable period should be.
To account for this, the strategy can be assigned the `startup_candle_count` attribute.
This should be set to the maximum number of candles that the strategy requires to calculate stable indicators.
In this example strategy, this should be set to 100 (`startup_candle_count = 100`), since the longest needed history is 100 candles.
``` python
dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100)
```
By letting the bot know how much history is needed, backtest trades can start at the specified timerange during backtesting and hyperopt.
2019-10-27 09:56:38 +00:00
!!! Warning
2019-10-28 12:05:54 +00:00
`startup_candle_count` should be below `ohlcv_candle_limit` (which is 500 for most exchanges) - since only this amount of candles will be available during Dry-Run/Live Trade operations.
#### Example
Let's try to backtest 1 month (January 2019) of 5m candles using an example strategy with EMA100, as above.
``` bash
freqtrade backtesting --timerange 20190101-20190201 --ticker-interval 5m
```
2019-10-28 12:05:54 +00:00
Assuming `startup_candle_count` is set to 100, backtesting knows it needs 100 candles to generate valid buy signals. It will load data from `20190101 - (100 * 5m)` - which is ~2019-12-31 15:30:00.
If this data is available, indicators will be calculated with this extended timerange. The instable startup period (up to 2019-01-01 00:00:00) will then be removed before starting backtesting.
!!! Note
2019-10-28 12:05:54 +00:00
If data for the startup period is not available, then the timerange will be adjusted to account for this startup period - so Backtesting would start at 2019-01-01 08:30:00.
### Buy signal rules
Edit the method `populate_buy_trend()` in your strategy file to update your buy strategy.
It's important to always return the dataframe without removing/modifying the columns `"open", "high", "low", "close", "volume"`, otherwise these fields would contain something unexpected.
This will method will also define a new column, `"buy"`, which needs to contain 1 for buys, and 0 for "no action".
2018-01-18 07:06:37 +00:00
2019-08-27 04:41:07 +00:00
Sample from `user_data/strategies/sample_strategy.py`:
2018-01-02 02:17:10 +00:00
```python
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
2018-01-02 02:17:10 +00:00
"""
Based on TA indicators, populates the buy signal for the given dataframe
:param dataframe: DataFrame populated with indicators
:param metadata: Additional information, like the currently traded pair
2018-01-02 02:17:10 +00:00
:return: DataFrame with buy column
"""
dataframe.loc[
(
2019-10-15 17:38:23 +00:00
(qtpylib.crossed_above(dataframe['rsi'], 30)) & # Signal: RSI crosses above 30
2019-10-14 18:13:34 +00:00
(dataframe['tema'] <= dataframe['bb_middleband']) & # Guard
(dataframe['tema'] > dataframe['tema'].shift(1)) & # Guard
(dataframe['volume'] > 0) # Make sure Volume is not 0
2018-01-02 02:17:10 +00:00
),
'buy'] = 1
return dataframe
```
2019-10-14 18:13:34 +00:00
!!! Note
2019-10-15 17:38:23 +00:00
Buying requires sellers to buy from - therefore volume needs to be > 0 (`dataframe['volume'] > 0`) to make sure that the bot does not buy/sell in no-activity periods.
2019-10-14 18:13:34 +00:00
### Sell signal rules
2018-01-02 02:17:10 +00:00
Edit the method `populate_sell_trend()` into your strategy file to update your sell strategy.
Please note that the sell-signal is only used if `use_sell_signal` is set to true in the configuration.
It's important to always return the dataframe without removing/modifying the columns `"open", "high", "low", "close", "volume"`, otherwise these fields would contain something unexpected.
This will method will also define a new column, `"sell"`, which needs to contain 1 for sells, and 0 for "no action".
2019-08-27 04:41:07 +00:00
Sample from `user_data/strategies/sample_strategy.py`:
2018-01-02 02:17:10 +00:00
```python
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
2018-01-02 02:17:10 +00:00
"""
Based on TA indicators, populates the sell signal for the given dataframe
:param dataframe: DataFrame populated with indicators
:param metadata: Additional information, like the currently traded pair
2018-01-02 02:17:10 +00:00
:return: DataFrame with buy column
"""
dataframe.loc[
(
2019-10-15 17:38:23 +00:00
(qtpylib.crossed_above(dataframe['rsi'], 70)) & # Signal: RSI crosses above 70
2019-10-14 18:13:34 +00:00
(dataframe['tema'] > dataframe['bb_middleband']) & # Guard
2019-10-15 12:50:51 +00:00
(dataframe['tema'] < dataframe['tema'].shift(1)) & # Guard
2019-10-14 18:13:34 +00:00
(dataframe['volume'] > 0) # Make sure Volume is not 0
2018-01-02 02:17:10 +00:00
),
'sell'] = 1
return dataframe
```
### Minimal ROI
This dict defines the minimal Return On Investment (ROI) a trade should reach before selling, independent from the sell signal.
It is of the following format, with the dict key (left side of the colon) being the minutes passed since the trade opened, and the value (right side of the colon) being the percentage.
2018-01-02 02:17:10 +00:00
2018-12-30 16:14:03 +00:00
```python
minimal_roi = {
"40": 0.0,
"30": 0.01,
"20": 0.02,
"0": 0.04
}
```
The above configuration would therefore mean:
- Sell whenever 4% profit was reached
2019-01-01 15:33:02 +00:00
- Sell when 2% profit was reached (in effect after 20 minutes)
- Sell when 1% profit was reached (in effect after 30 minutes)
- Sell when trade is non-loosing (in effect after 40 minutes)
The calculation does include fees.
To disable ROI completely, set it to an insanely high number:
2018-01-02 02:17:10 +00:00
```python
minimal_roi = {
"0": 100
}
```
While technically not completely disabled, this would sell once the trade reaches 10000% Profit.
To use times based on candle duration (ticker_interval or timeframe), the following snippet can be handy.
This will allow you to change the ticket_interval for the strategy, and ROI times will still be set as candles (e.g. after 3 candles ...)
``` python
from freqtrade.exchange import timeframe_to_minutes
class AwesomeStrategy(IStrategy):
2020-02-27 13:04:12 +00:00
ticker_interval = "1d"
ticker_interval_mins = timeframe_to_minutes(ticker_interval)
minimal_roi = {
2020-02-27 13:04:12 +00:00
"0": 0.05, # 5% for the first 3 candles
str(ticker_interval_mins * 3)): 0.02, # 2% after 3 candles
str(ticker_interval_mins * 6)): 0.01, # 1% After 6 candles
}
```
### Stoploss
Setting a stoploss is highly recommended to protect your capital from strong moves against you.
Sample:
``` python
stoploss = -0.10
2018-01-02 02:17:10 +00:00
```
This would signify a stoploss of -10%.
2019-05-20 18:06:13 +00:00
For the full documentation on stoploss features, look at the dedicated [stoploss page](stoploss.md).
2019-09-04 04:56:25 +00:00
If your exchange supports it, it's recommended to also set `"stoploss_on_exchange"` in the order_types dictionary, so your stoploss is on the exchange and cannot be missed due to network problems, high load or other reasons.
2019-05-20 18:06:35 +00:00
For more information on order_types please look [here](configuration.md#understand-order_types).
### Timeframe (ticker interval)
This is the set of candles the bot should download and use for the analysis.
Common values are `"1m"`, `"5m"`, `"15m"`, `"1h"`, however all values supported by your exchange should work.
Please note that the same buy/sell signals may work well with one timeframe, but not with the others.
This setting is accessible within the strategy methods as the `self.ticker_interval` attribute.
### Metadata dict
The metadata-dict (available for `populate_buy_trend`, `populate_sell_trend`, `populate_indicators`) contains additional information.
Currently this is `pair`, which can be accessed using `metadata['pair']` - and will return a pair in the format `XRP/BTC`.
The Metadata-dict should not be modified and does not persist information across multiple calls.
Instead, have a look at the section [Storing information](#Storing-information)
### Storing information
2019-10-08 19:07:38 +00:00
Storing information can be accomplished by creating a new dictionary within the strategy class.
2019-10-08 19:07:38 +00:00
The name of the variable can be chosen at will, but should be prefixed with `cust_` to avoid naming collisions with predefined strategy variables.
```python
class Awesomestrategy(IStrategy):
# Create custom dictionary
cust_info = {}
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# Check if the entry already exists
if "crosstime" in self.cust_info[metadata["pair"]:
self.cust_info[metadata["pair"]["crosstime"] += 1
else:
self.cust_info[metadata["pair"]["crosstime"] = 1
```
2019-03-23 18:40:52 +00:00
!!! Warning
The data is not persisted after a bot-restart (or config-reload). Also, the amount of data should be kept smallish (no DataFrames and such), otherwise the bot will start to consume a lot of memory and eventually run out of memory and crash.
2019-03-23 18:40:52 +00:00
!!! Note
If the data is pair-specific, make sure to use pair as one of the keys in the dictionary.
2020-05-12 20:25:57 +00:00
***
### Additional data (informative_pairs)
#### Get data for non-tradeable pairs
Data for additional, informative pairs (reference pairs) can be beneficial for some strategies.
2020-05-13 19:37:50 +00:00
Ohlcv data for these pairs will be downloaded as part of the regular whitelist refresh process and is available via `DataProvider` just as other pairs (see below).
2020-05-12 20:25:57 +00:00
These parts will **not** be traded unless they are also specified in the pair whitelist, or have been selected by Dynamic Whitelisting.
The pairs need to be specified as tuples in the format `("pair", "interval")`, with pair as the first and time interval as the second argument.
Sample:
``` python
def informative_pairs(self):
return [("ETH/USDT", "5m"),
("BTC/TUSD", "15m"),
]
```
!!! Warning
As these pairs will be refreshed as part of the regular whitelist refresh, it's best to keep this list short.
All intervals and all pairs can be specified as long as they are available (and active) on the used exchange.
It is however better to use resampling to longer time-intervals when possible
to avoid hammering the exchange with too many requests and risk being blocked.
***
2018-12-29 07:47:25 +00:00
### Additional data (DataProvider)
The strategy provides access to the `DataProvider`. This allows you to get additional data to use in your strategy.
2018-12-30 09:25:47 +00:00
All methods return `None` in case of failure (do not raise an exception).
2019-03-27 09:51:13 +00:00
Please always check the mode of operation to select the correct method to get data (samples see below).
2018-12-29 07:47:25 +00:00
2018-12-30 09:25:47 +00:00
#### Possible options for DataProvider
2020-05-12 20:25:57 +00:00
- [`available_pairs`](#available_pairs) - Property with tuples listing cached pairs with their intervals (pair, interval).
- [`current_whitelist()`](#current_whitelist) - Returns a current list of whitelisted pairs. Useful for accessing dynamic whitelists (ie. VolumePairlist)
- [`get_pair_dataframe(pair, timeframe)`](#get_pair_dataframepair-timeframe) - This is a universal method, which returns either historical data (for backtesting) or cached live data (for the Dry-Run and Live-Run modes).
2019-11-02 19:34:06 +00:00
- `historic_ohlcv(pair, timeframe)` - Returns historical data stored on disk.
- `market(pair)` - Returns market data for the pair: fees, limits, precisions, activity flag, etc. See [ccxt documentation](https://github.com/ccxt/ccxt/wiki/Manual#markets) for more details on the Market data structure.
2020-05-12 20:25:57 +00:00
- `ohlcv(pair, timeframe)` - Currently cached candle (OHLCV) data for the pair, returns DataFrame or empty DataFrame.
- [`orderbook(pair, maximum)`](#orderbookpair-maximum) - Returns latest orderbook data for the pair, a dict with bids/asks with a total of `maximum` entries.
2020-05-15 15:46:28 +00:00
- [`ticker(pair)`](#tickerpair) - Returns current ticker data for the pair. See [ccxt documentation](https://github.com/ccxt/ccxt/wiki/Manual#price-tickers) for more details on the Ticker data structure.
2018-12-30 09:25:47 +00:00
- `runmode` - Property containing the current runmode.
2020-05-12 20:25:57 +00:00
#### Example Usages:
#### *available_pairs*
``` python
if self.dp:
for pair, timeframe in self.dp.available_pairs:
print(f"available {pair}, {timeframe}")
```
#### *current_whitelist()*
2020-05-13 10:49:16 +00:00
Imagine you've developed a strategy that trades the `5m` timeframe using signals generated from a `1d` timeframe on the top 10 volume pairs by volume.
2020-05-12 20:25:57 +00:00
The strategy might look something like this:
2020-05-13 10:49:16 +00:00
*Scan through the top 10 pairs by volume using the `VolumePairList` every 5 minutes and use a 14 day ATR to buy and sell.*
2020-05-12 20:25:57 +00:00
2020-05-13 10:49:16 +00:00
Due to the limited available data, it's very difficult to resample our `5m` candles into daily candles for use in a 14 day ATR. Most exchanges limit us to just 500 candles which effectively gives us around 1.74 daily candles. We need 14 days at least!
2020-05-12 20:25:57 +00:00
Since we can't resample our data we will have to use an informative pair; and since our whitelist will be dynamic we don't know which pair(s) to use.
This is where calling `self.dp.current_whitelist()` comes in handy.
```python
class SampleStrategy(IStrategy):
# strategy init stuff...
2020-05-13 10:49:16 +00:00
ticker_interval = '5m'
2020-05-12 20:25:57 +00:00
# more strategy init stuff..
def informative_pairs(self):
# get access to all pairs available in whitelist.
pairs = self.dp.current_whitelist()
# Assign tf to each pair so they can be downloaded and cached for strategy.
informative_pairs = [(pair, '1d') for pair in pairs]
return informative_pairs
def populate_indicators(self, dataframe, metadata):
# Get the informative pair
informative = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe='1d')
# Get the 14 day ATR.
atr = ta.ATR(informative, timeperiod=14)
2020-05-13 10:49:16 +00:00
# Do other stuff
2020-05-12 20:25:57 +00:00
```
#### *get_pair_dataframe(pair, timeframe)*
2018-12-30 09:25:47 +00:00
2018-12-29 07:47:25 +00:00
``` python
2020-05-12 20:25:57 +00:00
# fetch live / historical candle (OHLCV) data for the first informative pair
2018-12-29 07:47:25 +00:00
if self.dp:
2019-08-19 22:32:02 +00:00
inf_pair, inf_timeframe = self.informative_pairs()[0]
informative = self.dp.get_pair_dataframe(pair=inf_pair,
2019-11-02 19:34:06 +00:00
timeframe=inf_timeframe)
2018-12-29 07:47:25 +00:00
```
!!! Warning "Warning about backtesting"
2019-08-19 22:32:02 +00:00
Be carefull when using dataprovider in backtesting. `historic_ohlcv()` (and `get_pair_dataframe()`
for the backtesting runmode) provides the full time-range in one go,
2019-01-21 19:22:49 +00:00
so please be aware of it and make sure to not "look into the future" to avoid surprises when running in dry/live mode).
!!! Warning "Warning in hyperopt"
This option cannot currently be used during hyperopt.
2019-03-25 18:31:10 +00:00
2020-05-12 20:25:57 +00:00
#### *orderbook(pair, maximum)*
2019-06-06 15:48:26 +00:00
``` python
if self.dp:
2020-02-12 22:44:46 +00:00
if self.dp.runmode.value in ('live', 'dry_run'):
2019-06-06 15:48:26 +00:00
ob = self.dp.orderbook(metadata['pair'], 1)
dataframe['best_bid'] = ob['bids'][0][0]
dataframe['best_ask'] = ob['asks'][0][0]
```
2019-08-16 04:29:19 +00:00
!!! Warning
The order book is not part of the historic data which means backtesting and hyperopt will not work if this
method is used.
2019-06-06 15:48:26 +00:00
#### *ticker(pair)*
``` python
if self.dp:
if self.dp.runmode.value in ('live', 'dry_run'):
ticker = self.dp.ticker(metadata['pair'])
dataframe['last_price'] = ticker['last']
dataframe['volume24h'] = ticker['quoteVolume']
dataframe['vwap'] = ticker['vwap']
```
2020-05-15 15:46:28 +00:00
!!! Warning
Although the ticker data structure is a part of the ccxt Unified Interface, the values returned by this method can
vary for different exchanges. For instance, many exchanges do not return `vwap` values, the FTX exchange
does not always fills in the `last` field (so it can be None), etc. So you need to carefully verify the ticker
data returned from the exchange and add appropriate error handling / defaults.
2020-05-15 15:46:28 +00:00
2020-05-12 20:25:57 +00:00
***
### Additional data (Wallets)
2018-12-29 07:47:25 +00:00
The strategy provides access to the `Wallets` object. This contains the current balances on the exchange.
2019-03-23 18:40:52 +00:00
!!! Note
2019-01-21 19:22:49 +00:00
Wallets is not available during backtesting / hyperopt.
2018-12-29 07:47:25 +00:00
Please always check if `Wallets` is available to avoid failures during backtesting.
``` python
if self.wallets:
free_eth = self.wallets.get_free('ETH')
used_eth = self.wallets.get_used('ETH')
total_eth = self.wallets.get_total('ETH')
```
#### Possible options for Wallets
- `get_free(asset)` - currently available balance to trade
- `get_used(asset)` - currently tied up balance (open orders)
- `get_total(asset)` - total available balance - sum of the 2 above
2020-05-12 20:25:57 +00:00
***
### Additional data (Trades)
A history of Trades can be retrieved in the strategy by querying the database.
At the top of the file, import Trade.
```python
from freqtrade.persistence import Trade
```
The following example queries for the current pair and trades from today, however other filters can easily be added.
``` python
2020-02-12 22:44:46 +00:00
if self.config['runmode'].value in ('live', 'dry_run'):
trades = Trade.get_trades([Trade.pair == metadata['pair'],
Trade.open_date > datetime.utcnow() - timedelta(days=1),
Trade.is_open == False,
]).order_by(Trade.close_date).all()
# Summarize profit for this pair.
curdayprofit = sum(trade.close_profit for trade in trades)
```
Get amount of stake_currency currently invested in Trades:
``` python
2020-02-12 22:44:46 +00:00
if self.config['runmode'].value in ('live', 'dry_run'):
total_stakes = Trade.total_open_trades_stakes()
```
Retrieve performance per pair.
Returns a List of dicts per pair.
``` python
2020-02-12 22:44:46 +00:00
if self.config['runmode'].value in ('live', 'dry_run'):
performance = Trade.get_overall_performance()
```
Sample return value: ETH/BTC had 5 trades, with a total profit of 1.5% (ratio of 0.015).
``` json
{'pair': "ETH/BTC", 'profit': 0.015, 'count': 5}
```
!!! Warning
Trade history is not available during backtesting or hyperopt.
2019-12-22 08:55:40 +00:00
### Prevent trades from happening for a specific pair
Freqtrade locks pairs automatically for the current candle (until that candle is over) when a pair is sold, preventing an immediate re-buy of that pair.
Locked pairs will show the message `Pair <pair> is currently locked.`.
#### Locking pairs from within the strategy
Sometimes it may be desired to lock a pair after certain events happen (e.g. multiple losing trades in a row).
Freqtrade has an easy method to do this from within the strategy, by calling `self.lock_pair(pair, until)`.
2019-12-22 08:59:25 +00:00
`until` must be a datetime object in the future, after which trading will be reenabled for that pair.
2019-12-22 08:55:40 +00:00
Locks can also be lifted manually, by calling `self.unlock_pair(pair)`.
2019-12-22 08:59:25 +00:00
To verify if a pair is currently locked, use `self.is_pair_locked(pair)`.
2019-12-22 08:55:40 +00:00
!!! Note
Locked pairs are not persisted, so a restart of the bot, or calling `/reload_config` will reset locked pairs.
2019-12-22 08:55:40 +00:00
!!! Warning
Locking pairs is not functioning during backtesting.
2019-12-22 08:55:40 +00:00
##### Pair locking example
``` python
from freqtrade.persistence import Trade
from datetime import timedelta, datetime, timezone
# Put the above lines a the top of the strategy file, next to all the other imports
# --------
# Within populate indicators (or populate_buy):
2020-02-12 22:44:46 +00:00
if self.config['runmode'].value in ('live', 'dry_run'):
2019-12-22 08:55:40 +00:00
# fetch closed trades for the last 2 days
trades = Trade.get_trades([Trade.pair == metadata['pair'],
Trade.open_date > datetime.utcnow() - timedelta(days=2),
Trade.is_open == False,
]).all()
# Analyze the conditions you'd like to lock the pair .... will probably be different for every strategy
sumprofit = sum(trade.close_profit for trade in trades)
if sumprofit < 0:
# Lock pair for 12 hours
self.lock_pair(metadata['pair'], until=datetime.now(timezone.utc) + timedelta(hours=12))
2019-12-22 08:55:40 +00:00
```
### Print created dataframe
To inspect the created dataframe, you can issue a print-statement in either `populate_buy_trend()` or `populate_sell_trend()`.
You may also want to print the pair so it's clear what data is currently shown.
``` python
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe.loc[
(
#>> whatever condition<<<
),
'buy'] = 1
# Print the Analyzed pair
print(f"result for {metadata['pair']}")
# Inspect the last 5 rows
print(dataframe.tail())
return dataframe
```
2019-05-09 06:56:27 +00:00
Printing more than a few rows is also possible (simply use `print(dataframe)` instead of `print(dataframe.tail())`), however not recommended, as that will be very verbose (~500 lines per pair every 5 seconds).
2018-12-26 12:06:25 +00:00
### Specify custom strategy location
If you want to use a strategy from a different directory you can pass `--strategy-path`
2018-12-26 12:06:25 +00:00
```bash
freqtrade trade --strategy AwesomeStrategy --strategy-path /some/directory
2018-12-26 12:06:25 +00:00
```
2020-02-06 14:02:11 +00:00
### Derived strategies
The strategies can be derived from other strategies. This avoids duplication of your custom strategy code. You can use this technique to override small parts of your main strategy, leaving the rest untouched:
``` python
2020-02-06 14:02:11 +00:00
class MyAwesomeStrategy(IStrategy):
...
stoploss = 0.13
trailing_stop = False
# All other attributes and methods are here as they
# should be in any custom strategy...
...
class MyAwesomeStrategy2(MyAwesomeStrategy):
# Override something
stoploss = 0.08
trailing_stop = True
```
Both attributes and methods may be overriden, altering behavior of the original strategy in a way you need.
2020-02-06 14:02:11 +00:00
### Common mistakes when developing strategies
2019-10-21 04:51:48 +00:00
Backtesting analyzes the whole time-range at once for performance reasons. Because of this, strategy authors need to make sure that strategies do not look-ahead into the future.
This is a common pain-point, which can cause huge differences between backtesting and dry/live run methods, since they all use data which is not available during dry/live runs, so these strategies will perform well during backtesting, but will fail / perform badly in real conditions.
2019-10-21 04:51:48 +00:00
The following lists some common patterns which should be avoided to prevent frustration:
- don't use `shift(-1)`. This uses data from the future, which is not available.
- don't use `.iloc[-1]` or any other absolute position in the dataframe, this will be different between dry-run and backtesting.
- don't use `dataframe['volume'].mean()`. This uses the full DataFrame for backtesting, including data from the future. Use `dataframe['volume'].rolling(<window>).mean()` instead
- don't use `.resample('1h')`. This uses the left border of the interval, so moves data from an hour to the start of the hour. Use `.resample('1h', label='right')` instead.
### Further strategy ideas
To get additional Ideas for strategies, head over to our [strategy repository](https://github.com/freqtrade/freqtrade-strategies). Feel free to use them as they are - but results will depend on the current market situation, pairs used etc. - therefore please backtest the strategy for your exchange/desired pairs first, evaluate carefully, use at your own risk.
Feel free to use any of them as inspiration for your own strategies.
We're happy to accept Pull Requests containing new Strategies to that repo.
2018-01-02 02:17:10 +00:00
2019-09-26 17:36:09 +00:00
We also got a *strategy-sharing* channel in our [Slack community](https://join.slack.com/t/highfrequencybot/shared_invite/enQtNjU5ODcwNjI1MDU3LTU1MTgxMjkzNmYxNWE1MDEzYzQ3YmU4N2MwZjUyNjJjODRkMDVkNjg4YTAyZGYzYzlhOTZiMTE4ZjQ4YzM0OGE) which is a great place to get and/or share ideas.
2018-06-08 08:57:52 +00:00
2018-01-02 02:17:10 +00:00
## Next step
2018-06-08 08:57:52 +00:00
Now you have a perfect strategy you probably want to backtest it.
2018-12-31 12:39:18 +00:00
Your next step is to learn [How to use the Backtesting](backtesting.md).