<inputclass="md-option"data-md-color-media=""data-md-color-scheme="default"data-md-color-primary="blue-grey"data-md-color-accent="tear"aria-label="Switch to dark mode"type="radio"name="__palette"id="__palette_0">
<labelclass="md-header__button md-icon"title="Switch to dark mode"for="__palette_1"hidden>
<inputclass="md-option"data-md-color-media=""data-md-color-scheme="slate"data-md-color-primary="blue-grey"data-md-color-accent="tear"aria-label="Switch to light mode"type="radio"name="__palette"id="__palette_1">
<labelclass="md-header__button md-icon"title="Switch to light mode"for="__palette_0"hidden>
<p>This page explains how to customize your strategies, add new indicators and set up trading rules.</p>
<p>Please familiarize yourself with <ahref="../bot-basics/">Freqtrade basics</a> first, which provides overall info on how the bot operates.</p>
<h2id="develop-your-own-strategy">Develop your own strategy<aclass="headerlink"href="#develop-your-own-strategy"title="Permanent link">¶</a></h2>
<p>The bot includes a default strategy file.
Also, several other strategies are available in the <ahref="https://github.com/freqtrade/freqtrade-strategies">strategy repository</a>.</p>
<p>You will however most likely have your own idea for a strategy.
This document intends to help you convert your strategy idea into your own strategy.</p>
<p>To get started, use <code>freqtrade new-strategy --strategy AwesomeStrategy</code> (you can obviously use your own naming for your strategy).
This will create a new strategy file from a template, which will be located under <code>user_data/strategies/AwesomeStrategy.py</code>.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>This is just a template file, which will most likely not be profitable out of the box.</p>
</div>
<detailsclass="hint">
<summary>Different template levels</summary>
<p><code>freqtrade new-strategy</code> has an additional parameter, <code>--template</code>, which controls the amount of pre-build information you get in the created strategy. Use <code>--template minimal</code> to get an empty strategy without any indicator examples, or <code>--template advanced</code> to get a template with most callbacks defined.</p>
</details>
<h3id="anatomy-of-a-strategy">Anatomy of a strategy<aclass="headerlink"href="#anatomy-of-a-strategy"title="Permanent link">¶</a></h3>
<p>A strategy file contains all the information needed to build a good strategy:</p>
<ul>
<li>Indicators</li>
<li>Entry strategy rules</li>
<li>Exit strategy rules</li>
<li>Minimal ROI recommended</li>
<li>Stoploss strongly recommended</li>
</ul>
<p>The bot also include a sample strategy called <code>SampleStrategy</code> you can update: <code>user_data/strategies/sample_strategy.py</code>.
You can test it with the parameter: <code>--strategy SampleStrategy</code></p>
<p>Additionally, there is an attribute called <code>INTERFACE_VERSION</code>, which defines the version of the strategy interface the bot should use.
The current version is 3 - which is also the default when it's not set explicitly in the strategy.</p>
<p>Future versions will require this to be set.</p>
<p><strong>For the following section we will use the <ahref="https://github.com/freqtrade/freqtrade/blob/develop/freqtrade/templates/sample_strategy.py">user_data/strategies/sample_strategy.py</a>
file as reference.</strong></p>
<divclass="admonition note">
<pclass="admonition-title">Strategies and Backtesting</p>
<p>To avoid problems and unexpected differences between Backtesting and dry/live modes, please be aware
that during backtesting the full time range is passed to the <code>populate_*()</code> methods at once.
It is therefore best to use vectorized operations (across the whole dataframe, not loops) and
avoid index referencing (<code>df.iloc[-1]</code>), but instead use <code>df.shift()</code> to get to the previous candle.</p>
</div>
<divclass="admonition warning">
<pclass="admonition-title">Warning: Using future data</p>
<p>Since backtesting passes the full time range to the <code>populate_*()</code> 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 <ahref="#common-mistakes-when-developing-strategies">Common Mistakes</a> section of this document.</p>
<p>Pandas provides fast ways to calculate metrics. To benefit from this speed, it's advised to not use loops, but use vectorized methods instead.</p>
<p>Vectorized operations perform calculations across the whole range of data and are therefore, compared to looping through each row, a lot faster when calculating indicators.</p>
<p>As a dataframe is a table, simple python comparisons like the following will not work</p>
<p>Buy and sell signals need indicators. You can add more indicators by extending the list contained in the method <code>populate_indicators()</code> from your strategy file.</p>
<p>You should only add the indicators used in either <code>populate_entry_trend()</code>, <code>populate_exit_trend()</code>, or to populate another indicator, otherwise performance may suffer.</p>
<p>It's important to always return the dataframe without removing/modifying the columns <code>"open", "high", "low", "close", "volume"</code>, otherwise these fields would contain something unexpected.</p>
<pclass="admonition-title">Want more indicator examples?</p>
<p>Look into the <ahref="https://github.com/freqtrade/freqtrade/blob/develop/freqtrade/templates/sample_strategy.py">user_data/strategies/sample_strategy.py</a>.
<p>Most indicators have an instable startup period, in which they are either not available (NaN), 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 <code>startup_candle_count</code> attribute.
This should be set to the maximum number of candles that the strategy requires to calculate stable indicators. In the case where a user includes higher timeframes with informative pairs, the <code>startup_candle_count</code> does not necessarily change. The value is the maximum period (in candles) that any of the informatives timeframes need to compute stable indicators.</p>
<p>You can use <ahref="../recursive-analysis/">recursive-analysis</a> to check and find the correct <code>startup_candle_count</code> to be used.</p>
<p>In this example strategy, this should be set to 400 (<code>startup_candle_count = 400</code>), since the minimum needed history for ema100 calculation to make sure the value is correct is 400 candles.</p>
<p>By letting the bot know how much history is needed, backtest trades can start at the specified timerange during backtesting and hyperopt.</p>
<divclass="admonition warning">
<pclass="admonition-title">Using x calls to get OHLCV</p>
<p>If you receive a warning like <code>WARNING - Using 3 calls to get OHLCV. This can result in slower operations for the bot. Please check if you really need 1500 candles for your strategy</code> - you should consider if you really need this much historic data for your signals.
Having this will cause Freqtrade to make multiple calls for the same pair, which will obviously be slower than one network request.
As a consequence, Freqtrade will take longer to refresh candles - and should therefore be avoided if possible.
This is capped to 5 total calls to avoid overloading the exchange, or make freqtrade too slow.</p>
</div>
<divclass="admonition warning">
<pclass="admonition-title">Warning</p>
<p><code>startup_candle_count</code> should be below <code>ohlcv_candle_limit * 5</code> (which is 500 * 5 for most exchanges) - since only this amount of candles will be available during Dry-Run/Live Trade operations.</p>
<p>Assuming <code>startup_candle_count</code> is set to 400, backtesting knows it needs 400 candles to generate valid buy signals. It will load data from <code>20190101 - (400 * 5m)</code> - which is ~2018-12-30 11:40: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.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>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-02 09:20:00.</p>
</div>
<h3id="entry-signal-rules">Entry signal rules<aclass="headerlink"href="#entry-signal-rules"title="Permanent link">¶</a></h3>
<p>Edit the method <code>populate_entry_trend()</code> in your strategy file to update your entry strategy.</p>
<p>It's important to always return the dataframe without removing/modifying the columns <code>"open", "high", "low", "close", "volume"</code>, otherwise these fields would contain something unexpected.</p>
<p>This method will also define a new column, <code>"enter_long"</code> (<code>"enter_short"</code> for shorts), which needs to contain 1 for entries, and 0 for "no action". <code>enter_long</code> is a mandatory column that must be set even if the strategy is shorting only.</p>
<p>Sample from <code>user_data/strategies/sample_strategy.py</code>:</p>
<spanclass="p">(</span><spanclass="n">dataframe</span><spanclass="p">[</span><spanclass="s1">'volume'</span><spanclass="p">]</span><spanclass="o">></span><spanclass="mi">0</span><spanclass="p">)</span><spanclass="c1"># Make sure Volume is not 0</span>
<spanclass="p">(</span><spanclass="n">dataframe</span><spanclass="p">[</span><spanclass="s1">'volume'</span><spanclass="p">]</span><spanclass="o">></span><spanclass="mi">0</span><spanclass="p">)</span><spanclass="c1"># Make sure Volume is not 0</span>
<spanclass="p">(</span><spanclass="n">dataframe</span><spanclass="p">[</span><spanclass="s1">'volume'</span><spanclass="p">]</span><spanclass="o">></span><spanclass="mi">0</span><spanclass="p">)</span><spanclass="c1"># Make sure Volume is not 0</span>
<p>Buying requires sellers to buy from - therefore volume needs to be > 0 (<code>dataframe['volume'] > 0</code>) to make sure that the bot does not buy/sell in no-activity periods.</p>
</div>
<h3id="exit-signal-rules">Exit signal rules<aclass="headerlink"href="#exit-signal-rules"title="Permanent link">¶</a></h3>
<p>Edit the method <code>populate_exit_trend()</code> into your strategy file to update your exit strategy.
The exit-signal can be suppressed by setting <code>use_exit_signal</code> to false in the configuration or strategy.
<code>use_exit_signal</code> will not influence <ahref="#colliding-signals">signal collision rules</a> - which will still apply and can prevent entries.</p>
<p>It's important to always return the dataframe without removing/modifying the columns <code>"open", "high", "low", "close", "volume"</code>, otherwise these fields would contain something unexpected.</p>
<p>This method will also define a new column, <code>"exit_long"</code> (<code>"exit_short"</code> for shorts), which needs to contain 1 for exits, and 0 for "no action".</p>
<p>Sample from <code>user_data/strategies/sample_strategy.py</code>:</p>
<spanclass="p">(</span><spanclass="n">dataframe</span><spanclass="p">[</span><spanclass="s1">'volume'</span><spanclass="p">]</span><spanclass="o">></span><spanclass="mi">0</span><spanclass="p">)</span><spanclass="c1"># Make sure Volume is not 0</span>
<spanclass="p">(</span><spanclass="n">dataframe</span><spanclass="p">[</span><spanclass="s1">'volume'</span><spanclass="p">]</span><spanclass="o">></span><spanclass="mi">0</span><spanclass="p">)</span><spanclass="c1"># Make sure Volume is not 0</span>
<spanclass="p">(</span><spanclass="n">dataframe</span><spanclass="p">[</span><spanclass="s1">'volume'</span><spanclass="p">]</span><spanclass="o">></span><spanclass="mi">0</span><spanclass="p">)</span><spanclass="c1"># Make sure Volume is not 0</span>
<p>This dict defines the minimal Return On Investment (ROI) a trade should reach before exiting, independent from the exit signal.</p>
<p>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.</p>
<spanclass="s2">"0"</span><spanclass="p">:</span><spanclass="mf">0.05</span><spanclass="p">,</span><spanclass="c1"># 5% for the first 3 candles</span>
<spanclass="nb">str</span><spanclass="p">(</span><spanclass="n">timeframe_mins</span><spanclass="o">*</span><spanclass="mi">3</span><spanclass="p">):</span><spanclass="mf">0.02</span><spanclass="p">,</span><spanclass="c1"># 2% after 3 candles</span>
<spanclass="nb">str</span><spanclass="p">(</span><spanclass="n">timeframe_mins</span><spanclass="o">*</span><spanclass="mi">6</span><spanclass="p">):</span><spanclass="mf">0.01</span><spanclass="p">,</span><spanclass="c1"># 1% After 6 candles</span>
<spanclass="p">}</span>
</code></pre></div>
<detailsclass="info">
<summary>Orders that don't fill immediately</summary>
<p><code>minimal_roi</code> will take the <code>trade.open_date</code> as reference, which is the time the trade was initialized / the first order for this trade was placed.<br/>
This will also hold true for limit orders that don't fill immediately (usually in combination with "off-spot" prices through <code>custom_entry_price()</code>), as well as for cases where the initial order is replaced through <code>adjust_entry_price()</code>.
The time used will still be from the initial <code>trade.open_date</code> (when the initial order was first placed), not from the newly placed order date.</p>
<p>This is the set of candles the bot should download and use for the analysis.
Common values are <code>"1m"</code>, <code>"5m"</code>, <code>"15m"</code>, <code>"1h"</code>, however all values supported by your exchange should work.</p>
<p>Please note that the same entry/exit signals may work well with one timeframe, but not with the others.</p>
<p>This setting is accessible within the strategy methods as the <code>self.timeframe</code> attribute.</p>
<p>The metadata-dict (available for <code>populate_entry_trend</code>, <code>populate_exit_trend</code>, <code>populate_indicators</code>) contains additional information.
Currently this is <code>pair</code>, which can be accessed using <code>metadata['pair']</code> - and will return a pair in the format <code>XRP/BTC</code>.</p>
<p>The Metadata-dict should not be modified and does not persist information across multiple calls.
Instead, have a look at the <ahref="../strategy-advanced/#Storing-information">Storing information</a> section.</p>
<p>By default, freqtrade will attempt to load strategies from all <code>.py</code> files within <code>user_data/strategies</code>.</p>
<p>Assuming your strategy is called <code>AwesomeStrategy</code>, stored in the file <code>user_data/strategies/AwesomeStrategy.py</code>, then you can start freqtrade with <code>freqtrade trade --strategy AwesomeStrategy</code>.
Note that we're using the class-name, not the file name.</p>
<p>You can use <code>freqtrade list-strategies</code> to see a list of all strategies Freqtrade is able to load (all strategies in the correct folder).
It will also include a "status" field, highlighting potential problems.</p>
<detailsclass="hint">
<summary>Customize strategy directory</summary>
<p>You can use a different directory by using <code>--strategy-path user_data/otherPath</code>. This parameter is available to all commands that require a strategy.</p>
<h3id="get-data-for-non-tradeable-pairs">Get data for non-tradeable pairs<aclass="headerlink"href="#get-data-for-non-tradeable-pairs"title="Permanent link">¶</a></h3>
<p>Data for additional, informative pairs (reference pairs) can be beneficial for some strategies.
OHLCV data for these pairs will be downloaded as part of the regular whitelist refresh process and is available via <code>DataProvider</code> just as other pairs (see below).
These parts will <strong>not</strong> be traded unless they are also specified in the pair whitelist, or have been selected by Dynamic Whitelisting.</p>
<p>The pairs need to be specified as tuples in the format <code>("pair", "timeframe")</code>, with pair as the first and timeframe as the second argument.</p>
<spanclass="p">(</span><spanclass="s2">"ETH/USDT"</span><spanclass="p">,</span><spanclass="s2">"5m"</span><spanclass="p">,</span><spanclass="s2">""</span><spanclass="p">),</span><spanclass="c1"># Uses default candletype, depends on trading_mode (recommended)</span>
<spanclass="p">(</span><spanclass="s2">"ETH/USDT"</span><spanclass="p">,</span><spanclass="s2">"5m"</span><spanclass="p">,</span><spanclass="s2">"spot"</span><spanclass="p">),</span><spanclass="c1"># Forces usage of spot candles (only valid for bots running on spot markets).</span>
<spanclass="p">(</span><spanclass="s2">"BTC/TUSD"</span><spanclass="p">,</span><spanclass="s2">"15m"</span><spanclass="p">,</span><spanclass="s2">"futures"</span><spanclass="p">),</span><spanclass="c1"># Uses futures candles (only bots with `trading_mode=futures`)</span>
<spanclass="p">(</span><spanclass="s2">"BTC/TUSD"</span><spanclass="p">,</span><spanclass="s2">"15m"</span><spanclass="p">,</span><spanclass="s2">"mark"</span><spanclass="p">),</span><spanclass="c1"># Uses mark candles (only bots with `trading_mode=futures`)</span>
<p>In most common case it is possible to easily define informative pairs by using a decorator. All decorated <code>populate_indicators_*</code> methods run in isolation,
not having access to data from other informative pairs, in the end all informative dataframes are merged and passed to main <code>populate_indicators()</code> method.
When hyperopting, use of hyperoptable parameter <code>.value</code> attribute is not supported. Please use <code>.range</code> attribute. See <ahref="../hyperopt/#optimizing-an-indicator-parameter">optimizing an indicator parameter</a>
<spanclass="sd"> :param timeframe: Informative timeframe. Must always be equal or higher than strategy timeframe.</span>
<spanclass="sd"> :param asset: Informative asset, for example BTC, BTC/USDT, ETH/BTC. Do not specify to use</span>
<spanclass="sd"> current pair. Also supports limited pair format strings (see below)</span>
<spanclass="sd"> :param fmt: Column format (str) or column formatter (callable(name, asset, timeframe)). When not</span>
<spanclass="sd"> specified, defaults to:</span>
<spanclass="sd"> * {base}_{quote}_{column}_{timeframe} if asset is specified.</span>
<spanclass="sd"> * {column}_{timeframe} if asset is not specified.</span>
<spanclass="sd"> Pair format supports these format variables:</span>
<spanclass="sd"> * {base} - base currency in lower case, for example 'eth'.</span>
<spanclass="sd"> * {BASE} - same as {base}, except in upper case.</span>
<spanclass="sd"> * {quote} - quote currency in lower case, for example 'usdt'.</span>
<spanclass="sd"> * {QUOTE} - same as {quote}, except in upper case.</span>
<spanclass="sd"> Format string additionally supports this variables.</span>
<spanclass="sd"> * {asset} - full name of the asset, for example 'BTC/USDT'.</span>
<spanclass="sd"> * {column} - name of dataframe column.</span>
<spanclass="sd"> * {timeframe} - timeframe of informative dataframe.</span>
<spanclass="sd"> :param ffill: ffill dataframe after merging informative pair.</span>
<spanclass="sd"> :param candle_type: '', mark, index, premiumIndex, or funding_rate</span>
<spanclass="sd">"""</span>
</code></pre></div>
</details>
<detailsclass="example">
<summary>Fast and easy way to define informative pairs</summary>
<p>Most of the time we do not need power and flexibility offered by <code>merge_informative_pair()</code>, therefore we can use a decorator to quickly define informative pairs.</p>
<p>Do not use <code>@informative</code> decorator if you need to use data of one informative pair when generating another informative pair. Instead, define informative pairs
manually as described <ahref="#complete-data-provider-sample">in the DataProvider section</a>.</p>
</div>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>Use string formatting when accessing informative dataframes of other pairs. This will allow easily changing stake currency in config without having to adjust strategy code.</p>
<p>Alternatively column renaming may be used to remove stake currency from column names: <code>@informative('1h', 'BTC/{stake}', fmt='{base}_{column}_{timeframe}')</code>.</p>
<p>Methods tagged with <code>@informative()</code> decorator must always have unique names! Re-using same name (for example when copy-pasting already defined informative method)
will overwrite previously defined method and not produce any errors due to limitations of Python programming language. In such cases you will find that indicators
created in earlier-defined methods are not available in the dataframe. Carefully review method names and make sure they are unique!</p>
<p>This method helps you merge an informative pair to a regular dataframe without lookahead bias.
It's there to help you merge the dataframe in a safe and consistent way.</p>
<p>Options:</p>
<ul>
<li>Rename the columns for you to create unique columns</li>
<li>Merge the dataframe without lookahead bias</li>
<li>Forward-fill (optional)</li>
</ul>
<p>For a full sample, please refer to the <ahref="#complete-data-provider-sample">complete data provider example</a> below.</p>
<p>All columns of the informative dataframe will be available on the returning dataframe in a renamed fashion:</p>
<divclass="admonition example">
<pclass="admonition-title">Column renaming</p>
<p>Assuming <code>inf_tf = '1d'</code> the resulting columns will be:</p>
<divclass="highlight"><pre><span></span><code><spanclass="s1">'date'</span><spanclass="p">,</span><spanclass="s1">'open'</span><spanclass="p">,</span><spanclass="s1">'high'</span><spanclass="p">,</span><spanclass="s1">'low'</span><spanclass="p">,</span><spanclass="s1">'close'</span><spanclass="p">,</span><spanclass="s1">'rsi'</span><spanclass="c1"># from the original dataframe</span>
<spanclass="s1">'date_1d'</span><spanclass="p">,</span><spanclass="s1">'open_1d'</span><spanclass="p">,</span><spanclass="s1">'high_1d'</span><spanclass="p">,</span><spanclass="s1">'low_1d'</span><spanclass="p">,</span><spanclass="s1">'close_1d'</span><spanclass="p">,</span><spanclass="s1">'rsi_1d'</span><spanclass="c1"># from the informative dataframe</span>
</code></pre></div>
</div>
<detailsclass="example">
<summary>Column renaming - 1h</summary>
<p>Assuming <code>inf_tf = '1h'</code> the resulting columns will be:</p>
<divclass="highlight"><pre><span></span><code><spanclass="s1">'date'</span><spanclass="p">,</span><spanclass="s1">'open'</span><spanclass="p">,</span><spanclass="s1">'high'</span><spanclass="p">,</span><spanclass="s1">'low'</span><spanclass="p">,</span><spanclass="s1">'close'</span><spanclass="p">,</span><spanclass="s1">'rsi'</span><spanclass="c1"># from the original dataframe</span>
<spanclass="s1">'date_1h'</span><spanclass="p">,</span><spanclass="s1">'open_1h'</span><spanclass="p">,</span><spanclass="s1">'high_1h'</span><spanclass="p">,</span><spanclass="s1">'low_1h'</span><spanclass="p">,</span><spanclass="s1">'close_1h'</span><spanclass="p">,</span><spanclass="s1">'rsi_1h'</span><spanclass="c1"># from the informative dataframe</span>
</code></pre></div>
</details>
<detailsclass="example">
<summary>Custom implementation</summary>
<p>A custom implementation for this is possible, and can be done as follows:</p>
<divclass="highlight"><pre><span></span><code><spanclass="c1"># Shift date by 1 candle</span>
<spanclass="c1"># This is necessary since the data is always the "open date"</span>
<spanclass="c1"># and a 15m candle starting at 12:15 should not know the close of the 1h candle from 12:00 to 13:00</span>
<p>Using informative timeframes smaller than the dataframe timeframe is not recommended with this method, as it will not use any of the additional information this would provide.
To use the more detailed information properly, more advanced methods should be applied (which are out of scope for freqtrade documentation, as it'll depend on the respective need).</p>
</div>
<h2id="additional-data-dataprovider">Additional data (DataProvider)<aclass="headerlink"href="#additional-data-dataprovider"title="Permanent link">¶</a></h2>
<p>The strategy provides access to the <code>DataProvider</code>. This allows you to get additional data to use in your strategy.</p>
<p>All methods return <code>None</code> in case of failure (do not raise an exception).</p>
<p>Please always check the mode of operation to select the correct method to get data (samples see below).</p>
<divclass="admonition warning">
<pclass="admonition-title">Hyperopt</p>
<p>Dataprovider is available during hyperopt, however it can only be used in <code>populate_indicators()</code> within a strategy.
It is not available in <code>populate_buy()</code> and <code>populate_sell()</code> methods, nor in <code>populate_indicators()</code>, if this method located in the hyperopt file.</p>
</div>
<h3id="possible-options-for-dataprovider">Possible options for DataProvider<aclass="headerlink"href="#possible-options-for-dataprovider"title="Permanent link">¶</a></h3>
<ul>
<li><ahref="#available_pairs"><code>available_pairs</code></a> - Property with tuples listing cached pairs with their timeframe (pair, timeframe).</li>
<li><ahref="#current_whitelist"><code>current_whitelist()</code></a> - Returns a current list of whitelisted pairs. Useful for accessing dynamic whitelists (i.e. VolumePairlist)</li>
<li><ahref="#get_pair_dataframepair-timeframe"><code>get_pair_dataframe(pair, timeframe)</code></a> - This is a universal method, which returns either historical data (for backtesting) or cached live data (for the Dry-Run and Live-Run modes).</li>
<li><ahref="#get_analyzed_dataframepair-timeframe"><code>get_analyzed_dataframe(pair, timeframe)</code></a> - Returns the analyzed dataframe (after calling <code>populate_indicators()</code>, <code>populate_buy()</code>, <code>populate_sell()</code>) and the time of the latest analysis.</li>
<li><code>historic_ohlcv(pair, timeframe)</code> - Returns historical data stored on disk.</li>
<li><code>market(pair)</code> - Returns market data for the pair: fees, limits, precisions, activity flag, etc. See <ahref="https://github.com/ccxt/ccxt/wiki/Manual#markets">ccxt documentation</a> for more details on the Market data structure.</li>
<li><code>ohlcv(pair, timeframe)</code> - Currently cached candle (OHLCV) data for the pair, returns DataFrame or empty DataFrame.</li>
<li><ahref="#orderbookpair-maximum"><code>orderbook(pair, maximum)</code></a> - Returns latest orderbook data for the pair, a dict with bids/asks with a total of <code>maximum</code> entries.</li>
<li><ahref="#tickerpair"><code>ticker(pair)</code></a> - Returns current ticker data for the pair. See <ahref="https://github.com/ccxt/ccxt/wiki/Manual#price-tickers">ccxt documentation</a> for more details on the Ticker data structure.</li>
<li><code>runmode</code> - Property containing the current runmode.</li>
<p>Imagine you've developed a strategy that trades the <code>5m</code> timeframe using signals generated from a <code>1d</code> timeframe on the top 10 volume pairs by volume.</p>
<p>The strategy might look something like this:</p>
<p><em>Scan through the top 10 pairs by volume using the <code>VolumePairList</code> every 5 minutes and use a 14 day RSI to buy and sell.</em></p>
<p>Due to the limited available data, it's very difficult to resample <code>5m</code> candles into daily candles for use in a 14 day RSI. Most exchanges limit us to just 500-1000 candles which effectively gives us around 1.74 daily candles. We need 14 days at least!</p>
<p>Since we can't resample the data we will have to use an informative pair; and since the whitelist will be dynamic we don't know which pair(s) to use.</p>
<p>This is where calling <code>self.dp.current_whitelist()</code> comes in handy.</p>
<summary>Plotting with current_whitelist</summary>
<p>Current whitelist is not supported for <code>plot-dataframe</code>, as this command is usually used by providing an explicit pairlist - and would therefore make the return values of this method misleading.</p>
<pclass="admonition-title">Warning about backtesting</p>
<p>In backtesting, <code>dp.get_pair_dataframe()</code> behavior differs depending on where it's called.
Within <code>populate_*()</code> methods, <code>dp.get_pair_dataframe()</code> returns the full timerange. Please make sure to not "look into the future" to avoid surprises when running in dry/live mode.
Within <ahref="../strategy-callbacks/">callbacks</a>, you'll get the full timerange up to the current (simulated) candle.</p>
<p>This method is used by freqtrade internally to determine the last signal.
It can also be used in specific callbacks to get the signal that caused the action (see <ahref="../strategy-advanced/">Advanced Strategy Documentation</a> for more details on available callbacks).</p>
<divclass="highlight"><pre><span></span><code><spanclass="c1"># fetch current dataframe</span>
<p>The orderbook structure is aligned with the order structure from <ahref="https://github.com/ccxt/ccxt/wiki/Manual#order-book-structure">ccxt</a>, so the result will look as follows:</p>
<p>Therefore, using <code>ob['bids'][0][0]</code> as demonstrated above will result in using the best bid price. <code>ob['bids'][0][1]</code> would look at the amount at this orderbook position.</p>
<divclass="admonition warning">
<pclass="admonition-title">Warning about backtesting</p>
<p>The order book is not part of the historic data which means backtesting and hyperopt will not work correctly if this method is used, as the method will return uptodate values.</p>
<p>The dataprovider <code>.send_msg()</code> function allows you to send custom notifications from your strategy.
Identical notifications will only be sent once per candle, unless the 2<sup>nd</sup> argument (<code>always_send</code>) is set to True.</p>
<divclass="highlight"><pre><span></span><code><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">dp</span><spanclass="o">.</span><spanclass="n">send_msg</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"</span><spanclass="si">{</span><spanclass="n">metadata</span><spanclass="p">[</span><spanclass="s1">'pair'</span><spanclass="p">]</span><spanclass="si">}</span><spanclass="s2"> just got hot!"</span><spanclass="p">)</span>
<spanclass="c1"># Force send this notification, avoid caching (Please read warning below!)</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">dp</span><spanclass="o">.</span><spanclass="n">send_msg</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"</span><spanclass="si">{</span><spanclass="n">metadata</span><spanclass="p">[</span><spanclass="s1">'pair'</span><spanclass="p">]</span><spanclass="si">}</span><spanclass="s2"> just got hot!"</span><spanclass="p">,</span><spanclass="n">always_send</span><spanclass="o">=</span><spanclass="kc">True</span><spanclass="p">)</span>
</code></pre></div>
<p>Notifications will only be sent in trading modes (Live/Dry-run) - so this method can be called without conditions for backtesting.</p>
<divclass="admonition warning">
<pclass="admonition-title">Spamming</p>
<p>You can spam yourself pretty good by setting <code>always_send=True</code> in this method. Use this with great care and only in conditions you know will not happen throughout a candle to avoid a message every 5 seconds.</p>
<spanclass="p">(</span><spanclass="n">dataframe</span><spanclass="p">[</span><spanclass="s1">'rsi_1d'</span><spanclass="p">]</span><spanclass="o"><</span><spanclass="mi">30</span><spanclass="p">)</span><spanclass="o">&</span><spanclass="c1"># Ensure daily RSI is < 30</span>
<spanclass="p">(</span><spanclass="n">dataframe</span><spanclass="p">[</span><spanclass="s1">'volume'</span><spanclass="p">]</span><spanclass="o">></span><spanclass="mi">0</span><spanclass="p">)</span><spanclass="c1"># Ensure this candle had volume (important for backtesting)</span>
<p>Wallets behaves differently depending on the function it's called.
Within <code>populate_*()</code> methods, it'll return the full wallet as configured.
Within <ahref="../strategy-callbacks/">callbacks</a>, you'll get the wallet state corresponding to the actual simulated wallet at that point in the simulation process.</p>
</div>
<p>Please always check if <code>wallets</code> is available to avoid failures during backtesting.</p>
<h3id="possible-options-for-wallets">Possible options for Wallets<aclass="headerlink"href="#possible-options-for-wallets"title="Permanent link">¶</a></h3>
<ul>
<li><code>get_free(asset)</code> - currently available balance to trade</li>
<li><code>get_used(asset)</code> - currently tied up balance (open orders)</li>
<li><code>get_total(asset)</code> - total available balance - sum of the 2 above</li>
</ul>
<hr/>
<h2id="additional-data-trades">Additional data (Trades)<aclass="headerlink"href="#additional-data-trades"title="Permanent link">¶</a></h2>
<p>A history of Trades can be retrieved in the strategy by querying the database.</p>
<p>For a full list of available methods, please consult the <ahref="../trade-object/">Trade object</a> documentation.</p>
<divclass="admonition warning">
<pclass="admonition-title">Warning</p>
<p>Trade history is not available in <code>populate_*</code> methods during backtesting or hyperopt, and will result in empty results.</p>
</div>
<h2id="prevent-trades-from-happening-for-a-specific-pair">Prevent trades from happening for a specific pair<aclass="headerlink"href="#prevent-trades-from-happening-for-a-specific-pair"title="Permanent link">¶</a></h2>
<p>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.</p>
<p>Locked pairs will show the message <code>Pair <pair> is currently locked.</code>.</p>
<h3id="locking-pairs-from-within-the-strategy">Locking pairs from within the strategy<aclass="headerlink"href="#locking-pairs-from-within-the-strategy"title="Permanent link">¶</a></h3>
<p>Sometimes it may be desired to lock a pair after certain events happen (e.g. multiple losing trades in a row).</p>
<p>Freqtrade has an easy method to do this from within the strategy, by calling <code>self.lock_pair(pair, until, [reason])</code>.
<code>until</code> must be a datetime object in the future, after which trading will be re-enabled for that pair, while <code>reason</code> is an optional string detailing why the pair was locked.</p>
<p>Locks can also be lifted manually, by calling <code>self.unlock_pair(pair)</code> or <code>self.unlock_reason(<reason>)</code> - providing reason the pair was locked with.
<code>self.unlock_reason(<reason>)</code> will unlock all pairs currently locked with the provided reason.</p>
<p>To verify if a pair is currently locked, use <code>self.is_pair_locked(pair)</code>.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>Locked pairs will always be rounded up to the next candle. So assuming a <code>5m</code> timeframe, a lock with <code>until</code> set to 10:18 will lock the pair until the candle from 10:15-10:20 will be finished.</p>
</div>
<divclass="admonition warning">
<pclass="admonition-title">Warning</p>
<p>Manually locking pairs is not available during backtesting, only locks via Protections are allowed.</p>
<h2id="print-created-dataframe">Print created dataframe<aclass="headerlink"href="#print-created-dataframe"title="Permanent link">¶</a></h2>
<p>To inspect the created dataframe, you can issue a print-statement in either <code>populate_entry_trend()</code> or <code>populate_exit_trend()</code>.
You may also want to print the pair so it's clear what data is currently shown.</p>
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"result for </span><spanclass="si">{</span><spanclass="n">metadata</span><spanclass="p">[</span><spanclass="s1">'pair'</span><spanclass="p">]</span><spanclass="si">}</span><spanclass="s2">"</span><spanclass="p">)</span>
<p>Printing more than a few rows is also possible (simply use <code>print(dataframe)</code> instead of <code>print(dataframe.tail())</code>), however not recommended, as that will be very verbose (~500 lines per pair every 5 seconds).</p>
<h2id="common-mistakes-when-developing-strategies">Common mistakes when developing strategies<aclass="headerlink"href="#common-mistakes-when-developing-strategies"title="Permanent link">¶</a></h2>
<h3id="peeking-into-the-future-while-backtesting">Peeking into the future while backtesting<aclass="headerlink"href="#peeking-into-the-future-while-backtesting"title="Permanent link">¶</a></h3>
<p>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.</p>
<p>The following lists some common patterns which should be avoided to prevent frustration:</p>
<ul>
<li>don't use <code>shift(-1)</code> or other negative values. This uses data from the future in backtesting, which is not available in dry or live modes.</li>
<li>don't use <code>.iloc[-1]</code> or any other absolute position in the dataframe within <code>populate_</code> functions, as this will be different between dry-run and backtesting. Absolute <code>iloc</code> indexing is safe to use in callbacks however - see <ahref="../strategy-callbacks/">Strategy Callbacks</a>.</li>
<li>don't use <code>dataframe['volume'].mean()</code>. This uses the full DataFrame for backtesting, including data from the future. Use <code>dataframe['volume'].rolling(<window>).mean()</code> instead</li>
<li>don't use <code>.resample('1h')</code>. This uses the left border of the interval, so moves data from an hour to the start of the hour. Use <code>.resample('1h', label='right')</code> instead.</li>
<p>You may also want to check the 2 helper commands <ahref="../lookahead-analysis/">lookahead-analysis</a> and <ahref="../recursive-analysis/">recursive-analysis</a>, which can each help you figure out problems with your strategy in different ways.
Please treat them as what they are - helpers to identify most common problems. A negative result of each does not guarantee that there's none of the above errors included.</p>
<p>When conflicting signals collide (e.g. both <code>'enter_long'</code> and <code>'exit_long'</code> are 1), freqtrade will do nothing and ignore the entry signal. This will avoid trades that enter, and exit immediately. Obviously, this can potentially lead to missed entries.</p>
<p>The following rules apply, and entry signals will be ignored if more than one of the 3 signals is set:</p>
<p>To get additional Ideas for strategies, head over to the <ahref="https://github.com/freqtrade/freqtrade-strategies">strategy repository</a>. 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.</p>
<scriptid="__config"type="application/json">{"base":"..","features":["content.code.annotate","search.share","content.code.copy","navigation.top","navigation.footer"],"search":"../assets/javascripts/workers/search.b8dbb3d2.min.js","translations":{"clipboard.copied":"Copied to clipboard","clipboard.copy":"Copy to clipboard","search.result.more.one":"1 more on this page","search.result.more.other":"# more on this page","search.result.none":"No matching documents","search.result.one":"1 matching document","search.result.other":"# matching documents","search.result.placeholder":"Type to start searching","search.result.term.missing":"Missing","select.version":"Select version"},"version":{"alias":true,"provider":"mike"}}</script>