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/

    Candlestick Strategy: Indicator, Datafeed, or Parameter Issue?

    Indicators/Strategies/Analyzers
    1
    1
    73
    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.
    • GregFekas
      GregFekas last edited by

      Hello everyone,
      I am new to backtrader and first time poster here. I want to create a strategy based on candlestick patterns. My strategy looks for all the possible candlestick patterns, but i find and calculate this outside of the strategy class. I am not sure how to handle this issue. Is it a 'custom' indicator issue? Is it an extended datafeed issue (PandasData or something like that)? Can i pass a pandas.Series as a strategy parameter (i pretty sure i can't)? My best bet was this , but i my application asks from the user for inputs so overloading it with CSV files, well.. it did not seem right. Also i could not make it work.

      Here is my code:

      import pandas_ta as ta
      import backtrader as bt
      import yfinance as yf
      import pandas as pd
      from datetime import date,timedelta
      
      
      YESTERDAY=date.today()-timedelta(days=1) 
      ONEYEARAGO=YESTERDAY - timedelta(days=365)
      TICKER = 'tsla'
      
      
      df = pd.DataFrame()
      df = yf.download(TICKER,start=ONEYEARAGO , end=YESTERDAY)
      candle_df= df.ta.cdl_pattern(name='all') #look for candlestick patterns
      for col in candle_df.columns:
          if (candle_df[col]==0).all():             #if you dont find a pattern drop it
              candle_df.drop(col,axis=1,inplace=True)
      
      candle_df["sum"] = candle_df.sum(axis=1)    #for the patterns found create the sum of them
      df['sum'] = candle_df['sum']
      
      cerebro = bt.Cerebro()
      cerebro.adddata(df)# Here my code breaks
      #AttributeError: 'DataFrame' object has no attribute 'setenvironment'#
      
      cerebro.addstrategy(AllCandles,sum=df['sum']) # This will never work but...
      cerebro.run()
      cerebro.plot()
      
      #
      class AllCandles(bt.Strategy):
      
          params = dict(sum) #I am not sure to access the sum of candles
      
          def __init__(self):
              signal = self.p.sum[0]  #but i want achieve this
      
          def next(self):
              if not self.position:
                  if self.signal > 0:
                      self.buy()
              else:
                  if self.signal < 0:
                      self.sell()
      

      I looked around the forum i did not find anyone with a similar issue. I hope it's something cause on my luck of experience. Any help?

      P.S I am not sure how to tittle the topic either...

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors