User wants to use 10 years of December contract for wheat and calculate the average daily close for every moment up to expiration.
| TimeSeries contract1945 = ...; // Market data of December 1945. TimeSeries contract1950 = ...; // Market data of December 1950. // Build the contracts time series. TimeSeries contracts = new TimeSeries(); contracts.alignement = Countdown; contracts.add( "1945", contract1945.get("Close") ); contracts.add( "1950", contract1950.get("Close") ); // Calculate the average close of all contract for every moment // up to expiration. // Since no padding is done, no need to check if the // values are defined. for each index in contracts { double sumOfClose = 0.0; for each variable in contracts sumOfClose += contracts.get( variable ).data[index]; // The app can then calculate the average. double average = sumOfClose / contracts.nbVariable; ... // The timestamp is the "time left until expiration". contracts.timestamps[index] } |
An alternative way to do the iteration:
| // Same, but using padding for iteration. User is responsible // to check if a value is a pad before using it. contracts.padding = enabled; for each index in contracts { double sumOfClose = 0.0; int nbClose = 0; for each variable in contracts { Value close = contracts.get( variable ).value[index]; if( close.isDefined() ) { nbClose++; sumOfClose += close.data; } } // The app can then calculate the average. double average = sumOfClose / nbClose; ... } |
Another way to do the iteration:
| // Same but using the requirement that padded values // representation as double precision are NaN. for each index in contracts { double sumOfClose = 0.0; int nbClose = 0; for each variable in contracts { double close = contracts.get( variable ).data[index]; if( close != NaN ) { nbClose++; sumOfClose += close; } } // The app can then calculate the average. double average = sumOfClose / nbClose; ... } |
WARNING: Work in progress, solution provided is a vague proposal in no particular language and is VERY likely to change as the design/interface is defined.