KSTX Indicator - Help with a simple code problem !

 

Hello guys.

I made an indicator from KST Indicator, which consists on the sustraction of KST minus its 9 period EMA.

The code is very simple, but as I have 0 idea of programming, I had to make from looking at other indicators code.

I finally made the indicator as I wished but there is one problem with the realtime/forward plotting, as you will see inthe following screenshots.

If chart is updated, the indicator works correctly>

Ive checked the code a ton of times but I have no idea of what it could be. Can anyone give me a help on this??? Thank you very much :)

Here is the code;

#define vers   "1.0"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 FireBrick
#property indicator_color2 Green
//#property  indicator_width1 3
//#property  indicator_width2 3

//----
//extern color color1 = FireBrick;
//extern color color2 = White;



extern int X1 = 10;
extern int X2 = 15;
extern int X3 = 20;
extern int X4 = 30;

extern int AVG1 = 5;
extern int AVG2 = 10;
extern int AVG3 = 15;
extern int AVG4 = 20;

extern double W1 = -1;
extern double W2 = -2;
extern double W3 = -3;
extern double W4 = -4;

extern int period = 15;


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

double KSTXBufUp[];
double KSTXBufDown[];

double ROC1Buf[];
double ROC2Buf[];
double ROC3Buf[];
double ROC4Buf[];

double ROC1MABuf[];
double ROC2MABuf[];
double ROC3MABuf[];
double ROC4MABuf[];

double KSTCBuf[];
double KSTAvgBuf[];
double KSTXBuf[];



string short_name;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void init() 
{
  IndicatorBuffers(3);
    
  //SetIndexDrawBegin(0,1);
  SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
  SetIndexBuffer(0, KSTXBufUp);
  SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2); 
  SetIndexBuffer(1, KSTXBufDown);
  //SetIndexStyle(2,DRAW_LINE); 
  //SetIndexBuffer(2, KSTCBuf);
  //SetIndexBuffer(3, KSTAvgBuf);
  //SetIndexBuffer(4, KSTBuf);
  //SetIndexLabel(0, "KST");
  
  //SetIndexDrawBegin(0, MathMax(MathMax(X1, X2), MathMax(X3, X4)));
  
  ArraySetAsSeries(KSTCBuf, true);
  ArraySetAsSeries(KSTAvgBuf, true);
  ArraySetAsSeries(KSTXBuf, true);
  ArraySetAsSeries(ROC1Buf, true);
  ArraySetAsSeries(ROC2Buf, true);
  ArraySetAsSeries(ROC3Buf, true);
  ArraySetAsSeries(ROC4Buf, true);
  ArraySetAsSeries(ROC1MABuf, true);
  ArraySetAsSeries(ROC2MABuf, true);
  ArraySetAsSeries(ROC3MABuf, true);
  ArraySetAsSeries(ROC4MABuf, true);
  
  
  short_name = "KSTX";
  IndicatorShortName(short_name);
}

void deinit()
{
}

void start() 
{  
  int size = ArraySize(ROC1Buf);
  if (size < Bars)
  {
    size = Bars;    
    ArrayResize(ROC1Buf, size);
    ArrayResize(ROC2Buf, size);
    ArrayResize(ROC3Buf, size);
    ArrayResize(ROC4Buf, size);
    
    ArrayResize(ROC1MABuf, size);
    ArrayResize(ROC2MABuf, size);
    ArrayResize(ROC3MABuf, size);
    ArrayResize(ROC4MABuf, size);
    ArrayResize(KSTXBuf, size);
    ArrayResize(KSTAvgBuf, size);
    ArrayResize(KSTCBuf, size);
  }
  
  
  int counted_bars = IndicatorCounted();
  int limit = Bars - counted_bars;
  if (counted_bars > 0) limit++;

  for(int i=0; i<limit; i++) 
  {
    if (i+X1 >= Bars) continue;
    if (i+X2 >= Bars) continue;
    if (i+X3 >= Bars) continue;
    if (i+X4 >= Bars) continue;
    
    ROC1Buf[i] = (1.0 - Close[i]/Close[i+X1]) * 100.0;
    ROC2Buf[i] = (1.0 - Close[i]/Close[i+X2]) * 100.0;
    ROC3Buf[i] = (1.0 - Close[i]/Close[i+X3]) * 100.0;
    ROC4Buf[i] = (1.0 - Close[i]/Close[i+X4]) * 100.0;
  }
    
  for(i=0; i<limit; i++) 
  {
    ROC1MABuf[i] = iMAOnArray(ROC1Buf, 0, AVG1, 0, MODE_EMA, i);
    ROC2MABuf[i] = iMAOnArray(ROC2Buf, 0, AVG2, 0, MODE_EMA, i);
    ROC3MABuf[i] = iMAOnArray(ROC3Buf, 0, AVG3, 0, MODE_EMA, i);
    ROC4MABuf[i] = iMAOnArray(ROC4Buf, 0, AVG4, 0, MODE_EMA, i);
        
  }
  
  for(i=0; i<limit; i++)
  {
    KSTCBuf[i] = ROC1MABuf[i] * W1 + ROC2MABuf[i] * W2 + ROC3MABuf[i] * W3 + ROC4MABuf[i] * W4;
   }  
   
  for(i=0; i<limit; i++)
  {    
    KSTAvgBuf[i] = iMAOnArray(KSTCBuf, 0, period, 0, MODE_EMA, i);
    
  }  
  
    for(i=0; i<limit; i++)
  {    
    KSTXBuf[i] = KSTCBuf[i] - KSTAvgBuf[i];
    //KSTXBuf[i] = KSTAvgBuf[i];
    if (KSTXBuf[i]>KSTXBuf[i-1])
    { KSTXBufUp[i] = KSTXBuf[i]; 
      KSTXBufDown[i] = EMPTY_VALUE;
     if (KSTXBuf[i-1]<KSTXBuf[i-2]) KSTXBufUp[i-1]=KSTXBuf[i-1];
    }
    if (KSTXBuf[i]<KSTXBuf[i-1])
    { KSTXBufDown[i] = KSTXBuf[i];
      KSTXBufUp[i] = EMPTY_VALUE;
      if (KSTXBuf[i-1]>KSTXBuf[i-2]) KSTXBufDown[i-1]=KSTXBuf[i-1];
    }
  }  
  
 
}
Cheers!
 
Where do you want helping ? here or here: http://www.forexfactory.com/showthread.php?t=455072
 

I just asked for a clue on both sites to maybe get a quicker answer... Is that bad? Im sorry. You can delete the thread if you want to.

Thanks for your help!

 
thedasn:

I just asked for a clue on both sites to maybe get a quicker answer... Is that bad? Im sorry. You can delete the thread if you want to.

Thanks for your help!

Asking in two places at once could men 2 people spend time to come to the same answer . . . meaning that one person wasted their time. You can do it if you want just be aware of what you are doing . . .
 
thedasn:

Hello guys.

I made an indicator from KST Indicator, which consists on the sustraction of KST minus its 9 period EMA.

The code is very simple, but as I have 0 idea of programming, I had to make from looking at other indicators code.

I finally made the indicator as I wished but there is one problem with the realtime/forward plotting, as you will see inthe following screenshots.

If chart is updated, the indicator works correctly>

Ive checked the code a ton of times but I have no idea of what it could be. Can anyone give me a help on this??? Thank you very much :)

Here is the code;

When you resize your arrays you are adding fee cells to the wrong end . . . print the first few cell values ( 0 to 5 ) before and after the resize and see for yourself.

Try this . . . let me know either way, if it's still wrong please be very specific about what is wrong.

Files:
 

You should also comment out these lines . . .

      if (KSTXBuf[i]>KSTXBuf[i-1])
         { 
         KSTXBufUp[i] = KSTXBuf[i]; 
         // KSTXBufDown[i] = EMPTY_VALUE;                            // comment out or remove
         if (KSTXBuf[i-1]<KSTXBuf[i-2]) KSTXBufUp[i-1]=KSTXBuf[i-1];
         }

      if (KSTXBuf[i]<KSTXBuf[i-1])
         { 
         KSTXBufDown[i] = KSTXBuf[i];
         // KSTXBufUp[i] = EMPTY_VALUE;                             // comment out or remove
         if (KSTXBuf[i-1]>KSTXBuf[i-2]) KSTXBufDown[i-1]=KSTXBuf[i-1];
         }
 

Allright, im taking a look thru the code while some candles get completed to see if it works. I apologize for my limited programming knowledge; as I want to understand what the solution is and learn from it, i was checking the code you corrected and I was wondering what are these lines you added:

Print("Pre resize: ROC1Buf - ", ROC1Buf[0], " ", ROC1Buf[1], " ", ROC1Buf[2], " ", ROC1Buf[3], " ", ROC1Buf[4], " ", ROC1Buf[5], " ", ROC1Buf[6] );

Print("Post resize: ROC1Buf - ", ROC1Buf[0], " ", ROC1Buf[1], " ", ROC1Buf[2], " ", ROC1Buf[3], " ", ROC1Buf[4], " ", ROC1Buf[5], " ", ROC1Buf[6] );

and

   if(limit > 1) Print("Limit = ", limit);

Ok, printing seems to be perfect now, theres only a tiny issue, which is that red line gets printed below green line in forward plotting

 
thedasn:

Allright, im taking a look thru the code while some candles get completed to see if it works. I apologize for my limited programming knowledge; as I want to understand what the solution is and learn from it, i was checking the code you corrected and I was wondering what are these lines you added:

and

Those Prints are in there for debugging reasons, look at the experts tab . . .


What exactly do you mean by . . .

. . . red line gets printed below green line in forward plotting

If you have a Red or Green line section less 2 bars long you have no option but to have overlapping Red and Green lines . . .
 

Ok, yes you are right, im looking at it. What I mean by that is when indicator is plotted in real time, green line is always on top; while in the past candles, they are not overlapped. If chart us updated, it gets solved, but that leaves me at the same starting point, which is the need to update the chart very frequently (right button, refresh). In the screenshot I attached before you can check that plotting issue between the indicator boxes 2 and 4.

Edit; in fact refreshing actually does nothing, green is all the time on top.

Edit2; I removed comment bars on both up and down KSTXBufUp[i] = EMPTY_VALUE; lines, now green and red are plotted fine.

Edit3; definitely the problem is this empty_value introduction in the array which causes the problem in the indicator, after compiling with that line active again the same initial wrong behaviour is present. But without it, it doesnt get plotted with red/green correctly...

 

hi thedasn,

can you tell me the name or even better give me a link of the indicator that paints the gray bars at the bottom of chart 0, it writes Spread 1 Point candle 00:01.

Thanks in beforehand,

Gooly

 

hi gooly,

I will share with you, here you have the indicators requested, both candle clock and spread plotter.

The grey bars are the default tick volume that comes with mt4.

Enjoy

Files:
spread2.ex4  3 kb
Reason: