Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Adding data to cerebro with buy/sell signal

    General Code/Help
    3
    10
    995
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • V
      vishamar last edited by vishamar

      Dear Community,

      I'm trying to add a merged list to cerebro for each day of the backtesting period that contains the (OHLCV data, stock name) pairs for the particular day.
      Stocks to buy I store in a list (cerebrolistBUY) with their names, stocks to sell, contrarily, have a '-' sign before their names in another list (cerebrolistSELL). The two lists are merged and passed to cerebro:

      cerebrolist=cerebrolistBUY+cerebrolistSELL
      for i in range(len(cerebrolist)):
          cerebro = bt.Cerebro()
          cerebro.addstrategy(TestStrategy)
          data = bt.feeds.PandasData(dataname=cerebrolist[i][0])
          cerebro.adddata(data, name=cerebrolist[i][1])
      

      How could I create the buy and sell signal in the next() method? Indicators are already used for selecting the stocks for the particular day (i.e., creating the 2 lists), so only their names show whether they are to be bought or sold (have a minus sign before their names or not).

      Thank you in advance for any help!

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        It would really help if you showed a snippet of your lists. Because it is unclear what you are actually adding to the system.

        V 1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          In any case you may want to check this: Blog - Evaluating External Historical Performance

          1 Reply Last reply Reply Quote 0
          • A
            ab_trader last edited by ab_trader

            The snippet loads all data feeds to cerebro from the list cerebrolist. There are no designation what pair should be bought/sold on what day. So with the snippet above I don't think that it is possible to issue any signals.

            V 1 Reply Last reply Reply Quote 0
            • V
              vishamar @backtrader last edited by

              @backtrader
              For a given day: cerebrolistSELL=[( Open High Low Close Volume
              Date
              2018-10-31 1.65 1.7 1.62 1.7 507791.0, '-FLT.V'), ( Open High Low Close Volume
              Date
              2018-10-31 0.6 0.62 0.6 0.62 2644244.0, '-SY.V')]

              1 Reply Last reply Reply Quote 0
              • V
                vishamar @ab_trader last edited by

                @ab_trader
                The designation is the presence or absence of the minus sign before the stock name (stored in cerebrolist[i][1]).
                Actually I would like to refer to that in the next() method.

                1 Reply Last reply Reply Quote 0
                • A
                  ab_trader last edited by ab_trader

                  So in the next() you obtain the current data date, look up this date in your list and buy or sell corresponding tickets.

                  1 Reply Last reply Reply Quote 0
                  • B
                    backtrader administrators @vishamar last edited by

                    Given the data above

                    @vishamar said in Adding data to cerebro with buy/sell signal:

                    cerebrolistSELL=[(            Open  High   Low  Close    Volume
                    Date
                    2018-10-31  1.65   1.7  1.62    1.7  507791.0, '-FLT.V'), (            Open  High  Low  Close     Volume
                    Date
                    2018-10-31   0.6  0.62  0.6   0.62  2644244.0, '-SY.V')]
                    

                    and given the code

                    @vishamar said in Adding data to cerebro with buy/sell signal:

                    cerebrolist=cerebrolistBUY+cerebrolistSELL
                    for i in range(len(cerebrolist)):
                        cerebro = bt.Cerebro()
                        cerebro.addstrategy(TestStrategy)
                        data = bt.feeds.PandasData(dataname=cerebrolist[i][0])
                        cerebro.adddata(data, name=cerebrolist[i][1])
                    

                    I don't really think this code is actually doing anything. It would seem you are attempting to pass individual days to PandasData with cerebrolist[i][0], and I say attempting because you seem to be passing the name of the fields and the fields all at once.

                    Does the code even work?

                    In any case, such a data structure doesn't seem directly compatible as input to a data feed. You need a complete set of OHLCV for the given data and not the data of days in which you buy/sell that data.

                    V 1 Reply Last reply Reply Quote 2
                    • V
                      vishamar @backtrader last edited by

                      @backtrader
                      Thank you for the clarification.
                      I modified the script to create a list of stocks for a given backtesting period that my indicators show to be worth buying or selling for each day and pass their OHLCV data for the given period to cerebro.
                      I have now these kinds of lists:

                      cerebrolistBUY=[(datetime.datetime(2018, 10, 11, 0, 0), 'FLT.V'), (datetime.datetime(2018, 10, 31, 0, 0), 'SY.V')]
                      cerebrolistSELL=[(datetime.datetime(2018, 10, 19, 0, 0), '-FLT.V')]
                      

                      Can I use somehow in the next() method the date and the minus sign for buy/sell signal?

                      1 Reply Last reply Reply Quote 0
                      • B
                        backtrader administrators last edited by

                        If you manage to get a meaningful data feed, you can always compare the datetime and if it's a match you sell if the name starts with - and otherwise buy.

                        Your best bet is to transform the list to match the format in the blog post linked above (Evaluating External Historical Performance)

                        1 Reply Last reply Reply Quote 1
                        • 1 / 1
                        • First post
                          Last post
                        Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                        $(document).ready(function () { app.coldLoad(); }); }