Collection Manipulation

getindex

TFire uses a syntax familiar from Python and Julia among others for accessing parts of a Collection.

Say we have the following Collection,

TFire> collection
-| Collection |- (Continuous)
Tickers: 2, MMM AOS
Samples: 8562

The Collection may be accessed by one of the tickers in the Collection or a list of such tickers. This creates a Collection with only those tickers.

TFire> collection["MMM"]
-| Collection |- (Continuous)
Ticker: MMM
Samples: 3352

This is short for getindex(collection, "MMM") and may thus hereafter be referred to as a getindex method.

Examples of some of the ways of accessing parts of a Collection are presented below. All of the getindex methods presented below creates a new Collection. If instead the Collection is to be modified inplace, the equivalent keepat! method may be used.

The Collection may be accessed by one date/datetime in the collection, or again, a list of such dates/datetimes. This creates a Collection where only the samples with an evaluation date/datetime equivalent to those requested are kept for all Assets.

TFire> collection[Date(2010,1,12)]
-| Collection |- (Continuous)
Tickers: 2, MMM AOS
Samples: 2

Further a Collection may be accessed by matching it to another Collection. In the example below the tickers and dates of collection_mmm is used to access collection.

TFire> collection_mmm = collection["MMM"];

TFire> collection_mmm_new = collection[collection_mmm]
-| Collection |- (Continuous)
Ticker: MMM
Samples: 3352

TFire> collection_mmm == collection_mmm_new
true

Set Operations

Set operations are fully supported on Collections, Below follows some examples of set operations in combination with the getindex methods defined above.

TFire> collection["MMM"] + collection["AOS"] == collection
true

Here the + is equivalent to the union operation.

TFire> collection - collection["AOS"] == collection["MMM"]
true

The - is equivalent to the set difference operation.

TFire> intersect(collection, collection["MMM"]) == collection["MMM"]
true

The intersect function here of course represents the intersect set operation.

Filtering

A Collection may be filtered by a user defined function acting on any DataView of the Collection.

Below is an example taken from Tutorial - Basic Analysis of Time Series Data, where a Collection with an MACD Layer is filtered with a user defined function macd_signal.

TFire> function macd_signal(args)
            macd_hist = args[1]
            return macd_hist[end-1] < 0 && macd_hist[end] > 0
       end

TFire> dv_macd = DVLayerField(collection_macd, :macd_histogram, LayerMACD);

TFire> collection_macd_filtered = filter_collection(collection_macd, [dv_macd], macd_signal)