For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'crossover'

  • Hi Guys, I am new ot BT. I am trying to replicate one of the strategies just for practice purposes. I get the above message for my crossover indicator. Can you please assis whith what may be going wrong? Many thanks!

    Pleas see below my code:

    import os, sys, argparse
    import pandas as pd
    import backtrader as bt
    import math
    class GoldenCross(bt.Strategy):
        params = dict(fast=50, slow=200, order_percentage = 0.95, ticker = "SPY")
        def __int__(self):
            self.fast_moving_average = bt.ind.SMA(
                self.feed.close, period, plotname = "50 day moving average"
            self.slow_moving_average = bt.ind.SMA(
                self.feed.close, period =self.params.slow, plotname = "200 day moving average"
            self.crossover = bt.ind.CrossOver(self.fast_moving_average, self.slow_moving_average)
        def next(self):
            if self.position.size ==0:
                if self.crossover >0:
                    amount_to_invest = (self.params.order_percentage *
                    self.size = math.floor(amount_to_invest/
                    print("Buy {} Share of {} at {}".format(self.size, self.params.ticker,[0]))
            if self.position.size >0:
                if self.crossover<0:
                    print("Sell {} Share of {} at {}".format(self.size, self.params.ticker,[0]))
    cerebro = bt.Cerebro()
    spy_prices = pd.read_csv("C:/Users/mitco/OneDrive/Documents/Courses/Python/Data/SPY.csv", index_col="Date", parse_dates=True)
    feed = bt.feeds.PandasData(dataname = spy_prices)

  • Should be

    def __init__(self):


    def __int__(self):

  • @ab_trader said in 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'crossover':

    def init(self):

    Hi ab_trader, thanks a lot for your help.

Log in to reply