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/

    Too many positional arguments for constructor call - CrossOver Function

    Indicators/Strategies/Analyzers
    2
    10
    1552
    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.
    • C
      chhrissi2909 last edited by

      Hey Guys,

      I am new to Backtrader and want to code my first simple strategy. I decided to code the GoldenCross strategy. Now my code should be alright, but the compiler gives me an error called: "Too many positional arguments for constructor call". The error is in line 19. of the strategy.py..

      (strategy.py):

      import math
      import backtrader as bt
      
      class GoldenCross(bt.Strategy):
          params = (("fast", 50), 
                      ("slow", 200), 
                      ("order percentage", 0.95), 
                      ("ticker", "AAPL"))
          
          def __init__(self):
              self.fast_moving_average = bt.indicators.SMA(
                 self.data.close, 
                 period=self.p.fast,
                 plotname="50 day moving average")
              self.slow_moving_average = bt.indicators.SMA(
                 self.data.close, 
                 period=self.p.fast,
                 plotname="200 day moving average")
              self.crossover = bt.indicators.CrossOver(self.fast_moving_average, self.slow_moving_average)
          
          def next(self):
              pass
      

      (run.py):

      import os, sys, argparse
      import pandas as pd 
      import backtrader as bt 
      from Strategien.GoldenCross import GoldenCross
      
      cerebro = bt.Cerebro()
      cerebro.broker.setcash(100000)
      
      symbol = "AAPL"
      stock_prices = pd.read_csv("/Users/christianbrendel/Desktop/allgemein/Visual Studio/Stock Data/S&P500 Aktien 1H/" + symbol + ".csv", index_col="date", parse_dates=True)
      
      feed = bt.feeds.PandasData(dataname=stock_prices)
      cerebro.adddata(feed)
      
      cerebro.addstrategy(GoldenCross)
      cerebro.run()
      cerebro.plot()
      
      
      1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        I've tried to run your code and it runs ok on my machine, almost as is (the only changes I did is to change the path to the csv file and use the self.slow as a param to self.slow_moving_average instead of self.p.fast).

        Figure_0.png

        You may check that backtrader is actually imported from the correct location (try to print bt.__file__)

        C 1 Reply Last reply Reply Quote 0
        • C
          chhrissi2909 @vladisld last edited by

          hey @vladisld thanks for you help. You are absolutely right with the parameter. But My code does not run anyway.. I tried to check the print you told me. The result is: "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader/init.py" wich sounds right for me.. :/ Do you have a mother tip why it works for you but not for me..

          1 Reply Last reply Reply Quote 0
          • vladisld
            vladisld last edited by

            What environment/IDE are you using ? Could you run your code using plain python from command line and see if it works (it should) ?

            C 2 Replies Last reply Reply Quote 0
            • C
              chhrissi2909 @vladisld last edited by

              @vladisld said in Too many positional arguments for constructor call - CrossOver Function:

              run your code using plain python from command line

              Hey, sorry I don't know how to do that... Why is it not just running... :/

              1 Reply Last reply Reply Quote 0
              • C
                chhrissi2909 @vladisld last edited by

                @vladisld Bildschirmfoto 2020-06-09 um 21.09.36.png

                here you can see my whole code and the two scripts. With the GoldenCross.py script I want to write a strategy and with the run.py script I want to run the strategy.

                C 1 Reply Last reply Reply Quote 0
                • C
                  chhrissi2909 @chhrissi2909 last edited by

                  @chhrissi2909 I use a MacBook and on my mamboed there is installed python 2.7 and python 3.8.. Is it possible, that the python3.8 version is not compatible with backorder?

                  vladisld 1 Reply Last reply Reply Quote 0
                  • vladisld
                    vladisld last edited by

                    Ah, it is not a compiler, it is a python syntax checker error. It is happening because all those syntax checkers are not smart enough to understand dynamic typing and metaclasses used in backtrader. Do not worry about it, just run you code - it will work.

                    C 1 Reply Last reply Reply Quote 1
                    • vladisld
                      vladisld @chhrissi2909 last edited by

                      @chhrissi2909 said in Too many positional arguments for constructor call - CrossOver Function:

                      Is it possible, that the python3.8 version is not compatible with backorder

                      It is completely compatible. Backtrader is routinely tested with python 3.8, 3.7, 3.6.

                      1 Reply Last reply Reply Quote 1
                      • C
                        chhrissi2909 @vladisld last edited by

                        @vladisld My code did not run.. the error was printed and then nothing happened.. But Now I put a self. in front of the line and now there is no error anymore. Now it looks like:

                        self.crossover = self.bt.indicators.crossover(self.fast_moving_average, self.slow_moving_average)
                        

                        But now there is a mother error in the run.py script. it is called "Unexpected keyword argument 'dataname' in constructor call" the error is in this in the 14. line of the run.py code. there:

                        feed = bt.feeds.PandasData(dataname=stock_prices)
                        

                        do you understand this problem? :0

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