3 consecutive red close within a limited period

Can anyone please help me with the following

Need an AFL entry to find the last 3 consecutive red candles/bars (within the last 15* days) and
from that finding need to plot an horizontal line (extended) from the close of the middle red candle/bar.

found something similar to my request under : Consecutive down closes within only the last 20 bars : but I would like to plot the close from the middle bar of it
(i am not sure I posted this yesterday but whether it got posted or not in the forum due to verification pending, that time), sorry if it is in duplicate posting.
Thanks

1 Like

@shajan, Welcome to the forum.

Glad to see you are searching and reading.

Here is one that will help you out (in general) : How to ask a good question

Applying that here, show us your code (use code blocks), and tell us where you are having specific issues. Then the very helpful and knowledgeable members can give much more direct help.

1 Like

I am sorry if my request was not clear, please see this image . I want identify the red bars from last occurance and plot a line on the close value of the middle bar.!3redbar

@shajan,

I am sure there are many ways of doing this.... My initial point was you have not shown what you have tried.

If you show us the code, we can see how you are attempting to work out the problem. We don't know your coding skills or level, and don't know how you are trying to solve it.

But here are a couple of key words that might help...
Barssince, Sum, Ref.

Again, if you can show us your code, tell us where you are not getting the result that you want/expect, we are much more able to help. And, you will learn more. Most of the helpful members of this forum like helping, but do not want to be a "free" coding service.

2 Likes

The below is I tried with the price chart, but nothing shown......I tried to understand, as you mentioned, Barsince, Sum, Ref, to use to search backword but failed.

for ( i = BarCount-1; i < BarCount-15 ; i-- ); // search inside last 15 bars
tb=C[i]; //in finding third down bar
sb=C[i-1]; // second third bar...need close to plot
fb=C[i-2]; // first down bar in consecutive series

if (tb < sb)
{
if (sb <fb)

{
  myc=SelectedValue(sb);           //need close vALUE from second down bar
	   Plot(myc,"myc",colorPink,styleLine);  // trial for saved value
    Plot(sb,"secondbar",colorRed,styleLine);  //trial for direct value
}

else
{}
}
else
{
}

@shajan, glad to see you got your "verified badge".

As a New Forum Member, I am making the assumption that you are NEW to AB/AFL. Nothing here meant to offend.

Now looking at your code... in general "LOOPS" are bad (Not really, but generally not needed in AFL).

Let's start with a couple of Hints...

Hint 1:
Think of coding the AFL like you would in a Spreadsheet. Assume you can only look above the current cell, and to the left of the current cell (and above and too the left).
For Example. Say you are at Cell E5. You can reference any of the rows 1-5, and any Column A-E.
So if Row 1-4 contains the OHLC values, you can make row 5 contain the Average of all of them by calcuating. In Excel E5 = (A5+B5+C5+D5)/4. In AFL

myAvg = (O+H+L+C)/4;  // 

The AFL is an array calculation that does the work for ALL elements in the row.
Hope this helps.

Hint 2:
Using an Exploration is a great way to see what data values you are actually working with.
You will probably have to do some reading on this one...

Now on to your issue....
Let's start with a down close. Do you mean Down from Open or Down from Yesterday. You can easily change the code to what ever definition you want to use. But here is my starting point...

// 3 Consecutive red close

isdownclose = C<Ref(C,-1);
numdownclosein3 = Sum(isdownclose, 3);

Filter = 1;
AddColumn(isdownclose, "Down Close");
AddColumn(numdownclosein3, "# Down");


This code will create a (very) basic Exploration, where you can see the results.

Next step is ... learning about BarsSince.

As you can see I want you to do the work to learn. I hated it when I just wanted an answer, but I found that I learned more, and retained it better, since I did the work... Hope it is the same for you.

2 Likes

Thanks for the response , I will try to learn more from the given hint.

since you mentioned "Do you mean Down from Open or Down from Yesterday." I shall try to explain again.
from the last 15 days or say 21 days, there may be many occurance of continues negative close of 3 or more bars. (once I get into that, may be can tweak it for contunues down close from previous day). I want to find the last 3 contunes bar of negative close and plot a horizontal line from the close of 2nd bar (from that last 3)

I will try more and more to find a solution, (the problem I don't have any prior knowledge of any programming, and am not that young to read and remember things) so I am going thru different afl's and trying to learn from that by immediately extracting some portion of that.

@shajan, the Down comment was to make sure that YOU are sure what you are looking at.

This exploration shows both styles of down days. Just make sure you are coding the one you want.

// Forum Help
// 3 Consecutive red close

isdownclose = C<Ref(C,-1);  // Reference to Yesterday's Close
indaydownclose = C<O;
numdownclosein3 = Sum(isdownclose, 3);
numindaydownin3 = Sum(indaydownclose,3);

Filter = 1;  // Show ALL Data points

AddColumn(isdownclose, "Down Close");
AddColumn(numdownclosein3, "# Down");
AddColumn(indaydownclose, "In Day Down");
AddColumn(numindaydownin3, "# InDay Down");

Thanks. Will check it tomorrow when I have access to the computer where I have Amibroker installed.

In addition to what @snoopy.pa30 has provided you. You could study and adjust the below to suit your own specific requirements.

if (Status("Action") == actionIndicator)
{
	plotLine_Start = Null;
	plotLine_Extend = Null;

	upBar = Close > Open;
	downBar = Close < Open AND Close < Ref(Close, -1);
	downBars3 = LLV(downBar, 3);
	OccuranceBar = upBar AND Ref(downBars3, -1);
	
	plotLine_Start = IIf(BarsSince(OccuranceBar) < 3, ValueWhen(OccuranceBar, Ref(Close, -2), 1), Null);
	plotLine_Extend = ValueWhen(OccuranceBar, Ref(Close, -2),  1);

	PlotShapes(downBars3 * shapeCircle, colorLightBlue, 0, Low);
	PlotShapes(OccuranceBar * shapeStar, colorBlue, 0, Low);
	PlotShapes(OccuranceBar * shapeUpTriangle, colorRed, 0, Low, -12, -2); // Mark start of plot line

	Plot(plotLine_Start, "", colorBlueGrey, styleThick|styleLine, Null, Null, -2);
	Plot(plotLine_Extend, "", colorBlue, styleThick|styleLine);
}

Thank you @TrendSurfer. You mostly understood my requirement. I have tried the with it. In first look it is doing as what I need . But still I want it to limit to find in last 15bars, instead of it to search thru all bars.

As you said, will spend time on it and adjust it as per requirement, if anything further needed) (for identifying and plotting)....and will study further from afl programming guide about limiting it into last 15bars.

Thanks

// 3 Consecutive red close
redCandle = C<O AND Ref(C,-1)<Ref(O,-1) AND Ref(C,-2)<Ref(O,-2);

//sum how many 3orMore red candles found in last 15 candles
numRedCandle = Sum(redCandle, 15);

Filter = numRedCandle > 1;  // Show ALL Data points

AddColumn(redCandle, "redCandle");
AddColumn(numRedCandle, "NumredCandle");

@TrendSurfer... I tried with the formula you posted...somehow it is missing the later sequence. so I just tried to understand the formula you gave and started making some changes. it seems to start giving result,eventhough the last sequence was before 15days, i am happy that , I am slowly getting into it. just to show you am pasting an image, upper one is with your formula and below is with some adjustments in the formula

it seems image not loadedmyfile

Change this line

by (insert an equal sign after Less

Filter = numRedCandle >= 1;

@awilson Thank you for looking into my request. Please note that actually am looking to plot a line , not just to count the number of occurance.., but I will check whether I can use it and link it with some line to limit the bars

It's not missing - the condition was not met! Adjust condition to suit your logic.

Below is update code to include your 'Range Window' of 15 Bars.

if (Status("Action") == actionIndicator)
{
	plotLine_Start = Null;
	plotLine_Extend = Null;
	
	rangeWindow_15 = Barcount - BarIndex() < 15;

	upBar = Close > Open;
	downBar = Close < Open AND Close < Ref(Close, -1);
	downBars3 = LLV(downBar, 3);
	OccuranceBar = upBar AND Ref(downBars3, -1);
	OccuranceBar_InRange = OccuranceBar AND rangeWindow_15;
	
	plotLine_Start = IIf(BarsSince(OccuranceBar_InRange) < 3, ValueWhen(OccuranceBar_InRange, Ref(Close, -2), 1), Null);
	plotLine_Extend = ValueWhen(OccuranceBar_InRange, Ref(Close, -2),  1);
	
	PlotShapes(downBars3 * shapeCircle, colorLightBlue, 0, Low);
	PlotShapes(OccuranceBar * shapeStar, colorBlue, 0, Low);
	PlotShapes(OccuranceBar * shapeUpTriangle, colorRed, 0, Low, -12, -2); // mark first bar of plot line
	
	Plot(plotLine_Start, "", colorBlueGrey, styleThick|styleLine, Null, Null, -2);
	Plot(plotLine_Extend, "", colorBlue, styleThick|styleLine);
}

@TrendSurfer Thank you very much.

as you said it was not met the logic...and the reason is that "upbar". when it is there, if there are continues down bar of 4 (or more) or just a negative close prior to the 3 consecutive downbar, the formula ignores the sequence. So I used it without the usage of "upbar". and it works fine.

Evenif I try to learn , I would not have able to make such full formulas. So thank a lot for your helping mind.

1 Like