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

dt = date(int(dttxt[0:4]), int(dttxt[5:7]), int(dttxt[8:10]))



  • I want to convert the date format from this %Y-%m-%d to
    %m-%d-%Y . Please mention how.



  • Error dt = date(int(dttxt[0:4]), int(dttxt[5:7]), int(dttxt[8:10])). The date format of this %Y-%m-%d but i need %m-%d-%Y . Please mention how.

    CSV Data,

    11/3/1994,2.7,2.75,2.675,2.71,89999.99598
    11/7/1994,2.67,2.67,2.651,2.651,19999.99911
    11/8/1994,2.75,2.775,2.75,2.775,69999.99687
    11/10/1994,2.8,2.8,2.8,2.8,9999.999553



  • @shoaibmalek21
    Have a look here for date formats.

    And here for backtrader instructions.

    You could try the following:

    data = btfeeds.GenericCSVData(
        dataname='mydata.csv',
    
        fromdate=datetime.datetime(2000, 1, 1),
        todate=datetime.datetime(2000, 12, 31),
    
        nullvalue=0.0,
    
        dtformat=('%-m-%-d-%Y'),
     
        datetime=0,
        time=1,
        high=2,
        low=3,
        open=4,
        close=5,
        volume=6,
        openinterest=-1
    )
    


  • Thanks a lot @run-out , it's helpful :)

    data_string[found.end():])
    ValueError: unconverted data remains: AM

    I want to use both Date formats. Anyone please help me.

    CSV Data,

    11/3/1994 00:00:00,2.7,2.75,2.675,2.71,89999.99598
    11/7/1994 12:00:00 AM,2.67,2.67,2.651,2.651,19999.99911



  • Both date formats in the same dataset and column?



  • Yes, @run-out



  • IMHO it is easier to just pre-process the csv file to bring all the dates to the same format and only then feed it to backtrader.



  • If I understand correctly, you are trying to write the class for your own data feed. Probably this piece:

            dttxt = linetokens[next(i)]
            dt = date(int(dttxt[0:4]), int(dttxt[5:7]), int(dttxt[8:10]))
    

    The following might work in your case for daily only data (from your 1st post)

    dtlst = dttxt.split('/')
    dt = date(int(dtlst[2]), int(dtlst[0]), int(dtlst[1]))
    


  • Thanks, @ab_trader

    Now, I want to use both Date format. Both date formats in the same dataset and column. It's able to import only one format Please help me how to import both.

    Error: data_string[found.end():])
    ValueError: unconverted data remains: AM

    CSV Data,

    11/3/1994 00:00:00,2.7,2.75,2.675,2.71,89999.99598
    11/7/1994 12:00:00 AM,2.67,2.67,2.651,2.651,19999.99911
    11/8/1994 00:00:00,2.7,2.75,2.675,2.71,89999.99598
    11/9/1994 12:00:00 AM,2.67,2.67,2.651,2.651,19999.99911



  • I am not sure what do you mean saying both. If you have both daily and intraday bars in the same file, than split them to have two separate files.

    Take a look on how the csv data feeds parsed in the original bt package, this will be the best approach I think.



  • @shoaibmalek21 said in dt = date(int(dttxt[0:4]), int(dttxt[5:7]), int(dttxt[8:10])):

    Now, I want to use both Date format.

    You are going to have to pre-process this. I recommend using pandas for the job, because you will end up with a dataframe you can just import into backtrader.

    First step, let's import the csv file. I have added header titles. I've also added a 'PM' for thoroughness.

    import pandas as pd
    
    df = pd.read_csv("mix_index.csv")
    print(df)
                   datetime   open   high   low   close    volume
    0     11/3/1994 00:00:00  2.700  2.750 2.675   2.710 89999.996
    1  11/7/1994 12:00:00 AM  2.670  2.670 2.651   2.651 19999.999
    2     11/8/1994 00:00:00  2.700  2.750 2.675   2.710 89999.996
    3  11/9/1994 12:00:00 PM  2.670  2.670 2.651   2.651 19999.999
    

    Next, we are going to select the subrows with 'AM' or 'PM' and use pandas to_datetime to convert these to usable dates and times.

    # Select the AM/PM rows and convert to datetime into a new column 'newdt'
    
    df.loc[df["datetime"].str.contains("|".join(["AM", "PM"])), "newdt"] = pd.to_datetime(
        df.loc[df["datetime"].str.contains("|".join(["AM", "PM"])), "datetime"]
    )
    
    print(df)
     	datetime 	        open 	high 	low 	close 	volume 	        newdt
    0 	11/3/1994 00:00:00 	2.700 	2.750 	2.675 	2.710 	89999.996 	NaT
    1 	11/7/1994 12:00:00 AM 	2.670 	2.670 	2.651 	2.651 	19999.999 	1994-11-07 00:00:00
    2 	11/8/1994 00:00:00 	2.700 	2.750 	2.675 	2.710 	89999.996 	NaT
    3 	11/9/1994 12:00:00 PM 	2.670 	2.670 	2.651 	2.651 	19999.999 	1994-11-09 12:00:00
    

    Next with the other rows.

    # Use '~' to select the non-AM/PM rows and convert to datetime into a new column 'newdt'
    
    df.loc[~df["datetime"].str.contains("|".join(["AM", "PM"])), "newdt"] = pd.to_datetime(
        df.loc[~df["datetime"].str.contains("|".join(["AM", "PM"])), "datetime"]
    )
    
    print(df)
     	datetime 	        open 	high 	low 	close 	volume 	        newdt
    0 	11/3/1994 00:00:00 	2.700 	2.750 	2.675 	2.710 	89999.996 	1994-11-03 00:00:00
    1 	11/7/1994 12:00:00 AM 	2.670 	2.670 	2.651 	2.651 	19999.999 	1994-11-07 00:00:00
    2 	11/8/1994 00:00:00 	2.700 	2.750 	2.675 	2.710 	89999.996 	1994-11-08 00:00:00
    3 	11/9/1994 12:00:00 PM 	2.670 	2.670 	2.651 	2.651 	19999.999 	1994-11-09 12:00:00
    

    Then clean up.

    # Clean up, delete the original offending 'datetime' column, rename the new column to
    # the original datetime name, set the datetime to index and sort for good measure.
    del df["datetime"]
    df = df.rename(columns={"newdt": "datetime"})
    df = df.set_index("datetime").sort_index()
    print(df)
                          open   high   low   close    volume
    datetime                                                 
    1994-11-03 00:00:00  2.700  2.750 2.675   2.710 89999.996
    1994-11-07 00:00:00  2.670  2.670 2.651   2.651 19999.999
    1994-11-08 00:00:00  2.700  2.750 2.675   2.710 89999.996
    1994-11-09 12:00:00  2.670  2.670 2.651   2.651 19999.999
    

    You can now import this into backtrader using the pandas data feed module.

    # Pass it to the backtrader datafeed and add it to the cerebro
     data = bt.feeds.PandasData(dataname=df)
     cerebro.adddata(data)
    


  • Your help & support is greatly appreciated. Thanks a bunch! @run-out


Log in to reply
 

});