Comments (8)
Close is used for calculating return metrics such as sharpe, val_price for using target value and percentage, price is order price, so setting price
to close * 100
should work.
from vectorbt.
@polakowo Thank you! but I can not get right result.
import numpy as np
import pandas as pd
import vectorbt as vbt
close = pd.Series([1, 2, 3, 4, 5], index=pd.date_range('2020-01-01', periods=5))
price = close * 100
val_price = close * 100
size = [1, np.nan, np.nan, np.nan, -1]
portfolio = vbt.Portfolio.from_orders(close, size,
price=price,
val_price=val_price, close_first=True)
#
print(portfolio.positions().records)
#
print(portfolio.value())
portfolio.trades().plot()
id col size entry_idx entry_price entry_fees exit_idx exit_price \
0 0 0 1.0 0 100.0 0.0 4 500.0
exit_fees pnl return direction status
0 0.0 400.0 4.0 0 1
2020-01-01 1.0
2020-01-02 2.0
2020-01-03 3.0
2020-01-04 4.0
2020-01-05 500.0
Freq: D, dtype: float64
the entry_price should be 1, maybe need a new column entry_value.
the figure is wrong. entry_maskers not on the close price line.
from vectorbt.
But your order price is 100 * close
, so why do you expect the entry price to be 1? Entry price in your example is close[0] * 100
, which is 100, and not 1. Your exit price is close[-1] * 100
, which is 500, thus a P&L of 400. Value at the beginning is 1 instead of 100 because it uses your close (last price at each bar) instead of your order price to calculate various time series such as value and returns for consistency. Can you elaborate more on what you want to achieve?
Edit: I don't think there is currently a way to backtest futures, haven't checked that.
from vectorbt.
I think right output is this
id col size entry_idx entry_price entry_fees exit_idx exit_price exit_fees pnl return direction status entry_value exit_value
0 0 0 1.0 0 1.0 0.0 4 5.0 0.0 400.0 4.0 0 1 100.0 500.0
2020-01-01 100.0
2020-01-02 200.0
2020-01-03 300.0
2020-01-04 400.0
2020-01-05 500.0
Freq: D, dtype: float64
price
is quote price.
value
is asset value.
sorry I am not good at english. have to use code to explain it.
from vectorbt.
There is no easy way to implement your example without workarounds such as applying a factor of 100 directly on close:
import numpy as np
import pandas as pd
import vectorbt as vbt
close = pd.Series([100, 200, 300, 400, 500], index=pd.date_range('2020-01-01', periods=5))
size = [1, np.nan, np.nan, np.nan, -1]
portfolio = vbt.Portfolio.from_orders(close, size, close_first=True)
#
print(portfolio.positions().records)
#
print(portfolio.value())
id col size entry_idx entry_price entry_fees exit_idx exit_price \
0 0 0 1.0 0 100.0 0.0 4 500.0
exit_fees pnl return direction status
0 0.0 400.0 4.0 0 1
2020-01-01 100.0
2020-01-02 200.0
2020-01-03 300.0
2020-01-04 400.0
2020-01-05 500.0
Freq: D, dtype: float64
As said, futures trading is no part of vectorbt as of now.
Do you have a similar example of how you do it in let's say backtrader?
from vectorbt.
thank you all the same
Commissions: Stocks vs Futures
https://www.backtrader.com/docu/commission-schemes/commission-schemes/
mult (default: 1.0) For future like instruments this determines the multiplicator to apply to profit and loss calculations.
cerebro.broker.setcommission(commission=2.0, margin=2000.0, mult=10.0)
https://github.com/mementum/backtrader/blob/master/samples/commission-schemes/commission-schemes.py
Futures and Spot Compensation
https://www.backtrader.com/docu/order-creation-execution/futurespot/future-vs-spot/
https://github.com/mementum/backtrader/blob/master/samples/future-spot/future-spot.py
https://github.com/mementum/backtrader/search?p=1&q=mult
from vectorbt.
I will mark this as an enhancement, as it means adding support for future-like instruments. But I can't give any estimates on when or whether it can be implemented - a lot of changes have to be done to the backbone to make it possible. For example, you may want to buy 10 units with a multiplier of 100 and another 20 units with a multiplier of 50, so you need to track those multipliers at each timestamp to generate the value - this adds complexity to and further reduces the performance of the solution.
from vectorbt.
value = price * multiplier
margin = value* 0.2
vbt.Portfolio.from_orders(
close,
val_price,
value,
margin,
)
maybe prepare some data by user before call from_orders
is an easy way
from vectorbt.
Related Issues (20)
- tp_stop with accumulate gives wrong result
- Data mismatch
- Incorrect Position Size Allocation Across Multiple Assets HOT 3
- For the same precision data, there is an accuracy error in the results.
- Issues with combining multiple plots into subplots in 1 figure HOT 1
- Plotting Error: Subplot 'trade_pnl' raised an exception HOT 1
- Getting Daily PNL Portfolio Change
- What is the difference in the documentation of vectorbt PRO vs the open source vectorbt?
- What is the benchmark plotted in pf.plot().show()?
- How to use run_combs in combination with other signals?
- Questions about backtesting
- StopLoss group_by
- Plots missing "close" curve
- Import Errors HOT 5
- Plotting with custom benchmark_rets got error (VectorBT used undefined attribute 'obj')
- Datetime support with from_order_func HOT 2
- Dockerfile changes to run /apps/candlestick-patterns app
- Multiprocessing Error When Creating Portofiolio
- Pandas MultiIndex on axis 0 HOT 1
- Closing all positions at the end of the day HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vectorbt.