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

'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 =self.params.fast, 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.broker.cash)
                    self.size = math.floor(amount_to_invest/self.data.close)
    
                    print("Buy {} Share of {} at {}".format(self.size, self.params.ticker, self.data.close[0]))
    
                    self.buy(size=self.size)
    
            if self.position.size >0:
                if self.crossover<0:
                    print("Sell {} Share of {} at {}".format(self.size, self.params.ticker, self.data.close[0]))
                    self.close()
    
    cerebro = bt.Cerebro()
    
    cerebro.broker.add_cash(100000)
    
    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)
    
    cerebro.adddata(feed)
    
    cerebro.addstrategy(GoldenCross)
    
    cerebro.run()
    
    cerebro.plot()
    


  • Should be

    def __init__(self):
    

    Not

    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
 

});