简体   繁体   中英

Mql4 Meta trader4, I can not make the indicator shift left simultaneously with the candles when new bar is formed

Below is the current code. The indicator compiles and runs When running the waves move with the candles as new bar forms. But when you turn the platform off for some time and then turn it back on The candles and sin waves are now no longer synchronized. Somehow the subwindow rests back to when the platform was turned on. So no one out of 92 looky-loos wants to tackle this problem. I tried to put 3 pictures on here to help explain, but the system here will not display the pictures. If you are a coder trying to help, copy the code here, and paste it into your platform. Run it. Mark a high peak or low trough with a text symbol that will not move and then put a vertical line through the text character and the candle above it. as the program proceeds the vertical line will move with the candles and the wave moves with the vertical line, the text marker stays put so you can see where you started. Then turn off your platform with the indicator still loaded, wait for several candle periods then turn the platform back on and you will see that the waves reset back to the right. the vertical line continued on with the appropriate candle and the text marker also reset to the right with the peak or valley of the sin wave.

 //---- indicator settings
#property indicator_separate_window
#property indicator_buffers 8 // 7
#property indicator_color1 Red
#property indicator_color2 Orange
#property indicator_color3 Yellow
#property indicator_color4 Lime
#property indicator_color5 Blue
#property indicator_color6 DodgerBlue
#property indicator_color7 DarkViolet
#property indicator_color8 White



//---- indicator buffers
double ExtBuffer1[], ExtBuffer2[], ExtBuffer3[], ExtBuffer4[], 
ExtBuffer5[],ExtBuffer6[], ExtBuffer7[], ExtBuffer8[];     
double Dgr1[], Dgr2[], Dgr3[], Dgr4[], Dgr5[], Dgr6[], Dgr7[], 
Dgr8[];
extern datetime StartTime=D'1999.11.10 00:00';

//+-------------------------------------------------------------+
//| Custom indicator initialization function |
//+-------------------------------------------------------------+
int init()
{
IndicatorBuffers(7); // 6
SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,ExtBuffer1); 
SetIndexShift(0,295);       
SetIndexStyle(1,DRAW_LINE); SetIndexBuffer(1,ExtBuffer2); 
SetIndexShift(1,264);     
SetIndexStyle(2,DRAW_LINE); SetIndexBuffer(2,ExtBuffer3); 
SetIndexShift(2,203); 
SetIndexStyle(3,DRAW_LINE); SetIndexBuffer(3,ExtBuffer4); 
SetIndexShift(3,166);      
SetIndexStyle(4,DRAW_LINE); SetIndexBuffer(4,ExtBuffer5); 
SetIndexShift(4,74); 
SetIndexStyle(5,DRAW_LINE); SetIndexBuffer(5,ExtBuffer6); 
SetIndexShift(5,102); // DodgerBlue
SetIndexStyle(6,DRAW_LINE); SetIndexBuffer(6,ExtBuffer7); 
SetIndexShift(6,78); // Dark Violet
SetIndexStyle(7,DRAW_LINE); 
SetIndexBuffer(7,ExtBuffer8); SetIndexShift(7,85); // White



   SetLevelValue(0,0);
   SetIndexStyle(0,DRAW_LINE,0,3); // 0
   SetIndexStyle(2,DRAW_LINE,0,3); // 0
   SetIndexStyle(3,DRAW_LINE,0,3); // 0
   SetIndexStyle(4,DRAW_LINE,0,3); // 0
   SetIndexStyle(5,DRAW_LINE,0,3); // 0
   SetIndexStyle(6,DRAW_LINE,0,3); // 0
   SetIndexStyle(7,DRAW_LINE,0,3); // 0





return(0);
}
//+-------------------------------------------------------------+
//| Accelerator/Decelerator Oscillator |
//+-------------------------------------------------------------+
int start()
{
   int limit; //  -----------------------------mine

   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   //-----------------------------------------mine  

   int Shift;
   int i;
   int b; // mine
   Shift=iBarShift(Symbol(),PERIOD_CURRENT,StartTime);
   ArrayResize(Dgr1,Shift+1); 
   ArrayResize(Dgr2,Shift+1); 
   ArrayResize(Dgr3,Shift+1); 
   ArrayResize(Dgr4,Shift+1);
   ArrayResize(Dgr5,Shift+1); 
   ArrayResize(Dgr6,Shift+1); 
   ArrayResize(Dgr7,Shift+1); 
   ArrayResize(Dgr8,Shift+1);

   MyCalc(Shift,1);

   for(b=Shift; b<limit; b++) // mine
  { 
   for(i=Shift; i>=0; i--) // >
   {
      ExtBuffer1[i]=Dgr1[i];
      ExtBuffer2[i]=Dgr2[i];
      ExtBuffer3[i]=Dgr3[i];
      ExtBuffer4[i]=Dgr4[i];
      ExtBuffer5[i]=Dgr5[i];
      ExtBuffer6[i]=Dgr6[i];
      ExtBuffer7[i]=Dgr7[i];
      ExtBuffer8[i]=Dgr8[i];






   }
  } // mine
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; Dgr3[i]=i*2.5; Dgr4[i]=i*2.5; 
      Dgr5[i]=i*2.5;

      double val1=i*2.5;
      double val2=i*2.5;
      double val3=i*2.5;
      double val4=i*2.5;
      double val5=i*2.5;
      double val6=i*2.5;
      double val7=i*2.5;
      double val8=i*2.5;


      Dgr1[i]=MathSin(3.14159*val1/298)- 1/2 * Yhigh;
      Dgr2[i]=MathSin(3.14159*val2/149)- 1/2 * Yhigh;
      Dgr3[i]=MathSin(3.14159*val3/98)- 1/2 * Yhigh;
      Dgr4[i]=MathSin(3.14159*val4/75)- 1/2 * Yhigh;
      Dgr5[i]=.5*MathSin(3.14159*val5/60)- 1/2 * Yhigh;
      Dgr6[i]=.5*MathSin(3.14159*val6/48)- 1/2 * Yhigh;
      Dgr7[i]=.5*MathSin(3.14159*val7/42)- 1/2 * Yhigh;                                                                      
      Dgr8[i]=.5*MathSin(3.14159*val8/38)- 1/2 * Yhigh;

   }
    }

Your problem is that the indicator starts drawing at D'1999.11.10 00:00' , however as this date is no doubt not available in your visible chart history, the indicator instead starts redrawing as the earliest available bar. This continues as you run the indicator, however when restarting MQL4, the visible bars is limited by your settings (Tools>Options>Charts>Max bars in chart) and upon reloading the indicator again reverts to start drawing at the earliest available bar which has now changed. As stated in my original comment, your calculation are not directly linked to candles so they will not shift consistently. If you wish to make them consistently, make the start point consistent somehow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM