polakowo / vectorbt

Find your trading edge, using the fastest engine for backtesting, algorithmic trading, and research.
https://vectorbt.dev
Other
4.33k stars 618 forks source link

portfolio stats calculation for dca strategies. not appear all buy orders only first #688

Open spainbox opened 8 months ago

spainbox commented 8 months ago

A DCA Dollar cost average, make several buy orders and later close the trade when it reach a profit from last average price, what i see is that in orders are only registered the first buy and last close but not other entries between first buy and last exit

i will explain with an simple example:

in this code i have 3 orders: 2 entries (buy_dates = ['2017-11-09', '2017-11-12'] ) and 1 exit (sell_date = '2017-11-14'), however portfolio.orders.records_readable only show first entry and exit: import pandas as pd import numpy as np import vectorbt as vbt

Descargar los datos

eth_price = vbt.YFData.download('ETH-USD').get('Close')

Crear señales de entrada y salida

entries = pd.Series(False, index=eth_price.index) exits = pd.Series(False, index=eth_price.index)

Configurar las fechas de compra y venta

buy_dates = ['2017-11-09', '2017-11-12'] sell_date = '2017-11-14'

Asignar las señales de compra y venta

entries[buy_dates] = True exits[sell_date] = True

Crear el portfolio

portfolio = vbt.Portfolio.from_signals(eth_price, entries, exits, freq='D')

print(eth_price[:6])

Imprimir los trades

trades = portfolio.trades print(trades.records_readable)

Imprimir las estadísticas del portfolio

stats = portfolio.stats() print(stats)

print (portfolio.orders.records_readable)

this is the result:

Date 2017-11-09 00:00:00+00:00 320.884003 2017-11-10 00:00:00+00:00 299.252991 2017-11-11 00:00:00+00:00 314.681000 2017-11-12 00:00:00+00:00 307.907990 2017-11-13 00:00:00+00:00 316.716003 2017-11-14 00:00:00+00:00 337.631012

Freq: D, Name: Close, dtype: float64 Exit Trade Id Column Size Entry Timestamp Avg Entry Price Entry Fees Exit Timestamp Avg Exit Price Exit Fees PnL Return Direction Status Position Id 0 0 0 0.311639 2017-11-09 00:00:00+00:00 320.884003 0.0 2017-11-14 00:00:00+00:00 337.631012 0.0 5.219023 0.05219 Long Closed 0 Start 2017-11-09 00:00:00+00:00 End 2024-01-30 00:00:00+00:00 Period 2274 days 00:00:00 Start Value 100.0 End Value 105.219023 Total Return [%] 5.219023 Benchmark Return [%] 619.782758 Max Gross Exposure [%] 100.0 Total Fees Paid 0.0 Max Drawdown [%] 6.741069 Max Drawdown Duration 4 days 00:00:00 Total Trades 1 Total Closed Trades 1 Total Open Trades 0 Open Trade PnL 0.0 Win Rate [%] 100.0 Best Trade [%] 5.219023 Worst Trade [%] 5.219023 Avg Winning Trade [%] 5.219023 Avg Losing Trade [%] NaN Avg Winning Trade Duration 5 days 00:00:00 Avg Losing Trade Duration NaT Profit Factor inf Expectancy 5.219023 Sharpe Ratio 0.202396 Calmar Ratio 0.121631 Omega Ratio 1.643893 Sortino Ratio 0.324209 dtype: object Order Id Column Timestamp Size Price Fees Side 0 0 0 2017-11-09 00:00:00+00:00 0.311639 320.884003 0.0 Buy 1 1 0 2017-11-14 00:00:00+00:00 0.311639 337.631012 0.0 Sell

polakowo commented 7 months ago

accumulate=True with a finite size will execute each entry