How to create On Screen Stochastic Indicator - in the same chart window????

TerryTosan
65
TerryTosan  

Hello there,

I am just trying to create my own indicator, Idea for the indicator is I wanted to see Stochastic on my charts instead of separate window.

For that I create indicator and attaching the code of it.

//+------------------------------------------------------------------+
//|                                               Test Indicator.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Black
#property indicator_color2 White
//---- input parameters
extern int KPeriod=5;
extern int DPeriod=3;
extern int Slowing=3;
//---- buffers
double MainBuffer[];
double SignalBuffer[];
double HighesBuffer[];
double LowesBuffer[];
//----
int draw_begin1=0;
int draw_begin2=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(4);
   SetIndexBuffer(2, HighesBuffer);
   SetIndexBuffer(3, LowesBuffer);
//---- indicator lines
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0, MainBuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1, SignalBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="Sto("+KPeriod+","+DPeriod+","+Slowing+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   SetIndexLabel(1,"Signal");
//----
   draw_begin1=KPeriod+Slowing;
   draw_begin2=draw_begin1+DPeriod;
   SetIndexDrawBegin(0,draw_begin1);
   SetIndexDrawBegin(1,draw_begin2);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| On Screen Ocsilator                              |
//+------------------------------------------------------------------+
int start()
  {
      int    i,k;
   int    counted_bars=IndicatorCounted();
   double price;
//----
   if(Bars<=draw_begin2) return(0);
//---- initial zero
   if(counted_bars<1)
     {
      for(i=1;i<=draw_begin1;i++) MainBuffer[Bars-i]=0;
      for(i=1;i<=draw_begin2;i++) SignalBuffer[Bars-i]=0;
     }
//---- minimums counting
   i=Bars-KPeriod;
   if(counted_bars>KPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double min=1000000;
      k=i+KPeriod-1;
      while(k>=i)
        {
         price=Low[k];
         if(min>price) min=price;
         k--;
        }
      LowesBuffer[i]=min;
      i--;
     }
//---- maximums counting
   i=Bars-KPeriod;
   if(counted_bars>KPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double max=-1000000;
      k=i+KPeriod-1;
      while(k>=i)
        {
         price=High[k];
         if(max<price) max=price;
         k--;
        }
      HighesBuffer[i]=max;
      i--;
     }
//---- %K line
   i=Bars-draw_begin1;
   if(counted_bars>draw_begin1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double sumlow=0.0;
      double sumhigh=0.0;
      for(k=(i+Slowing-1);k>=i;k--)
        {
         sumlow+=Close[k]-LowesBuffer[k];
         sumhigh+=HighesBuffer[k]-LowesBuffer[k];
        }
      if(sumhigh==0.0) MainBuffer[i]=100.0;
      else MainBuffer[i]=sumlow/sumhigh*100;
      i--;
     }
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
//---- signal line is simple movimg average
   for(i=0; i<limit; i++)
      SignalBuffer[i]=iMAOnArray(MainBuffer,Bars,DPeriod,0,MODE_SMA,i);
//----
   return(0);
  }
//+------------------------------------------------------------------+

Now the problem is whenever I insert indicator in chart nothing appear but the values of that "On Screen Stochastic"* data window is showing in data window.

here is the image of my problem


I was not the student of IT so not that much in coding what I can do is just copy paste.

If any one already this On Screen Stochastic then please share that indicator with me

OR

Help me out with coding to that stochastic lines can appear in in the same chart window.

thanks,

Terry

Simon Gniadkowski
17374
Simon Gniadkowski  
The scale on your chart for the NIFY goes from 4815.70 to 5060.50 . . . how do you expect to see anything with a value of 77.197 on this chart ?
TerryTosan
65
TerryTosan  
RaptorUK:
The scale on your chart for the NIFY goes from 4815.70 to 5060.50 . . . how do you expect to see anything with a value of 77.197 on this chart ?


If you have ever used any Stochastic then most of them have upper limit of 100 and lower limit of 0 so that data windows is showing value of Indicator where my cursor is in the image.

Over all idea is I wanted to get stochastic indicator on the chart not on separate windows.,.,. could you please help me out????

Simon Gniadkowski
17374
Simon Gniadkowski  
I don't use Technical Indicators . . . you could scale the values to give you a purely visual representation . . .
TerryTosan
65
TerryTosan  
RaptorUK:
I don't use Technical Indicators . . . you could scale the values to give you a purely visual representation . . .


I have seen this indicator on netdania.com

below is the image for your reference


Is that possible to correct the coding which Ihave used to create my indicator?


in my 1st image the value of this indicator 0-100 shows in data windows but the problem is lines like above image are not showing up in my indicator!!!!

Simon Gniadkowski
17374
Simon Gniadkowski  
TerryTosan:


I have seen this indicator on netdania.com

Yes, that chart has a 2nd Y axis on the left hand side . . MT4 doesn't have this facility.
William Roeder
24689
William Roeder  
TerryTosan:
Over all idea is I wanted to get stochastic indicator on the chart not on separate windows
Since MT4 doesn't have overlay windows you can scale the output to the chart but the actual values will not be useful.
double  indicator(int iBar){    return ( iStochastic(..., iBar) );  }
int start(){
    // Visible portion of the screen
    int     iLeft           = WindowFirstVisibleBar(),
            iRight          = iLeft - WindowBarsPerChart();
    if (iRight<0)    iRight = 0;  // Tester/Chart Shift
    double  chart.top       = WindowPriceMax(),
            chart.bottom    = WindowPriceMin(),
            chart.range     = chart.top-chart.bottom;
    /*++++ Adjust if Desired.*/{
    chart.top  -= chart.range*0.05;     chart.bottom += chart.range*0.05;
    chart.range = chart.top-chart.bottom;
    /*---- Adjust if Desired.*/}

    /*++++ option one scale actual indicator limits to the screen{
    #define INF 0x6FFFFFFF  // Not quite infinite, Jul 2029, or 1,879,048,191
    double  indicator.max   = 0,
            indicator.min   = INF;
    for (int iBar = iLeft; iBar >= iRight; iBar--){ // Indicator's real min/max.
        double value = indicator(iBar);
        if (indicator.max < value) indicator.max = value;
        if (indicator.min > value) indicator.min = value;
    }
    /*---- option one scale actual indicator to the screen*/}
    /*++++ option two scale indicator limits to the screen*/{
    double  indicator.max =   0,                    // Indicator's real limits.
            indicator.min = 100;
    /*---- option two scale indicator limits to the screen*/}
    double  indicator.range = indicator.max - indicator.min;
    if (indicator.range == 0.0) indicator.range = 1.0;

    // Draw indicator as a main overlay
    for (iBar = iLeft; iBar >= iRight; iBar--){ 
        double  value       = indicator(iBar),
                scaled_0_1  = (value - indicator.min) / indicator.range;
        ValueOnChart[iBar]  = scaled_0_1 * chart.range + chart.bottom;
    }