Code Monkey home page Code Monkey logo

Comments (4)

NowBothWhiteAndRed avatar NowBothWhiteAndRed commented on August 11, 2024 1

Makes sense!
This is very cool. Thanks so much.

from vectorbt.

polakowo avatar polakowo commented on August 11, 2024

As said, when having multiple entries back to back, exit generation is tricky. Suppose you have two entries and the exit for the first entry comes after the exit for the second entry. Your position would then open with the first entry but close based on condition of the second entry (although the second entry was never executed since you already are in position). That's why vectorbt let's you only generate exits that are before the next entry, such that you certainly know which exit belongs to which entry. Other backtesting libraries don't have this limitation since they define take profit conditions when issuing an order, such that each exit condition can be easily associated with an entry order.

Now, I made some changes to the signaling logic and added a keyword argument iteratively. What it does is looks for the first entry and generates an exit (if any). It then looks for an entry after that exit and repeats the process. This way, you will have perfectly aligned entry and exit arrays.

import vectorbt as vbt
import pandas as pd

sig = pd.DataFrame({
    'a': [True, False, False, False, False],
    'b': [True, False, True, False, True],
    'c': [True, True, True, False, False]
})
print(sig)
       a      b      c
0   True   True   True
1  False  False   True
2  False   True   True
3  False  False  False
4  False   True  False
ts = pd.Series([1, 2, 3, 4, 5])
new_entries, new_exits = sig.vbt.signals.generate_take_profit_exits(ts, [0.1, 0.5], iteratively=True)
print(new_entries)
print(new_exits)
take_profit    0.1                  0.5              
                 a      b      c      a      b      c
0             True   True   True   True   True   True
1            False  False  False  False  False  False
2            False   True   True  False   True   True
3            False  False  False  False  False  False
4            False   True  False  False  False  False
take_profit    0.1                  0.5              
                 a      b      c      a      b      c
0            False  False  False  False  False  False
1             True   True   True   True   True   True
2            False  False  False  False  False  False
3            False   True   True  False  False  False
4            False  False  False  False   True   True

Does it makes sense now?

This logic only works with accumulate=False in Portfolio.from_signals method, that is, it can't be applied on strategies that gradually increase or decrease a position.

from vectorbt.

NowBothWhiteAndRed avatar NowBothWhiteAndRed commented on August 11, 2024

Makes sense. Tested the change and it looks good! One minor issue,
If you run the example file with:

dmac_exits = dmac_entries.vbt.signals.generate_take_profit_exits(ts=ohlcv['Open'], stops=profit,
                                                                 iteratively=True)
# PORTFOLIO
dmac_portfolio = vbt.Portfolio.from_signals(ohlcv['Open'], dmac_entries, dmac_exits, freq='1M', accumulate=False)

You will get the following exception:

ValueError: shape mismatch: objects cannot be broadcast to a single shape

This looks to be because:

 dmac_entries.vbt.signals.generate_take_profit_exits(ts=ohlcv['Open'], stops=profit,
                                                                 iteratively=True)

Now returns two objects. After some testing, it looks like the 2nd object is the correct one. (Ie. exits are generated where I'd expect them to be). Just using he second object would be fine but letting you know in case it's a symptom of something else.

from vectorbt.

polakowo avatar polakowo commented on August 11, 2024

This is a correct behavior: when iteratively=True it returns both "cleaned" entries and exits. You should use the entries returned by the method, since it no more contains multiple signals in a row, but only those that come after an exit took place.

Also note that if you have an entry and an exit at the same tick (as you would have if you used your original dmac_entries object) it won't perform exit, but buy/sell a difference between what you hold and the size specified at this tick. Unless you do accumulate=True, your entries and exits should not overlap and should go strictly one after another (that's what's called iteratively in vectorbt).

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.