HHV price chart from the moment of Buy

Hello everyone,
I'm Andrzej and I have been using Amibroker for 1.5 years, so I'm a ~beginner and this is my first post :slight_smile: For 2 weeks now, I have been struggling to get the correct Highest High Price from the moment I Buy the contract. I used the BarsSince (Buy) function but on the chart you can see below - the yellow line - does not start at the High Price level at the Buy-bar, and sometimes the line breaks down, even though there was no Buy transaction. In my code, I used Exrem to filter extra signals, but that doesn't help. I was looking for a solution on the forum but did not find it.
Thank you in advance for pointing the way..

image

and the code:

SetOption("InitialEquity", 10000 );
SetOption( "FuturesMode", True);
SetOption( "AllowSameBarExit", True);
SetOption( "CommissionMode", 1 ); 
SetOption( "CommissionAmount", 0.5 );

RoundLotSize = 1;
PointValue = 10; 
TickSize = 1;

BuyPrice = Open;

Av2 = (Ref(O,-2)+Ref(C,-2))/2 < (Ref(O,-1)+Ref(C,-1))/2;
WT = Ref(C,-1)>Ref(MA(C,15),-1);
stopAmount = IIf( WT, ATR(5), 0.5*ATR(5));

Buy = Ref(MA(C,15),-1)>Ref(MA(C,15),-2) AND Ref(C,-1)>Ref(MA(C,15),-1) AND Ref(C,-1)>Ref(O,-1) AND Av2;
Sell = (Ref(O,0)<Ref(O,-1) AND Ref(O,0)<Ref(Avg,-1)) OR (Ref(O,0)<Ref(Avg,-2) AND (Ref(C,-1) < (BuyPrice-stopAmount)));

Buy =  Exrem(Buy, Sell); 
Sell = Exrem(Sell, Buy);


///////////////////////////////////////////////////////////////
NB = BarsSince(Buy); // number of bars
HP = Ref(HHV(H, NB),0);     // highest price since Buy
Plot( HP, "HighestPrice", colorYellow, styleLine | styleLine ); 
///////////////////////////////////////////////////////////////


PlotShapes( IIf(Ref(Buy, 0), shapeUpArrow, shapeNone), colorWhite, 0, Ref(BuyPrice, -1), -12 );
PlotShapes( IIf(Ref(Sell, 0), shapeDownArrow, shapeNone), colorYellow, 0, Ref(SellPrice, -1), -24 );

The answer is in the Knowledge Base:

Actually

HP = Ref(HHV(H, NB),0); // highest price since Buy

is the same thing as:

HP = HHV(H, NB);  // should be faster then Ref(HHV(H, NB),0)

and also

IIf(Ref(Buy, 0)

is the same thing as:

IIf(Buy, // should be faster then Ref(Buy, 0)

Here is the code for your "HHV since any Buy":

HP = HHV(H, BarsSince(Buy)); 
Plot( HP, "HighestSinceBuy", colorLime, styleDashed | styleLine ); 

Hope helps. :smiley:

There is HighestSince() function that does the thing directly
http://www.amibroker.com/f?highestsince

// single function call
result = HighestSince( Buy, High );

It is strongly recommended to display categorized list of functions to easily find better, direct ways of doing things:

http://www.amibroker.com/guide/a_catfunref.html

As I wrote in KB article, HighestSince() works fine if you don't have repeated excesive signals. If you do, you either have to remove them, or handle repetitions on your own.

1 Like

Thank you for your advices,
Alexlin - I simplified the formula by removing the Ref, I understand it will perform faster, however the line routing did not improve anyway..
I used direct function "HighestSince" and it looks OK now (see below), I can't believe the solution is so simple :slight_smile: - thanks Tomasz - I wrote like this :

HP = HighestSince( Buy,  High,  1 );

image

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.