Using an indicator inside another indicator - using its UI

Moderator: admin

Using an indicator inside another indicator - using its UI

Postby jhouchin » Wed Sep 21, 2011 9:17 am

Hello,

I am using indicators inside another indicator. Many (most) of these indicators already have setup the UI for setting up the indicator.

Is there a way to allow that to be used in the new indicator, for the creation of this internal indicator?

It seems that it would be nice for code reuse, to be able to explicitly allow the use of the internal indicator's UI. Maybe something like

function Init()
...
-- prefix parameter is a prefix to be applied to the below indicators parameters.
-- this allows for avoiding any name collisions.
indicator.parameters.addIndicatorUI("MACD",prefix)
...

This way I can reuse the UI of an indicator and without copy and paste.
This would also encourage creation of small individual indicators which can then be composed into larger groupings for signals and strategies.

Just a few thoughts.

If there is already a way to do this, can you point me to some documentation or provide a few lines here to demonstrate.

Thanks.

Jimmie
jhouchin
FXCodeBase: Confirmed User
 
Posts: 46
Joined: Fri Feb 25, 2011 1:38 pm
Location: Texas

Re: Using an indicator inside another indicator - using its UI

Postby sunshine » Wed Sep 21, 2011 9:23 am

Please read How to Use Another Indicator in Your Code?
The example is the standard Marketscope MACD indicator which uses EMA indicator.
sunshine
 

Re: Using an indicator inside another indicator - using its UI

Postby jhouchin » Wed Sep 21, 2011 9:51 am

sunshine wrote:Please read How to Use Another Indicator in Your Code?
The example is the standard Marketscope MACD indicator which uses EMA indicator.


I have read that. But the MACD is never created as an internal indicator. It only mentioned at the top of the page. The only internal indicator are moving averages which only use a single parameter outside of the name and source.

Code: Select all
    -- copied from the above page
    mva7 = core.indicators:create("MVA", source, 7);
   -- apply the first EMA on the median
    ema1 = core.indicators:create("EMA", median, N);
    -- apply the second EMA on the first EMA result
    ema2 = core.indicators:create("EMA", ema1.DATA, M);


These three uses from the above page only have:
ma = indicators:create(movingAvergageName, source, singleParameter)

So it doesn't answer the question on how to use an indicator with multiple required input parameters in its creation. Such as:

Code: Select all
macd = indicators:create(indicatorName, source, p1, p2, p3 --...)


Thanks.
jhouchin
FXCodeBase: Confirmed User
 
Posts: 46
Joined: Fri Feb 25, 2011 1:38 pm
Location: Texas

Re: Using an indicator inside another indicator - using its UI

Postby jhouchin » Wed Sep 21, 2011 9:56 am

My apologies, I may have misread what you were trying to say.

I was reading into it the content from my other thread.

As I reread. I guess you are speaking of using the indicator manager to create the indicator.

I will look into that.

Thanks
jhouchin
FXCodeBase: Confirmed User
 
Posts: 46
Joined: Fri Feb 25, 2011 1:38 pm
Location: Texas

Re: Using an indicator inside another indicator - using its UI

Postby jhouchin » Wed Sep 21, 2011 10:06 am

It appears I am missing it all over. :)

This applies to my errant reply to sunshine. I just want to clear it all up.

My apologies to all.

Apparently I have not traveled far enough down the documentation.
I didn't click on enough links to get this far.

http://www.fxcodebase.com/documents/Ind ... reate.html
http://www.fxcodebase.com/documents/Ind ... tance.html

These address multiple parameters to an indicator.

Thanks for providing this wonderful tool and for the documentation, even if I didn't traverse far enough.
jhouchin
FXCodeBase: Confirmed User
 
Posts: 46
Joined: Fri Feb 25, 2011 1:38 pm
Location: Texas

Re: Using an indicator inside another indicator - using its UI

Postby sunshine » Thu Sep 22, 2011 3:26 am

Please feel free to post with any other questions.
sunshine
 

Re: Using an indicator inside another indicator - using its UI

Postby jhouchin » Thu Sep 22, 2011 4:26 pm

I have reviewed the suggested materials as best as I can. I see how to create instances of other indicators with multiple parameters inside my indicator.

But what I still have failed to find is any way to use that indicators UI
from its Init() function in my indicators UI without copying and pasting its source.

I would like to provide a UI in my indicator to populate the parameters of the internal indicators that I use. If possible without having to rewrite the UI code or copy and paste the UI code.

In an indicator that I am using for learning I have two moving averages, fast and slow.
I have this in my Init() function.

Code: Select all
function Init()
   indicator:name("My Moving Average Cross");
   indicator:description("A Moving Average Cross.");
   indicator:requiredSource(core.Bar);
   indicator:type(core.Indicator);
   indicator:setTag("group", "My Indicators");

   indicator.parameters:addGroup("Calculate");
   --  Fast Moving Average setup
   indicator.parameters:addInteger("FMA_N", "Fast MA Period", "The Period of the Fast MA", 13, 2, 1000);
   indicator.parameters:addString("FMA_M", "Moving Average Method", "The methods marked by an asterisk (*) require the appropriate indicators to be loaded.", "MVA");
     -- 9 lines of alternatives deleted for brevity ...
   indicator.parameters:addString("FMA_P", "Fast Moving Average Price", "", "C");
     -- 7 lines of alternatives deleted for brevity ...


This is about 20 lines of code per moving average for a UI which hopefully could have been reused, just as much as the calculation is in the Update() function.

Copying and pasting isn't hard. But it doesn't foster code reuse properly. Nor does automatically propagate bug fixes. It isn't DRY.

Is what I am looking for available? Have I overlooked some documentation?

I would like to see smaller indicators being used to compose more complex systems.
To me UI reuse would help.

All the documentation I have seen is in the Prepare() and Update() functions.

Thanks.
jhouchin
FXCodeBase: Confirmed User
 
Posts: 46
Joined: Fri Feb 25, 2011 1:38 pm
Location: Texas

Re: Using an indicator inside another indicator - using its UI

Postby Victor.Tereschenko » Fri Sep 23, 2011 4:51 am

You can extract the common code into another Lua file and load it from both indicators.
For an example:
averages_common.lua (should be placed in the FXTS2 folder), containst common code to add algorithm alternatives
Code: Select all
function addAlgorithms(paramID)
    indicator.parameters:addStringAlternative(paramID, "MVA", "", "MVA");
    indicator.parameters:addStringAlternative(paramID, "EMA", "", "EMA");
    indicator.parameters:addStringAlternative(paramID, "KAMA", "", "KAMA");
    indicator.parameters:addStringAlternative(paramID, "Wilder", "", "Wilder");
    indicator.parameters:addStringAlternative(paramID, "LWMA", "", "LWMA");
    indicator.parameters:addStringAlternative(paramID, "SineWMA", "", "SineWMA");
    indicator.parameters:addStringAlternative(paramID, "TriMA", "", "TriMA");
    indicator.parameters:addStringAlternative(paramID, "LSMA", "", "LSMA");
    indicator.parameters:addStringAlternative(paramID, "SMMA", "", "SMMA");
    indicator.parameters:addStringAlternative(paramID, "HMA", "", "HMA");
    indicator.parameters:addStringAlternative(paramID, "ZeroLagEMA", "", "ZeroLagEMA");
    indicator.parameters:addStringAlternative(paramID, "DEMA", "", "DEMA");
    indicator.parameters:addStringAlternative(paramID, "T3", "", "T3");
    indicator.parameters:addStringAlternative(paramID, "ITrend", "", "ITrend");
    indicator.parameters:addStringAlternative(paramID, "Median", "", "Median");
    indicator.parameters:addStringAlternative(paramID, "GeoMean", "", "GeoMean");
    indicator.parameters:addStringAlternative(paramID, "REMA", "", "REMA");
    indicator.parameters:addStringAlternative(paramID, "ILRS", "", "ILRS");
    indicator.parameters:addStringAlternative(paramID, "IE/2", "", "IE/2");
    indicator.parameters:addStringAlternative(paramID, "TriMAgen", "", "TriMAgen");
    indicator.parameters:addStringAlternative(paramID, "JSmooth", "", "JSmooth");
end


Averages.lua (should be imported)
Code: Select all
dofile("averages_common.lua");

function Init()
    indicator:name("Averages indicator");
    indicator:description("Averages indicator");
    indicator:requiredSource(core.Tick);
    indicator:type(core.Indicator);

    indicator.parameters:addGroup("Calculation");
    indicator.parameters:addString("Method", "Method", "", "MVA");
    addAlgorithms("Method"); -- function from averages_common.lua
...

ma_diff.lua (should be imported)
Code: Select all

dofile("averages_common.lua");

-- Indicator profile initialization routine
-- Defines indicator profile properties and indicator parameters
function Init()
    indicator:name("Simple test");
    indicator:description("Simple test.");
    indicator:requiredSource(core.Tick);
    indicator:type(core.Oscillator);
    indicator:setTag("group", "TEST");
   
    indicator.parameters:addColor("clr", "Color", "Color", core.rgb(255, 0, 0));
   
    indicator.parameters:addInteger("FMA_N", "Fast MA Period", "The Period of the Fast MA", 13, 2, 1000);
    indicator.parameters:addString("FMA_M", "Fast Moving Average Method", "", "MVA");
    addAlgorithms("FMA_M"); -- function from averages_common.lua
   
    indicator.parameters:addInteger("SMA_N", "Slow MA Period", "The Period of the Slow MA", 26, 2, 1000);
    indicator.parameters:addString("SMA_M", "Slow Moving Average Method", "", "MVA");
    addAlgorithms("SMA_M"); -- function from averages_common.lua
end

But this method has a negative side: Marketscope dosn't import dependencies (averages_common.lua). The easiest way to add such an indicator into Marketscope is to use installer like this
Attachments
Averages2.lua
(18.18 KiB) Downloaded 669 times
averages_common.lua
(1.66 KiB) Downloaded 687 times
ma_diff.lua
(1.85 KiB) Downloaded 719 times
“There are only three sports: bullfighting, motor racing, and mountaineering; all the rest are merely games.” (c) Ernest Hemingway
Victor.Tereschenko
FXCodeBase: Confirmed User
 
Posts: 144
Joined: Fri Nov 19, 2010 8:55 am

Re: Using an indicator inside another indicator - using its UI

Postby jhouchin » Fri Sep 23, 2011 10:10 am

@Victor.Tereschenko

Thanks for the reply.

In the absence of an official way of using the UI from the indicators Init() function.
Your method looks very nice and very usable. Thanks for demonstrating this for me. It will help me as I compose my trading system.

As far as installing the file, you are simply stating that I need to place my required "common.lua" file in the C:\Program Files\Candleworks\FXTS2\ directory or wherever Trading Station resides on the system.

Where did the implementations of all these averages come from? Were they already available somewhere here?

I am playing with your Averages now. Thanks for the education.

Jimmie
jhouchin
FXCodeBase: Confirmed User
 
Posts: 46
Joined: Fri Feb 25, 2011 1:38 pm
Location: Texas

Re: Using an indicator inside another indicator - using its UI

Postby jhouchin » Fri Sep 23, 2011 3:40 pm

@Victor.Tereschenko

As per my education from you, I have started refactoring some of my common ui code into a file called common_ui.lua. Below is my start at this. I think it will work out well. I can factor out commonalities in my code and make reuse much nicer.
Code: Select all
function addUI_MA(periodID, periodDescription, periodLongDescription, periodDefault, periodMin, periodMax, methodID, priceID)
--   periodID, string, id of parameter for this UI element
--   periodDescription, string, description of this parameter
--   periodLongDescription, optional, string, longer description of this parameter. Appears in description box at bottom in Market Scope
--   periodDefault, integer, default value of moving average period.
--   periodMin, integer, minimum value of moving average period
--   periodMax, integer, maximum value of moving average period
--   methodID, string, ID of the moving average method to be used for moving average calculation.
--   priceID, string, ID of the price source for moving average.
   indicator.parameters:addInteger(periodID, periodComment, periodLongComment, periodDefault, periodMin, periodMax)
   indicator.parameters:addString(methodID, "Moving Average Method", "The methods marked by an asterisk (*) require the appropriate indicators to be loaded.", "MVA")
    indicator.parameters:addStringAlternative(methodID, "MVA", "", "MVA")
    indicator.parameters:addStringAlternative(methodID, "EMA", "", "EMA")
    indicator.parameters:addStringAlternative(methodID, "KAMA", "", "KAMA")
    indicator.parameters:addStringAlternative(methodID, "Wilder", "", "Wilder")
    indicator.parameters:addStringAlternative(methodID, "LWMA", "", "LWMA")
    indicator.parameters:addStringAlternative(methodID, "SineWMA", "", "SineWMA")
    indicator.parameters:addStringAlternative(methodID, "TriMA", "", "TriMA")
    indicator.parameters:addStringAlternative(methodID, "LSMA", "", "LSMA")
    indicator.parameters:addStringAlternative(methodID, "SMMA", "", "SMMA")
    indicator.parameters:addStringAlternative(methodID, "HMA", "", "HMA")
    indicator.parameters:addStringAlternative(methodID, "ZeroLagEMA", "", "ZeroLagEMA")
    indicator.parameters:addStringAlternative(methodID, "DEMA", "", "DEMA")
    indicator.parameters:addStringAlternative(methodID, "T3", "", "T3")
    indicator.parameters:addStringAlternative(methodID, "ITrend", "", "ITrend")
    indicator.parameters:addStringAlternative(methodID, "Median", "", "Median")
    indicator.parameters:addStringAlternative(methodID, "GeoMean", "", "GeoMean")
    indicator.parameters:addStringAlternative(methodID, "REMA", "", "REMA")
    indicator.parameters:addStringAlternative(methodID, "ILRS", "", "ILRS")
    indicator.parameters:addStringAlternative(methodID, "IE/2", "", "IE/2")
    indicator.parameters:addStringAlternative(methodID, "TriMAgen", "", "TriMAgen")
    indicator.parameters:addStringAlternative(methodID, "JSmooth", "", "JSmooth")
   addUI_Price(priceID)
end

function addUI_Price(priceID)
--   priceID, string, ID of the price source for indicator.
   indicator.parameters:addString(priceID, "Price input for indicator", "", "C")
   indicator.parameters:addStringAlternative(priceID, "Open", "", "O")
   indicator.parameters:addStringAlternative(priceID, "High", "", "H")
   indicator.parameters:addStringAlternative(priceID, "Low", "", "L")
   indicator.parameters:addStringAlternative(priceID, "Close", "", "C")
   indicator.parameters:addStringAlternative(priceID, "Median", "", "M")
   indicator.parameters:addStringAlternative(priceID, "Typical", "", "T")
   indicator.parameters:addStringAlternative(priceID, "Weighted", "", "W")


With this I can create my 2 moving averages simply like this:
Code: Select all
-- (periodID, periodDescription, periodLongDescription, periodDefault, periodMin, periodMax, methodID, priceID)
addUI_MA("FMA_N", "Fast MA Period", "", 13, 2, 1000, "FMA_M", "FMA_P")
addUI_MA("SMA_N", "Slow MA Period", "", 34, 2, 1000, "SMA_M", "SMA_P")


I like this.

Thanks for the education and insight.
jhouchin
FXCodeBase: Confirmed User
 
Posts: 46
Joined: Fri Feb 25, 2011 1:38 pm
Location: Texas

Next

Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 3 guests