Merge pull request #508 from gcarq/faster_backtesting

Faster backtesting
This commit is contained in:
Gérald LONLAS 2018-02-06 22:59:45 -08:00 committed by GitHub
commit aa7aeb046e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 22 deletions

View File

@ -301,12 +301,13 @@ def min_roi_reached(trade: Trade, current_rate: float, current_time: datetime) -
return True
# Check if time matches and current rate is above threshold
time_diff = (current_time - trade.open_date).total_seconds() / 60
for duration, threshold in sorted(strategy.minimal_roi.items()):
if time_diff > float(duration) and current_profit > threshold:
time_diff = (current_time.timestamp() - trade.open_date.timestamp()) / 60
for duration_string, threshold in strategy.minimal_roi.items():
duration = float(duration_string)
if time_diff > duration and current_profit > threshold:
return True
logger.debug('Threshold not reached. (cur_profit: %1.2f%%)', float(current_profit) * 100.0)
if time_diff < duration:
return False
return False

View File

@ -78,15 +78,13 @@ def get_sell_trade_entry(pair, row, buy_subset, ticker, trade_count_lock, args):
)
# calculate win/lose forwards from buy point
sell_subset = ticker[ticker.index > row.Index][['close', 'sell']]
sell_subset = ticker[ticker.index > row.Index][['close', 'sell', 'buy']]
for row2 in sell_subset.itertuples(index=True):
if max_open_trades > 0:
# Increase trade_count_lock for every iteration
trade_count_lock[row2.Index] = trade_count_lock.get(row2.Index, 0) + 1
# Buy is on is in the buy_subset there is a row that matches the date
# of the sell event
buy_signal = (buy_subset.index == row2.Index).any()
buy_signal = row2.buy
if(should_sell(trade, row2.close, row2.Index, buy_signal, row2.sell)):
return row2, (pair,
trade.calc_profit_percent(rate=row2.close),

View File

@ -187,8 +187,8 @@ class Trade(_DECL_BASE):
"""
open_trade_price = self.calc_open_trade_price()
close_trade_price = self.calc_close_trade_price(
rate=Decimal(rate or self.close_rate),
fee=Decimal(fee or self.fee)
rate=(rate or self.close_rate),
fee=(fee or self.fee)
)
return float("{0:.8f}".format(close_trade_price - open_trade_price))
@ -206,8 +206,8 @@ class Trade(_DECL_BASE):
open_trade_price = self.calc_open_trade_price()
close_trade_price = self.calc_close_trade_price(
rate=Decimal(rate or self.close_rate),
fee=Decimal(fee or self.fee)
rate=(rate or self.close_rate),
fee=(fee or self.fee)
)
return float("{0:.8f}".format((close_trade_price / open_trade_price) - 1))

View File

@ -7,6 +7,7 @@ import os
import sys
import logging
import importlib
from collections import OrderedDict
from pandas import DataFrame
from freqtrade.strategy.interface import IStrategy
@ -69,7 +70,9 @@ class Strategy(object):
)
# Minimal ROI designed for the strategy
self.minimal_roi = self.custom_strategy.minimal_roi
self.minimal_roi = OrderedDict(sorted(
self.custom_strategy.minimal_roi.items(),
key=lambda tuple: float(tuple[0]))) # sort after converting to number
# Optimal stoploss designed for the strategy
self.stoploss = self.custom_strategy.stoploss

View File

@ -271,10 +271,6 @@ def test_calc_profit(limit_buy_order, limit_sell_order):
# Lower than open rate
assert trade.calc_profit(rate=0.00000123, fee=0.003) == -0.00089092
# Only custom fee without sell order applied
with pytest.raises(TypeError):
trade.calc_profit(fee=0.003)
# Test when we apply a Sell order. Sell higher than open rate @ 0.00001173
trade.update(limit_sell_order)
assert trade.calc_profit() == 0.00006217
@ -299,10 +295,6 @@ def test_calc_profit_percent(limit_buy_order, limit_sell_order):
# Get percent of profit with a custom rate (Lower than open rate)
assert trade.calc_profit_percent(rate=0.00000123) == -0.88863827
# Only custom fee without sell order applied
with pytest.raises(TypeError):
trade.calc_profit_percent(fee=0.003)
# Test when we apply a Sell order. Sell higher than open rate @ 0.00001173
trade.update(limit_sell_order)
assert trade.calc_profit_percent() == 0.06201057