optimize: set correct typehints

This commit is contained in:
gcarq 2018-03-17 22:43:36 +01:00
parent 5532cedcdd
commit 5327533188
3 changed files with 28 additions and 21 deletions

View File

@ -2,7 +2,7 @@
import json
import os
from typing import Optional, List, Dict
from typing import Optional, List, Dict, Tuple
import gzip
from freqtrade.exchange import get_ticker_history
@ -13,8 +13,8 @@ from user_data.hyperopt_conf import hyperopt_optimize_conf
logger = Logger(name=__name__).get_logger()
def trim_tickerlist(tickerlist, timerange):
(stype, start, stop) = timerange
def trim_tickerlist(tickerlist: List[Dict], timerange: Tuple[Tuple, int, int]) -> List[Dict]:
stype, start, stop = timerange
if stype == (None, 'line'):
return tickerlist[stop:]
elif stype == ('line', None):
@ -25,7 +25,10 @@ def trim_tickerlist(tickerlist, timerange):
return tickerlist
def load_tickerdata_file(datadir, pair, ticker_interval, timerange=None):
def load_tickerdata_file(
datadir: str, pair: str,
ticker_interval: int,
timerange: Optional[Tuple[Tuple, int, int]] = None) -> Optional[List[Dict]]:
"""
Load a pair from file,
:return dict OR empty if unsuccesful
@ -55,12 +58,12 @@ def load_tickerdata_file(datadir, pair, ticker_interval, timerange=None):
return pairdata
def load_data(datadir: str, ticker_interval: int, pairs: Optional[List[str]] = None,
refresh_pairs: Optional[bool] = False, timerange=None) -> Dict[str, List]:
def load_data(datadir: str, ticker_interval: int,
pairs: Optional[List[str]] = None,
refresh_pairs: Optional[bool] = False,
timerange: Optional[Tuple[Tuple, int, int]] = None) -> Dict[str, List]:
"""
Loads ticker history data for the given parameters
:param ticker_interval: ticker interval in minutes
:param pairs: list of pairs
:return: dict
"""
result = {}

View File

@ -3,8 +3,8 @@
"""
This module contains the backtesting logic
"""
from typing import Dict, Tuple, Any
from argparse import Namespace
from typing import Dict, Tuple, Any, List, Optional
import arrow
from pandas import DataFrame, Series
from tabulate import tabulate
@ -101,7 +101,10 @@ class Backtesting(object):
])
return tabulate(tabular_data, headers=headers, floatfmt=floatfmt)
def _get_sell_trade_entry(self, pair, buy_row, partial_ticker, trade_count_lock, args):
def _get_sell_trade_entry(
self, pair: str, buy_row: DataFrame,
partial_ticker: List, trade_count_lock: Dict, args: Dict) -> Optional[Tuple]:
stake_amount = args['stake_amount']
max_open_trades = args.get('max_open_trades', 0)
trade = Trade(
@ -132,7 +135,7 @@ class Backtesting(object):
sell_row.date
return None
def backtest(self, args) -> DataFrame:
def backtest(self, args: Dict) -> DataFrame:
"""
Implements backtesting functionality
@ -273,7 +276,7 @@ class Backtesting(object):
)
def setup_configuration(args) -> Dict[str, Any]:
def setup_configuration(args: Namespace) -> Dict[str, Any]:
"""
Prepare the configuration for the backtesting
:param args: Cli args from Arguments()
@ -289,7 +292,7 @@ def setup_configuration(args) -> Dict[str, Any]:
return config
def start(args) -> None:
def start(args: Namespace) -> None:
"""
Start Backtesting script
:param args: Cli args from Arguments()

View File

@ -10,10 +10,11 @@ import os
import pickle
import signal
import sys
from argparse import Namespace
from functools import reduce
from math import exp
from operator import itemgetter
from typing import Dict, Any, Callable
from typing import Dict, Any, Callable, List
import numpy
import talib.abstract as ta
@ -240,7 +241,7 @@ class Hyperopt(Backtesting):
return trade_loss + profit_loss + duration_loss
@staticmethod
def generate_roi_table(params) -> Dict[int, float]:
def generate_roi_table(params: Dict) -> Dict[int, float]:
"""
Generate the ROI table thqt will be used by Hyperopt
"""
@ -335,7 +336,7 @@ class Hyperopt(Backtesting):
]),
}
def has_space(self, space) -> bool:
def has_space(self, space: str) -> bool:
"""
Tell if a space value is contained in the configuration
"""
@ -433,7 +434,7 @@ class Hyperopt(Backtesting):
return populate_buy_trend
def generate_optimizer(self, params) -> Dict:
def generate_optimizer(self, params: Dict) -> Dict:
if self.has_space('roi'):
self.analyze.strategy.minimal_roi = self.generate_roi_table(params)
@ -496,7 +497,7 @@ class Hyperopt(Backtesting):
results.duration.mean(),
)
def start(self):
def start(self) -> None:
timerange = Arguments.parse_timerange(self.config.get('timerange'))
data = load_data(
datadir=self.config.get('datadir'),
@ -571,7 +572,7 @@ class Hyperopt(Backtesting):
# Store trials result to file to resume next time
self.save_trials()
def signal_handler(self, sig, frame):
def signal_handler(self, sig, frame) -> None:
"""
Hyperopt SIGINT handler
"""
@ -585,7 +586,7 @@ class Hyperopt(Backtesting):
sys.exit(0)
def start(args) -> None:
def start(args: Namespace) -> None:
"""
Start Backtesting script
:param args: Cli args from Arguments()