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