Too many positional arguments for constructor call - CrossOver Function
-
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()
-
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 toself.slow_moving_average
instead ofself.p.fast
).You may check that backtrader is actually imported from the correct location (try to print
bt.__file__
) -
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..
-
What environment/IDE are you using ? Could you run your code using plain python from command line and see if it works (it should) ?
-
@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... :/
-
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.
-
@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?
-
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.
-
@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.
-
@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