-
Hi there! I am new to vectorbt (and python for that matter), so I am trying to start from the very basic to try to wrap my head around this awesome library. This is what I am trying to do: simulate an entry accompanied by four taking profit targets (taking 25% percent of size each time for simplicity), and an initial Stop Loss. The SL would adjust as the targets are hit: first (when TP1 is reached) SL would go to breakeven (entry) price then, when TP2 is hit SL would go to TP1 price, etc. This is what I have managed to do so far:
I am stuck now trying to add more than one target. I have tried to pass a list of floats instead of a single float to Needless to say, I haven't been able to test the SL adjust part. I suspect I should use I have been through the examples, but maybe I am losing something. So if anyone can point me in the right direction, I'd greatly appreciate it. Thanks a lot, and sorry for the newbie question. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
Hi and welcome @GogeiD, you can work with only one SL/TP target per entry - you can't have multiple TP stops for a single entry signal. This also can't be properly represented using an array since this array must have a maximum of two dimensions, both of which are reserved for time and assets respectively. I will think of how to enable this. But for now, you can construct your signals array to account for multiple TP signals. For example, you can call this function (using Or, you can dynamically generate signals by defining your own |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot GogeiD.
El mar., 18 de octubre de 2022 4:14 p. m., GogeiD ***@***.***>
escribió:
… Hi @egrafeuille <https://github.com/egrafeuille>. Well, I pretty much
followed the instructions made by Oleg. From my Dataframe containing the
exit signals at different price targets, I generated a dictionary with
arrays of boolean values that indicate a which timestamp the target was
reached. For those exit signals, I used
entries.vbt.signals.generate_ohlc_stop_exits. Then, I combined those
arrays in one using the | operator.
To get the sizes for each exit, created an array that look for a True
value and assign a predefined % amount, depending on which target it was
referencing. Finally, I passed those arrays to vbt.Portfolio.from_signals.
I am going to past the code snippet below but, frankly, it won't make much
sense to you since you don't know how the signals are structured, and
honestly I am still pretty much new to Python, so I am sure there is a
better way to do it. Also, bear in mind that I haven't updated vbt since I
posted this, so I don't know how it's going to work with the current
version. Anyway, I hope this helps a little!
for i in range(1, 5):
out_dict = {}
exit_data["tp_pct" + str(i)] = pd.pivot_table(
signals, index=signals.index, columns=fields, values="T" + str(i)
)
exit_data["tp_pct" + str(i)] = exit_data["tp_pct" + str(i)].reindex(
index=close.index
)
exit_data["exit_tp" + str(i)] = entries.vbt.signals.generate_ohlc_stop_exits(
open=open,
high=high,
low=low,
close=close,
tp_stop=exit_data["tp_pct" + str(i)],
until_next=False,
skip_until_exit=True,
out_dict=out_dict,
)
target_price[i - 1] = out_dict["stop_price"].fillna(method="ffill").to_numpy()
exits = (
exit_data["exit_tp1"]
| exit_data["exit_tp2"]
| exit_data["exit_tp3"]
| exit_data["exit_tp4"]
)
size = (
(entries * size_targets[1])
+ (exit_data["exit_tp1"] * size_targets[2])
+ (exit_data["exit_tp2"] * size_targets[3])
+ (exit_data["exit_tp3"] * size_targets[4])
+ (exit_data["exit_tp4"] * size_targets[5])
)
—
Reply to this email directly, view it on GitHub
<#230 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAYH7KQXQNDFL4FKWWRFM5DWD3ZK3ANCNFSM5DS2O4OQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
@polakowo Hi, I wonder if vectorbt.pro support this feature now or it's on planing? |
Beta Was this translation helpful? Give feedback.
Hi and welcome @GogeiD,
you can work with only one SL/TP target per entry - you can't have multiple TP stops for a single entry signal. This also can't be properly represented using an array since this array must have a maximum of two dimensions, both of which are reserved for time and assets respectively. I will think of how to enable this.
But for now, you can construct your signals array to account for multiple TP signals. For example, you can call this function (using
entries.vbt.signals.generate_ohlc_stop_exits(...)
) to generate exits for 15% above the entry price, then do the same for 30%, 45%, etc. and combine all of them into a single exits array (exits = exits1.vbt | exits2.vbt |…