implement trade count lock for backtesting

This commit is contained in:
gcarq 2017-11-21 22:33:34 +01:00
parent f3ba3ddd54
commit 02ca2ed585
3 changed files with 17 additions and 3 deletions

View File

@ -79,6 +79,7 @@ def populate_buy_trend(dataframe: DataFrame) -> DataFrame:
return dataframe return dataframe
def populate_sell_trend(dataframe: DataFrame) -> DataFrame: def populate_sell_trend(dataframe: DataFrame) -> DataFrame:
""" """
Based on TA indicators, populates the sell signal for the given dataframe Based on TA indicators, populates the sell signal for the given dataframe

View File

@ -54,6 +54,7 @@ def default_conf():
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def backtest_conf(): def backtest_conf():
return { return {
"max_open_trades": 3,
"stake_currency": "BTC", "stake_currency": "BTC",
"stake_amount": 0.01, "stake_amount": 0.01,
"minimal_roi": { "minimal_roi": {

View File

@ -85,22 +85,32 @@ def generate_text_table(data: Dict[str, Dict], results: DataFrame, stake_currenc
def backtest(backtest_conf, processed, mocker): def backtest(backtest_conf, processed, mocker):
trades = [] trades = []
trade_count_lock = {}
exchange._API = Bittrex({'key': '', 'secret': ''}) exchange._API = Bittrex({'key': '', 'secret': ''})
mocker.patch.dict('freqtrade.main._CONF', backtest_conf) mocker.patch.dict('freqtrade.main._CONF', backtest_conf)
for pair, pair_data in processed.items(): for pair, pair_data in processed.items():
pair_data['buy'] = 0 pair_data['buy'], pair_data['sell'] = 0, 0
pair_data['sell'] = 0
ticker = populate_sell_trend(populate_buy_trend(pair_data)) ticker = populate_sell_trend(populate_buy_trend(pair_data))
# for each buy point # for each buy point
for row in ticker[ticker.buy == 1].itertuples(index=True): for row in ticker[ticker.buy == 1].itertuples(index=True):
# Check if max_open_trades has already been reached for the given date
if not trade_count_lock.get(row.date, 0) < backtest_conf['max_open_trades']:
continue
# Increase lock
trade_count_lock[row.date] = trade_count_lock.get(row.date, 0) + 1
trade = Trade( trade = Trade(
open_rate=row.close, open_rate=row.close,
open_date=row.date, open_date=row.date,
amount=backtest_conf['stake_amount'], amount=backtest_conf['stake_amount'],
fee=exchange.get_fee() * 2 fee=exchange.get_fee() * 2
) )
# calculate win/lose forwards from buy point # calculate win/lose forwards from buy point
for row2 in ticker[row.Index:].itertuples(index=True): for row2 in ticker[row.Index + 1:].itertuples(index=True):
# Increase trade_count_lock for every iteration
trade_count_lock[row2.date] = trade_count_lock.get(row2.date, 0) + 1
if min_roi_reached(trade, row2.close, row2.date) or row2.sell == 1: if min_roi_reached(trade, row2.close, row2.date) or row2.sell == 1:
current_profit = trade.calc_profit(row2.close) current_profit = trade.calc_profit(row2.close)
@ -140,6 +150,8 @@ def test_backtest(backtest_conf, mocker):
config['stake_currency'], config['stake_amount'] config['stake_currency'], config['stake_amount']
)) ))
print('Using max_open_trades: {} ...'.format(config['max_open_trades']))
# Print timeframe # Print timeframe
min_date, max_date = get_timeframe(data) min_date, max_date = get_timeframe(data)
print('Measuring data from {} up to {} ...'.format( print('Measuring data from {} up to {} ...'.format(