N periods candle indicator

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

N periods candle indicator

Postby Alexander.Gettinger » Wed Oct 27, 2010 12:20 am

Indicator for calculating the candle formed by the last N periods:

Open = Open price N periods ago
Close = Close price current period
High = Higher price of the last N periods
Low = Lower price of the last N periods

NCandle.png


Code: Select all
function Init()
    indicator:name("NCandle indicator");
    indicator:description("NCandle indicator");
    indicator:requiredSource(core.Bar);
    indicator:type(core.Indicator);

    indicator.parameters:addGroup("Calculation");
    indicator.parameters:addInteger("N", "N", "", 10);

    indicator.parameters:addGroup("Style");
    indicator.parameters:addColor("HIGH", "Color for High", "Color for High", core.rgb(0,0,255));
    indicator.parameters:addColor("LOW", "Color for Low", "Color for Low", core.rgb(255,0,255));
    indicator.parameters:addColor("UP", "Color for UP", "Color for UP", core.rgb(0,255,0));
    indicator.parameters:addColor("DOWN", "Color for DOWN", "Color for DOWN", core.rgb(255,0,0));
    indicator.parameters:addInteger("widthLinReg", "Line width", "Line width", 3, 1, 5);
    indicator.parameters:addInteger("styleLinReg", "Line style", "Line style", core.LINE_SOLID);
    indicator.parameters:setFlag("styleLinReg", core.FLAG_LINE_STYLE);
    indicator.parameters:addInteger("Transparency", "Transparency", "", 50,0,100);
end

local first;
local source = nil;
local N;
local High;
local Low;
local hUP=nil;
local hDN=nil;
local lUP=nil;
local lDN=nil;

function Prepare()
    source = instance.source;
    N=instance.parameters.N;
    first = source:first();
    local name = profile:id() .. "(" .. source:name() .. ", " .. instance.parameters.N .. ")";
    instance:name(name);
    High = instance:addStream("High", core.Line, name .. ".High", "High", instance.parameters.HIGH, 0);
    Low = instance:addStream("Low", core.Line, name .. ".Low", "Low", instance.parameters.LOW, 0);
    hUP=instance:addInternalStream(0, 0);
    hDN=instance:addInternalStream(0, 0);
    lUP=instance:addInternalStream(0, 0);
    lDN=instance:addInternalStream(0, 0);
    High:setWidth(instance.parameters.widthLinReg);
    High:setStyle(instance.parameters.styleLinReg);
    Low:setWidth(instance.parameters.widthLinReg);
    Low:setStyle(instance.parameters.styleLinReg);
    instance:createChannelGroup("UpGroup","Up" , hUP, hDN, instance.parameters.UP, 100-instance.parameters.Transparency);
    instance:createChannelGroup("DnGroup","Dn" , lUP, lDN, instance.parameters.DOWN, 100-instance.parameters.Transparency);
end

function Update(period, mode)
   if (period>source:size()-N) and (period>=first+N) then
    local Max=core.max(source.high,core.rangeTo(source:size()-1,N));
    local Min=core.min(source.low,core.rangeTo(source:size()-1,N));
    core.drawLine(High,core.range(source:size()-N,period),Max,source:size()-N,Max,period);
    core.drawLine(Low,core.range(source:size()-N,period),Min,source:size()-N,Min,period);
    High[source:size()-1-N]=nil;
    Low[source:size()-1-N]=nil;
    local Open=source.open[source:size()-N];
    local Close=source.close[source:size()-1];
    if Open>Close then
     core.drawLine(lUP,core.range(source:size()-N,period),Open,source:size()-N,Open,period);
     core.drawLine(lDN,core.range(source:size()-N,period),Close,source:size()-N,Close,period);
     core.drawLine(hUP,core.range(source:size()-N,period),0,source:size()-N,0,period);
     core.drawLine(hDN,core.range(source:size()-N,period),0,source:size()-N,0,period);
     lUP[source:size()-1-N]=nil;
     lDN[source:size()-1-N]=nil;
     hUP[source:size()-1-N]=nil;
     hDN[source:size()-1-N]=nil;
    else
     core.drawLine(hUP,core.range(source:size()-N,period),Close,source:size()-N,Close,period);
     core.drawLine(hDN,core.range(source:size()-N,period),Open,source:size()-N,Open,period);
     core.drawLine(lUP,core.range(source:size()-N,period),0,source:size()-N,0,period);
     core.drawLine(lDN,core.range(source:size()-N,period),0,source:size()-N,0,period);
     lUP[source:size()-1-N]=nil;
     lDN[source:size()-1-N]=nil;
     hUP[source:size()-1-N]=nil;
     hDN[source:size()-1-N]=nil;
    end
   else
    High[period]=nil;
    Low[period]=nil;
   end
   
end

NCandle.lua
(3.89 KiB) Downloaded 1592 times

Modified NCandle.lua
(5.85 KiB) Downloaded 750 times

MT4/MQ4 version
viewtopic.php?f=38&t=66241
Alexander.Gettinger
FXCodeBase: Confirmed User
 
Posts: 3785
Joined: Wed Mar 31, 2010 9:40 pm
Location: Russia, Omsk

Re: N periods candle indicator

Postby mcarr005 » Wed Oct 27, 2010 10:15 pm

Thank you very much, nice work!
mcarr005
 
Posts: 12
Joined: Mon Oct 25, 2010 2:56 pm

Re: N periods candle indicator

Postby rretch » Wed Jul 27, 2011 4:08 pm

More nice work Alexander.

Would it be possible to make the indicator with an option to not include the current bar?


thanks -rob
rretch
 
Posts: 25
Joined: Mon Jun 06, 2011 3:08 pm

Re: N periods candle indicator

Postby dirtycat » Mon Sep 26, 2011 10:06 pm

rretch wrote:More nice work Alexander.

Would it be possible to make the indicator with an option to not include the current bar?


thanks -rob


I second that!

thx

d
dirtycat
 
Posts: 3
Joined: Fri Aug 26, 2011 11:03 pm

Re: N periods candle indicator

Postby Apprentice » Tue Sep 27, 2011 4:53 am

I will forward this request to Alex.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: N periods candle indicator

Postby Alexander.Gettinger » Thu Oct 06, 2011 11:10 am

Please, see this version of indicator.
Alexander.Gettinger
FXCodeBase: Confirmed User
 
Posts: 3785
Joined: Wed Mar 31, 2010 9:40 pm
Location: Russia, Omsk

Re: N periods candle indicator

Postby dirtycat » Tue Oct 11, 2011 2:49 am

is there any way possible to make this a strategy? if current candle closes below the lowest price of n periods go short.. vice versa for long. I have searched the strategies and havent seen anything for a simple strategy as this.. thanks

dc
dirtycat
 
Posts: 3
Joined: Fri Aug 26, 2011 11:03 pm

Re: N periods candle indicator

Postby dirtycat » Tue Oct 11, 2011 3:00 am

aww scratch that.. found one that does exactly what I need thanks for the aweseome indicator

dc
dirtycat
 
Posts: 3
Joined: Fri Aug 26, 2011 11:03 pm

Re: N periods candle indicator

Postby Apprentice » Sun Mar 19, 2017 10:21 am

Indicator was revised and updated.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: N periods candle indicator

Postby elsigis » Mon Aug 07, 2017 9:36 am

Great work guys! I have been using this indicator for about 2 years now. A longer setting of 10 to 15 bars shows when a market is in a range and 3 to 5 period setting gives some of the best stoploss levels in trending markets.

I would like to request a small feature to be added: ignore inside bars; So that when and inside bar occurs one can have the option of the indicator counting the bars but ignoring the inside bar. This will help to reduce the chance of being stopped out too early when the market slows down.

Thanks you.
elsigis
 
Posts: 21
Joined: Mon Sep 27, 2010 11:34 pm

Next

Return to Custom Indicators

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 47 guests