polakowo / vectorbt

Find your trading edge, using the fastest engine for backtesting, algorithmic trading, and research.
https://vectorbt.dev
Other
4.27k stars 613 forks source link

Import data from .csv file #448

Closed brankoo124 closed 6 months ago

brankoo124 commented 2 years ago

Hi, I am trying to import data from .csv file, which structure is exactly same as the csv's exported from yahoo finance (cryptocurrencies, so no AdjClose column). Is there any built in way of doing it?

polakowo commented 2 years ago

Hi, you can use pd.read_csv (https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html) to load the data into a DataFrame

utekakca commented 2 years ago

Hi I am trying the same but no luck. I try these:

vbt.Data.from_data(df.close)

piotryordanov commented 2 years ago

@utekakca @brankoo124 you can try something like this:

def get_data(asset="BTCUSD", start=0, end=0):
    filename = "data/{}.csv".format(asset)
    df = pd.read_csv(filename)
    df['Date'] = pd.to_datetime(df['Date'])
    df = df.set_index('Date')

    if (end != 0):
        df = df[start:end]
    return df

def get_vector_data(asset="BTCUSD", start=0, end=0):
    class CustomData(vbt.Data):
         @classmethod
         def download_symbol(cls, symbol, start=0, end=0):
             return get_data(symbol, start, end)

    return CustomData.download([asset], start=start, end=end).get()

data = get_vector_data("BTCUSD")