- ago
Here is a handy UserStrategyBase extension method for C# developers. Put the following in your (supporting) library to use it. (I presume you know what to do for creating and building a library for use with WL.). The method returns the greatest FirstValidIndex for all TimeSeries-based member fields in your strategy. Note that it doesn't account for static fields. Adjust for your needs.

See the code comments for additional details.

CODE:
using System.Reflection; using WealthLab.Backtest; using WealthLab.Core; namespace WLUtility.Extensions { public static partial class UserStrategyBaseExtensions { /// <summary> /// Go through all TimeSeries-based fields in the UserStrategyBase and return the first valid index by /// utilizing reflection to find the greatest TimeSeries FirstValidIndex property value. /// </summary> /// <param name="userStrategyBase"></param> /// <returns>The greatest first valid index of the TimeSeries-based fields of the given userStrategyBase.</returns> public static int GetFirstValidIndex(this UserStrategyBase userStrategyBase) { var firstValidIndex = 0; // Use reflection to discover TimeSeries-based fields and then find the greatest FirstValidIndex. // Note: properties have backing fields, so we need only scan the fields. foreach (var fieldInfo in userStrategyBase.GetType() .GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { // look for TimeSeries or inherited from TimeSeries if (!fieldInfo.FieldType.IsAssignableTo(typeof(TimeSeries))) { // not a TimeSeries-based field continue; } var fieldValue = (TimeSeries) fieldInfo.GetValue(userStrategyBase); if (fieldValue != null && fieldValue.FirstValidIndex > firstValidIndex) { firstValidIndex = fieldValue.FirstValidIndex; } } return firstValidIndex; } } }


At the end of your strategy's Initialize() method, call the extension method as follows:

CODE:
StartIndex = this.GetFirstValidIndex();
2
204
0 Replies

Reply

Bookmark

Sort
Currently there are no replies yet. Please check back later.