amibroker

Need help in Amibroker AFL code when I hard code previous days to scan it works fine but when I get it by a function it gives wrong result


I am trying to get the High and Low for N number of days where N is the days when InnerCandle formed. from that day to till day High Low.

Code works fine when I put Days hardcoded but when I get it by function, it returns all bars High Low.

chart

Below is the code for high low till innerCandle

_SECTION_BEGIN("N_Day_HiLo");

YDayH = TimeFrameGetPrice("H", inDaily, -1);  // yesterdays high
YDayL = TimeFrameGetPrice("L", inDaily, -1);  //  low
YDayC = TimeFrameGetPrice("C", inDaily, -1);  //  close
DayO = TimeFrameGetPrice("O", inDaily);  // current day open
////////////////////////////////////////////////////////////////////////////////////////////////////////
Y2DayH = TimeFrameGetPrice("H", inDaily, -2);  // day before yesterdays high
Y2DayL = TimeFrameGetPrice("L", inDaily, -2);  //  day before low
Y2DayC = TimeFrameGetPrice("C", inDaily, -2);  //  day before close
////////////////////////////////////////////////////////////////////////////////////////////////////////

 
 PlotShapes(IIf(YDayH<Y2DayH AND YDayH>Y2DayL AND YDayL>Y2DayL AND YDayL<Y2DayH, shapeSquare, shapeNone),colorWhite, 0, H, Offset=55);

 
 PlotShapes(IIf(Ref(H<YDayH AND H>YDayL AND L>YDayL AND L<YDayH,1), shapeHollowCircle, shapeNone) , colorRed, 0,H, Offset=90);


 function getInnerOuterCandleLocation(lookBackDays){
 position=0;
 if( lookBackDays <= BarCount ){
  innerCandleFromedPosition = 0;
  
  for(i = BarCount-1; i > BarCount-lookBackDays-1; i--)
  {

cond1 = YDayH[i]<Y2DayH[i];
cond2 = YDayH[i]>Y2DayL[i];
cond3 = YDayL[i]>Y2DayL[i];
cond4 = YDayL[i]<Y2DayH[i];

cond = cond1 AND cond2 AND cond3 AND cond4;

  if(cond){
      innerCandleFromedPosition=i;
      break;
    }
  }
  
  position = 200-innerCandleFromedPosition;
  }
  else {
  position = 0;
  }
  printf("day to inner candle %g", position);
  return position;
}
 



lookBackDays = Param("Look Back", 10, 1, 60, 1);

// Set chart display parameters
// Chart background is Black,
// Date Axis displayed,
// Long titles wrapped to next line
 
SetChartOptions(0, chartShowDates | chartWrapTitle);
SetChartBkColor(colorBlack);
 
// Locate Highest HIGH and Lowest LOW in last N days
 
N = getInnerOuterCandleLocation(lookBackDays); //daysback; // i am getting problem here if i put number hard coded it works fine

PriceStyle = ParamStyle("Chart Type", styleCandle, maskPrice);
LineStyle = ParamStyle("Line Style");
 
NDayHi = LastValue(HHV(H,N));// H[BarCount - 1 - N];
NDayLo = LastValue(llV(L,N)); //L[BarCount - 1 - N];

 
for(i = BarCount - 1 - N; i < BarCount - 1; i++)
{
if(H[i] > NDayHi)
{
NDayHi = H[i];
//XH = i;
}
if(L[i] < NDayLo)
{
NDayLo = L[i];
//XL = i;
}
}
 
 
 Filter = 1;
 
AddColumn(NDayHi,"High");
AddColumn(NDayLo,"Low");


 
// Define the Lines to be drawn
 
HLine = LineArray(BarCount - 1 - N, NDayHi, BarCount - 2, NDayHi);
LLine = LineArray(BarCount - 1 - N, NDayLo, BarCount - 2, NDayLo);
 
// Plot chart
 
_N(Title = StrFormat("{{NAME}} ({{INTERVAL}}) {{DATE}} {{OHLCX}} Vol=%1.0f\n{{VALUES}}", V));
 
Plot(C, "", colorGrey50, PriceStyle);
Plot(Hline, WriteVal(N, 1.0) + " Day Hi", colorBrightGreen, LineStyle);
Plot(LLine, WriteVal(N, 1.0) + " Day Lo", colorYellow, LineStyle);
 
_SECTION_END();

I am getting problem here:

N = getInnerOuterCandleLocation(lookBackDays); //daysback; // 

If i put number hard coded it works fine


Solution

  • A little too much for me to dissect but maybe this will help you out.

    You can easily find where the inside bars are using Amibrokers Inside() function that returns a boolean array of true when the bar is an inside bar or otherwise false.

    IsBarInside = Inside();                 // Array. True/1 if the bar is an inside bar.
    IsNextBarInside = Ref(IsBarInside, 1);  // Shift the array by +1 so that the next bar is aligned.
    IsLastBarInside = Ref(IsBarInside, -1); // Shift the array by -1 so that we are looking at the last bars value.
    
    PlotShapes(IIf(IsLastBarInside, shapeSquare, shapeNone), colorWhite, 0, H, Offset=55);
    PlotShapes(IIf(IsNextBarInside, shapeHollowCircle, shapeNone), colorRed, 0, H, Offset=55);