Giter Club home page Giter Club logo

Comments (8)

polakowo avatar polakowo commented on May 12, 2024

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.

wukan1986 avatar wukan1986 commented on May 12, 2024

@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.

polakowo avatar polakowo commented on May 12, 2024

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.

wukan1986 avatar wukan1986 commented on May 12, 2024

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.

polakowo avatar polakowo commented on May 12, 2024

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.

wukan1986 avatar wukan1986 commented on May 12, 2024

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.

polakowo avatar polakowo commented on May 12, 2024

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.

wukan1986 avatar wukan1986 commented on May 12, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.