For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

SQLite example

  • Dear Community,

    Does anyone has a SQLite example (database schema + class extending AbstractDatabase) that she is willing to share?
    For now, I'm just looking at setting of simple database with one table that will host daily bars (symbol, date, open, high, low, close, volume).

    Thanks a lot in advance

  • I have examples but could you be more clear what you want to have/do?

  • Hi @run-out, essentially, I want to store data in sqlite, create data feeds for each symbol from SQLite (instead of genericCsv of Panda feed) and run a backtest. Down the road, I want to snap latest bars from broker and append these prices to sqlite table.

  • Please take a look at the PSQL data feed implementation suggested in the following PR:

    it could be relatively easily converted to use SQLite API. However, you need an additional code to store new data since the above PR is only for data feed.

  • Did @vladisld answer your question or are you still looking for a straight up sqlite3 solution?

  • It is a partial match; I was hoping someone had already ready-to-run code. But if nobody has anything better; I'll find some time to look at this PR and adapt it. Thanks for checking @run-out

  • Just out of curiosity, why SQLite and not some time series database like Influxdb?

  • In short, I'm designing an Expert Advisor based on backtrader. I worked with daily bars. SQLite will be clearly fast enough for my needs (in fact, I'm already ok with CSV perf). I will be snapping latest market data from a broker (for this; it will be easier/cleaner to add data to the database than CSV), compute signals and list down order required for rebalancing the portfolio.

  • @lampalork In my case I simply connect to sqlite3 using pandas, create a dataframe, and then input this into backtrader using pandas feeds.

    import sqlite3
    pdict = {}
    # Create database engine
    engine = sqlite3.connect("filepath", detect_types=sqlite3.PARSE_DECLTYPES)
    table = 'tablename'
    sql_start = pdict["from_date"]
    sql_end = pdict["to_date"]
    sql = (
            "SELECT * FROM "
            + table
            + " WHERE datetime BETWEEN '"
            + sql_start
            + "' AND '"
            + sql_end
            + "'"
    df_ohlcv = pd.read_sql(sql, con=engine)
    df_ohlcv = df_ohlcv.set_index("datetime", drop=True)
    df_ohlcv.index = pd.to_datetime(df_ohlcv.index)

    From here you can input into backtrader as a pandas dataframe.

    If you have multiple tickers, then you just grab the tickers from the dataframe

    tickers_list = df['tickers'].unique()

    Then cycle through the tickers to filter your dataframe and add in the datas one at a time.

  • @run-out thanks a lot. I m building a dedicated feed using sqlalchemy (i.e. not using pandas). I ll publish as soon as I get a stable version

Log in to reply