Relative Stength Index with zone and trend highlighting

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

Relative Stength Index with zone and trend highlighting

Postby Nikolay.Gekht » Mon Jan 03, 2011 4:25 pm

This indicator is just a regular RSI calculated using this formula:

RSI(n) is
pos := if close >= close[-1] then close - close[-1] else 0
neg := if close < close[-1] then close[-1] - close else 0
rs := WMA(pos, n) / WMA(neg, n)
rsi := 100 - (100 / (1 + rs))


View of this indicator is optimized for working and displaying the results of the "Combining RSI with RSI" strategy described in the Peter Konner's article in January 2011 issues of "Stock & Commodities"

The indicator additionally:
a) Changes the line color depending on the trend direction shown by RSI indicator.
b) Highlight areas above and below the levels chosen

rsi_pk.png


Source code:
Code: Select all
function Init()
    indicator:name("Relative Stength Index with zone and trend highlighting");
    indicator:description("");
    indicator:requiredSource(core.Tick);
    indicator:type(core.Oscillator);
    indicator:setTag("group", "Classic Oscillators");

    indicator.parameters:addInteger("N", "Number of Periods", "", 17, 2, 1000);

    indicator.parameters:addGroup("Style");
    indicator.parameters:addColor("clrRSI", "Line (neutral)", "", core.rgb(0, 255, 255));
    indicator.parameters:addColor("clrRSIUp", "Line (uptrend)", "", core.rgb(0, 255, 0));
    indicator.parameters:addColor("clrRSIDn", "Line (downtrend)", "", core.rgb(255, 0, 0));
    indicator.parameters:addInteger("widthRSI", "Line Width (in pixels)", "", 1, 1, 5);
    indicator.parameters:addInteger("styleRSI", "Line Style", "", core.LINE_SOLID);
    indicator.parameters:setFlag("styleRSI", core.FLAG_LEVEL_STYLE);
    indicator.parameters:addBoolean("fill", "Highlight Areas over/under levels", "", true);
    indicator.parameters:addInteger("transparency", "Highlight transparency (%)", "", 30);
    indicator.parameters:addColor("clrFill", "Over Level Fill Color", "", core.rgb(255, 255, 0));
    indicator.parameters:addColor("clrBg", "Background Fill Color", "Leave this parameter in its default value", core.COLOR_BACKGROUND);

    indicator.parameters:addGroup("Levels");
    indicator.parameters:addInteger("overbought", "Overbought level", "", 60, 0, 100);
    indicator.parameters:addInteger("oversold", "Oversold level", "", 40, 0, 100);
    indicator.parameters:addInteger("level_overboughtsold_width", "Level Line Width (in pixels)", "", 1, 1, 5);
    indicator.parameters:addInteger("level_overboughtsold_style", "Level Line Style", "", core.LINE_DOT);
    indicator.parameters:addColor("level_overboughtsold_color", "","", core.COLOR_CUSTOMLEVEL);
    indicator.parameters:setFlag("level_overboughtsold_style", core.FLAG_LEVEL_STYLE);
end

local n;
local source;
local first1, first2;
local pos, neg;
local wmap, wman;
local rsi;
local clr, clrUp, clrDn;
local trend;

local fill, clrFill, clrBg;
local ob_level;
local ob_highlight1;
local ob_highlight2;
local os_level;
local os_highlight1;
local os_highlight2;

function Prepare(onlyName)
    source = instance.source;

    local name;
    n = instance.parameters.N;

    name = profile:id() .. "(" .. source:name() .. "," .. n .. ")";
    instance:name(name);
    if onlyName then
        return ;
    end

    first1 = source:first() + 1;
    pos = instance:addInternalStream(first1, 0);
    neg = instance:addInternalStream(first1, 0);
    wmap = core.indicators:create("WMA", pos, n);
    wman = core.indicators:create("WMA", neg, n);
    first2 = wmap.DATA:first();

    clr = instance.parameters.clrRSI;
    clrUp = instance.parameters.clrRSIUp;
    clrDn = instance.parameters.clrRSIDn;


    RSI = instance:addStream("RSI", core.Line, name, "RSI", clr, first2);
    RSI:setWidth(instance.parameters.widthRSI);
    RSI:setStyle(instance.parameters.styleRSI);
    RSI:setPrecision(2);

    trend = instance:addInternalStream(0, 0);

    RSI:addLevel(0, core.LINE_NONE);
    RSI:addLevel(instance.parameters.oversold, instance.parameters.level_overboughtsold_style, instance.parameters.level_overboughtsold_width, instance.parameters.level_overboughtsold_color);
    RSI:addLevel(instance.parameters.overbought, instance.parameters.level_overboughtsold_style, instance.parameters.level_overboughtsold_width, instance.parameters.level_overboughtsold_color);
    RSI:addLevel(100, core.LINE_NONE);

    fill = instance.parameters.fill;
    if fill then
        clrFill = instance.parameters.clrFill;
        clrBg = instance.parameters.clrBg;
        ob_level = instance.parameters.overbought;
        ob_highlight1 = instance:addInternalStream(first2, 0);
        ob_highlight2 = instance:addInternalStream(first2, 0);
        instance:createChannelGroup("ob", "ob", ob_highlight1, ob_highlight2, clrFill, 100 - instance.parameters.transparency);
        os_level = instance.parameters.oversold;
        os_highlight1 = instance:addInternalStream(first2, 0);
        os_highlight2 = instance:addInternalStream(first2, 0);
        instance:createChannelGroup("os", "os", os_highlight1, os_highlight2, clrFill, 100 - instance.parameters.transparency);
    end
end

function Update(period, mode)
    if period >= first1 then
        local diff = source[period] - source[period - 1];
        if diff >= 0 then
            pos[period] = diff;
            neg[period] = 0;
        else
            neg[period] = -diff;
            pos[period] = 0;
        end
    end
    wmap:update(mode);
    wman:update(mode);
    if period >= first2 then
        local p, n, rs;
        p = wmap.DATA[period];
        n = wman.DATA[period];

        if n == 0 then
            RSI[period] = 0;
        else
            rs = p / n;
            RSI[period] = 100 - (100 / (1 + rs));
        end
        local t;
        t = trend[period - 1];

        if RSI[period - 1] <= ob_level and
           RSI[period] > ob_level then
            t = 1;
        elseif RSI[period - 1] >= os_level and
               RSI[period] < os_level then
            t = -1;
        end

        if t == 0 then
            RSI:setColor(period, clr);
        elseif t == 1 then
            RSI:setColor(period, clrUp);
        elseif t == -1 then
            RSI:setColor(period, clrDn);
        end

        trend[period] = t;
    else
        trend[period] = 0;
    end

    if fill and period >= first2 then
        ob_highlight1[period] = RSI[period];
        ob_highlight1:setColor(period, clrFill);
        ob_highlight2[period] = ob_level;
        if RSI[period] < ob_level then
            if RSI[period - 1] < ob_level then
                ob_highlight1[period] = nil;
            else
                ob_highlight1:setColor(period, clrBg);
            end
        else
            if RSI[period - 1] < ob_level then
                ob_highlight1[period - 1] = RSI[period - 1];
                ob_highlight1:setColor(period - 1, clrBg);
            end
        end
        os_highlight1[period] = RSI[period];
        os_highlight1:setColor(period, clrFill);
        os_highlight2[period] = os_level;

        if RSI[period] > os_level then
            if RSI[period - 1] > os_level then
                os_highlight1[period] = nil;
            else
                os_highlight1:setColor(period, clrBg);
            end
        else
            if RSI[period - 1] > os_level then
               os_highlight1[period - 1] = RSI[period - 1];
               os_highlight1:setColor(period - 1, clrBg);
            end
        end
    end
end

RSI_PK.lua
(6.68 KiB) Downloaded 1681 times

RSI_PK Averages.lua
(17.62 KiB) Downloaded 98 times

RSI_PK with Averages Filter.lua
(20.24 KiB) Downloaded 86 times

RSI_PK with Averages Filter.7510109079.lua
(18.72 KiB) Downloaded 86 times

Averages.lua is available here.
viewtopic.php?f=17&t=2430
Nikolay.Gekht
FXCodeBase: Base Builder
 
Posts: 1222
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Relative Stength Index with zone and trend highlighting

Postby raychan » Sun Jul 29, 2012 12:24 am

I would appreciate if you can write a strategy that purely applies the RSI_PK indicator for use on trade station, not the Combining RSI with RSI strategy. Simply buy on green and sell on red of the indicator. Many Thanks.
raychan
 
Posts: 6
Joined: Sun May 29, 2011 6:48 pm

Re: Relative Stength Index with zone and trend highlighting

Postby SuperTrader » Tue Aug 21, 2012 1:16 am

Thank you Nikolay, this a visually very helpful version of the RSI (specially the highlighting of areas above and below the levels chosen). :) Could we please have the same visual representation for the standard CCI indicator ? (I mean just the same highlighting of the OB/OS areas, NOT the changing of the line color depending on the trend direction). Thank you very much in advance !
User avatar
SuperTrader
 
Posts: 84
Joined: Thu May 20, 2010 12:52 pm
Location: Europe

Re: Relative Stength Index with zone and trend highlighting

Postby Apprentice » Wed Aug 22, 2012 1:44 am

Your request is added to the development list.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 19427
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

RSI_PK with the dots or arrows

Postby Jeffreyvnlk » Tue Jul 09, 2013 12:01 am

Appreciated if you could make a second version of this great indicator which placing the dots or arrows around the bars on the price chart when RSI >70 or <30.Thanks
Jeffreyvnlk
FXCodeBase: Graduate
 
Posts: 302
Joined: Wed Apr 11, 2012 2:17 pm

Re: Relative Stength Index with zone and trend highlighting

Postby Apprentice » Tue Jul 09, 2013 4:30 am

Your request is added to the development list.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 19427
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Relative Stength Index with zone and trend highlighting

Postby Jeffreyvnlk » Tue Jul 09, 2013 6:05 pm

Apprentice wrote:Your request is added to the development list.


I believe i found the solution on the thread which made from you as well:
viewtopic.php?f=17&t=25319&p=43572&hilit=RSI+overlay#p43572

So thank you
Jeffreyvnlk
FXCodeBase: Graduate
 
Posts: 302
Joined: Wed Apr 11, 2012 2:17 pm

Re: Relative Stength Index with zone and trend highlighting

Postby Jeffreyvnlk » Wed Jul 10, 2013 1:06 am

Apprentice wrote:Your request is added to the development list.


I believe you already made some kind of RSI overlay somewhere in this forum. Sorry for interupting
Jeffreyvnlk
FXCodeBase: Graduate
 
Posts: 302
Joined: Wed Apr 11, 2012 2:17 pm

Re: Relative Stength Index with zone and trend highlighting

Postby mulligan » Wed Mar 02, 2016 7:31 pm

Very good indicator. I can't match the line color change with current RSI signals without a lot of repeating. Could you possibly add an alert to this indicator based on line color uptrend and downtrend. Thanks very much.
mulligan
FXCodeBase: Initiate
 
Posts: 114
Joined: Wed May 16, 2012 4:43 pm

Re: Relative Stength Index with zone and trend highlighting

Postby Apprentice » Thu Mar 03, 2016 4:59 am

Alert Option Added.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 19427
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Next

Return to Custom Indicators

Who is online

Users browsing this forum: Google [Bot] and 14 guests