Unable to feed my custom DataFrame to backtrader
-
Hi, I have a custom Pandas DataFrame and I want to feed this data that has different column names to backtrader. For do this, I wrote my code according to documentation:
-
@ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:
Hi, I have a custom Pandas DataFrame and I want to feed this data that has different column names to backtrader. For do this, I wrote my code according to documentation:
from __future__ import (absolute_import, division, print_function, unicode_literals) import datetime # For datetime objects # Import the backtrader platform import backtrader as bt # data class class PandasData(bt.feeds.DataBase): ''' The ``dataname`` parameter inherited from ``feed.DataBase`` is the pandas DataFrame ''' params = ( ('datetime', None), ('open', -1), ('high', -1), ('low', -1), ('close', -1), ('Volume', -1), ('pos_side', -1), ('entry', -1), ('exit', -1), ('pos_result', -1), ('ema1', 15), ('ema2', 16), ('ema3', 17), ) # Create a Stratey class TestStrategy(bt.Strategy): def log(self, txt, dt=None): ''' Logging function for this strategy''' dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.2f' % self.dataclose[0]) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() # Add a strategy cerebro.addstrategy(TestStrategy) # Create a Data Feed data_bt = PandasData(dataname=data) # Add the Data Feed to Cerebro cerebro.adddata(data_bt) # Set our desired cash start cerebro.broker.setcash(1000.0) # Print out the starting conditions print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Run over everything cerebro.run() # Print out the final result print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
-
@ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:
@ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:
Hi, I have a custom Pandas DataFrame and I want to feed this data that has different column names to backtrader. For do this, I wrote my code according to documentation:
from __future__ import (absolute_import, division, print_function, unicode_literals) import datetime # For datetime objects # Import the backtrader platform import backtrader as bt # data class class PandasData(bt.feeds.DataBase): ''' The ``dataname`` parameter inherited from ``feed.DataBase`` is the pandas DataFrame ''' params = ( ('datetime', None), ('open', -1), ('high', -1), ('low', -1), ('close', -1), ('Volume', -1), ('pos_side', -1), ('entry', -1), ('exit', -1), ('pos_result', -1), ('ema1', 15), ('ema2', 16), ('ema3', 17), ) # Create a Stratey class TestStrategy(bt.Strategy): def log(self, txt, dt=None): ''' Logging function for this strategy''' dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.2f' % self.dataclose[0]) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() # Add a strategy cerebro.addstrategy(TestStrategy) # Create a Data Feed data_bt = PandasData(dataname=data) # Add the Data Feed to Cerebro cerebro.adddata(data_bt) # Set our desired cash start cerebro.broker.setcash(1000.0) # Print out the starting conditions print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Run over everything cerebro.run() # Print out the final result print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
But, this code doesn't work and the length of the feed data (data_bt) will be 0!
I tried different methods from different sites for do this but I couldn't solve the problem, Please help me to feed my data to backtrader. Thank you!
-
@ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:
@ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:
@ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:
Hi, I have a custom Pandas DataFrame and I want to feed this data that has different column names to backtrader. For do this, I wrote my code according to documentation:
from __future__ import (absolute_import, division, print_function, unicode_literals) import datetime # For datetime objects # Import the backtrader platform import backtrader as bt # data class class PandasData(bt.feeds.DataBase): ''' The ``dataname`` parameter inherited from ``feed.DataBase`` is the pandas DataFrame ''' params = ( ('datetime', None), ('open', -1), ('high', -1), ('low', -1), ('close', -1), ('Volume', -1), ('pos_side', -1), ('entry', -1), ('exit', -1), ('pos_result', -1), ('ema1', 15), ('ema2', 16), ('ema3', 17), ) # Create a Stratey class TestStrategy(bt.Strategy): def log(self, txt, dt=None): ''' Logging function for this strategy''' dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.2f' % self.dataclose[0]) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() # Add a strategy cerebro.addstrategy(TestStrategy) # Create a Data Feed data_bt = PandasData(dataname=data) # Add the Data Feed to Cerebro cerebro.adddata(data_bt) # Set our desired cash start cerebro.broker.setcash(1000.0) # Print out the starting conditions print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Run over everything cerebro.run() # Print out the final result print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
But, this code doesn't work and the length of the feed data (data_bt) will be 0!
I tried different methods from different sites for do this but I couldn't solve the problem, Please help me to feed my data to backtrader. Thank you!
-
@ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:
Hi, I have a custom Pandas DataFrame and I want to feed this data that has different column names to backtrader. For do this, I wrote my code according to documentation:
this is the output of the above code. (I add the print sections to better represent the problem)