can anyone tell me why the code below fails to draw line(testBuffer) on the chart? Thanks a lot

 
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_label1  "Test Trailing Stop"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrMagenta
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2

double testBuffer[];

int OnInit()
{
   SetIndexBuffer(0, testBuffer, INDICATOR_DATA);
   ArraySetAsSeries(testBuffer, true);
   ArrayResize(testBuffer, Bars(_Symbol, _Period));
   ArrayInitialize(testBuffer, EMPTY_VALUE);
   IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   return(INIT_SUCCEEDED);
}

void OnTick()
{
   static int last_bar_count = 0;
   int current_bar_count = Bars(_Symbol, _Period);
   if(current_bar_count > last_bar_count)
   {
      double src_array[] = { iOpen(_Symbol, _Period, 0)};
      ArrayInsert(testBuffer, src_array, 0, 0, 1);
      last_bar_count = current_bar_count;
   }
   testBuffer[0] = iClose(_Symbol, _Period, 0); // Use iClose to get close price
   ChartRedraw();
}
 
qdp qdp:

Maybe you need to read the online documents again.

OnTick is not used in indicators.

 
Michael Charles Schefe #:

Maybe you need to read the online documents again.

OnTick is not used in indicators.

I  know this is not an indicator, but an EA。Is it possible to use SetIndexBuffer to draw lines on the chart? For instance, I want to draw trailing stop on the chart with non-EMPTY_VALUE when there is an open position , and EMPTY_VALUE with closed position?
 
qdp qdp #:
I  know this is not an indicator, but an EA。Is it possible to use SetIndexBuffer to draw lines on the chart? 

no. I suggest that you search this site, and specificly codebase. I am sure you will find an ea or indicator that will do what you want, already coded.