Comments (4)
Makes sense!
This is very cool. Thanks so much.
from vectorbt.
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.
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.
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)
- set sl/tp to be triggered on exact values
- Portfolio.stats consumes large amount of memory. HOT 1
- Need to add requirement: nbformat>=4.2.0
- Vectorbt documentation code examples throws unsupported error. HOT 1
- Assertion Error with VectorBT Portfolio from MultiIndex DataFrame
- Reverse position with from_signals HOT 1
- 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
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.