No access to WealthScript cosmetic methods in custom indicator
Author: ksarkar77
Creation Date: 11/21/2019 10:19 PM
profile picture

ksarkar77

#1
Hello,

I have created some on the fly indicators using Strategy code. I was looking into the aspect of coding them as a formal indicator so that I can change parameters from GUI. During this process, I noticed that I do not have access to functions like DrawLabel, DrawPolygon, SetSeriesBarColor etc.

Does this mean that wealth lab can only create simple indicators and any complex cosmetic stuff has to be handled in the Strategy? or there is a way I can wrap the strategy into an indicator code and pass the parameters of Indicator to a Strategy (aka - on the fly indicator code)?

profile picture

superticker

#2
QUOTE:
During [the indicator creation] process, I noticed that I do not have access to functions like DrawLabel, DrawPolygon, SetSeriesBarColor etc.
That's because these functions are part of "WealthScript" which MyStrategy inherits from as defined in the code below:
CODE:
Please log in to see this code.

In contrast, an indicator definition inherits from DataSeries, not WealthScript, so it doesn't have these functions (e.g. DrawLabel).

QUOTE:
Does this mean that wealth lab can only create simple indicators and any complex cosmetic stuff has to be handled in the Strategy?
In other words, you're asking if your indicator can inherit from WealthScript instead of DataSeries. That "might" be possible, but I would not do that--bad design. The intent of an indicator is to create a simple static C# datatype for high speed execution, and inheriting from DataSeries facilities that. Also, the indicator you create has to be a static function (i.e., not passed by reference) to work with the WL GUI.

Step 1) What I would do is create your indicator inheriting from DataSeries the way you would normally do. That finishes that.
Step 2) Next, create custom library function where you pass an instance of WealthScript into this function (which you would call from MyStrategy). Now you can reference WealthScript functions from that instance that you passed into your custom function.
Step 3) Within your custom function, include a using statement referencing your indicator code library so you can call your indicator from inside your custom function.

I do this all the time in my code with one exception: I avoid Step 3 altogether. Instead, I call my custom indicator from MyStrategy directly (not through a custom function), then to dress it up, I then call my custom function from MyStrategy later on. By only calling my indicators and custom functions from MyStrategy directly (not chaining internally), I achieve more flexibility at the MyStrategy level.
profile picture

Eugene

#3
QUOTE:
Does this mean that wealth lab can only create simple indicators and any complex cosmetic stuff has to be handled in the Strategy?

That's the correct approach.
profile picture

ksarkar77

#4
Hello Superticker,

Thank you for your time and trying to explain the approach.

I have a question

You mentioned about custom library function. I am not having a good understanding of it. Where this function needs to be written, in the indicator code or in the strategy code?


Let me give you one example of what I am trying to achieve. Suppose I want to write a moving average indicator which changes color based on the up or down direction (up direction green, down direction red). And suppose you want 2 of these MA's on your chart one with shorter time frame and one with longer time frame. And you want the ability to take one of them off on the fly or add one with different period lengths. I can understand that these all can be done using Strategy code, however, it needs manual coding, commenting out, etc every time I want to make a change. Where an Indicator solution will be much easier in this case.

Is there any workaround indicator code template you can suggest me where I can actually wrap a wealth script object and pass the parameters from indicator code? I just want the indicators to behave like other charting platforms like Tradestation or Ninja traders. Is it at all possible?
profile picture

Eugene

#5
QUOTE:
Is it at all possible?

Sorry, it's simply not possible to achieve the objective in your example through a Wealth-Lab indicator. A formal indicator (a tuple of DataSeries + IndicatorHelper) does the job of indicator calculation and your Strategy does all the rest. That's how it works.

As superticker suggests, you can wrap up your frequently used code that changes color, conditionally plots one or two MAs etc. in a class library. Here's one for example, you may already know it: Download Community Components source code. Once you get it done, all you have to call a couple lines from your class library in your Strategies (like you do with other Community Components methods) to invoke your cosmetic chart logic.
profile picture

ksarkar77

#6
Thanks Eugine, although I am a little disappointed, I am happy that I am not going to waste my time thinking about this anymore! :-)
profile picture

superticker

#7
As suggested in Post# 5, I would download the Community Components source library and pick one of the classes in there that passes an instance of WealthScript into it as your template model. With that WeathScript reference inside, you can now call all the WealthScript functions.

Now, as discussed in Step# 3, you can call indicators (either WL or your own) inside these custom functions as well. So you can introduce indicators into your custom functions. When developing this, I would initially develop it as an auxiliary function inside MyStrategy first. After it's debugged, then you can move it into your own Local.Components.dll library.

Software Engineering editorial mode on: What you should not do is complicate an indicator implementation (Step# 1) with lots of cosmetic stuff. That violates the Principle of Locality of your code execution (creating more processor cache misses), which will increase your execution time. It also violates the principle of Information Hiding (used in object-oriented program design) by exposing the indicator code implementation to extraneous cosmetic functions. In object-oriented code design, we try to be single minded such that we create many short, simple objects with specific functions, then web them together in higher level code (such as MyStrategy or a custom library function). This software engineering approach creates more maintainable code when code changes come along without introducing "side effects". Side effects are a special kind of bug created when modification on one part of the code breaks another.

QUOTE:
Suppose I want to write a moving average indicator which changes color based on the up or down direction ...
Now you're talking about "chart styles", which is off topic to this discussion. Yes, you can write your own custom chart style, but that's a different WL object and a different discussion topic. Look up how to create "chart styles" and start a new discussion topic if you have questions. There's a ChartStyle library http://www2.wealth-lab.com/WL5Wiki/AllPages.aspx?Cat=ChartStyles you can download if you want to look at some of them.

UPDATE: I agree, ChartStyles are more about stylizing Bars rather than Indicators; my mistake.
profile picture

Eugene

#8
I don't recommend ChartStyles for this matter. Even if if were possible, using a ChartStyle to conditionally color an indicator sounds like much more trouble than using a class library that contains a custom method that uses an indicator. ChartStyles render Bars, not paint indicators.
profile picture

superticker

#9
QUOTE:
... using a ChartStyle to conditionally color an indicator sounds like much more trouble than using a class library that contains a custom method ...
Some of the existing ChartStyles sort of use conditional coloring for Bars (e.g. Enhanced Candlesticks), but I agree that using a custom class library instead (Local.Components.dll) would be easier to implement and more appropriate for indicators. The ability to superimpose one similar indicator over the other with different coloring objectives can produce the same layered-coloring effect with much less work. The panes below were done that way with only a few lines of code.

The trick is to order the superimposed layering in the right order so it hides what you don't want to see and lets the colors you do want to see show through. Some 50% transparency was used in this example to partially let some colored objects show through. If there's an interested party, start a new forum topic on colorizing indicators, and I can share some of my code.
profile picture

Eugene

#10
Here's the Knowledge Base article on the technique:

Indicators | Plot Oscillators with Different Shades of Colors
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).