Go Back   DailyFX Forex Forum | FX Forum

View Single Post
  #5 (permalink)  
Old 07-23-2009, 11:58 AM
fruitaly's Avatar
fruitaly fruitaly is offline
Registered User
 
Join Date: Jul 2009
Posts: 2
fruitaly is an unknown quantity at this point
BB Channel Width

Hi, Here is my first indicator with Indicator SDK. The BB channel width indicator . The Up/Down color is defined by DMI (I take the data from DMI built-in indicator). There is channel width SMA also. When the Histogram break up/down the SMA, it tell, the BB channel width is broader/tighter than average. As a note, the DMI uses core.Bar, while the BB is actually suitable with core.Tick. Then for the compability, I should put the applied price as a parameter. Hope this useful for us.

Code:
-- Indicator profile initialization routine
-- Defines indicator profile properties and indicator parameters
-- TODO: Add minimal and maximal value of numeric parameters and default color of the streams
function Init()
    indicator:name(resources:get("name"));
    indicator:description(resources:get("description"));
    indicator:requiredSource(core.Bar);
    indicator:type(core.Oscillator);

    indicator.parameters:addInteger("BBP", resources:get("param_BBP_name"), resources:get("param_BBP_description"), 20);
    indicator.parameters:addInteger("BBSD", resources:get("param_BBSD_name"), resources:get("param_BBSD_description"), 2);
    indicator.parameters:addInteger("BBPR", resources:get("param_BBPR_name"), resources:get("param_BBPR_description"), 0);
    indicator.parameters:addInteger("DMIP", resources:get("param_DMIP_name"), resources:get("param_DMIP_description"), 14);
    indicator.parameters:addInteger("MAP", resources:get("param_MAP_name"), resources:get("param_MAP_description"), 60);
    indicator.parameters:addColor("BBUp_color", resources:get("param_BBUp_color_name"), resources:get("param_BBUp_color_description"), core.rgb(0, 168, 0));
    indicator.parameters:addColor("BBDn_color", resources:get("param_BBDn_color_name"), resources:get("param_BBDn_color_description"), core.rgb(168, 0, 0));
    indicator.parameters:addColor("BBLine_color", resources:get("param_BBLine_color_name"), resources:get("param_BBLine_color_description"), core.rgb(255, 255, 0));
    indicator.parameters:addColor("MALine_color", resources:get("param_MALine_color_name"), resources:get("param_MALine_color_description"), core.rgb(128, 128, 128));
end

-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams
-- TODO: Refine the first period calculation for each of the output streams.
-- TODO: Calculate all constants, create instances all subsequent indicators and load all required libraries
-- Parameters block
local BBP;
local BBSD;
local BBPR;
local DMIP;
local MAP;

local first;
local source = nil;

-- dmi
local dmi = nil;

-- Streams block
local BBUp = nil;
local BBDn = nil;
local BBLine = nil;
local MALine = nil;

-- Routine
function Prepare()
    BBP = instance.parameters.BBP;
    BBSD = instance.parameters.BBSD;
    BBPR = instance.parameters.BBPR;
    DMIP = instance.parameters.DMIP;
    MAP = instance.parameters.MAP;
    source = instance.source;
    
    if BBP > DMIP then first = source:first() + BBP - 1; else first = source:first() + DMIP - 1; end

    -- DMI prepare
    dmi = core.indicators:create("DMI", source, DMIP);

    local name = profile:id() .. "(" .. source:name() .. ", " .. BBP .. ", " .. BBSD .. ", " .. BBPR .. ", " .. DMIP .. ", " .. MAP .. ")";
    instance:name(name);
    BBUp = instance:addStream("BBUp", core.Bar, name .. ".BBUp", "BBUp", instance.parameters.BBUp_color, first);
    BBDn = instance:addStream("BBDn", core.Bar, name .. ".BBDn", "BBDn", instance.parameters.BBDn_color, first);
    BBLine = instance:addStream("BBLine", core.Line, name .. ".BBLine", "BBLine", instance.parameters.BBLine_color, first);
    MALine = instance:addStream("MALine", core.Line, name .. ".MALine", "MALine", instance.parameters.MALine_color, first);
end

-- Indicator calculation routine
-- TODO: Add your code for calculation output values
function Update(period, mode)
    dmi:update(mode);
    
    if period >= first and source:hasData(period) then
        local p = core.rangeTo(period, BBP);
        
        -- Set Price because it use core.Bar
        local ml;
        local d;
        if BBPR == 0 then ml = core.avg(source.close, p); d = core.stdev(source.close, p);
        elseif BBPR == 1 then ml = core.avg(source.open, p); d = core.stdev(source.open, p);
        elseif BBPR == 2 then ml = core.avg(source.high, p); d = core.stdev(source.high, p);
        elseif BBPR == 3 then ml = core.avg(source.low, p); d = core.stdev(source.low, p);
        end

        local plus = dmi.DIP[period];
        local minus = dmi.DIM[period];

        local bbWidth = (ml + BBSD * d) - (ml - BBSD * d);
        
        -- set to nil the histograms
        BBUp[period] = nil; BBDn[period] = nil;
        -- revalue the histograms based on DMI+/-
        if plus>minus then BBUp[period] = bbWidth; else BBDn[period] = bbWidth; end
        
        BBLine[period] = bbWidth;
        
        MALine[period] = nil;
        if period >= first + MAP then
            local p = core.rangeTo(period, MAP);
            local ml = core.avg(BBLine,p);
            MALine[period] = ml;
        end
    end
end
Attached Images
 
Reply With Quote
 


Disclaimer: Trading foreign exchange on margin carries a high level of risk, and may not be suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to trade foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with foreign exchange trading, and seek advice from an independent financial advisor if you have any doubts. Any opinions, news, research, analyses, prices, or other information contained on this website is provided as general market commentary and does not constitute investment advice. Forex Capital Markets LLC. will not accept liability for any loss or damage, including without limitation to, any loss of profit, which may arise directly or indirectly from use of or reliance on such information.

All times are GMT -5. The time now is 09:56 PM.
Copyright ©2009 Daily FX. All Rights Reserved.