switch to properly using dates as indexes, makes date based searching and slicing a lot faster

This commit is contained in:
Janne Sinivirta 2018-02-05 19:22:49 +02:00
parent 8c7b29734e
commit cf7c6d2e9c

View File

@ -71,28 +71,28 @@ def get_sell_trade_entry(pair, row, buy_subset, ticker, trade_count_lock, args):
stake_amount = args['stake_amount']
max_open_trades = args.get('max_open_trades', 0)
trade = Trade(open_rate=row.close,
open_date=row.date,
open_date=row.Index,
stake_amount=stake_amount,
amount=stake_amount / row.open,
fee=exchange.get_fee()
)
# 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):
if max_open_trades > 0:
# 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
# of the sell event
buy_signal = not buy_subset[buy_subset.date == row2.date].empty
if(should_sell(trade, row2.close, row2.date, buy_signal, row2.sell)):
buy_signal = not buy_subset[buy_subset.index == row2.Index].empty
if(should_sell(trade, row2.close, row2.Index, buy_signal, row2.sell)):
return row2, (pair,
trade.calc_profit_percent(rate=row2.close),
trade.calc_profit(rate=row2.close),
(row2.date - row.date).seconds // 60
), row2.date
(row2.Index - row.Index).seconds // 60
), row2.Index
return None
@ -120,22 +120,23 @@ def backtest(args) -> DataFrame:
for pair, pair_data in processed.items():
pair_data['buy'], pair_data['sell'] = 0, 0
ticker = populate_sell_trend(populate_buy_trend(pair_data))
ticker.set_index('date', inplace=True)
# for each buy point
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]
for row in buy_subset.itertuples(index=True):
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
if max_open_trades > 0:
# 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
if max_open_trades > 0:
# 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 = get_sell_trade_entry(pair, row, buy_subset, ticker,
trade_count_lock, args)
@ -148,8 +149,8 @@ def backtest(args) -> DataFrame:
# record a tuple of pair, current_profit_percent,
# entry-date, duration
records.append((pair, trade_entry[1],
row.date.strftime('%s'),
row2.date.strftime('%s'),
row.Index.strftime('%s'),
row2.Index.strftime('%s'),
row.Index, trade_entry[3]))
# For now export inside backtest(), maybe change so that backtest()
# returns a tuple like: (dataframe, records, logs, etc)