Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a column for outstanding balance #3020

Open
orehunt opened this issue Mar 5, 2020 · 2 comments · May be fixed by #3059
Open

a column for outstanding balance #3020

orehunt opened this issue Mar 5, 2020 · 2 comments · May be fixed by #3059
Labels
Enhancement Enhancements to the bot. Get lower priority than bugs by default.

Comments

@orehunt
Copy link
Contributor

orehunt commented Mar 5, 2020

Having a column that shows the outstanding balance on each candle can be useful. What is the outstanding balance? The sum of the virtual profits of all open trades on each candle, this is a very slow version:

from pandas import Series, Timedelta, date_range, DatetimeIndex

hloc = kwargs["processed"]
config = kwargs["config"]
timeframe = config["timeframe"]
timedelta = Timedelta(timeframe)

date_index: DatetimeIndex
date_index = date_range(start=min_date, end=max_date, freq=timeframe, normalize=True)
balance = {pair: Series(0.0, index=date_index) for pair in hloc}
balance_total = Series(0.0, index=date_index)
close_time = results["close_time"].values
open_time = results["open_time"].values
for pair in hloc:
    hloc[pair].set_index("date", inplace=True)
for date in date_index.values:
    # set the balance for all the opens that are not instantly closed
    where = (open_time == date) & (close_time != date)
    for _, tr in results.loc[where].iterrows():
        pair = tr["pair"]
        close = hloc[pair].at[date, "close"]
        # balance at date
        bal_at_date = 1 - tr["open_rate"] / close - slippage
        balance[pair][date] += bal_at_date
    # set the balance for all the closes
    for _, tr in results.loc[close_time == date].iterrows():
        pair = tr["pair"]
        # balance at date, since it's the close we take the profits minus slippage
        bal_at_date = tr["profit_percent"] - slippage
        balance[tr["pair"]][date] += bal_at_date
        # since we are closing, update all the balances since open time
        # ranges are inclusive:[):exclusive
        for open_date in (
            balance[pair]
            .loc[tr["open_time"] + timedelta : tr["close_time"]]
            .index
        ).values:
            close = hloc[pair].at[open_date, "close"]
            balance[pair][open_date] += 1 - tr["open_rate"] / close - slippage
    # settle the balance for all the pairs at current date
    for pair in balance:
        balance_total[date] += balance[pair][date]

I am trying to come up with a vectorized one, also not sure how much would this calculation impact performance if it was applied on the main backtest loop

@xmatthias
Copy link
Member

You should look at btanalysis.py - this should be very similar to the function analyze_trade_parallelism() we have to test max_open_trades is respected - but using sum() instead of count().

But let me ask- what for / why would you need this?
I'm not certain in which usecase this should be used ... - knowing how many trades are open at any given time is usually enough ... ?

@orehunt
Copy link
Contributor Author

orehunt commented Mar 6, 2020

you can't do it with the sum, you have to match the close price at each candle for every non closed trade

But let me ask- what for / why would you need this?

It is probably in the same vein as disable_max_market_positions and enable_position_stacking since it gives you a clearer picture with less data.
If you for example try to run objectives like sortino/sharpe in a mono trend timerange it will optimize always for bigger stoplosses (if market is bull) or smaller stoplosses (if market is bear) akin to future knowledge

@orehunt orehunt linked a pull request Mar 12, 2020 that will close this issue
@xmatthias xmatthias added the Enhancement Enhancements to the bot. Get lower priority than bugs by default. label Dec 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Enhancements to the bot. Get lower priority than bugs by default.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants