Finding the median of Preferred Value parameters in a DataSet
Author: superticker
Creation Date: 7/5/2017 8:10 PM
profile picture

superticker

#1
I'm wondering if someone has written some "Strategy" code to read Preferred Values (PVs) in a DataSet and write them out to a file or the clipboard? I can code this myself, but if someone has already done it, that would save me some coding.

What I really want to do is determine the median value of each PV in a DataSet. I'm thinking I can just export these results into Excel to do that, but if someone knows a C# library function that can compute the median of an array, that would save me that step.

Ultimately, I want to try out the new Exhaustive To Local Maxima Optimizer. To leverage this optimizer's effectiveness, I'll need to set the default value of each PV to its median, since this optimizer employs the default as the initial guess when solving for parameters.

The Wealth-Lab optimizer "Results tab" will return the average for each PV (which places each value in the middle of its PV range), but I really need the median, not the average, to maximize this optimizer's effectiveness.
profile picture

Eugene

#2
Here are some building blocks for you.

1. There's no shortage of examples that illustrate how to calculate median, it's pretty simple:

Add a Median Method to a List
Calculate median in c#

2. Same as with saving to files. This Wiki example demonstrates that Copy to Clipboard is easy too.

3. Now, here's how to read a Strategy's XML file and extract the PreferredValues tag's raw value. Prerequisites:

A. Check references to System.Core and System.Xml
B. Add a File reference to C:\Windows\Microsoft.NET\Framework64\v.4.0.30319\System.Xml.Linq.dll

What's left is to parse it (should be pretty evident):

EDIT 07/06/2017 (A slightly more complete example)

CODE:
Please log in to see this code.
profile picture

superticker

#3
QUOTE:
Now, here's how to read a Strategy's XML file and extract the PreferredValues tag.
I thought about reading the XML file directly as you suggest. And thanks for the great example doing so. But my "tentative" thinking is to let Wealth-Lab do the work of reading the XML file, then "sample the PV variables" with the CreateParameter.Value and CreateParameter.ValueInt property. (However, I may change my mind and read the XML file directly if I create a stand alone application.)

I'm also thinking plotting the PV values as a frequency histogram to verify there aren't any double maxima might be wise (as opposed to just computing the median and going with that). If two maxima do exist for a PV variable (Oh dear.), I many wish to split the DataSet into two.

Thanks a million for the quick reply and all the links. I may initially go with the clipboard approach because it requires the least code to getting my PV data into Excel for creating the PV histogram. And I may post my solution.

In the past, I've used the Particle Swarm optimizer. It's very fast, but it doesn't use the PV default values for its initial guess. That's an advantage for a totally new strategy (since one has no idea what the defaults should be), but for an established strategy, using the default for the initial guess (as the Exhaustive To Local Maxima optimizer does) gives an edge.
profile picture

superticker

#4
He's another solution that samples in memory PVs so you don't need to read them in separately from the XML source file. (You'll need to read the XML file directly as in the first solution if you're writing a stand alone application, which can be executed on any strategy without modifying its code.)

The code below needs to supplement (i.e., be included in addition to ...) one's existing strategy code. The StringBuilder instance also needs to be declared at the top as shown, and the names of the PVs properly adjusted to match your own. One's strategy code can follow this supplemental code as shown in the Execute() procedure. This supplemental code should be commented out when not needed.
CODE:
Please log in to see this code.
After running the code, the clipboard will contain all the PVs, which can then be pasted into Excel. Now you can plot a histogram of each PV variable to see if there are two maxima suggesting that the dataset should be split into two.
profile picture

superticker

#5
Please note, in order to make the Wealth-Lab write-string-to-the-clipboard call
CODE:
Please log in to see this code.

available to Wealth-Lab, the Community Components library, WealthLab.Components.Community.dll, must already be installed in Wealth-Lab's home program directory. You don't need a using Community.Components statement at the top for this form of the call.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).