Go Back   DailyFX Forum

View Single Post
  #2 (permalink)  
Old 07-03-2009, 02:52 PM
Nikolay.Gekht Nikolay.Gekht is offline
Member
 
Join Date: Oct 2008
Posts: 474
Nikolay.Gekht is on a distinguished road
An example of the pipsize usage is in the Swing Index standard indicator. Please look at the Prepare method.
Code:
-- The formula is described in the Kaufman "Trading Systems and Methods" chapter 12 "Charting Systems" (page 286-287)

-- Indicator profile initialization routine
-- Defines indicator profile properties and indicator parameters
function Init()
    indicator:name(resources:get("name"));
    indicator:description(resources:get("description"));
    indicator:requiredSource(core.Bar);
    indicator:type(core.Oscillator);

    indicator.parameters:addColor("clrSI", resources:get("param_clrSI_name"), resources:get("param_clrSI_description"), core.rgb(255, 0, 0));
end

-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams
-- Parameters block

local first;
local source = nil;

-- Streams block
local SI = nil;
local T = nil;

-- Routine
function Prepare()
    source = instance.source;
    first = source:first() + 1;

    local name = profile:id() .. "(" .. source:name() .. ")";
    instance:name(name);
    SI = instance:addStream("SI", core.Line, name, "SI", instance.parameters.clrSI, first)
    T = source:pipSize() * 300;
end

-- Indicator calculation routine
function Update(period)
    if period >= first then
        local nom = source.close[period] - source.close[period - 1]
            + (.5 * (source.close[period] - source.open[period]))
            + (.25 * (source.close[period - 1] - source.open[period - 1]));

        local closePrev = source.close[period - 1];
        local highCurr = source.high[period];
        local lowCurr = source.low[period];
        local hc = math.abs(highCurr - closePrev);
        local lc = math.abs(lowCurr - closePrev);
        local hl = math.abs(highCurr - lowCurr);
        local co = math.abs(closePrev - source.open[period - 1]);

        local TR = math.max(hc, lc, hl);
        local ER = 0;
        if (closePrev > highCurr) then
            ER = hc;
        elseif (closePrev < lowCurr) then
            ER = lc;
        end
        local SH = co;
        local R = TR - 0.5 * ER + 0.25 * SH;
        local K = math.max(hc, lc);

        if (math.abs(R) < 1e-10) then
            SI[period] = 0;
        else
            SI[period] = 50.0 * nom * (K / T) / R;
        end
    end
end
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:13 PM.
Copyright ©2009 Daily FX. All Rights Reserved.