Asking about making Highestsince function with If statement

Hi All

As the buy signals are repetitive, I can not use the Highestsince

My goal is to make Higestsince(Buy, close) that I want to find the highest close value while the Buy signal is still open.

That's why I tried to make Higestsince with If statement but I can not find where my coding is wrong.

please check the below coding and if u know how to fix

note : the below coding is following the some question which is

I added only the New Highestsince looping

_SECTION_BEGIN( "Test" );
SetChartOptions( 0, chartShowArrows | chartShowDates );

//Plot CandleSticks
//Plot(C, "Price", ParamColor( "Color", colorDefault), ParamStyle( "Style", styleCandle, maskPrice ) );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " + WriteVal( V, 1.0 ) + " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

// Contract specifications
TickSize = 0.25;
PointValue = 50;
RoundLotSize = 0;
PositionSize = MarginDeposit = 1;

//Money Management
SetPositionSize( 1, spsValue ) ; // 1 Lakh in each Trade

//TRADE DELAYS
SetTradeDelays( 0, 0, 0, 0 );

/// Code source
/// https://forum.amibroker.com/t/counting-bars-in-trade-resulting-in-2-sell-exit-strategies/1255/2
/// by Tomasz Janeczko
r = RSI( 2 );
Buy = r < 22; // state form

Sell1 = r > 72; // state form
Sell2 = r > 92; // state form

Sell = 0;

BarsInTrade = arrBarsInTrade = 0;
Highestprice = arrsHighestprice = 0;
nbars = 10;
for( i = 0; i < BarCount; i++ ) {
    if( BarsInTrade > 0 )
        BarsInTrade++;
    else if( Buy[ i ] ) 
		BarsInTrade = 1;
		
	if(Highestprice == 0 && Buy[ i ])
	{
	Highestprice = Highest(Close);
	}
		

    if( BarsInTrade < nbars AND Sell1[ i ] ) {
        Sell[ i ] = 1;
        BarsInTrade = 0;
        Highestprice = 0;
    }

    if( BarsInTrade >= nbars AND Sell2[ i ] ) {
        Sell[ i ] = 1;
        BarsInTrade = 0;
        Highestprice = 0;
    }
    
    arrBarsInTrade[ i ] = BarsInTrade;// this is array variable
    arrsHighestprice[ i ] = Highestprice;}
AddColumn( Buy, "Buy" );
AddColumn( Sell, "Sell" );
AddColumn( arrBarsInTrade, "BarsInTrade" );

BuyPrice = Close;
SellPrice = Close;

Filter = 1;

_SECTION_END();

Using an array function in BarCount loop is bad performance and not good code.

If you just want the highest Close while in trade,
then you could just modify the previous if statement a bit like this.

    if( BarsInTrade > 0 ) {
        BarsInTrade++;
        Highestprice = Max( Highestprice, Close[i] );   // code for highest close
    }
    else if( Buy[ i ] ) 
		BarsInTrade = 1;
1 Like

You have marked your own post with the same wrong code as Solution which is not the case.
Kindly correct that and mark my post if it worked.

Ah Sorry for marking the wrong reply and I make it correct

Thank you nsm

1 Like

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