backtrader | Python Backtesting library for trading strategies | Cryptocurrency library

 by   mementum Python Version: 1.9.78.123 License: GPL-3.0

kandi X-RAY | backtrader Summary

kandi X-RAY | backtrader Summary

backtrader is a Python library typically used in Blockchain, Cryptocurrency applications. backtrader has no bugs, it has no vulnerabilities, it has build file available, it has a Strong Copyleft License and it has high support. You can install using 'pip install backtrader' or download it from GitHub, PyPI.

Python Backtesting library for trading strategies
Support
    Quality
      Security
        License
          Reuse

            kandi-support Support

              backtrader has a highly active ecosystem.
              It has 10912 star(s) with 3351 fork(s). There are 585 watchers for this library.
              OutlinedDot
              It had no major release in the last 12 months.
              backtrader has no issues reported. There are 43 open pull requests and 0 closed requests.
              OutlinedDot
              It has a negative sentiment in the developer community.
              The latest version of backtrader is 1.9.78.123

            kandi-Quality Quality

              backtrader has 0 bugs and 0 code smells.

            kandi-Security Security

              backtrader has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
              backtrader code analysis shows 0 unresolved vulnerabilities.
              There are 0 security hotspots that need review.

            kandi-License License

              backtrader is licensed under the GPL-3.0 License. This license is Strong Copyleft.
              Strong Copyleft licenses enforce sharing, and you can use them when creating open source projects.

            kandi-Reuse Reuse

              backtrader releases are not available. You will need to build from source code and install.
              Deployable package is available in PyPI.
              Build file is available. You can build the component from source.
              It has 28498 lines of code, 2090 functions and 345 files.
              It has medium code complexity. Code complexity directly impacts maintainability of the code.

            Top functions reviewed by kandi - BETA

            kandi has reviewed backtrader and discovered the below as its top functions. This is intended to give you an instant insight into backtrader implemented functionality, and help decide if they suit your requirements.
            • Get historical minute data
            • Sends an IQ query message
            • Add data to the pandas dataframe
            • Send a command to the server
            • Handle move event
            • Update the canvas
            • Ignore an event
            • Called when an event is changed
            • Update the plot
            • Write dataframe to the InfluxDB database
            • Returns a list of tickers from the given file
            • Get a list of tickers from a file
            • Clear the background
            Get all kandi verified functions for this library.

            backtrader Key Features

            No Key Features are available at this moment for backtrader.

            backtrader Examples and Code Snippets

            README.rst
            Pythondot img1Lines of Code : 0dot img1License : Strong Copyleft (GPL-3.0)
            copy iconCopy
            [2018-11-16] After some testing it would seem that data downloads can be
            again relied upon over the web interface (or API ``v7``)
            The ticket system is (was, actually) more often than not abused to ask for
            advice about samples.
            from datetime import da  
            Installation
            Pythondot img2Lines of Code : 0dot img2License : Strong Copyleft (GPL-3.0)
            copy iconCopy
            If ``matplotlib`` is not installed and you wish to do some plotting
            pip install git+https://github.com/blampe/IbPy.git  
            backtrader - ibtest
            Pythondot img3Lines of Code : 418dot img3License : Non-SPDX (GNU General Public License v3.0)
            copy iconCopy
            #!/usr/bin/env python
            # -*- coding: utf-8; py-indent-offset:4 -*-
            ###############################################################################
            #
            # Copyright (C) 2015-2020 Daniel Rodriguez
            #
            # This program is free software: you can redistribute it   
            backtrader - oandatest
            Pythondot img4Lines of Code : 370dot img4License : Non-SPDX (GNU General Public License v3.0)
            copy iconCopy
            #!/usr/bin/env python
            # -*- coding: utf-8; py-indent-offset:4 -*-
            ###############################################################################
            #
            # Copyright (C) 2015-2020 Daniel Rodriguez
            #
            # This program is free software: you can redistribute it   
            backtrader - vctest
            Pythondot img5Lines of Code : 297dot img5License : Non-SPDX (GNU General Public License v3.0)
            copy iconCopy
            #!/usr/bin/env python
            # -*- coding: utf-8; py-indent-offset:4 -*-
            ###############################################################################
            #
            # Copyright (C) 2015-2020 Daniel Rodriguez
            #
            # This program is free software: you can redistribute it   
            AttributeError: 'NoneType' object has no attribute '_id'
            Pythondot img6Lines of Code : 44dot img6License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            # from ast import Constant
            from operator import imod
            import os, sys
            import config
            from binance.client import Client
            import backtrader
            import pandas as pd
            import datetime, time
            
            
            client = Client(config.Binanceapikey, config.BinancesecretKey
            How to make a Dockerfile and run it for multiple Python files?
            Pythondot img7Lines of Code : 22dot img7License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            FROM python:3.8
            
            RUN pip install numpy 
            RUN pip install python-binance
            RUN pip install pandas
            RUN pip install backtrader
            RUN pip install matplotlib
            RUN pip install websocket_client
            
            # TA-Lib
            RUN wget http://prdownloads.sourceforge.net/ta-l
            long and short strategy with macd indicator in Backtrader
            Pythondot img8Lines of Code : 30dot img8License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            if self.data.MACD_hist[0]>0:
                        if self.position.size<0 and self.data.MACD_hist[-1]<0  :
                                 self.close()
                                 self.log('CLOSE SHORT POSITION, %.2f' % self.dataclose[0])
                   
                      
            How I display the graph that return by backtrader in streamlit?
            Pythondot img9Lines of Code : 29dot img9License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
             matplotlib.use('Agg')
            
             figure = cerebro.plot()[0][0]
            
             st.pyplot(figure)
            
            import streamlit as st
            import backtrader as bt
            import matplotlib
            
            # Use a backend that does
            import custom multitimeframe data into backtrader
            Pythondot img10Lines of Code : 62dot img10License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            class firstStrategy(bt.Strategy):
            
                def __init__(self):
                    self.rsi1m = bt.indicators.RSI_SMA(self.data0.close, period=21) # rsi 1m
                    self.rsi5m = bt.indicators.RSI_SMA(self.data1.close, period=21) # rsi 5m
                    self.rsi15

            Community Discussions

            QUESTION

            Backtrader issue - optstrategy AttributeError: module 'collections' has no attribute 'Iterable'
            Asked 2022-Feb-22 at 01:11

            I'm trying to optimize my strategy on Backtrader with python but keeps getting this error and i can't find anything on the web showing why I'm getting it. My code is simple and loosely based on the quick start examples:

            ...

            ANSWER

            Answered 2022-Feb-19 at 20:05

            try change python 3.10 to python 3.8 version

            Source https://stackoverflow.com/questions/70782000

            QUESTION

            AttributeError: 'NoneType' object has no attribute '_id'
            Asked 2022-Jan-24 at 18:51

            I'm trying to make a trading bot and am using backtrader for the same. I have been trying to debug this issue but couldn't find a solution yet. The code is as shown below

            ...

            ANSWER

            Answered 2022-Jan-24 at 18:51

            You do not have a return in GetHistoricalData so it is sending None to adddata(). Maybe you need to return the dataframe? if not specify your intent.

            Source https://stackoverflow.com/questions/70838910

            QUESTION

            How to make a Dockerfile and run it for multiple Python files?
            Asked 2022-Jan-23 at 18:40

            So, I'm new to Docker. I want to use Ta-Lib and freqtrade libraries for a bot I want to build. The problem is that I don't want to build the Dockerfile over and over again. I have been copying my python code in the Dockerfile as shown below but as soon as I change the python code, I have to rebuild the Dockerfile because I changed the copied python script. Each build takes over 10 minutes to complete and this makes my life very difficult to test the bot. Is there a way that I build the Dockerfile with all the dependencies and requirements first and then simply run my script using the image made? So that I don't have to rebuild the entire thing just after adding one line of code? The Dockerfile is as shown below

            ...

            ANSWER

            Answered 2022-Jan-23 at 18:20

            First, you should copy your python file as late as possible.

            Source https://stackoverflow.com/questions/70825144

            QUESTION

            long and short strategy with macd indicator in Backtrader
            Asked 2022-Jan-07 at 12:45

            I just switched from Matlab to python and even newer to the backtrader library for backtestingtrading strategies. My questions might seem obvious.

            My problem seems similar to this : https://community.backtrader.com/topic/2857/wanted-exit-long-and-open-short-on-the-same-bar-and-vice-versa

            and this : https://community.backtrader.com/topic/2797/self-close-does-not-clear-position The code below is a simple MACD strategy. Here is the code :

            ...

            ANSWER

            Answered 2022-Jan-07 at 12:45

            In your code you are showing the following:

            Source https://stackoverflow.com/questions/70593041

            QUESTION

            How I display the graph that return by backtrader in streamlit?
            Asked 2021-Dec-07 at 14:40

            I try to do back testing on stock data using backtrading library in python. and I use this simple strategy

            ...

            ANSWER

            Answered 2021-Dec-07 at 14:40

            I'm not that familiar with backtrader so i took an example from their documentation on how to create a plot. The data used in the plot can be downloaded from their github repository.

            The solution contains the following steps:

            1. make sure we use a matplotlib backend that doesn't display the plots to the user because we want to display it in the Streamlit app and the plot() function of backtrader displays the plot. this can be done using:

            Source https://stackoverflow.com/questions/70253527

            QUESTION

            Open multiple positions in Backtrader
            Asked 2021-Nov-04 at 10:24

            Does someone know if it's possible to open multiple positions with only a single data feed? I am trying to do a second buy whilst in a position, which doesn't seem to be possible.

            Nobody seems to adress this issue. Does anyone have any experience with Backtrader and have any input?

            ...

            ANSWER

            Answered 2021-Nov-04 at 10:24

            If you are just trying to buy more stock to add to your position, then yes, you should be able to do this and if you cannot recheck your strategy code in next.

            If you are trying to track two separate positions of the same data...

            One cannot have two separate positions in the same data feed. You may trade additional positions if you like but they will be combined in Backtrader. Even if you use two strategies you will still have one combined broker.

            The reason for this is to simulate as near as possible real world conditions. If you have a brokerage account you most likely would have just one postion. (I know there are exceptions)

            One solution would be to track your trading manually in a dictionary trades that result from different signals/sub-strategies. It's a bit more tedious to develop but very doable.

            Source https://stackoverflow.com/questions/69791309

            QUESTION

            Getting data in Backtrader issue
            Asked 2021-Jul-20 at 02:52

            I am trying to write a backtesting strategy on Backtrader in Python, and below is the code that is giving me the error. I am using the latest version of backtrader as of July 2, 2021.

            ...

            ANSWER

            Answered 2021-Jul-04 at 17:55

            try to upgrade your backtrader. On 3rd of July there is a new release. I did update it but it still does not work. The problem is that it does not bt.feed.YahooFinance, take the data in the correct format. It is a new bug... I am waiting for them to fix it as well.

            Source https://stackoverflow.com/questions/68229953

            QUESTION

            How To Handle date format in GenericCSVData in Backtrade
            Asked 2021-Jul-09 at 19:35

            I was trying a simple example that prints datetime and close price from my csv. It's printing date correctly but for the time it's printing 23:59:59 only. Can you please help me to check what am I doing wrong?

            Here is my code:

            ...

            ANSWER

            Answered 2021-Jul-08 at 16:44

            You need to tell Backtrader the timeframe and compression, otherwise it has no idea what intervals it's working with.

            Source https://stackoverflow.com/questions/68268241

            QUESTION

            How to use SMA signal from one symbol to trade another symbol
            Asked 2021-May-25 at 12:51

            My idea is to track crude oil commodity prices and trade an ETF based on the movements of Oil.

            Logic is:

            • If price crosses SMA100 upwards trending for Oil, buy ETF
            • If price crosses SMA100 downwards trending for oil, sell ETF

            I use a generic SMA strategy but I cannot make it use the data from oil. Based on the plot it uses SMA 100 for the ETF.

            ...

            ANSWER

            Answered 2021-May-25 at 12:51

            QUESTION

            backtrader how to use analyzer to access data?
            Asked 2021-Apr-24 at 11:46

            I am very new to this. backtrader have a addwriter can write down some data,
            cerebro.addwriter(bt.WriterFile, csv=True, out='outputfiles3\{}cerebro.csv'.format(ticker)) but buy and sell price always not match to the execute price.

            so alternatively:

            i did cerebro.addanalyzer(WritingAnalyzer) before cerebro.run()

            so I am trying to build the csv file with 'datetime','open','close','cash','value','position size' but I don't know how to access those data. I can only point at current day close price with self.data[0]

            I don't know how to do it right. I hope someone might able to give me some directions.

            ...

            ANSWER

            Answered 2021-Apr-24 at 11:46

            You need to handle your analyzer a bit differently. You can literally grab data at every bar and then have it available to you at the end.

            Create a new analyzer, in my case I made:

            Source https://stackoverflow.com/questions/67239891

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network

            Vulnerabilities

            No vulnerabilities reported

            Install backtrader

            You can install using 'pip install backtrader' or download it from GitHub, PyPI.
            You can use backtrader like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.

            Support

            For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
            Install
          • PyPI

            pip install backtrader

          • CLONE
          • HTTPS

            https://github.com/mementum/backtrader.git

          • CLI

            gh repo clone mementum/backtrader

          • sshUrl

            git@github.com:mementum/backtrader.git

          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link