Subclass from WealthScript
Author: Penny
Creation Date: 7/7/2009 1:36 PM
profile picture

Penny

#1
I would like to subclass from WealthLab.WealthScript to facilitate the creation of a family of strategies. It appears that this is possible, as long as the derived class (C) is named "WealthScript" and is abstract. This seems not to interfere with the discovery and instantiation of the Strategy class, based on C, by Wealth-Lab.NET (as shown in the following example).
CODE:
Please log in to see this code.
Is this the "prescribed" method to subclass from WealthLab.WealthScript? Is the strategy discovery mechanism documented in some fashion and is it configurable? Can future releases of Wealth-Lab.NET be expected to support this usage model?
profile picture

Eugene

#2
Creation of a family of strategies is possible. One such example of a strategy library is the ActiveTrader Strategy Pack extension. Its C# source code can be downloaded in our WL5 Wiki: Home - Strategies.ActiveTrader.

Here's the currently available documentation that shows how to derive a class from WealthScript and implement the StrategyHelper class:

How can I debug my trading strategies in Wealth-Lab 6?
profile picture

Penny

#3
Thanks for your quick reply, Eugene. It is appreciated.

I understand the mechanisms through which "known good" strategy code can be packaged into an extension or assembly. They represent a "family" of strategies to be sure, but not what I refer to.

In deriving the strategy class from a subclass (C) of WealthLab.WealthScript, rather then directly from WealthLab.WealthScript itself, it is possible to embed fields and methods (even a constructor with associated strategy parameters) into C.

Doing so allows code re-use via the "Is A" relationships "X.Strategy Is A Library.WealthScript", "X.Strategy Is A WealthLab.WealthScript" and "Library.WealthScript Is A WealthLab.WealthScript". This provides a distinctly different capability then that afforded by the "Has A" relationship created when, say, a class constructor is passed an instance of WealthLab.WealthScript as a parameter or that provided by the capacity to reference code from an external assembly. Examples of these latter types of code re-use in Wealth-Lab have previously been cited through this forum.

My question might be paraphased as follows. Although no documentation or examples appear to exist, Wealth-Lab apparently allows abstract subclasses of WealthLab.WealthScript named "WealthScript" to be used as the base class for strategies. This is most certainly useful. Is this a feature that the developers have thought about and intend to support in future releases or a passing quirk (no slight intended) of the implementation?

As always, your assistance is much appreciated.
profile picture

Cone

#4
We've confirmed that you can rest assured that this design will not change going forward. You can declare sub-classes of WealthScript and use these as the basis for new strategies.
profile picture

Penny

#5
Thanks, Cone.

'May be worth documenting on the Wiki and, perhaps, in the Programming Guide. 'Just a thought.
profile picture

Eugene

#6
I would appreciate if you could tell me what is the difference between the "has-a" relationship and passing WealthScript by reference? Thank you.

Example 1:
CODE:
Please log in to see this code.


Example 2 by Aleksey:
CODE:
Please log in to see this code.

profile picture

Penny

#7
In the Wiki article you cite, the topic starter's solution passes the parameter "this" by reference to "testFunc", since "this" is a class instance and any instance of a C# class is a reference type.

In the example by Aleksey from that same article, the parameter "this" is passed to a class constructor by reference and the receiving class stores it in the field "_ws". That class instance now contains ("Has A") class instance stored in the reference type field "_ws", although that class was instantiated by Wealth-Lab.

"Has A" relationships are created through instantiation (in the case of reference types) and storage. "Is A" relationships are created through inheritance".
profile picture

avishn

#8
It appears that the mentioned above approach works fine when both base class and subclass are in the same script. Is there a way to partition code into multiple scripts, i.e. to have a base class in one scripts file and a subclass in a different one?

I'm trying to create a family of strategies which share common set of indicators and cosmetic chart features. Ideally I would want to have all the common code in the base class in a separate file and then every concrete strategy would be in it's own file. I've tried both approaches above (i.e. subclassing and passing "this" WealthScript reference), but in either case it forces me to put all concrete strategies in a single file, which creates certain problems when I want to run different versions of a strategy in the monitor window at the same time.
profile picture

Eugene

#9
How about having the base class in an assembly (not in a strategy's XML file) and then plugging it in with a "using" statement?
profile picture

avishn

#10
I know that approach has been recommended before. I was just hoping that there's some way to make multiple scripts "visible" to each other within WL. I guess I will need to step outside of the app to accomplish it. Thank you for the answer.
profile picture

Eugene

#11
Frankly, I haven't experimented with making XML strategies visible to each other. Maybe it would require dynamic code compilation or some API trick, I don't know, but stepping outside of WL5 with a full-blown IDE to build an assembly seems like an (easier) way to go.
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).