Use SelectedValue(c) as Buy

I am researching discretionary buy signals (breakout of wedge, Cup and handle, ...) and testing different trailing stops. I am manually going through each stock and trying to simply select the breakout bar (SelectedValue) and have it plot a trail stop. I am most likely using SelectedValue wrong, but my impression was once I select the bar it will create the Buy signal and use the close price. Buy = SelectedValue(c); What happens is this creates multiple buy signals.

StopLevel = 1 - Param("trailing stop %", 20, 0.1, 30, 0.1)/100;
 
Buy = SelectedValue(c);
Sell = 0; 
trailARRAY = Null;
trailstop = 0;
 
for( i = 1; i < BarCount; i++ )
{
 
   if( trailstop == 0 AND Buy[ i ] ) 
   { 
      trailstop = High[ i ] * stoplevel;
   }
   else Buy[ i ] = 0; // remove excess buy signals

   if( trailstop > 0 AND Low[ i ] < trailstop )
   {
      Sell[ i ] = 1;
      SellPrice[ i ] = trailstop;
      trailstop = 0;
   }

   if( trailstop > 0 )
   {
      trailstop = Max( High[ i ] * stoplevel, trailstop );
      trailARRAY[ i ] = trailstop;
   }

} 
 
PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);

Plot( Close,"Price",colorBlack,styleBar);
Plot( trailARRAY,"trailing stop level", colorCustom12);

Screenshot_1

Thanks

@erictreaster welcome to this Community.

I suggest replacing your "Buy" line with:

bi = BarIndex(); 
Buy = (bi == SelectedValue(bi));
BuyPrice = SelectedValue(C);
3 Likes

beppe,

Thanks! That worked. I looked everywhere and couldn't figure it out. I wouldn't have figured this out on my own.

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