How to create a DataSeries based on a subset of another DataSeries?
Author: JohnTong
Creation Date: 1/22/2018 8:28 PM

#### JohnTong

#1
How to create a dataseries based on a sub set of another dataseries ? For example, create a smaller dataseries that contains the last 20 days of Close [ ] ?

#### Eugene

#2
Please read this WealthScript Programming Guide (Help menu) > DataSeries > Characteristics of DataSeries, #2. If questions remain could you clarify what's the point in doing this?

#### superticker

#3
QUOTE:
... could you clarify what's the point in doing this?
This is a really polite way of saying, "Partial DataSeries computations have little practical value in WL simulations."

In order for WL to simulate all trades across the entire data range, values for all bars (except those before the "start bar") have to be known. Now you can redefine the start bar of the simulation to be somewhere inside the data range. Is this what you're actually wanting to do?

---
For non-simulation--display only--purposes, I have defined a linear regression fit equation, which I solve with MathNet on the last ten bars of a DataSeries: y = beta0 + beta1*(1/x) + beta2*(x). I do that by passing the transformed DataSeries of interest into my own routine that removes the last 10 bar values and places them in a MathNet Vector data type, which is then passed into its regression fitting routine. The fit leads to an extrapolated price gain for the off-the-chart bar, which is displayed on the chart.

POINT: The problem with this approach is that it's only good for the off-the-chart bar, and not for the WL simulation itself. So it only works for interactive trade evaluation (single value displayed on chart), not WL trade simulations--major limitation.

I have thought about transforming this implementation into a WL indicator, for employing in WL simulations, but that would mean performing the 10-bar regression fit for every bar in the chart. Not exactly CPU efficient since the regression fit employs "indirect" (iterative approximation) numerical methods. So I'm kind of stuck not using this all-bars indicator approach for "production" WL simulations, although I could use it for model-optimization research purposes on a few select stocks.

#### JohnTong

#4
My goal is to do some calculation, for example, find the max or average value from 2/3/2017 to 10/5/2017. Instead of looping through each value in that period, I wonder if there is some easy way using sub data set ?

#### Eugene

#5
The easiest way is to build a function that would loop through the DataSeries and filter out the bars outside your range.

#### Cone

#6
Does the AvgValueFromDates() function do what you want?

CODE:

#### Eugene

#7
Cone, nice code. Let's skin this cat once more - this time using LINQ (finding the maximum value as JohnTong asked along the way). ;)

Prerequisites:

1. "References..." > NET Framework > check "System.Core"
2. Community Components installed (for DataSeries.ToList to work).

CODE: