Search code examples
mql4

mql4 separate window indicator using sin wave function


Below is the original code that creates a sin wave in a separate window for MetaTrader 4. My question is how to code it for a 2nd, or 3rd sin wave. The original code is compiled and is functional. I will also attach the code I added to the original, it still creates the 1st sin wave but not the second wave. I need help coding the 2nd and 3rd waves. Thanks.

This is the original working code:

//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 2 // org 1
#property indicator_color1 Red //

//---- indicator buffers
double ExtBuffer1[];
double Dgr[];
extern datetime StartTime=D'1999.11.10 00:00';

//+---------------------------------------------------------------+
//| Custom indicator initialization function |
//+---------------------------------------------------------------+
int init()
{
IndicatorBuffers(1);
SetIndexStyle(0,DRAW_LINE); // 0
SetLevelValue(0,0); //
//----indicator buffers mapping //
SetIndexBuffer(0,ExtBuffer1);
SetIndexShift(0,25); // B. added
//---- initialization done
return(0);
}
//+---------------------------------------------------------------+
//| Accelerator/Decelerator Oscillator |
//+---------------------------------------------------------------+
int start()
{
int Shift;
int i;
Shift=iBarShift(Symbol(),PERIOD_D1,StartTime); // Only run on PERIOD_D1
ArrayResize(Dgr,Shift+1); // ArrayResize(Dgr,Shift+1); ?????????????????
MyCalc(Shift,1); // ??? 1
for(i=Shift; i>=0; i--)
ExtBuffer1[i]=Dgr[i];

return(0);
}
//+---------------------------------------------------------------+
void MyCalc(int Shift,int i Yhigh)
{
int i;
for(i=Shift;i>=0;i--) // should be >
{
Dgr[i]=i*2.5;
// ..... B. added code below
double val=i*2.5; // 2.5

Dgr[i]=MathSin(3.14159*val/149)+Yhigh; // 149 ,,, Origina code ... 
Dgr[i]=MathSin(3.14159*val/180)+Yhigh; changing the /180 to lower degree changes the 
frequency
// ..... B. added above code
//Dgr[i]=MathSin(3.14159*Dgr[i]/180)+Yhigh; // Original Dgr[i]=MathSin(3.14159*- 
Dgr[i]/180)+Yhigh;
}
}

This code below is the same code, but with my attempt at creating the 2nd sin wave code added to the original code.

//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 2 // org 1
#property indicator_color1 Red //
#property indicator_color2 Orange //



//---- indicator buffers

double ExtBuffer1[];
double ExtBuffer2[];

double Dgr[];
double Dgr2[];

extern datetime StartTime=D'1999.11.10 00:00';


//+---------------------------------------------------------------+
//| Custom indicator initialization function |
//+---------------------------------------------------------------+
int init()
{
IndicatorBuffers(2);
SetIndexStyle(0,DRAW_LINE); // 0
SetIndexStyle(1,DRAW_LINE); // 0

SetLevelValue(0,0); //

//----indicator buffers mapping //

SetIndexBuffer(0,ExtBuffer1);
SetIndexBuffer(1,ExtBuffer2);

SetIndexShift(0,25); // B. added
SetIndexShift(1,25); // B. added

//---- initialization done
return(0);
}
//+---------------------------------------------------------------+
//| Accelerator/Decelerator Oscillator |
//+---------------------------------------------------------------+
int start()
{
int Shift;
int i;

Shift=iBarShift(Symbol(),PERIOD_D1,StartTime); // Only run on PERIOD_D1
ArrayResize(Dgr,Shift+1); // ArrayResize(Dgr,Shift+1); ?????????????????
ArrayResize(Dgr2,Shift+1);

MyCalc(Shift,1); // ??? 1
for(i=Shift; i>=0; i--)
ExtBuffer1[i]=Dgr[i];
ExtBuffer2[i]=Dgr2[i];


return(0);
}
//+---------------------------------------------------------------+

void MyCalc(int Shift, int Yhigh)
{
int i;

for(i=Shift;i>=0;i--) // should be >
{
Dgr[i]=i*2.5;
Dgr2[i]=i*2.5;


double val=i*2.5; // 2.5
double val2=i*2.5; // 2.5

Dgr[i]=MathSin(3.14159*val/149)+Yhigh; // 149
Dgr2[i]=MathSin(3.14159*val2/49)+Yhigh; // 49

}
}

Solution

  • You've just mixed up some lines of your code. Try the following.

    //---- indicator settings
    #property indicator_separate_window
    #property indicator_buffers 4
    #property indicator_color1 Red
    #property indicator_color2 Orange
    
    //---- indicator buffers
    double ExtBuffer1[], ExtBuffer2[];
    double Dgr1[], Dgr2[];
    extern datetime StartTime=D'1999.11.10 00:00';
    
    //+---------------------------------------------------------------+
    //| Custom indicator initialization function |
    //+---------------------------------------------------------------+
    int init()
    {
       IndicatorBuffers(2);
       SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,ExtBuffer1); SetIndexShift(0,25);
       SetIndexStyle(1,DRAW_LINE); SetIndexBuffer(1,ExtBuffer2); SetIndexShift(1,25);
       SetLevelValue(0,0);
    return(0);
    }
    //+---------------------------------------------------------------+
    //| Accelerator/Decelerator Oscillator |
    //+---------------------------------------------------------------+
    int start()
    {
       int Shift;
       int i;
       Shift=iBarShift(Symbol(),PERIOD_D1,StartTime);
       ArrayResize(Dgr1,Shift+1); ArrayResize(Dgr2,Shift+1);
       MyCalc(Shift,1);
       for(i=Shift; i>=0; i--)
       {
          ExtBuffer1[i]=Dgr1[i];
          ExtBuffer2[i]=Dgr2[i];
       }
    return(0);
    }
    //+---------------------------------------------------------------+
    void MyCalc(int Shift,int Yhigh)
    {
       int i;
       for(i=Shift;i>=0;i--)
       {
          Dgr1[i]=i*2.5; Dgr2[i]=i*2.5; 
          double val1=i*2.5;
          double val2=i*2.5;
          Dgr1[i]=MathSin(3.14159*val1/149)+Yhigh;
          Dgr2[i]=MathSin(3.14159*val2/49)+Yhigh;
       }
    }