Click or drag to resize

PortfolioStorage

PortfolioStorage functionality allows to combine indicators in one storage to accelerate calculation of a large amount of indicators in portfolio due to optimal memory values placement.

Implementation and usage

The following constructor creates an instance of the portfolio storage:

PortfolioStorage

The class contains following methods:

Method

Description

methodDispose

Disposes storage.

methodGetLocalStorage(Int32, Type)

Returns local storage for the given combination of type and period.

The class DataStorage represents data storage. This class implements an interface IDataStorageClient, which represents Data Storage client interface. To construct a data storage the following constructor is used: DataStorage(Int32, Type). The following methods are in the DataStorage class:

Method

Description

methodRegister(IDataStorageClient)

Registers data storage in data storage client.

methodPrepare

Prepares data storage. Called by data storage clients.

To use the PortfolioStorage functionality you should create a storage and initialize the desired indicators within the storage. For example:

C#
1using FinAnalysis.Base.BaseClasses;
2Sma sma1;
3using (PortfolioStorage localStorage = new PortfolioStorage())
4{
5    sma1 = new Sma(30);
6}
7//...
8sma1.Add(CurrentPrice);
Code Sample

The example of PortfolioStorage class usage:

C#
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using FinAnalysis.Base;
 6using FinAnalysis.TA;
 7using FinAnalysis.Base.BaseClasses;
 8
 9namespace PortfolioStorageSample
10{
11    class PortfolioStorageSample
12    {
13        const int BARS_COUNT = 65536;
14        const int INDEX_SIZE = 1024;
15        const int PERIOD = 256;
16
17        static void Main(string[] args)
18        {
19            Random random = new Random();
20
21            BaseSimpleIndicator<Double>[] indicatorLists = new BaseSimpleIndicator<double>[INDEX_SIZE];
22            BaseSimpleIndicator<Double>[] indicatorListsEtalon = new BaseSimpleIndicator<double>[INDEX_SIZE];
23
24            using (PortfolioStorage localStorage = new PortfolioStorage())
25            {
26                for (int i = 0; i < INDEX_SIZE; ++i)
27                    indicatorLists[i] = new Sma(PERIOD);
28            }
29
30            for (int i = 0; i < INDEX_SIZE; ++i)
31                indicatorListsEtalon[i] = new Sma(PERIOD);
32
33            Double[] sampleData = new Double[BARS_COUNT];
34            for (int b = 0; b < BARS_COUNT; ++b)
35                sampleData[b] = random.NextDouble();
36
37            // Measuring indicators calculation time using portfolio storage.
38            DateTime startTimePortfolio = DateTime.Now;
39            for (int b = 0; b < BARS_COUNT; ++b)
40            {
41                for (int i = 0; i < INDEX_SIZE; ++i)
42                {
43                    indicatorLists[i].Add(sampleData[b]);
44                }
45            }
46            DateTime endTimePortfolio = DateTime.Now;
47
48            // Measuring indicators calculation time not using portfolio storage.
49            DateTime startTimeEtalon = DateTime.Now;
50            for (int b = 0; b < BARS_COUNT; ++b)
51            {
52                for (int i = 0; i < INDEX_SIZE; ++i)
53                {
54                    indicatorListsEtalon[i].Add(sampleData[b]);
55                }
56            }
57            DateTime endTimeEtalon = DateTime.Now;
58
59            Console.WriteLine("Using portfolio storage: {0} milliseconds", (endTimePortfolio - startTimePortfolio).TotalMilliseconds);
60            Console.WriteLine("Not using portfolio storage: {0} milliseconds", (endTimeEtalon - startTimeEtalon).TotalMilliseconds);
61        }
62    }
63}