Merge commit 'c5400b6c37c7de64a86c9db39a4d0fa9169b35f6' into feature/objectify

This commit is contained in:
Gerald Lonlas 2018-03-03 10:01:06 +08:00
commit bbb1a31fda
3 changed files with 20 additions and 19 deletions

View File

@ -73,7 +73,6 @@ class Backtesting(object):
:return: pretty printed table with tabulate as str :return: pretty printed table with tabulate as str
""" """
stake_currency = self.config.get('stake_currency') stake_currency = self.config.get('stake_currency')
ticker_interval = self.ticker_interval
floatfmt = ('s', 'd', '.2f', '.8f', '.1f') floatfmt = ('s', 'd', '.2f', '.8f', '.1f')
tabular_data = [] tabular_data = []
@ -86,7 +85,7 @@ class Backtesting(object):
len(result.index), len(result.index),
result.profit_percent.mean() * 100.0, result.profit_percent.mean() * 100.0,
result.profit_BTC.sum(), result.profit_BTC.sum(),
result.duration.mean() * ticker_interval, result.duration.mean(),
len(result[result.profit_BTC > 0]), len(result[result.profit_BTC > 0]),
len(result[result.profit_BTC < 0]) len(result[result.profit_BTC < 0])
]) ])
@ -97,7 +96,7 @@ class Backtesting(object):
len(results.index), len(results.index),
results.profit_percent.mean() * 100.0, results.profit_percent.mean() * 100.0,
results.profit_BTC.sum(), results.profit_BTC.sum(),
results.duration.mean() * ticker_interval, result.duration.mean(),
len(results[results.profit_BTC > 0]), len(results[results.profit_BTC > 0]),
len(results[results.profit_BTC < 0]) len(results[results.profit_BTC < 0])
]) ])
@ -108,27 +107,27 @@ class Backtesting(object):
max_open_trades = args.get('max_open_trades', 0) max_open_trades = args.get('max_open_trades', 0)
trade = Trade( trade = Trade(
open_rate=row.close, open_rate=row.close,
open_date=row.date, open_date=row.Index,
stake_amount=stake_amount, stake_amount=stake_amount,
amount=stake_amount / row.open, amount=stake_amount / row.open,
fee=exchange.get_fee() fee=exchange.get_fee()
) )
# calculate win/lose forwards from buy point # calculate win/lose forwards from buy point
sell_subset = ticker[ticker.date > row.date][['close', 'date', 'sell']] sell_subset = ticker[ticker.index > row.Index][['close', 'sell']]
for row2 in sell_subset.itertuples(index=True): for row2 in sell_subset.itertuples(index=True):
if max_open_trades > 0: if max_open_trades > 0:
# Increase trade_count_lock for every iteration # Increase trade_count_lock for every iteration
trade_count_lock[row2.date] = trade_count_lock.get(row2.date, 0) + 1 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 # Buy is on is in the buy_subset there is a row that matches the date
# of the sell event # of the sell event
buy_signal = not buy_subset[buy_subset.date == row2.date].empty buy_signal = (buy_subset.index == row2.Index).any()
if( if(
self.analyze.should_sell( self.analyze.should_sell(
trade=trade, trade=trade,
rate=row2.close, rate=row2.close,
date=row2.date, date=row2.Index,
buy=buy_signal, buy=buy_signal,
sell=row2.sell sell=row2.sell
) )
@ -139,9 +138,9 @@ class Backtesting(object):
pair, pair,
trade.calc_profit_percent(rate=row2.close), trade.calc_profit_percent(rate=row2.close),
trade.calc_profit(rate=row2.close), trade.calc_profit(rate=row2.close),
row2.Index - row.Index (row2.Index - row.Index).seconds // 60
),\ ),\
row2.date row2.Index
return None return None
def backtest(self, args) -> DataFrame: def backtest(self, args) -> DataFrame:
@ -174,22 +173,24 @@ class Backtesting(object):
ticker = self.populate_sell_trend( ticker = self.populate_sell_trend(
self.populate_buy_trend(pair_data) self.populate_buy_trend(pair_data)
) )
if 'date' in ticker:
ticker.set_index('date', inplace=True)
# for each buy point # for each buy point
lock_pair_until = None lock_pair_until = None
headers = ['buy', 'open', 'close', 'date', 'sell'] headers = ['buy', 'open', 'close', 'sell']
buy_subset = ticker[(ticker.buy == 1) & (ticker.sell == 0)][headers] buy_subset = ticker[(ticker.buy == 1) & (ticker.sell == 0)][headers]
for row in buy_subset.itertuples(index=True): for row in buy_subset.itertuples(index=True):
if realistic: if realistic:
if lock_pair_until is not None and row.date <= lock_pair_until: if lock_pair_until is not None and row.Index <= lock_pair_until:
continue continue
if max_open_trades > 0: if max_open_trades > 0:
# Check if max_open_trades has already been reached for the given date # Check if max_open_trades has already been reached for the given date
if not trade_count_lock.get(row.date, 0) < max_open_trades: if not trade_count_lock.get(row.Index, 0) < max_open_trades:
continue continue
if max_open_trades > 0: if max_open_trades > 0:
# Increase lock # Increase lock
trade_count_lock[row.date] = trade_count_lock.get(row.date, 0) + 1 trade_count_lock[row.Index] = trade_count_lock.get(row.Index, 0) + 1
ret = self._get_sell_trade_entry( ret = self._get_sell_trade_entry(
pair=pair, pair=pair,
@ -209,8 +210,8 @@ class Backtesting(object):
# record a tuple of pair, current_profit_percent, # record a tuple of pair, current_profit_percent,
# entry-date, duration # entry-date, duration
records.append((pair, trade_entry[1], records.append((pair, trade_entry[1],
row.date.strftime('%s'), row.Index.strftime('%s'),
row2.date.strftime('%s'), row2.Index.strftime('%s'),
row.Index, trade_entry[3])) row.Index, trade_entry[3]))
# For now export inside backtest(), maybe change so that backtest() # For now export inside backtest(), maybe change so that backtest()
# returns a tuple like: (dataframe, records, logs, etc) # returns a tuple like: (dataframe, records, logs, etc)

View File

@ -440,7 +440,7 @@ class Hyperopt(Backtesting):
total_profit = results.profit_percent.sum() total_profit = results.profit_percent.sum()
trade_count = len(results.index) trade_count = len(results.index)
trade_duration = results.duration.mean() * 5 trade_duration = results.duration.mean()
if trade_count == 0 or trade_duration > self.max_accepted_trade_duration: if trade_count == 0 or trade_duration > self.max_accepted_trade_duration:
print('.', end='') print('.', end='')

View File

@ -312,9 +312,9 @@ def test_generate_text_table():
'------- ----------- -------------- ' '------- ----------- -------------- '
'------------------ -------------- -------- ------\n' '------------------ -------------- -------- ------\n'
'BTC_ETH 2 15.00 ' 'BTC_ETH 2 15.00 '
'0.60000000 100.0 2 0\n' '0.60000000 20.0 2 0\n'
'TOTAL 2 15.00 ' 'TOTAL 2 15.00 '
'0.60000000 100.0 2 0' '0.60000000 20.0 2 0'
) )
assert backtesting._generate_text_table(data={'BTC_ETH': {}}, results=results) == result_str assert backtesting._generate_text_table(data={'BTC_ETH': {}}, results=results) == result_str