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/

    How to code a trading system that works with Backtrader?

    Indicators/Strategies/Analyzers
    2
    11
    1971
    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.
    • H
      hlad last edited by hlad

      Hi there

      I am new to python, and programming. So, excuse naivety in my doubts

      I have have implemented a class for a trading system that I learned recently. I have been trying to figure out how to use this class with backtrader to pass on the entry and exit signals.

      The system is purely based on price action, and does not use any of the standard time series indicators.

      The class methods calculates various attributes and related price points one bar at a time. I have used pandas DataFrame for holding the OHLC data, and fashioned a for loop to process the DataFeed (as BackTrader calls it).

      All trading decisions are based on the attributes calculated.

      Sample code:

      class myStrategy():
          def __init__():
              #initialize parameters
          def resample():
              #build the trading timeframe OHLC data from input price data
          def Attr1():
              #calculate attribute1
          def Attr2():
              #calculate attribute2
              #depends on Attr1
              #.
              #.
          def Attr5():
              #calculate attributeN
              #depends on Attr4(t-1), Attr5(t-1)
      
             #Attr4(t) also depends on Attr5(t-1)
              
      myObj = myStrategy()
      for idx,row in myObj.df.iterrows():
          #call methods to calculate attributes
          #
          #print the relevant data
      

      Thanks

      H B 2 Replies Last reply Reply Quote 0
      • H
        hlad @hlad last edited by

        @hlad
        Only last two attributes are dependent on previous bar attributes. Rest are calculated from the current bar price data

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

          @hlad said in How to code a trading system that works with Backtrader?:

          myObj = myStrategy()
          for idx,row in myObj.df.iterrows():
              #call methods to calculate attributes
              #
              #print the relevant data
          

          This is not how backtrader works. Your logic has to be implemented in next which is called with each bar. You'd probably want to follow the Docs - Quickstart

          It will give you a skeleton to work with.

          To add your dataframe instead of the CSV examples from the Quickstart use the PandasData data feed: Docs - Pandas DataFeed Example

          H 2 Replies Last reply Reply Quote 0
          • H
            hlad @backtrader last edited by hlad

            @backtrader said in How to code a trading system that works with Backtrader?:

            next wh

            As per my understanding:

            1. I will get the price data for current bar from backtrader class with following notations
            self.dataopen[-1] , self.datahigh[-1], self.datalow[-1] and self.dataclose[-1]
            

            please, confirm if this is correct

            1. I will have to repurpose my class functions to work within the
            class myStrategy(bt.Strategy):
            ...
            

            and decouple these functions from pandas data frame

            1. make trading decisions and call
            self.sell() or self.buy()
            

            How to specify the entry price?

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

              @hlad said in How to code a trading system that works with Backtrader?:

              please, confirm if this is correct

              Please read: Docs - Platform Concepts, with emphasis in the section: Indexing: 0 and -1

              And yes, you have to re-purpose if you want to use backtrader. If you want to use buy/sell/close or other family of methods like Docs - Target Orders (order_target_xxx) is up to you.

              H 1 Reply Last reply Reply Quote 0
              • H
                hlad @backtrader last edited by

                @backtrader said in How to code a trading system that works with Backtrader?:

                And yes, you have to re-purpose if you want to use backtrader. If you want to use buy/sell/close or other family of methods like D

                thanks

                will work on it and get back if I am stuck again

                1 Reply Last reply Reply Quote 0
                • H
                  hlad last edited by hlad

                  earlier problem solved, removing it

                  1 Reply Last reply Reply Quote 0
                  • H
                    hlad @backtrader last edited by

                    @backtrader

                    Hi

                    if i have multiple symbols in a portfolio to be tested, I will be passing the symbols along with the strategy to cerebro.

                    Does backtrader create separate objects for each symbol . Then process them separately barwise. Or it just has separate data feeds per symbol? which are used to calculate values of the indicators.

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

                      @hlad said in How to code a trading system that works with Backtrader?:

                      Does backtrader create separate objects for each symbol

                      Each data feed is an object. The question is probably meant to ask something entirely different.

                      1 Reply Last reply Reply Quote 0
                      • H
                        hlad last edited by

                        @backtrader

                        Yes, my indicator / system stores the previous state, and uses it to arrive at decision for next trading day.

                        It's like

                        • Compare today and previous day for parameters
                        • Use today's OHLC to and parameters to get price levels for tomorrow's price action
                        • The current state and price action relative to price action tomorrow will decide the next state.

                        The current implementation stores this state in variable inside the Class object created for the Stock being tested.
                        And the process of calculating the state is the most time consuming part of the whole logic. Doing it afresh every iteration will slow down the backtest considerably

                        @backtrader said in How to code a trading system that works with Backtrader?:

                        @hlad said in How to code a trading system that works with Backtrader?:

                        Does backtrader create separate objects for each symbol

                        Each data feed is an object. The question is probably meant to ask something entirely different.

                        Is each feed capable of storing such state variables?

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

                          @hlad said in How to code a trading system that works with Backtrader?:

                          Is each feed capable of storing such state variables?

                          Why shouldn't? The statement of problem is really unclear.

                          You can store things in additional lines defined for the data feed and you store and access the entire history. You can add your own attributes and store as many as you like in for example a list or just the last value in a simple instance attribute.

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