My VLine Code is ready but it is not working yet

 

Hello! Below is my lines of code to auto create VLine on other visible charts when I manually create it on the current chart. I don't know where I have made a mistake. Please I need your help. Thanks in advance.

int OnInit()
  {
   ChartSetInteger(0,CHART_EVENT_OBJECT_CREATE,1);
   return(INIT_SUCCEEDED);
  }

void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
  {
   if (ObjectType(sparam) == OBJ_VLINE && id == CHARTEVENT_OBJECT_CREATE)     // Condition for creating only vertical line
     {
      Print("(id = ",id,")", "(lparam = ",lparam,")", "(dparam = ",dparam,")", "(sparam = ",sparam,")", "(date = ",ObjectGet(sparam, OBJPROP_TIME1),")");  // Test-print parameters
      datetime VLineDate = ObjectGet(sparam,OBJPROP_TIME1);                   // Declare VLineDate variable as datetime to hold the date
                                                                          
      long currChart,prevChart=ChartFirst();                                  //--- variables for chart ID 
      int i=0,limit=100; 
      Print("ChartFirst =",ChartSymbol(prevChart)," ID =",prevChart);         // Test-print the previous and chart symbol
      while(i<limit)                                                          // We have certainly not more than 100 open charts 
        { 
         currChart=ChartNext(prevChart);                                      // Get the new chart ID by using the previous chart ID 
         if(currChart<0) break;                                               // Have reached the end of the chart list 
         if(ChartGetInteger(i,CHART_WINDOW_IS_VISIBLE,0,currChart) && ChartSymbol() != Symbol()) 
           {
            ObjectCreate(i,sparam,OBJ_VLINE,0,VLineDate,0);                   //Create identical vertical line on open charts
           }
         prevChart=currChart;                                                 // let's save the current chart ID for the ChartNext() 
         i++;                                                                 // Do not forget to increase the counter 
        }
     }
  }
 
macpee:

Hello! Below is my lines of code to auto create VLine on other visible charts when I manually create it on the current chart. I don't know where I have made a mistake. Please I need your help. Thanks in advance.


int OnInit()
  {
   ChartSetInteger(0,CHART_EVENT_OBJECT_CREATE,1);
   return(INIT_SUCCEEDED);
  }

void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
  {
   if (ObjectType(sparam) == OBJ_VLINE && id == CHARTEVENT_OBJECT_CREATE)     // Condition for creating only vertical line
     {
      Print("(id = ",id,")", "(lparam = ",lparam,")", "(dparam = ",dparam,")", "(sparam = ",sparam,")", "(date = ",ObjectGet(sparam, OBJPROP_TIME1),")");  // Test-print parameters
      datetime VLineDate = ObjectGet(sparam,OBJPROP_TIME1);                   // Declare VLineDate variable as datetime to hold the date
                                                                          
      long currChart,prevChart=ChartFirst();                                  //--- variables for chart ID 
      int i=0,limit=100; 
      Print("ChartFirst =",ChartSymbol(prevChart)," ID =",prevChart);         // Test-print the previous and chart symbol
      while(i<limit)                                                          // We have certainly not more than 100 open charts 
        { 
         currChart=ChartNext(prevChart);                                      // Get the new chart ID by using the previous chart ID 
         if(currChart<0) break;                                               // Have reached the end of the chart list 
         if(ChartGetInteger(i,CHART_WINDOW_IS_VISIBLE,0,currChart) && ChartSymbol() != Symbol()) 
           {
            ObjectCreate(i,sparam,OBJ_VLINE,0,VLineDate,0);                   //Create identical vertical line on open charts
           }
         prevChart=currChart;                                                 // let's save the current chart ID for the ChartNext() 
         i++;                                                                 // Do not forget to increase the counter 
        }
     }
  }

Check the parameters. 

Those two I marked are probably wrong. There should be a chart id you obtained by calling ChartNext().

 
Drazen Penic:

Check the parameters. 

Those two I marked are probably wrong. There should be a chart id you obtained by calling ChartNext().

int OnInit()
  {
   ChartSetInteger(0,CHART_EVENT_OBJECT_CREATE,1);
   return(INIT_SUCCEEDED);
  }

void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
  {
   if (ObjectType(sparam) == OBJ_VLINE && id == CHARTEVENT_OBJECT_CREATE)     // Condition for creating only vertical line
     {
      Print("(id = ",id,")", "(lparam = ",lparam,")", "(dparam = ",dparam,")", "(sparam = ",sparam,")", "(date = ",ObjectGet(sparam, OBJPROP_TIME1),")");  // Test-print parameters
      datetime VLineDate = ObjectGet(sparam,OBJPROP_TIME1);                   // Declare VLineDate variable as datetime to hold the date                                                                       
      long nextChart,currChart=ChartFirst();                                  //--- variables for chart ID 
      int i=0,limit=100;
      while(i < limit)                                                        // We have certainly not more than 100 open charts 
        {
         if(currChart<0) break;                                               // Have reached the end of the chart list 
         if(ChartSymbol(currChart) != Symbol()) 
           {
            Print("Chart_Symbol=", ChartSymbol(currChart), " Chart_ID=",currChart, " V_Line_Date=",VLineDate);  // Test print some variables
            ObjectCreate(currChart,OBJ_VLINE,0,VLineDate,0);                   //Create identical vertical line on open charts
           }
         nextChart=ChartNext(currChart);                                      // Get the new chart ID by using the previous chart ID 
         currChart=nextChart;                                                 // let's save the current chart ID for the ChartNext() 
         i++;                                                                 // Do not forget to increase the counter 
        }
     }
  }

I have improved on the program, but it only outputs the required number of VLines on the current chart and not on other open chart. Please any help?

 
macpee:

I have improved on the program, but it only outputs the required number of VLines on the current chart and not on other open chart. Please any help?

You can't have two objects with the same name on the chart.

Before ObjectCreate() you must check that you don't already have the line with that name - use ObjectFind().

If you find the line with the given name you should either delete/create it or move it.