Step A: Follow the PyAlgoTrade Document on the GenericBarFeed Class
In this link, see addBarsFromCSV() in CSV BarFeed in v0.16
In this link, see addBarsFromCSV() in CSV BarFeed in v0.17
Note
- The CSV file must have column names in the first row.
- Itβs good if the Adj Close column is empty.
- When working with several tools:
--- If all loaded tools are in the same time zone, the time zone parameter may not be specified.
--- If any of the loaded tools is in different time zones, you must set the time zone parameter. addBarsFromCSV( instrument, path, timezone = None )
Loads bars for this instrument from a CSV file. The instrument registers with the bar. Parameters:
(string) instrument - The identifier of the instrument.
(string) path - path to the CSV file.
(pytz) timezone - Time zone for localization of bars.
Check out pyalgotrade.marketsession .
Further:
A BarFeed loads bars from CSV files, which have the following format:
Date Time, Open, High, Low, Close, Volume, Adj Close 2013-01-01 13:59:00,13.51001,13.56,13.51,13.56789,273.88014126,13.51001
Step B: Preformats a Documented CSV File
Your CSV data will require a little common sense (before it can be used in PyAlgoTrade methods), however it is doable, and you can create a simple transformer either manually or using powerful numpy.genfromtxt() lambda- converters .
This sample code is intended to illustrate, to immediately see the powers of converters for your own transformations, since the CSV structure is different.
with open( getCsvFileNAME( ... ), "r" ) as aFH: numpy.genfromtxt( aFH, skip_header = 1, # Ref. pyalgotrade delimiter = ",", # vvvvvv # 2011.08.30,12:00,1791.20,1792.60,1787.60,1789.60,835 # 2011.08.30,13:00,1789.70,1794.30,1788.70,1792.60,550 # 2011.08.30,14:00,1792.70,1816.70,1790.20,1812.10,1222 # 2011.08.30,15:00,1812.20,1831.50,1811.90,1824.70,2373 # 2011.08.30,16:00,1824.80,1828.10,1813.70,1817.90,2215 converters = { 0: lambda aString: mPlotDATEs.date2num( datetime.datetime.strptime( aString, "%Y.%m.%d" ) ), #_______________________________________asFloat ( 1.0, +++ ) 1: lambda aString: ( ( int( aString[0:2] ) * 60 + int( aString[3:] ) ) / 60. / 24. ) # ( 15*60 + 00 ) / 60. / 24.__asFloat < 0.0, 1.0 ) # HH: :MM HH MM } )