vectorbt icon indicating copy to clipboard operation
vectorbt copied to clipboard

Create multiple entry/exit signals in vectorbt

Open mighty-phoenix opened this issue 1 year ago • 4 comments

I'm using vectorbt currently for backtesting. I have multiple entry signals before a following exit signal. But in the plots, I can see that the following entry signals after the first one are ignored until the exit signal for it is plotted. I want to find out how I can consider all the signals instead of ignoring. Thanks! @barnjamin @polakowo @izikeros @RileyMShea @emiliobasualdo Quick response would be highly appreciated. Thanks!

mighty-phoenix avatar Sep 13 '23 04:09 mighty-phoenix

Use accumulate=True

polakowo avatar Sep 15 '23 21:09 polakowo

  1. Please help me with accumulate=True and AccumulationMode.AddOnly. I use custom_indicator which outputs signals and then use backtest to check the signals.
  2. Everything works correctly and backtest works except accumulate. I am using a signal to enter and TP. I would like to have several open positions where each position waits for its TP. I mean to use position set or pyramiding. But accumulate=True or if I pass AccumulationMode it doesn't work either. And on the attached screenshot you can see that at the moment of fall we are waiting for TP and no more positions are opened and so on until the end of all data although there were signals and there were a lot of them.

Can you please tell me how to fix this or maybe I am doing something wrong? @barnjamin @polakowo @izikeros @RileyMShea @emiliobasualdo. Thanks in advance.

image

import vectorbt as vbt
import pandas as pd
import numpy as np

import vectorbt.portfolio.base
import vectorbt.portfolio.enums

def find_entry_level(price, line_values):
    return line_values[np.argmin(np.abs(line_values - price))]

def custom_indicator(data, num_orders=10):
    line_values = []

    high_range = np.max(data)
    low_range = np.min(data)

    percent_of_grid = (high_range - low_range) / low_range * 100
    step_size = (high_range - low_range) / (num_orders - 1)

    current_price = low_range
    line_values.append(current_price)
    percentage_distance = (step_size / low_range) * 100

    for i in range(1, num_orders):
        current_price += step_size
        line_values.append(current_price)

    data = np.array(data)
    signals = np.zeros_like(data, dtype=int)

    for i in range(1, len(data)):
        if data[i-1] > find_entry_level(data[i-1], line_values) and data[i] < find_entry_level(data[i], line_values):
            signals[i] = 1


    return signals


if __name__ == "__main__":
    data = pd.read_csv("df1.csv", index_col=0)
    data = data[200000:300000]
    data = data["Close"]


    ind = vbt.IndicatorFactory(class_name="Combination", short_name="comb",
                               input_names=["close"],
                               param_names=["num_orders"],
                               output_names=["value"]
                               ).from_apply_func(custom_indicator,
                                                 num_orders=20,
                                                 keep_pd=True)

    res = ind.run(data,
                  num_orders=20,
                  param_product=True)

    print(res.value)

    entries = res.value == 1.0

    x = vectorbt.portfolio.enums.AccumulationMode.AddOnly
    pf = vbt.Portfolio.from_signals(data, entries, accumulate=x, tp_stop=0.005 )

    print(pf.stats())
    pf.plot().show()
    

nonisich avatar Sep 28 '23 07:09 nonisich

I have a similar question as @nonisich I'm unable to open multiple positions at the same time... It works in a binary fashion. If a trade is already open, another trade isn't opened until the first one is closed. Even if I use accumulate=True That's your problem too right? @nonisich Thanks! @barnjamin @polakowo @izikeros @RileyMShea @emiliobasualdo

mighty-phoenix avatar Oct 04 '23 11:10 mighty-phoenix

This issue is stale because it has been open for 90 days with no activity.

github-actions[bot] avatar Feb 04 '24 01:02 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Feb 19 '24 01:02 github-actions[bot]

Did anyone solve this issue? Having the same problem here... I would like to test a Martin Gale type strategy but it isnt buying/selling multiple times.

matheusar avatar Jul 12 '24 15:07 matheusar