Hello
Is there a way to identify continuous trending bars for example continuous green bars based on n% move like below chart ?
Thanks
Deepak
Hello
Is there a way to identify continuous trending bars for example continuous green bars based on n% move like below chart ?
Thanks
Deepak
@jvdeepak of course this is possible. Start by defining your terms.
Start simple, show us your code on what you consider to be a "green bar".
And clarify what do you mean ?
@portfoliobuilder thanks for your reply.
This is how I define my GreenBar and I want to detect a series of such bars for 4% move. Particularly I am not sure how to detect this 4% move with such series of bars.
GreenBar = Ref(C,-1) > Ref(O,-1) AND C > Ref(C,-1) AND H > Ref(H,-1);
Regards
Deepak
Example
/// @link http://forum.amibroker.com/t/continuous-green-bars/5600/4
n_percent = 4;// minimum percentage run
nbars = 5;// minimum number of consecutive bars
GreenBar = Ref(C > O, -1) AND ROC(C, 1) > 0 AND ROC( H, 1) > 0;
condition = Sum(GreenBar, nbars ) >= nbars AND ROC(C, nbars) >= n_percent;
// alternative
//condition = Sum(GreenBar, nbars ) >= nbars AND (C - Ref(O,-nbars)) / Ref(O,-nbars) *100 >= n_percent;
Plot( C, "Price", colorDefault, styleCandle );
PlotShapes( condition * shapeUpArrow, colorGreen, layer = 0, L );
@jvdeepak since I was working on the answer when @fxshrat put his very nice work up, I thought I would post mine too.
I had a slightly different take on it, and used the 4% move as a move from the "Low" of the last bar that was not a "Green Bar" (obviously open to interpretation).
GreenBar = Ref( C, -1 ) > Ref( O, -1 ) AND C > Ref( C, -1 ) AND H > Ref( H, -1 );
CountConsecutiveGB = BarsSince( !GreenBar );
pctThreshold = 1.04; // you could create a Param here for values different that 4%
StartingLow = ValueWhen( !GreenBar, Low, 1 ); // the Low before the first Green Bar in the current sequence
// You want current bar to be Green and more than 4% from Low of last non-Green Bar
TargetAchieved = GreenBar AND Close > pctThreshold * StartingLow;
// let's debug this using Explorations
//////////////////
// Explore
//////////////////
GreenBar_color = IIf( GreenBar, colorLime, colorDefault );
StartingLow_color = IIf( GreenBar, colorRose, colorDefault );
Target_color = IIf( TargetAchieved, colorLightBlue, colorDefault );
Filter = 1;
AddColumn( Close, "Close" );
AddColumn( Low, "Low" );
AddColumn( GreenBar, "GreenBar", 1.0, colorDefault, GreenBar_color );
AddColumn( CountConsecutiveGB, "Count Consecutive GB", 1.0 );
AddColumn( StartingLow, "StartingLow", 1.2, colorDefault, StartingLow_color );
AddColumn( TargetAchieved, "TargetAchieved", 1.0, colorDefault, Target_color );
AddColumn( IIf( TargetAchieved, pctThreshold*StartingLow, Null ), "Target Level" );
Producing an Exploration (which I use for debugging almost everything, and I'm not sure if that's a good or bad thing)
//GreenBar = Ref(C,-1) > Ref(O,-1) AND C > Ref(C,-1) AND H > Ref(H,-1);
GreenBar = C > Ref(C,-1) AND H > Ref(H,-1);
npercent = 0.05;// minimum percentage run
lastgreenbar = Greenbar AND NOT Ref(Greenbar ,1);
firstgreenbar = Greenbar AND NOT Ref(Greenbar ,-1);
numgreenbars = BarsSince(firstgreenbar)+1;
upmovestartprice = Ref(Close, -numgreenbars );
largemove = (((Close- upmovestartprice )/upmovestartprice )> npercent) AND lastgreenbar ;
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
PlotShapes( largemove * shapecircle, colorblue, 0, H, 30 );
PlotShapes( lastgreenbar * shapeDownArrow, colorRed, 0, H );
PlotShapes( firstgreenbar * shapeUpArrow, colorGreen, 0, L );
Thank you @fxshrat and @portfoliobuilder.
@bobptz I suspect this is forward looking due to this line ?
Exactly. There is no way to know that you have the last GreenBar of the upward move, unless you check the next bar and see that it is a negative candle.
But you get a very good idea and you can adjust the code according to your needs.
in my opinion if you are looking for a sudden surge in price, either up or down in order to respond with a quick trade, hoping for a retrace of the price, it is more interesting to look at the relative price move within a certain number of bars.
for instance the chart below shows an up-surge and a down-surge.
the up-surge is calculated using:
SurgeATRPeriod = Param( "Surge ATR Period", 100, 2, 200, 1 );
SurgeATRMultiple = Param( "Surge ATR Multiple", 5, 0.1, 10, 0.1 );
SurgeLookback = Param( "Surge Lookback Period", 3, 1, 20, 1 );
UpSurgeSetup = H - Ref( L, -SurgeLookback ) > Ref( ATR( SurgeATRPeriod ), -SurgeLookback ) * SurgeATRMultiple;
@empottasch you read my mind except i was looking for only green bars (ie., close > open) or red bars ( C < O) during this rise. Thanks for your input, much appreciated.
you can add that as an additional constraint:
GreenBars = Sum( C > O, SurgeLookback ) == SurgeLookback;
UpSurgeSetup = H - Ref( L, -SurgeLookback ) > Ref( ATR( SurgeATRPeriod ), -SurgeLookback ) * SurgeATRMultiple && GreenBars;
HI
I also need same .please share if you find.
please send afl code