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/

    Help with plotting issue

    General Code/Help
    2
    2
    572
    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.
    • E
      eric last edited by

      I have created a custom datafeed. The dataframe I use have the columns:
      datetime, open, high, low, close, volume

      When I run the script with a strategy to plot simple moving average with my custom data feed I will get a chart with ohlc bars but no sma is plotted.

      When I use the inbuilt pandas data feed I get ohlc bars and sma plotted.

      I guess I have missed something. Can anyone see why sma is not plotted when using my custom data feed?

      Here are som code.

      The strategy:

      class St(bt.Strategy):
          def __init__(self):
              self.sma = bt.indicators.SimpleMovingAverage(self.data)
      

      My custom data feed:

      class CustomDataLoader(btfeeds.PandasData):
          lines = ('datetime', 'open', 'high', 'low', 'close', 'volume',)
          params = (('datetime', None), ('open', 0), ('high', 1), ('low', 2), ('close', 3), ('volume', 4),)
      

      How I load the data:

      data = CustomDataLoader(dataname=df)		# Plots only ohlc bars but no sma
      #data = bt.feeds.PandasData(dataname=df)	# Plots both ohlc bars and sma
      

      How I plot the result:

      # Plot the result
      cerebro.plot(style='candlestick')
      

      Any help would be appreciated.
      Thanks

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

        @eric said in Help with plotting issue:

        class CustomDataLoader(btfeeds.PandasData):
            lines = ('datetime', 'open', 'high', 'low', 'close', 'volume',)
        

        You are adding this lines to the existing lines in PandasData possible overriding them with the wrong order (I couldn't tell, it never occurred to me to repeat the same lines declaration overriding the existing lines)

        You only need to define the parameters to given them fixed known (to you) values and only new lines.

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