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

I cannot logging and print out the datetime



  • Hi all,

    I have a problem of printing out the log for trade

    I have the pandas dataframe as figure below. With the Index define by using pd.to_datetime(). Its dtype = datetime64[ns]
    3a27220a-0b0a-4858-80a2-a6e25bf8675a-image.png

    df.index
    >> DatetimeIndex(['2007-01-01 20:00:00', '2007-01-01 20:01:00',
                   '2007-01-01 20:02:00', '2007-01-01 20:03:00',
                   '2007-01-01 20:04:00', '2007-01-01 20:05:00',
                   ...
                   '2007-01-02 19:35:00', '2007-01-02 19:36:00',
                   '2007-01-02 19:37:00', '2007-01-02 19:38:00'],
                  dtype='datetime64[ns]', name='Date', length=1000, freq=None)
    

    Then, I defined the data to push into backtrader

    data = bt.feeds.PandasData(dataname=df)
    class strategy(bt.Strategy):
         def log(self, txt, dt=None):
                 dt = dt or self.data.datetime[0]
                 print('{} {}'.format(dt, txt))
    

    but when running cerebro, the datetime is not as what desired

    >>
    732677.8520833333 BUY at 118.66
    732677.8527777778 BUY EXECUTED at 118.67, cost 118.67, com 0.0011867000000000002
    732677.8784722222 SELL at 118.7
    732677.8791666667 SELL EXECUTED at price 118.71, cost 118.67, com 0.0011871
    732677.8791666667 OPERATION PROFIT, GROSS 0.03999999999999204, NET0.03762619999999204
    732677.8819444445 BUY at 118.73
    732677.8826388889 BUY EXECUTED at 118.73, cost 118.73, com 0.0011873
    732677.9 SELL at 118.73
    732677.901388889 SELL EXECUTED at price 118.75, cost 118.73, com 0.0011875000000000002
    

    How to fix this problem?

    Thank all!


  • administrators

    Just like in pandas, the datetime is encoded. You are printing the encoded value.

    The Docs - Quickstart Guide - https://www.backtrader.com/docu/quickstart/quickstart/ shows how to print the date in the log method (which you seem to have wrongly copied). Change date to to datetime and it will work.



  • @backtrader Thank you. I have other issue when cannot download the graph which is ploted


Log in to reply
 

});