feed – Basic feeds

Feeds are time series data providing abstractions. When these are included in the event dispatch loop, they emit an event as new data is available. Feeds are also responsible for updating the pyalgotrade.dataseries.DataSeries associated with each piece of data that the feed provides.

This package has basic feeds. For bar feeds refer to the barfeed – Bar providers section.

class pyalgotrade.feed.BaseFeed(maxLen)

Bases: pyalgotrade.observer.Subject

Base class for feeds.

Parameters:maxLen (int.) – The maximum number of values that each pyalgotrade.dataseries.DataSeries will hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end.

Note

This is a base class and should not be used directly.

__contains__(key)

Returns True if a pyalgotrade.dataseries.DataSeries for the given key is available.

__getitem__(key)

Returns the pyalgotrade.dataseries.DataSeries for a given key.

getNewValuesEvent()

Returns the event that will be emitted when new values are available. To subscribe you need to pass in a callable object that receives two parameters:

  1. A datetime.datetime instance.
  2. The new value.

CSV support

class pyalgotrade.feed.csvfeed.Feed(dateTimeColumn, dateTimeFormat, converter=None, delimiter=', ', timezone=None, maxLen=None)

Bases: pyalgotrade.feed.csvfeed.BaseFeed

A feed that loads values from CSV formatted files.

Parameters:
  • dateTimeColumn (string.) – The name of the column that has the datetime information.
  • dateTimeFormat (string.) – The datetime format. datetime.datetime.strptime will be used to parse the column.
  • converter (function.) – A function with two parameters (column name and value) used to convert the string value to something else. The default coverter will try to convert the value to a float. If that fails the original string is returned.
  • delimiter (string.) – The string used to separate values.
  • timezone (A pytz timezone.) – The timezone to use to localize datetimes. Check pyalgotrade.marketsession.
  • maxLen (int.) – The maximum number of values that each pyalgotrade.dataseries.DataSeries will hold. Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the opposite end. If None then dataseries.DEFAULT_MAX_LEN is used.
addValuesFromCSV(path)

Loads values from a file.

Parameters:path (string.) – The path to the CSV file.

CSV support Example

A file with the following format

Date,USD,GBP,EUR
2013-09-29,1333.0,831.203,986.75
2013-09-22,1349.25,842.755,997.671
2013-09-15,1318.5,831.546,993.969
2013-09-08,1387.0,886.885,1052.911
.
.
.

can be loaded like this:

from pyalgotrade.feed import csvfeed

feed = csvfeed.Feed("Date", "%Y-%m-%d")
feed.addValuesFromCSV("quandl_gold_2.csv")
for dateTime, value in feed:
    print dateTime, value

and the output should look like this:

1968-04-07 00:00:00 {'USD': 37.0, 'GBP': 15.3875, 'EUR': ''}
1968-04-14 00:00:00 {'USD': 38.0, 'GBP': 15.8208, 'EUR': ''}
1968-04-21 00:00:00 {'USD': 37.65, 'GBP': 15.6833, 'EUR': ''}
1968-04-28 00:00:00 {'USD': 38.65, 'GBP': 16.1271, 'EUR': ''}
1968-05-05 00:00:00 {'USD': 39.1, 'GBP': 16.3188, 'EUR': ''}
1968-05-12 00:00:00 {'USD': 39.6, 'GBP': 16.5625, 'EUR': ''}
1968-05-19 00:00:00 {'USD': 41.5, 'GBP': 17.3958, 'EUR': ''}
1968-05-26 00:00:00 {'USD': 41.75, 'GBP': 17.5104, 'EUR': ''}
1968-06-02 00:00:00 {'USD': 41.95, 'GBP': 17.6, 'EUR': ''}
1968-06-09 00:00:00 {'USD': 41.25, 'GBP': 17.3042, 'EUR': ''}
.
.
.
2013-07-28 00:00:00 {'USD': 1331.0, 'GBP': 864.23, 'EUR': 1001.505}
2013-08-04 00:00:00 {'USD': 1309.25, 'GBP': 858.637, 'EUR': 986.921}
2013-08-11 00:00:00 {'USD': 1309.0, 'GBP': 843.156, 'EUR': 979.79}
2013-08-18 00:00:00 {'USD': 1369.25, 'GBP': 875.424, 'EUR': 1024.964}
2013-08-25 00:00:00 {'USD': 1377.5, 'GBP': 885.738, 'EUR': 1030.6}
2013-09-01 00:00:00 {'USD': 1394.75, 'GBP': 901.292, 'EUR': 1055.749}
2013-09-08 00:00:00 {'USD': 1387.0, 'GBP': 886.885, 'EUR': 1052.911}
2013-09-15 00:00:00 {'USD': 1318.5, 'GBP': 831.546, 'EUR': 993.969}
2013-09-22 00:00:00 {'USD': 1349.25, 'GBP': 842.755, 'EUR': 997.671}
2013-09-29 00:00:00 {'USD': 1333.0, 'GBP': 831.203, 'EUR': 986.75}

Table Of Contents

Previous topic

dataseries – Basic dataseries classes

Next topic

barfeed – Bar providers

This Page