Highest high and lowest low of sell and buy bars

Hello everyone,

I have some observation which I don't understand them,

I have two time series one of them Buys and Second Sells,

How to get values of Sell highest high bar (Shh), Sell Lowest low bar (SLL), Buy highest high bar (Bhh) and Buy Lowest low bar (BLL)at the same time?

  for( i = 0; i < BarCount; i++ ) 
  {
   
       if( Buy[i] == 1 )
        {
        

            HighestHighBuy[i] = Max( H[i], HighestHighBuy[i - 1] );
            LowestLowBuy[i] = Min( L[i], LowestLowBuy[i - 1] );
            

        }
        else
        {
            HighestHighBuy[i] = H[i];
            LowestLowBuy[i] = L[i];

            lastBuyIndex = i;
        }

        if( Sell[i] == 1 )
        {

            HighestHighSell[i] = Max( H[i], HighestHighSell[i - 1] );
            LowestLowSell[i] = Min( L[i], LowestLowSell[i - 1] );
        }
        else
        {
            HighestHighSell[i] = H[i];
            LowestLowSell[i] = L[i];
            lastSellIndex = i;
        }


    


    if( Buy[i] == 1 )
    {
        lastBuyIndex = i;
    }



    if( Sell[i] == 1 )
    {
        lastSellIndex = i;
    }
    
    
}



Capture

Hi ahm.montaser,
in this kind of situation I usually store the information into different variables, so for example I would approach the solution storing 4 arrays:

  • one for the bars that are local highest when buy condition is true
  • one for the bars that are local lowest when buy condition is true
  • one for the bars that are local highest when sell condition is true
  • one for the bars that are local lowest when sell condition is true

each one could be determined writing something like this

bi = BarIndex();
//	naive system
trend = MA(C, 20);	//	
buyCondition = C > trend;
sellCondition = NOT buyCondition;

//	searching for the local highest high when buyCondition is true
buyEvent = ExRem(buyCondition, sellCondition);
highestSinceBuy_val = IIf(buyCondition, HighestSince(buyEvent, H, 1), Null);
highestSinceBuy_pos = IIf(buyCondition, HighestSinceBars(buyEvent, H, 1), Null);

buyLocalMax = 0;    // >> array that stores highest value at the right bar index <<

for ( idx = 0; idx < BarCount; idx++)
{
	if (buyCondition[idx] == False AND buyCondition[idx -1] == True)
	{
		shift = highestSinceBuy_pos[idx - 1];
		buyLocalMax[idx - shift - 1] =  highestSinceBuy_val[idx -1];
	}
}

//     write here the other cases... 
//     [...]

//    plots
PlotOHLC(O, H, L, C, "", colorGrey40, styleBar);
Plot(trend, "trend", colorBlue, styleLine|styleThick);
PlotShapes(IIf(buyCondition, shapeHollowUpArrow, shapeHollowDownArrow),IIf(buyCondition, colorPaleGreen,colorPink), 0, IIf(buyCondition, H, L));
PlotShapes(IIf(buyLocalMax != 0, shapeUpTriangle, shapeNone), colorBlue, 0, H, 20);

image

3 Likes

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