How to explore %ROC by Start Date to To Date

In the figure shown below , I use ROC(C,1).
2018-08-12_011600

My need is the fact that I would like to explore stock in any watch list by what percent rate of change from start date to end date.
for example AAV
start date 1/8/18 price=4.62
to date 10/8/18 price=4.24
% rate of change =(4.24-4.62)/4.62 = -8.22%

I don't know how to implement in AFL or enable for using in window analysis.

I show this feature in Efinance (local app in my county );

Day

Thank you in advance.
Pongthorn

fbr = Status( "firstbarinrange" );
lbr = Status( "lastbarinrange" );

startprice = ValueWhen(fbr, C);
endprice = ValueWhen(lbr, C);

rc = (endprice - startprice) / startprice * 100;

AddColumn( rc, "ROC", 1.2 );
Filter = 1;
//OR
// Filter = lbr;
7 Likes

@technqvi Fxshrat was faster, but because I also started working on this, here's my code:

bi = BarIndex();

SFBIR = Status( "firstbarinrange" );
SLBIR = Status( "lastbarinrange" );

Filter = SLBIR;

StartPrice = ValueWhen( SFBIR, C );
EndPrice = ValueWhen( SLBIR, C );
StartPriceBI = ValueWhen( SFBIR, bi );
EndPriceBI = ValueWhen( SLBIR, bi );

NumBars = EndPriceBI - StartPriceBI;
PercChange = ( EndPrice - StartPrice ) / ( StartPrice + 1e-9 ) * 100;

AddColumn( StartPrice, "StartPrice", 1.2 );
AddColumn( EndPrice, "EndPrice", 1.2 );
AddColumn( PercChange, "ROC  " + NumBars, 1.2, colorWhite, IIf( PercChange > 0, colorGreen, IIf( PercChange < 0, colorRed, colorBlue ) ) );

Image%203

6 Likes

Both 2 ways is ok for me.

Dear friend,

I just saw your wonderful code and was just trying to learn it. But its not working for me from my side. Even i pasted the same code of yours still not working don't know what's the problems. I'm using AB 5.70

Do check from your side..

bi = BarIndex();

SFBIR = Status( "firstbarinrange" );
SLBIR = Status( "lastbarinrange" );

Filter = SLBIR;

StartPrice = ValueWhen( SFBIR, C );
EndPrice = ValueWhen( SLBIR, C );
StartPriceBI = ValueWhen( SFBIR, bi );
EndPriceBI = ValueWhen( SLBIR, bi );

NumBars = EndPriceBI - StartPriceBI;
PercChange = ( EndPrice - StartPrice ) / ( StartPrice + 1e-9 ) * 100;

AddColumn( StartPrice, "StartPrice", 1.2 );
AddColumn( EndPrice, "EndPrice", 1.2 );
AddColumn( PercChange, "ROC  " + NumBars, 1.2, colorWhite, IIf( PercChange > 0, colorGreen, IIf( PercChange < 0, colorRed, colorBlue ) ) );

err1

Waiting for your feedback.

@sonjoejh This code calculates percent rate of change from start date to end date, so you need to change your Exploration's Range from 1 recent bar to From-To dates. It is even highlighted in yellow on my screenshot above.

obraz

Read here:

https://www.amibroker.com/guide/h_exploration.html
https://www.amibroker.com/guide/h_newanalysis.html


5.70 is an outdated version - not supported on this official forum. Don't be surprised if some of the codes which you can find here will not work for you, because during this 5 years (since that version was released), lots of new, useful functionalities have been implemented. I highly recommend updating to the newest version.

1 Like

Thank You my friend for pointing my mistake...

1 Like

Dear Friend,

I was just reworking on to find all stocks which have given only negative ROC. Just see the code but its not giving any results for me...don't know were is the error.

//Exploration to find stocks which have given negative return on capital.

bi = BarIndex();

fbr = Status("firstbarinrange");
lbr = Status("lastbarinragne");

startprice = ValueWhen(fbr,C);
endprice = ValueWhen(lbr,C);
startpriceBI = ValueWhen(fbr,bi);
endpriceBI = ValueWhen(lbr,bi);

rtoc = (endprice - startprice) / (startprice) * 100;

IsBelow = rtoc < 0;

Filter = IsBelow;

AddColumn(startprice,"Beg.Price",1.2);
AddColumn(endprice,"End.Price",1.2);
AddColumn(rtoc,"ROC",1.2,colorRed);

Awaiting for your reply.

Thanks a lots...Good Day to you.

This code works:

bi = BarIndex();

SFBIR = Status( "firstbarinrange" );
SLBIR = Status( "lastbarinrange" );

StartPrice = ValueWhen( SFBIR, C );
EndPrice = ValueWhen( SLBIR, C );
StartPriceBI = ValueWhen( SFBIR, bi );
EndPriceBI = ValueWhen( SLBIR, bi );

NumBars = EndPriceBI - StartPriceBI;
PercChange = ( EndPrice - StartPrice ) / ( StartPrice + 1e-9 ) * 100;

Filter = SLBIR AND PercChange < 0;

AddColumn( StartPrice, "StartPrice", 1.2 );
AddColumn( EndPrice, "EndPrice", 1.2 );
AddColumn( PercChange, "ROC  " + NumBars, 1.2, colorRed );

1 Like

Thank you my dear friend and for your prompt response.

@Milosz Thank you for a copy of your code.

Your code calculates percent rate of change from start date to end date , whereas I would prefer it calculate the rate of change from buy date to end date is this possible?

Displaying the ROC for the time in the trade would be useful for me.

SFBIR = Status( "firstbarinrange" );
//to
SFBIR = Status( "FirstVisibleBar" );
//didn't work as expected

any guidance?

The code I'm currently using only displays the ROC for the last period (weekly strategy) whereas I would prefer it calculate the rate of change from buy date to end date and NOT the last period.

PercentChg = ( ( C - Ref( C, -1 ) ) / Ref( C, -1 ) ) * 100;
AddColumn( IIf( BuySetUp == 1 OR TrailStopEx == 1, Close, Null ), "Close", 1.3 );
AddColumn( IIf( BuySetUp == 1 OR TrailStopEx == 1, PercentChg, Null ), "% Chg", 1.2, IIf( PercentChg > 0, colorGreen, IIf( PercentChg < 0, colorRed, colorBlue ) ) );
AddColumn( Lbit, "Weeks", 1.0, IIf( StaleStopHit == 1 AND TrailStopEx == 1, colorBlack, colorWhite ) ) ;

Skate

If you are interested in the rate of change from the latest Buy signal to the current moment (last bar), you can run below code selecting Range to 1 recent bar(s).

Buy%20ROC

A very simple example (not checking if the position is still open etc.):

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() );

bi = BarIndex();
Average1 = MA( C, 20 );
Average2 = MA( C, 80 );

Buy = Cross( Average1, Average2 );
Sell = Cross( Average2, Average1 );

Plot( average1, "AVG 1", colorViolet, styleLine | styleThick );
Plot( average2, "AVG 2", colorbrown, styleLine | styleThick );
PlotShapes( Buy*shapeCircle, colorGreen, 0, H );
PlotShapes( Sell*shapeCircle, colorRed, 0, L );

if( Status( "action" ) == actionExplore )
{

    LastBuyBi = LastValue( ValueWhen( Buy, bi ) );

    Filter = Status( "lastbarinrange" ) AND LastBuyBi > 0;

    StartPrice = LastValue( ValueWhen( Buy, C ) );
    EndPrice = LastValue( C );
    NumBars = LastValue( bi ) - LastBuyBi;
    LastBuyROC = ROC( C, NumBars );

    AddColumn( StartPrice, "Last Buy price", 1.2 );
    AddColumn( EndPrice, "End price", 1.2 );
    AddColumn( NumBars, "Bars since last Buy", 1 );
    AddColumn( LastBuyROC, "ROC (%) since last Buy", 1.2, colorWhite, IIf( LastBuyROC > 0, colorGreen, IIf( lastBuyROC < 0, colorRed, colorBlue ) ) );
}

4 Likes

Hi @skate,

Not sure if you need this or not but with @Milosz's code above, if a Buy is true when 'Status( "lastbarinrange" )' code will throw error because ROC Range cannot be zero.

Replace the two appropriate lines with these to prevent error:

 NumBars = Max( LastValue( bi ) - LastBuyBi, 1);
 LastBuyROC = IIf( Buy, 0, ROC( C, NumBars ) );
1 Like

Thank you for this beautiful code fxshrat!
Regards,
Tony R

Is there a way to get a series of dates, not just the start and end dates. The reason is that I am tracking movement with specific dates in mind. So the start date could be 14 Dec 2019 for instance, and a middle date say 23 MAr 2020 and end date being current date. I would reckon it would be to add the day count someplace but I am just starting out with AB. I have no coding experience so it is a challenge for me. Could there be more than one middle date. Many thanks. The code works perfectly for the start and end and I have been able to get some more data on the analysis front by adding columns.

Used your code and many thanks. Is there a way to get a series of dates, not just the start and end dates. The reason is that I am tracking movement with specific dates in mind. So the start date could be 14 Dec 2019 for instance, and a middle date say 23 MAr 2020 and end date being current date. I would reckon it would be to add the day count someplace but I am just starting out with AB. I have no coding experience so it is a challenge for me. Could there be more than one middle date. Many thanks. The code works perfectly for the start and end and I have been able to get some more data on the analysis front by adding columns.