Backtrader How to import an indicator from another file

Custom indicator import

As it is, I wrote a custom indicator class in the same script for explanation. However, it is more convenient to write only the custom indicator class in a separate py file and use it separately from the strategy script.

If the file name of the custom indicator is ** customindicator.py **, it will be at the beginning of the strategy.

ʻImport customindicator as cind` (any descriptive name)

In the init method of the strategy class

self.myind1 = cind.MyStochastics1(self.data)

Write.

Place customindicator.py in the same directory as the strategy script. If you are using Jupyter notebook and keep the default settings, it is C: \ Users \ username \.

customindicator.py



import backtrader as bt


class MyStochastic1(bt.Indicator):
    
    lines = ('k', 'd', ) 
    params = (
        ('k_period', 14), 
        ('d_period', 3),  #Comma at the end of the tuple(、)Put in
    )
    
    
    def __init__(self):
        highest = bt.ind.Highest(self.data, period=self.p.k_period)
        lowest = bt.ind.Lowest(self.data, period=self.p.k_period)        
       
        self.lines.k = k = (self.data - lowest) / (highest - lowest)    
        self.lines.d = bt.ind.SMA(k, period=self.p.d_period)

sto2.py


%matplotlib notebook
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import datetime  
import os.path  
import sys 
import backtrader as bt 
import customindicator as cind #

class TestStrategy(bt.Strategy):
    def __init__(self):
        
        self.myind1 = cind.MyStochastics1(self.data)   
        
        
if __name__ == '__main__':
    cerebro = bt.Cerebro()
    cerebro.addstrategy(TestStrategy)

    datapath = 'C:\\Users\\XXXX\\orcl-1995-2014.txt'

    # Create a Data Feed
    data = bt.feeds.YahooFinanceCSVData(
        dataname=datapath,        
        fromdate=datetime.datetime(2000, 1, 1),
        todate=datetime.datetime(2000, 12, 31),
        reverse=False)

    cerebro.adddata(data)
    cerebro.run(stdstats=False)
    cerebro.plot(style='candle')

Recommended Posts

Backtrader How to import an indicator from another file
How to convert Python to an exe file
How to make a Backtrader custom indicator
Import Excel file from Python (register to DB)
How to use import
How to create an article from the command line
How to turn a .py file into an .exe file
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
[Python] Another way to import
How to import a file anywhere you like in Python
How to generate a public key from an SSH private key
[Python Kivy] How to create an exe file with pyinstaller
[Python] How to output a pandas table to an excel file
How to read an Excel file (.xlsx) with Pandas [Python]
How to create an ISO file (CD image) on Linux
How to paste a CSV file into an Excel file using Pandas
How to use SWIG from waf
How to create an email user
[Python] How to import the library
[Django] How to read variables / constants defined in an external file
How to launch Explorer from WSL
How to resolve the error from toimage (from PIL.Image import fromarray as toimage)
How to access wikipedia from python
Python to switch from another language
How to get a job as an engineer from your 30s
How to convert from .mgz to .nii.gz
How to create a config file
How to read environment variables from .env file in PyCharm (on Mac)
How to build an application from the cloud using the Django web framework
Use BeautifulSoup to extract a link containing a string from an HTML file
[Python] How to convert db file to csv
[Note] Read a file from another directory
How to easily convert format from Markdown
Script to generate directory from json file
python3 How to install an external module
How to update Google Sheets from Python
How to create an NVIDIA Docker environment
How to utilize multi-core from multiple languages
[Python] How to use import sys sys.argv
How to access RDS from Lambda (python)
How to operate Linux from the console
How to create a repository from media
How to access the Datastore from the outside
How to batch insert from CSV file to Tableau hyper file with PostgreSQL-like COPY command
[Python] How to easily drop a child process started by multiprocess from another process
I made a package to create an executable file from Hy source code
How to read a CSV file with Python 2/3
How to disguise a ZIP file as a PNG file
How to make an embedded Linux device driver (11)
How to make WTForms TextArea correspond to file drop
How to make an embedded Linux device driver (8)
How to open a web browser from python
How to make an embedded Linux device driver (1)
How to make an embedded Linux device driver (4)
How to create a function object from a string
Study from Python Hour7: How to use classes
Summary of how to import files in Python 3
How to get results from id in Celery
From file to graph drawing in Python. Elementary elementary
How to create a JSON file in Python
How to make an embedded Linux device driver (7)