indicator doesn't show arrows

 

Hello


I am writing my first indicator and I want to show buy and sell signals with arrows. But somehow it doesn't work, is there anybody who can help me?


In this part of code I make a average of the Buffer0. When the direction of the Indicator changes, I want it to give a signal, but it doesn't work:


double now=1,last=0;
int direction=0;
int j;

...

double     buffer0[];
double     buffer1[];
double     UpArrow[];
double     DnArrow[];

...

   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
   SetIndexBuffer(0,buffer0);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexBuffer(1,buffer1);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,234);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,233);
   SetIndexBuffer(2,UpArrow);
   SetIndexBuffer(3,DnArrow);

...

int Limit=Bars-counted_bars; 
  for(j=0; j<Limit; j++)
   {
     buffer1[j]=iMAOnArray(buffer0,Bars,CrossLine,0,MODE_SMMA,j);
     now=buffer1[j];
   } 
   
  if(now>last)
   {
     if(direction<1)
      {
       UpArrow[0]=Close[0];
       Alert("BUY");
      } 
     direction=1;
   }  
  if(now<last)
   {
     if(direction>-1)
      {
       DnArrow[0]=Close[0];
       Alert("SELL");
      } 
     direction=-1;
   } 


There aren't any error messages...but there are no arrows or alert!


leMai

 
Last doesn't have a value and direction will probably never get a value since it is initialised to 0 and the tests are < not <=.
 
Ruptor:
Last doesn't have a value and direction will probably never get a value since it is initialised to 0 and the tests are < not <=.

Ok, thank you, I forgot to change the value of last after the code, now it works fine. The arrows are in the indicator window, what can I do that they appear in the chart window, but the rest of the indicator is still in the a seperate window?


Thanks for your help!!


leMai

 
#property indicator_chart_window
// Not
#property indicator_separate_window
 
I have never had the requirement for the arrows not to be on the indicator. If I did I would take the simple route of making 2 indicators one that draws the arrows on the price and hides the indicator line and the other that puts the indicator in a separate window without the arrows or has them as well. A template can then be set with both indicators so it is no trouble to load. Alternatively you could scale the indicator so it can be displayed in the chart window with the arrows.
 

Ok, thank you, before I try the possibility with the 2 indicators, I try to draw additional arrows in the chart window, but I don't know why. Unfortunaltelly I can't write anything to the buffer/SetIndexBuffer(as far as I know) and if I put in the close-function, it displays it in the indicator window! So what can I do?


leMai

 

So, now, in live, I have a new problem...offline the indicator shows the signlas in the right time, but when it's running online, there is even every finished minute a sell and a buy signal! this is my current code:


double now=1,last=0;
int direction=0;

...

int Limit=Bars-counted_bars; 
  for(j=0; j<Limit; j++)
   {
     buffer1[j]=iMAOnArray(buffer0,Bars,CrossLine,0,MODE_SMMA,j);
     now=buffer1[j];
     if(now>(last+(difference/1000)))
     {
       if(direction<1)
       {
        UpArrow[j]=buffer1[j];
        direction=1;
        Alert("BUY");
       } 
     }
       else  
         if(now<(last-(difference/1000)))
         {
           if(direction>-1)
           {
            DnArrow[j]=buffer1[j];
            direction=-1;
            Alert("SELL");
           }        
         } 
     last=buffer1[j];
   } 

Maybe it would help to if it would only go into the direction loop(the now&last-loops) if the bar(in this case one minute) is finished and a new begins! but I don't know how to code it but maybe that isn't the fault!

Is there a possibility that the start-function only starts, if a new bar has began?

Can you help me?

Thanks for your help!



leMai

 

Every tick on the open bar changes the MA so it is usual to make the decision on bar 1 not 0 otherwise you can get multiple directions as the MA flicks up and down.

 

Ok, I corrected everything, if I make a beacktest it looks fine....when I make an online test it shows arrows and signals, too...BUT there are sometimes "BUY" or "SELL" signals, but now arrow in the indicator window, sometimes both appears, and if I let the indicator run for a while and open a second indicator window with the same indicator, it shows other arrows at another moment since I started the other one! So how can this be? Please have a look at the whole indicator:


//--------------------------------------------------------------------
//                                             Copyright © 2010, LeMai 
//--------------------------------------------------------------------
#property copyright "Copyright © 2010,LeMai"
#property indicator_separate_window

#property indicator_buffers 4
#property indicator_color1 Orange
#property indicator_color2 Blue
#property indicator_color3 LimeGreen
#property indicator_color4 Red

extern int barsToCount=34;   
extern int CountBars=10000;   
extern int CrossLine=4;
extern double difference=2.0;

int j;
double now=1,last=0;
int direction=0;
double     buffer0[];
double     buffer1[];
double     UpArrow[];
double     DnArrow[];
//--------------------------------------------------------------------
int init()
{
   IndicatorShortName(StringConcatenate("TrendLR (",barsToCount,")"));
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
   SetIndexBuffer(0,buffer0);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexBuffer(1,buffer1);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,233);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,234);
   SetIndexBuffer(2,UpArrow);
   SetIndexBuffer(3,DnArrow);
   return(0);
}

//--------------------------------------------------------------------
int deinit()
{  
   return(0);
}

//--------------------------------------------------------------------
int start()
{ 
  int     counted_bars = IndicatorCounted(); 
  double  a, b, c, sumy, sumx, sumxy, sumx2;
  double  prev; 
  double  current; 
  if(Bars <= CountBars + barsToCount+1) return(0);
  int   limit=Bars-counted_bars-1;
  if (limit>CountBars) limit=CountBars;

  for(int shift = limit; shift >= 0; shift--) 
   {
    sumy=0.0; sumx=0.0; sumxy=0.0; sumx2=0.0;
    for(int i=0; i<barsToCount; i++)
     {
        sumy+=Close[i+shift];
       sumxy+=Close[i+shift]*i;
       sumx+=i;
       sumx2+=i*i;
     }   
    c=sumx2*barsToCount-sumx*sumx;
    if (c==0) c=0.1;
    b=(sumxy*barsToCount-sumx*sumy)/c;
    //a=(sumy-sumx*b)/barsToCount;
    current=-1000*b; 
    prev=current;
    buffer0[shift]= current;        
   }
  int Limit=Bars-counted_bars-1; 
  for(j=0; j<Limit; j++)
   {
     buffer1[j]=iMAOnArray(buffer0,Bars,CrossLine,0,MODE_SMMA,j);
     now=buffer1[j];
     if(now>(last+(difference/1000)))
     {
       if(direction<1)
       {
        UpArrow[j]=buffer1[j];
        direction=1;
        Alert("BUY");
       } 
     }
       else  
         if(now<(last-(difference/1000)))
         {
           if(direction>-1)
           {
            DnArrow[j]=buffer1[j];
            direction=-1;
            Alert("SELL");
           }        
         } 
     last=buffer1[j];
   } 
  
  
   
  
  return(0);
}



Where are the mistakes? What can I do that I get the same signals online as when I backtest it? You can compile and backtest it, then you'll see what I mean!

Thank you!



leMai

 

Try using these 2 lines

  limit=ArraySize(buffer0);
  for(j=limit; j>=0; j--)

you may need to check the direction the j block is counting and change it if it is wrong.

 

I changed the code to this one:


//--------------------------------------------------------------------
//                                             Copyright © 2010, LeMai 
//--------------------------------------------------------------------
#property copyright "Copyright © 2010,LeMai"
#property indicator_separate_window

#property indicator_buffers 4
#property indicator_color1 Orange
#property indicator_color2 Blue
#property indicator_color3 LimeGreen
#property indicator_color4 Red

extern int barsToCount=34;   
extern int CountBars=10000;   
extern int CrossLine=4;
extern double difference=2.0;

int j;
double now=1,last=0;
int direction=0;
double     buffer0[];
double     buffer1[];
double     UpArrow[];
double     DnArrow[];
//--------------------------------------------------------------------
int init()
{
   IndicatorShortName(StringConcatenate("TrendLR (",barsToCount,")"));
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
   SetIndexBuffer(0,buffer0);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexBuffer(1,buffer1);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,233);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,234);
   SetIndexBuffer(2,UpArrow);
   SetIndexBuffer(3,DnArrow);
   return(0);
}

//--------------------------------------------------------------------
int deinit()
{  
   return(0);
}

//--------------------------------------------------------------------
int start()
{ 
  int     counted_bars = IndicatorCounted(); 
  double  a, b, c, sumy, sumx, sumxy, sumx2;
  double  prev; 
  double  current; 
  if(Bars <= CountBars + barsToCount+1) return(0);
  int   limit=Bars-counted_bars-1;
  if (limit>CountBars) limit=CountBars;

  for(int shift = limit; shift >= 0; shift--) 
   {
    sumy=0.0; sumx=0.0; sumxy=0.0; sumx2=0.0;
    for(int i=0; i<barsToCount; i++)
     {
        sumy+=Close[i+shift];
       sumxy+=Close[i+shift]*i;
       sumx+=i;
       sumx2+=i*i;
     }   
    c=sumx2*barsToCount-sumx*sumx;
    if (c==0) c=0.1;
    b=(sumxy*barsToCount-sumx*sumy)/c;
    //a=(sumy-sumx*b)/barsToCount;
    current=-1000*b; 
    prev=current;
    buffer0[shift]= current;        
   }

  int Limit=ArraySize(buffer0);
  for(j=Limit; j>=0; j--) 
   { 
     
     buffer1[j]=iMAOnArray(buffer0,Bars,CrossLine,0,MODE_SMMA,j);
     now=buffer1[j];
     
    
     if(now>(last+(difference/1000)))
     {
       if(direction<1)
       {
        UpArrow[j]=buffer1[j];
        direction=1;
       } 
     }
       else  
         if(now<(last-(difference/1000)))
         {
           if(direction>-1)
           {
            DnArrow[j]=buffer1[j];
            direction=-1;
           }        
         } 
         
     last=buffer1[j];
   } 
  
  
   
  
  return(0);
}

I tried to include the 2 lines into the loop of my old code(look up), but it didn't work, so I worked this one out.

Now the same arrows appear like they do online with the old indicator...but these aren't the ones I like to have...the offline arrows from the old indicator are much better.

So what can I do that the other arrows appear and not the current?

Thanks!!!



leMai

Reason: