Moving Average Envelopes (MAE)

Here you can post and download custom indicators. PLEASE: Do not start topics unless you are posting your own indicator, they will be moved to appropriate section even if you do.

Moderator: admin

Moving Average Envelopes (MAE)

Postby TonyMod » Mon Nov 23, 2009 3:42 pm

Note: The indicator is updated. The new version can be found here: viewtopic.php?f=17&t=658.

Moving Average Envelopes or MAE indicator consists of 2 offset lines calculated off Simple Moving Average. Sometimes 3rd line of actual moving average is displayed between 2 offsetting lines.
These 2 offsetting lines are called “Envelope Factor” and their offset can be set in indicator parameters.

The moving average envelopes can be useful in identifying overbought or oversold conditions, where instrument prices hit the top or bottom of their trading range.

Calculation (N - number of periods, W - distance in 1/10 of percent):
Top Line = Moving Average(N) + (Moving Average(N) x W ÷ 1000)
Bottom Line = Moving Average(N) - (Moving Average(N) x W ÷ 1000)

MAE.jpg
SCREENSHOT: "Moving Average Envelopes"


Source Code:
Code: Select all
-- The indicator corresponds to the Moving Average indicator in MetaTrader.
-- The formula is described in the Kaufman "Trading Systems and Methods" chapter 4 "Trend Calculations" (page 67-70)

-- initializes the indicator
function Init()
    indicator:name("Moving Average Envelope (Custom Version)");
    indicator:description("Moving Average Envelope which supports 1/10 of percent in parameters");
    indicator:requiredSource(core.Tick);
    indicator:type(core.Indicator);

    indicator.parameters:addInteger("N", "Number of periods of Moving Average", "", 20, 2, 300);
    indicator.parameters:addDouble("W", "Band Offset in 1/10 of percent", "", 2.5, 0.001, 1000);
    indicator.parameters:addString("SM", "Show MVA?", "", "No");
    indicator.parameters:addStringAlternative("SM", "Yes", "", "Yes");
    indicator.parameters:addStringAlternative("SM", "No", "", "No");
    indicator.parameters:addColor("clrBand", "Color of envelope band", "", core.rgb(0, 0, 255));
    indicator.parameters:addColor("clrMva", "Color of moving average", "", core.rgb(255, 0, 0));
end

local first = 0;        -- first period we can calculate
local n = 0;            -- MVA parameter
local w = 0;            -- band width
local source = nil;     -- source
local mva = nil;        -- moving average
local up = nil;         -- upper band
local low = nil;        -- lower band

-- initializes the instance of the indicator
function Prepare()
    source = instance.source;
    n = instance.parameters.N;
    w = instance.parameters.W;

    first = n + source:first() - 1;
    local name = profile:id() .. "(" .. source:name() .. "," .. n .. "," .. w .. ")";
    instance:name(name);

    if instance.parameters.SM == "Yes" then
        mva = instance:addStream("MVA", core.Line, name .. ".MVA", "MVA", instance.parameters.clrMva,  first)
    end
    up = instance:addStream("UP", core.Line, name .. ".UP", "UP", instance.parameters.clrBand,  first)
    low = instance:addStream("LOW", core.Line, name .. ".LOW", "LOW", instance.parameters.clrBand,  first)
end

-- calculate the value
function Update(period)
    if (period >= first) then
        local v;
        v = core.avg(source, core.rangeTo(period, n));
        if mva ~= nil then
            mva[period] = v;
        end
        up[period] = v * (1 + w / 1000);
        low[period] = v * (1 - w / 1000);
    end
end


Download:
mae1.lua
(2.36 KiB) Downloaded 2634 times
Last edited by Nikolay.Gekht on Thu Apr 15, 2010 8:14 pm, edited 3 times in total.
Reason: UPD Nov, 24: W parameter is changed from integer to double. Nov, 25: Reduce minium for W params
Best Regards,

"TonyMod"
(FXCodeBase.com Forum Moderator)
TonyMod
FXCodeBase: Site Admin
 
Posts: 70
Joined: Wed Oct 21, 2009 1:57 pm
Location: New Jersey

Re: Moving Average Envelopes (MAE)

Postby rich@jhklr.net » Mon Nov 23, 2009 7:30 pm

WOW,
Thanks.....now, how do i get the following parameters
I will use 2 seperate envelopes on 1 chart with the following parameters

88 periods and .07 offset

and 44 and .14 offset.

when i try to put in .14 or .07 it changes to 2

when I use 1 and 2 respectivly, it is close but not quite.
rich@jhklr.net
FXCodeBase: Confirmed User
 
Posts: 9
Joined: Sun Nov 22, 2009 3:54 am

Re: Moving Average Envelopes (MAE)

Postby admin » Tue Nov 24, 2009 12:28 pm

Oh, I see the problem with 0.7 percentage. The W parameter was integer instead of double. Now the version in the first post of the this topic is updated. Please, download it again and replace the indicator in Marketscope. To replace the indicator use the "Manage custom indicator" form. First, remove the old indicator and then load a new version. Please, do not forget to restart Trading Station before use the updated indicator.

To setup your own parameters you can use "Parameters" tab of the "Add Indicator" form. You can switch to this tab and fill any applicable parameters when the indicator is selected on the "Indicator tab":

params.jpg
admin
FXCodeBase: Site Admin
 
Posts: 20
Joined: Tue Oct 20, 2009 10:02 am
Location: Paramus, NJ

Re: Moving Average Envelopes (MAE)

Postby rich@jhklr.net » Tue Nov 24, 2009 5:20 pm

It's still not quite working.
I can not input the .14 value at all, it just will not take it and the
.7 value isn't giving the same result as the .07
I tried adding a zero to the code and saving as an lua file but could not install it at all.

I haven't tried my 33/2.42 or 44/3.98 or 22/.094 yet.
rich@jhklr.net
FXCodeBase: Confirmed User
 
Posts: 9
Joined: Sun Nov 22, 2009 3:54 am

Re: Moving Average Envelopes (MAE)

Postby rich@jhklr.net » Tue Nov 24, 2009 5:39 pm

OK the .7 is working correctly (I wasnt showing enough periods in my confirmation)

BUT it will not take the .14 input at all
rich@jhklr.net
FXCodeBase: Confirmed User
 
Posts: 9
Joined: Sun Nov 22, 2009 3:54 am

Re: Moving Average Envelopes (MAE)

Postby admin » Wed Nov 25, 2009 2:05 pm

Oh, the minimum for W parameter was 0.5. It looks like Marketscope does not show a error message in this case. I'll report this problem. I updated the indicator again. Now the minimal distance is 0.0001%.
admin
FXCodeBase: Site Admin
 
Posts: 20
Joined: Tue Oct 20, 2009 10:02 am
Location: Paramus, NJ

Re: Moving Average Envelopes (MAE)

Postby rich@jhklr.net » Wed Nov 25, 2009 9:41 pm

I think this is it.
I have to move the decimal point on all, i.e, to get 88/.07 I have to input 88/.7, and to get 44/.94 I need to input 44/9.4 and 88/2.46 needs 88/24.6????/ but I guess the results are the same ??
they are aren't they??

Can't thank you enough.
rich@jhklr.net
FXCodeBase: Confirmed User
 
Posts: 9
Joined: Sun Nov 22, 2009 3:54 am

Re: Moving Average Envelopes (MAE)

Postby admin » Thu Nov 26, 2009 3:25 pm

Sorry, I probably don't understand the problem completely. I have tried to create MAE with W=7 (0.7%), green, 0.7 ((7/100)%), blue, and 0.07 ((7/1000)%), red. The result is on the snapshot:
params1.jpg
.
I have chosen 1/10 of percent instead the whole percent because almost all implementation I saw, such as Omega TS or MT4 use this scale. However, it's easy to change the scale if whole percent is more convenient for you. You have just replace 1000 with 100 in 53 and 54 lines of the code:
Code: Select all
        //use W as 1/10 of %
        up[period] = v * (1 + w / 1000);
        low[period] = v * (1 - w / 1000);

Code: Select all
        //use W as %
        up[period] = v * (1 + w / 100);
        low[period] = v * (1 - w / 100);
admin
FXCodeBase: Site Admin
 
Posts: 20
Joined: Tue Oct 20, 2009 10:02 am
Location: Paramus, NJ

Re: Moving Average Envelopes (MAE)

Postby rich@jhklr.net » Mon Nov 30, 2009 1:40 am

New ripple,
Can you base the whole thing on LWMA .
and add the corrected percentage??
rich@jhklr.net
FXCodeBase: Confirmed User
 
Posts: 9
Joined: Sun Nov 22, 2009 3:54 am

Re: Moving Average Envelopes (MAE)

Postby rich@jhklr.net » Mon Nov 30, 2009 6:38 am

Ignore last. I found it in the Data Source tab I believe.
rich@jhklr.net
FXCodeBase: Confirmed User
 
Posts: 9
Joined: Sun Nov 22, 2009 3:54 am

Next

Return to Custom Indicators

Who is online

Users browsing this forum: No registered users and 106 guests