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

Number of decimals in output



  • Hello. This is my first day with backtrader and I must say you have done a great work.

    I have a question about the output I get from running the following code.

    I read my data from sql database and create a pandas dataframe. I have set my own params for the datafeed.

    I will work with cryptocurrencies and they often have alot of decimals. My output only gives two decimals and I wonder if it is possible to change this to show say 6 decimals. Will this affect backtesting or is it only this output that is rounded?

    The code I run is this:

    # 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 custom datafeed 
        class CustomDataLoader(btfeeds.PandasData):
            lines = ('datetime','open','high','low','close','volume',)
            params = (('datetime', None),('open ',0),('high',1),('low',2),('close',3),('volume',-1))
        
        # Obtain a database connection to the MySQL instance
        connection = mdb.connect(host=db_host, database=db_name, user=db_user, password=db_pass)
      
        # Create dataframe
        df = psql.read_sql_query(sql, con=connection, index_col='price_date') 
        
        # print dataframe
        print(df)
        
        # Create a Data Feed
        data = CustomDataLoader(dataname=df)
    
        # Add the Data Feed to Cerebro
        cerebro.adddata(data)
    
        # Set our desired cash start
        cerebro.broker.setcash(100000.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())
    

    Here comes the output. I have printed the dataframe so you can se the original data:

                         open_price  high_price     ...      close_price      volume
    price_date                                      ...                             
    2016-12-29 12:00:00      0.0085      0.0085     ...           0.0085   383842354
    2016-12-29 12:15:00      0.0085      0.0085     ...           0.0084   871546155
    2016-12-29 12:30:00      0.0084      0.0084     ...           0.0083  1815853757
    2016-12-29 12:45:00      0.0083      0.0084     ...           0.0083  1267746603
    2016-12-29 13:00:00      0.0083      0.0084     ...           0.0084  1717625984
    
    [5 rows x 5 columns]
    Starting Portfolio Value: 100000.00
    2016-12-29, Close, 0.01
    2016-12-29, Close, 0.01
    2016-12-29, Close, 0.01
    2016-12-29, Close, 0.01
    2016-12-29, Close, 0.01
    Final Portfolio Value: 100000.00
    

    As you can see the values have been rounded to 0.01

    Regards
    Eric


  • administrators

    @eric said in Number of decimals in output:

        def next(self):
            # Simply log the closing price of the series from the reference
            self.log('Close, %.2f' % self.dataclose[0])
    


  • @backtrader
    Thank you for your quick respons. Problem solved!

    Eric


Log in to reply
 

});