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

Multiple Data Feeds (3555 csv files) on a single strategy

  • I am new here, just wondering if i could do such performance with backtrader.
    I have 3555 good csv files laying there.

    here is my codes:

    # -*- coding:utf-8 -*-
    # env python 3.6
    # 从兴趣开始学习Python
    # 我是pepCoder & pepTrader
    # email:
    # Blog:
    # Date: 2020/5/4 10:49
    from __future__ import (absolute_import, division, print_function,
    import datetime
    import os.path
    import sys
    import pandas as pd
    import backtrader as bt
    # Create a Strategy
    class TestStrategy(bt.Strategy):
        params = (
            ('maperiod', 30),
        def log(self, txt, dt=None):
            """ Logging function fot this strategy"""
            dt = dt or self.datas[0]
            print('%s, %s' % (dt.isoformat(), txt))
        def __init__(self):
            # Keep a reference to the "close" line in the data[0] dataseries
            self.dataclose = self.datas[0].close
            # To keep track of pending orders
            self.order = None
            self.buyprice = None
            self.buycomm = None
            # Add a MovingAverageSimple indicator
            self.sma = bt.indicators.SimpleMovingAverage(
                self.datas[0], period=self.params.maperiod)
            # Indicators for the plotting show
            bt.indicators.ExponentialMovingAverage(self.datas[0], period=90)
            bt.indicators.WeightedMovingAverage(self.datas[0], period=200).subplot = True
            # bt.indicators.StochasticSlow(self.datas[0])
            rsi = bt.indicators.RSI(self.datas[0])
            bt.indicators.SmoothedMovingAverage(rsi, period=10)
            bt.indicators.ATR(self.datas[0]).plot = False
        def notify_order(self, order):
            if order.status in [order.Submitted, order.Accepted]:
                # Buy/Sell order submitted/accepted to/by broker - Nothing to do
            # Check if an order has been completed
            # Attention: broker could reject order if not enough cash
            if order.status in [order.Completed]:
                if order.isbuy():
                        'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                    self.buyprice = order.executed.price
                    self.buycomm = order.executed.comm
                else:  # Sell
                    self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                self.bar_executed = len(self)
            elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                self.log('Order Canceled/Margin/Rejected')
            self.order = None
        def notify_trade(self, trade):
            if not trade.isclosed:
            self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %
                     (trade.pnl, trade.pnlcomm))
        def next(self):
            # Simply log the closing price of the series from the reference
            self.log('Close, %.2f' % self.dataclose[0])
            # Check if an order is pending ... if yes, we cannot send a 2nd one
            if self.order:
            # Check if we are in the market
            if not self.position:
                # Not yet ... we MIGHT BUY if ...
                if self.dataclose[0] > self.sma[0]:
                    # BUY, BUY, BUY!!! (with all possible default parameters)
                    self.log('BUY CREATE, %.2f' % self.dataclose[0])
                    # Keep track of the created order to avoid a 2nd order
                    self.order =
                if self.dataclose[0] < self.sma[0]:
                    # SELL, SELL, SELL!!! (with all possible default parameters)
                    self.log('SELL CREATE, %.2f' % self.dataclose[0])
                    # Keep track of the created order to avoid a 2nd order
                    self.order = self.sell()
    if __name__ == '__main__':
        cerebro = bt.Cerebro()
        root_path = '.\\__stock__'
        for root, dirs, files in os.walk(root_path):
            for csv in files:
                if csv.endswith('.csv'):
                    csv_path = os.path.join(root, csv)
                    df = pd.read_csv("{}".format(csv_path), encoding='gbk', index_col=0, parse_dates=True)
                    df = df.sort_values(by=['日期'], ascending=True)
                    df = df.loc[:, ['日期', '开盘价', '收盘价', '最高价', '最低价', '成交量']]
                    df = df.rename(columns={
                        '开盘价': 'open',
                        '收盘价': 'close',
                        '最高价': 'high',
                        '最低价': 'low',
                        '成交量': 'volume'})
                    df['openinterest'] = 0
                    data = bt.feeds.PandasData(dataname = df,
                                               fromdate = datetime.datetime(2015, 1, 1),
                                               todate = datetime.datetime(2020, 4, 15))
        # Add a FixedSize sizer according to the stake
        cerebro.addsizer(bt.sizers.FixedSize, stake=10)
        # Set the commission - 0.1% ... divide by 100 to remove the %
        print('Starting Portfolio Value: %.2f' %
        print('Final Portfolio value: %.2f' %

    I know it looks hilarious, plz give me hint where I should be heading. lolz

  • run your code and you will see if it is possible or not.

  • it runs separately, one by one each.

  • The way your strategy is implemented assumes only a single data feed is present. To be more exact, the strategy is working only with the first available data feed (it creates all the indicators for the data[0], and the next method only tracks the data[0] close price), despite multiple data feeds available.

    Please take a look at:

  • Do you wish to run your algorithm on all stocks at the same time or are you trying to run your algorithm for each stock independently?

  • @run-out ok, the codes I presented is just a practice work. Im trying to get to know BTr and get a hold of it. here is what im trying to deal with. Im trying to set up a way to find MA combo pattern, and then run all 3555 csv files to match the pattern, then trigger trades. This is the reason why i have 3555 csv files on my HDD. =D

  • Running multiple data feeds is a bit tricker. You have to cycle through them in next.

    Here is a good example/article.

Log in to reply