assigning strategy to edge from FreqtradeBot

This commit is contained in:
misagh 2018-11-08 00:22:46 +01:00
parent 866da8aaa1
commit e5c6499706
3 changed files with 32 additions and 30 deletions

View File

@ -2,6 +2,7 @@
""" Edge positioning package """
import logging
from typing import Any, Dict
from collections import namedtuple
import arrow
import numpy as np
@ -12,8 +13,7 @@ import freqtrade.optimize as optimize
from freqtrade.arguments import Arguments
from freqtrade.arguments import TimeRange
from freqtrade.strategy.interface import SellType
from freqtrade.strategy.resolver import IStrategy, StrategyResolver
from collections import namedtuple
logger = logging.getLogger(__name__)
@ -35,11 +35,11 @@ class Edge():
'pair_info',
['stoploss', 'winrate', 'risk_reward_ratio', 'required_risk_reward', 'expectancy'])
def __init__(self, config: Dict[str, Any], exchange=None) -> None:
def __init__(self, config: Dict[str, Any], exchange, strategy) -> None:
self.config = config
self.exchange = exchange
self.strategy: IStrategy = StrategyResolver(self.config).strategy
self.strategy = strategy
self.ticker_interval = self.strategy.ticker_interval
self.tickerdata_to_dataframe = self.strategy.tickerdata_to_dataframe
self.get_timeframe = optimize.get_timeframe

View File

@ -58,7 +58,7 @@ class FreqtradeBot(object):
self.exchange = Exchange(self.config)
# Initializing Edge only if enabled
self.edge = Edge(self.config, self.exchange) if \
self.edge = Edge(self.config, self.exchange, self.strategy) if \
self.config.get('edge', {}).get('enabled', False) else None
self.active_pair_whitelist: List[str] = self.config['exchange']['pair_whitelist']

View File

@ -1,4 +1,7 @@
from freqtrade.tests.conftest import get_patched_exchange
# pragma pylint: disable=missing-docstring, C0103
# pragma pylint: disable=protected-access, too-many-lines, invalid-name, too-many-arguments
from freqtrade.tests.conftest import get_patched_freqtradebot
from freqtrade.edge import Edge
from pandas import DataFrame, to_datetime
from freqtrade.strategy.interface import SellType
@ -23,8 +26,8 @@ _ohlc = {'date': 0, 'buy': 1, 'open': 2, 'high': 3, 'low': 4, 'close': 5, 'sell'
def test_adjust(mocker, default_conf):
exchange = get_patched_exchange(mocker, default_conf)
edge = Edge(default_conf, exchange)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
edge = Edge(default_conf, freqtrade.exchange, freqtrade.strategy)
mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock(
return_value={
'E/F': Edge._pair_info(-0.01, 0.66, 3.71, 0.50, 1.71),
@ -38,8 +41,8 @@ def test_adjust(mocker, default_conf):
def test_stoploss(mocker, default_conf):
exchange = get_patched_exchange(mocker, default_conf)
edge = Edge(default_conf, exchange)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
edge = Edge(default_conf, freqtrade.exchange, freqtrade.strategy)
mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock(
return_value={
'E/F': Edge._pair_info(-0.01, 0.66, 3.71, 0.50, 1.71),
@ -92,8 +95,8 @@ def _time_on_candle(number):
def test_edge_heartbeat_calculate(mocker, default_conf):
exchange = get_patched_exchange(mocker, default_conf)
edge = Edge(default_conf, exchange)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
edge = Edge(default_conf, freqtrade.exchange, freqtrade.strategy)
heartbeat = default_conf['edge']['process_throttle_secs']
# should not recalculate if heartbeat not reached
@ -135,11 +138,10 @@ def mocked_load_data(datadir, pairs=[], ticker_interval='0m', refresh_pairs=Fals
def test_edge_process_downloaded_data(mocker, default_conf):
default_conf['datadir'] = None
exchange = get_patched_exchange(mocker, default_conf)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.001))
mocker.patch('freqtrade.optimize.load_data', mocked_load_data)
mocker.patch('freqtrade.exchange.Exchange.refresh_tickers', MagicMock())
edge = Edge(default_conf, exchange)
edge = Edge(default_conf, freqtrade.exchange, freqtrade.strategy)
assert edge.calculate()
assert len(edge._cached_pairs) == 2
@ -148,13 +150,13 @@ def test_edge_process_downloaded_data(mocker, default_conf):
def test_process_expectancy(mocker, default_conf):
default_conf['edge']['min_trade_number'] = 2
exchange = get_patched_exchange(mocker, default_conf)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
def get_fee():
return 0.001
exchange.get_fee = get_fee
edge = Edge(default_conf, exchange)
freqtrade.exchange.get_fee = get_fee
edge = Edge(default_conf, freqtrade.exchange, freqtrade.strategy)
trades = [
{'pair': 'TEST/BTC',
@ -213,8 +215,8 @@ def test_process_expectancy(mocker, default_conf):
def test_case_1(mocker, default_conf):
exchange = get_patched_exchange(mocker, default_conf)
edge = Edge(default_conf, exchange)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
edge = Edge(default_conf, freqtrade.exchange, freqtrade.strategy)
stoploss = -0.99 # we don't want stoploss to be hit in this test
ticker = [
@ -233,8 +235,8 @@ def test_case_1(mocker, default_conf):
# 2) Two complete trades within dataframe (with sell hit for all)
def test_case_2(mocker, default_conf):
exchange = get_patched_exchange(mocker, default_conf)
edge = Edge(default_conf, exchange)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
edge = Edge(default_conf, freqtrade.exchange, freqtrade.strategy)
stoploss = -0.99 # we don't want stoploss to be hit in this test
ticker = [
@ -274,8 +276,8 @@ def test_case_2(mocker, default_conf):
# 3) Entered, sl 1%, candle drops 8% => Trade closed, 1% loss
def test_case_3(mocker, default_conf):
exchange = get_patched_exchange(mocker, default_conf)
edge = Edge(default_conf, exchange)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
edge = Edge(default_conf, freqtrade.exchange, freqtrade.strategy)
stoploss = -0.01 # we don't want stoploss to be hit in this test
ticker = [
@ -303,8 +305,8 @@ def test_case_3(mocker, default_conf):
# 4) Entered, sl 3 %, candle drops 4%, recovers to 1 % = > Trade closed, 3 % loss
def test_case_4(mocker, default_conf):
exchange = get_patched_exchange(mocker, default_conf)
edge = Edge(default_conf, exchange)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
edge = Edge(default_conf, freqtrade.exchange, freqtrade.strategy)
stoploss = -0.03 # we don't want stoploss to be hit in this test
ticker = [
@ -333,11 +335,11 @@ def test_case_4(mocker, default_conf):
# 5) Stoploss and sell are hit. should sell on stoploss
def test_case_5(mocker, default_conf):
exchange = get_patched_exchange(mocker, default_conf)
edge = Edge(default_conf, exchange)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
edge = Edge(default_conf, freqtrade.exchange, freqtrade.strategy)
exchange = get_patched_exchange(mocker, default_conf)
edge = Edge(default_conf, exchange)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
edge = Edge(default_conf, freqtrade.exchange, freqtrade.strategy)
stoploss = -0.03 # we don't want stoploss to be hit in this test
ticker = [