Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. Lammy
    3. Topics
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    L
    • Profile
    • Following 0
    • Followers 0
    • Topics 2
    • Posts 6
    • Best 0
    • Groups 0

    Topics created by Lammy

    • L

      Issue with BuySell obverver plot from cerebro.plot(). Disappearing with few data points
      General Code/Help • • Lammy

      4
      0
      Votes
      4
      Posts
      988
      Views

      emmepra

      Hey @Lammy have you solved this in the end?

      I'm trying to handle this arrows misplacement problem with my tick data (single-tick value).
      The only solution that I've found is to call:

      cerebro = bt.Cerebro(stdstats=False) cerebro.addobserver(bt.Observers.BuySell)

      This way the arrows will be printed above/under the priceline and maybe, as in my case, too close to it. But I think is better to have it too close than not having at all!

      Errata Corrige: this solution will lead to many problems when plotting more than one datas, I'm currently looking for another option to solve it!

    • L

      Dynamically set params and lines attribute of class PandasData
      General Code/Help • • Lammy

      8
      0
      Votes
      8
      Posts
      1309
      Views

      J

      Here is an example of what I did:

      Generating the lines and params tuples by passing in list(df.columns)

      def get_extra_df_columns(self, df_columns: list[str] = []) -> list[tuple]: lines: tuple = () params: tuple(tuple) = () for column in df_columns: if column not in ['symbol', 'open', 'high', 'low', 'close', 'volume']: lines = lines + (column,) params = params + ( (column, -1), ) return [lines, params]

      Creating the subclass by parsing the df to be parsed then returning it with the subclass attributes set dynamically:

      def _create_data_feeder(self, database: Database, df: pd.DataFrame ): lines, params = database.get_extra_df_columns(df_columns=list(df.columns) ) return type('PandasDataFeed', (bt.feeds.PandasData, ), {'lines':lines, 'params':params} )

      Adding the dataframe to cerebro via the created data_feeder subclass

      self.data_feeder = self._create_data_feeder(database=database, df=df) self.cerebro.adddata(self.data_feeder(dataname=df, name=symbol ))

      The extra columns in the df are vwap and supertrend and are accessible without hardcoding the lines and params

      class TestStrategy(bt.Strategy): def __init__(self): for i, d in enumerate(self.datas): bt.ind.SMA(d.supertrend, period=1, subplot=False, plotname='supertrend') bt.ind.SMA(d.vwap, period=1, subplot=False, plotname='vwap')
    • 1 / 1