How can I put my data in PyAlgoTrade?

I am trying to use PyAlogoTrade event profiler

However, I do not want to use data from yahoo! finance , I want to use my own, but I can’t understand how to understand CSV , it is in the format:

 Timestamp Low Open Close High BTC_vol USD_vol [8] [9] 2013-11-23 00 800 860 847.666666 886.876543 853.833333 6195.334452 5248330 0 2013-11-24 00 745 847.5 815.01 860 831.255 10785.94131 8680720 0 

Here is CSV

I want to do something like:

 def main(plot): instruments = ["AA", "AES", "AIG"] feed = yahoofinance.build_feed(instruments, 2008, 2009, ".") 

Then replace yahoofinance.build_feed(instruments, 2008, 2009, ".") my CSV

I tried:

 import csv with open( 'FinexBTCDaily.csv', 'rb' ) as csvfile: data = csv.reader( csvfile ) def main( plot ): feed = data 

But it throws an attribute error. Any ideas how to do this?

+5
source share
2 answers

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 } ) 
+2
source

I suggest creating your own Rowparser and Feed, which is much simpler than it sounds. yahoofeed

It also allows you to work with intraday data and clear data if necessary, for example, with a time stamp.

Another option, of course, is to parse your file and save it, so it looks like a yahoo feed. In your case, you have to adapt the columns and the timestamp.

+1
source

All Articles