optimize: set correct typehints
This commit is contained in:
parent
5532cedcdd
commit
5327533188
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from typing import Optional, List, Dict
|
from typing import Optional, List, Dict, Tuple
|
||||||
import gzip
|
import gzip
|
||||||
from freqtrade.exchange import get_ticker_history
|
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()
|
logger = Logger(name=__name__).get_logger()
|
||||||
|
|
||||||
|
|
||||||
def trim_tickerlist(tickerlist, timerange):
|
def trim_tickerlist(tickerlist: List[Dict], timerange: Tuple[Tuple, int, int]) -> List[Dict]:
|
||||||
(stype, start, stop) = timerange
|
stype, start, stop = timerange
|
||||||
if stype == (None, 'line'):
|
if stype == (None, 'line'):
|
||||||
return tickerlist[stop:]
|
return tickerlist[stop:]
|
||||||
elif stype == ('line', None):
|
elif stype == ('line', None):
|
||||||
@ -25,7 +25,10 @@ def trim_tickerlist(tickerlist, timerange):
|
|||||||
return tickerlist
|
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,
|
Load a pair from file,
|
||||||
:return dict OR empty if unsuccesful
|
:return dict OR empty if unsuccesful
|
||||||
@ -55,12 +58,12 @@ def load_tickerdata_file(datadir, pair, ticker_interval, timerange=None):
|
|||||||
return pairdata
|
return pairdata
|
||||||
|
|
||||||
|
|
||||||
def load_data(datadir: str, ticker_interval: int, pairs: Optional[List[str]] = None,
|
def load_data(datadir: str, ticker_interval: int,
|
||||||
refresh_pairs: Optional[bool] = False, timerange=None) -> Dict[str, List]:
|
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
|
Loads ticker history data for the given parameters
|
||||||
:param ticker_interval: ticker interval in minutes
|
|
||||||
:param pairs: list of pairs
|
|
||||||
:return: dict
|
:return: dict
|
||||||
"""
|
"""
|
||||||
result = {}
|
result = {}
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
"""
|
"""
|
||||||
This module contains the backtesting logic
|
This module contains the backtesting logic
|
||||||
"""
|
"""
|
||||||
|
from argparse import Namespace
|
||||||
from typing import Dict, Tuple, Any
|
from typing import Dict, Tuple, Any, List, Optional
|
||||||
import arrow
|
import arrow
|
||||||
from pandas import DataFrame, Series
|
from pandas import DataFrame, Series
|
||||||
from tabulate import tabulate
|
from tabulate import tabulate
|
||||||
@ -101,7 +101,10 @@ class Backtesting(object):
|
|||||||
])
|
])
|
||||||
return tabulate(tabular_data, headers=headers, floatfmt=floatfmt)
|
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']
|
stake_amount = args['stake_amount']
|
||||||
max_open_trades = args.get('max_open_trades', 0)
|
max_open_trades = args.get('max_open_trades', 0)
|
||||||
trade = Trade(
|
trade = Trade(
|
||||||
@ -132,7 +135,7 @@ class Backtesting(object):
|
|||||||
sell_row.date
|
sell_row.date
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def backtest(self, args) -> DataFrame:
|
def backtest(self, args: Dict) -> DataFrame:
|
||||||
"""
|
"""
|
||||||
Implements backtesting functionality
|
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
|
Prepare the configuration for the backtesting
|
||||||
:param args: Cli args from Arguments()
|
:param args: Cli args from Arguments()
|
||||||
@ -289,7 +292,7 @@ def setup_configuration(args) -> Dict[str, Any]:
|
|||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
def start(args) -> None:
|
def start(args: Namespace) -> None:
|
||||||
"""
|
"""
|
||||||
Start Backtesting script
|
Start Backtesting script
|
||||||
:param args: Cli args from Arguments()
|
:param args: Cli args from Arguments()
|
||||||
|
@ -10,10 +10,11 @@ import os
|
|||||||
import pickle
|
import pickle
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
from argparse import Namespace
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
from math import exp
|
from math import exp
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
from typing import Dict, Any, Callable
|
from typing import Dict, Any, Callable, List
|
||||||
|
|
||||||
import numpy
|
import numpy
|
||||||
import talib.abstract as ta
|
import talib.abstract as ta
|
||||||
@ -240,7 +241,7 @@ class Hyperopt(Backtesting):
|
|||||||
return trade_loss + profit_loss + duration_loss
|
return trade_loss + profit_loss + duration_loss
|
||||||
|
|
||||||
@staticmethod
|
@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
|
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
|
Tell if a space value is contained in the configuration
|
||||||
"""
|
"""
|
||||||
@ -433,7 +434,7 @@ class Hyperopt(Backtesting):
|
|||||||
|
|
||||||
return populate_buy_trend
|
return populate_buy_trend
|
||||||
|
|
||||||
def generate_optimizer(self, params) -> Dict:
|
def generate_optimizer(self, params: Dict) -> Dict:
|
||||||
if self.has_space('roi'):
|
if self.has_space('roi'):
|
||||||
self.analyze.strategy.minimal_roi = self.generate_roi_table(params)
|
self.analyze.strategy.minimal_roi = self.generate_roi_table(params)
|
||||||
|
|
||||||
@ -496,7 +497,7 @@ class Hyperopt(Backtesting):
|
|||||||
results.duration.mean(),
|
results.duration.mean(),
|
||||||
)
|
)
|
||||||
|
|
||||||
def start(self):
|
def start(self) -> None:
|
||||||
timerange = Arguments.parse_timerange(self.config.get('timerange'))
|
timerange = Arguments.parse_timerange(self.config.get('timerange'))
|
||||||
data = load_data(
|
data = load_data(
|
||||||
datadir=self.config.get('datadir'),
|
datadir=self.config.get('datadir'),
|
||||||
@ -571,7 +572,7 @@ class Hyperopt(Backtesting):
|
|||||||
# Store trials result to file to resume next time
|
# Store trials result to file to resume next time
|
||||||
self.save_trials()
|
self.save_trials()
|
||||||
|
|
||||||
def signal_handler(self, sig, frame):
|
def signal_handler(self, sig, frame) -> None:
|
||||||
"""
|
"""
|
||||||
Hyperopt SIGINT handler
|
Hyperopt SIGINT handler
|
||||||
"""
|
"""
|
||||||
@ -585,7 +586,7 @@ class Hyperopt(Backtesting):
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
def start(args) -> None:
|
def start(args: Namespace) -> None:
|
||||||
"""
|
"""
|
||||||
Start Backtesting script
|
Start Backtesting script
|
||||||
:param args: Cli args from Arguments()
|
:param args: Cli args from Arguments()
|
||||||
|
Loading…
Reference in New Issue
Block a user