Skip to content

Commit

Permalink
Introduce strategy_version variable
Browse files Browse the repository at this point in the history
  • Loading branch information
xmatthias committed Aug 26, 2019
1 parent 4fcfb1e commit 92011f8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions freqtrade/resolvers/strategy_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ def _load_strategy(
strategy._populate_fun_len = len(getfullargspec(strategy.populate_indicators).args)
strategy._buy_fun_len = len(getfullargspec(strategy.populate_buy_trend).args)
strategy._sell_fun_len = len(getfullargspec(strategy.populate_sell_trend).args)
if any([x == 2 for x in [strategy._populate_fun_len,
strategy._buy_fun_len,
strategy._sell_fun_len]]):
strategy.strategy_version = 1

try:
return import_strategy(strategy, config=config)
Expand Down
1 change: 1 addition & 0 deletions freqtrade/strategy/default_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class DefaultStrategy(IStrategy):
Default Strategy provided by freqtrade bot.
You can override it with your own strategy
"""
strategy_version: int = 2

# Minimal ROI designed for the strategy
minimal_roi = {
Expand Down
5 changes: 5 additions & 0 deletions freqtrade/strategy/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class IStrategy(ABC):
stoploss -> float: optimal stoploss designed for the strategy
ticker_interval -> str: value of the ticker interval to use for the strategy
"""
# Strategy interface version
# Default to version 2
# version 1 is the initial interface without metadata dict
# Version 2 populate_* include metadata dict
strategy_version: int = 2

_populate_fun_len: int = 0
_buy_fun_len: int = 0
Expand Down
25 changes: 25 additions & 0 deletions freqtrade/tests/strategy/test_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,31 @@ def test_call_deprecated_function(result, monkeypatch, default_conf):
assert resolver.strategy._populate_fun_len == 2
assert resolver.strategy._buy_fun_len == 2
assert resolver.strategy._sell_fun_len == 2
assert resolver.strategy.strategy_version == 1

indicator_df = resolver.strategy.advise_indicators(result, metadata=metadata)
assert isinstance(indicator_df, DataFrame)
assert 'adx' in indicator_df.columns

buydf = resolver.strategy.advise_buy(result, metadata=metadata)
assert isinstance(buydf, DataFrame)
assert 'buy' in buydf.columns

selldf = resolver.strategy.advise_sell(result, metadata=metadata)
assert isinstance(selldf, DataFrame)
assert 'sell' in selldf


def test_strategy_versioning(result, monkeypatch, default_conf):
default_conf.update({'strategy': 'DefaultStrategy'})
resolver = StrategyResolver(default_conf)
metadata = {'pair': 'ETH/BTC'}

# Make sure we are using a legacy function
assert resolver.strategy._populate_fun_len == 3
assert resolver.strategy._buy_fun_len == 3
assert resolver.strategy._sell_fun_len == 3
assert resolver.strategy.strategy_version == 2

indicator_df = resolver.strategy.advise_indicators(result, metadata=metadata)
assert isinstance(indicator_df, DataFrame)
Expand Down
7 changes: 5 additions & 2 deletions user_data/strategies/test_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class TestStrategy(IStrategy):
- the prototype for the methods: minimal_roi, stoploss, populate_indicators, populate_buy_trend,
populate_sell_trend, hyperopt_space, buy_strategy_generator
"""
# Strategy intervace version - allow new iterations of the strategy interface.
# Check the documentation or the Sample strategy to get the latest version.
strategy_version: int = 2

# Minimal ROI designed for the strategy.
# This attribute will be overridden if the config file contains "minimal_roi"
Expand Down Expand Up @@ -256,14 +259,14 @@ def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame
# Retrieve best bid and best ask
# ------------------------------------
"""
# first check if dataprovider is available
# first check if dataprovider is available
if self.dp:
if self.dp.runmode in ('live', 'dry_run'):
ob = self.dp.orderbook(metadata['pair'], 1)
dataframe['best_bid'] = ob['bids'][0][0]
dataframe['best_ask'] = ob['asks'][0][0]
"""

return dataframe

def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
Expand Down

0 comments on commit 92011f8

Please sign in to comment.