From eb53281434df691ef954dcb8b964ad7c4eeba74e Mon Sep 17 00:00:00 2001 From: misagh Date: Thu, 22 Nov 2018 00:04:20 +0100 Subject: [PATCH] python beginner problem resolved --- freqtrade/edge/__init__.py | 39 ++++++++++++----------- freqtrade/tests/edge/test_edge.py | 14 ++++---- freqtrade/tests/optimize/test_edge_cli.py | 13 ++------ 3 files changed, 29 insertions(+), 37 deletions(-) diff --git a/freqtrade/edge/__init__.py b/freqtrade/edge/__init__.py index dedaa19a3..009b80664 100644 --- a/freqtrade/edge/__init__.py +++ b/freqtrade/edge/__init__.py @@ -1,8 +1,7 @@ # pragma pylint: disable=W0603 """ Edge positioning package """ import logging -from typing import Any, Dict -from collections import namedtuple +from typing import Any, Dict, NamedTuple import arrow import numpy as np @@ -18,6 +17,16 @@ from freqtrade.strategy.interface import SellType logger = logging.getLogger(__name__) +class PairInfo(NamedTuple): + stoploss: float + winrate: float + risk_reward_ratio: float + required_risk_reward: float + expectancy: float + nb_trades: int + avg_trade_duration: float + + class Edge(): """ Calculates Win Rate, Risk Reward Ratio, Expectancy @@ -30,13 +39,6 @@ class Edge(): config: Dict = {} _cached_pairs: Dict[str, Any] = {} # Keeps a list of pairs - # pair info data type - _pair_info = namedtuple( - 'pair_info', - ['stoploss', 'winrate', 'risk_reward_ratio', 'required_risk_reward', 'expectancy', - 'nb_trades', 'avg_trade_duration'] - ) - def __init__(self, config: Dict[str, Any], exchange, strategy) -> None: self.config = config @@ -294,16 +296,15 @@ class Edge(): final = {} for x in df.itertuples(): - info = { - 'stoploss': x.stoploss, - 'winrate': x.winrate, - 'risk_reward_ratio': x.risk_reward_ratio, - 'required_risk_reward': x.required_risk_reward, - 'expectancy': x.expectancy, - 'nb_trades': x.nb_trades, - 'avg_trade_duration': x.avg_trade_duration - } - final[x.pair] = self._pair_info(**info) + final[x.pair] = PairInfo( + x.stoploss, + x.winrate, + x.risk_reward_ratio, + x.required_risk_reward, + x.expectancy, + x.nb_trades, + x.avg_trade_duration + ) # Returning a list of pairs in order of "expectancy" return final diff --git a/freqtrade/tests/edge/test_edge.py b/freqtrade/tests/edge/test_edge.py index 14c9114c3..fac055c17 100644 --- a/freqtrade/tests/edge/test_edge.py +++ b/freqtrade/tests/edge/test_edge.py @@ -4,7 +4,7 @@ import pytest import logging from freqtrade.tests.conftest import get_patched_freqtradebot -from freqtrade.edge import Edge +from freqtrade.edge import Edge, PairInfo from pandas import DataFrame, to_datetime from freqtrade.strategy.interface import SellType from freqtrade.tests.optimize import (BTrade, BTContainer, _build_backtest_dataframe, @@ -128,9 +128,9 @@ def test_adjust(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, 10, 60), - 'C/D': Edge._pair_info(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60), - 'N/O': Edge._pair_info(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60) + 'E/F': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60), + 'C/D': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60), + 'N/O': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60) } )) @@ -143,9 +143,9 @@ def test_stoploss(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, 10, 60), - 'C/D': Edge._pair_info(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60), - 'N/O': Edge._pair_info(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60) + 'E/F': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60), + 'C/D': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60), + 'N/O': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60) } )) diff --git a/freqtrade/tests/optimize/test_edge_cli.py b/freqtrade/tests/optimize/test_edge_cli.py index f8db3dec4..0d0f64e0c 100644 --- a/freqtrade/tests/optimize/test_edge_cli.py +++ b/freqtrade/tests/optimize/test_edge_cli.py @@ -4,7 +4,7 @@ from unittest.mock import MagicMock import json from typing import List -from freqtrade.edge import Edge +from freqtrade.edge import PairInfo from freqtrade.arguments import Arguments from freqtrade.optimize.edge_cli import (EdgeCli, setup_configuration, start) from freqtrade.tests.conftest import log_has, patch_exchange @@ -123,17 +123,8 @@ def test_generate_edge_table(edge_conf, mocker): edge_cli = EdgeCli(edge_conf) results = {} - info = { - 'stoploss': -0.01, - 'winrate': 0.60, - 'risk_reward_ratio': 2, - 'required_risk_reward': 1, - 'expectancy': 3, - 'nb_trades': 10, - 'avg_trade_duration': 60 - } + results['ETH/BTC'] = PairInfo(-0.01, 0.60, 2, 1, 3, 10, 60) - results['ETH/BTC'] = Edge._pair_info(**info) assert edge_cli._generate_edge_table(results).count(':|') == 7 assert edge_cli._generate_edge_table(results).count('| ETH/BTC |') == 1 assert edge_cli._generate_edge_table(results).count(