How to get dataframe from backtrader sample code

HI Alpaca forum community,
I need sma1 and sma2 and date and closing price of a stock that is calculated in below code. I tried all my best to extract this data from the code below but somehow could not get the final data in a dataframe. Can someone help with this please? All I need is a dataframe with columns: DateTime, Closing Price, SMA1(which is 10 SMA) and SMA2(which is 30SMA) as seen in the picture. Any help is much appreciated.

Below is the code:

class SmaCross(bt.SignalStrategy):
    def __init__(self):
        sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30)
        crossover = bt.ind.CrossOver(sma1, sma2)
        self.signal_add(bt.SIGNAL_LONG, crossover)

cerebro = bt.Cerebro()
cerebro.addstrategy(SmaCross)
store = alpaca_backtrader_api.AlpacaStore(key_id=ALPACA_API_KEY,secret_key=ALPACA_SECRET_KEY,paper=ALPACA_PAPER)
DataFactory = store.getdata

data0 = DataFactory(
        dataname=symbol_to_check,
        timeframe=bt.TimeFrame.TFrame("Minutes"),
        fromdate=pd.Timestamp('2019-11-15'),compression =1,
#         todate=pd.Timestamp('2018-11-17'),
        historical=True)

cerebro.adddata(data0)

cerebro.run()
cerebro.plot()
![to_BT_forum|452x186](upload://26N8Zwr0egv1yYsq2CHp5csdPYI.png)

You might try this:

class SmaCross(bt.SignalStrategy):
    def __init__(self):
        self.sma1, self.sma2 = bt.ind.SMA(period=1), bt.ind.SMA(period=2)
        crossover = bt.ind.CrossOver(sma1, sma2)
        self.signal_add(bt.SIGNAL_LONG, crossover)
    
    def next(self):
    #DateTime, Closing Price, SMA1(which is 10 SMA) and SMA2(which is 30SMA) 
        d = {'datetime': [self.data.datetime[0]], 'close': [self.data.close[0]], 
             'sma_10d': [self.sma1[0]], 'sma_30d': [self.sma2[0]]}
        df = pd.DataFrame(data=d)

I didn’t actually try it but it seems logical :slight_smile:

Hi Chris, Thanks for the reply.
is return df not necessary?

def next(self):
#DateTime, Closing Price, SMA1(which is 10 SMA) and SMA2(which is 30SMA) 
    d = {'datetime': [self.data.datetime[0]], 'close': [self.data.close[0]], 
         'sma_10d': [self.sma1[0]], 'sma_30d': [self.sma2[0]]}
    df = pd.DataFrame(data=d)
    return df

And I see the below:
image

How do i actually return df in the jupyter cell?

got it it works with SmaCross.df =df

Thank you