Help..drawing vertical lines in separate window indicator

 

Hello

Im trying to draw lines when the RSI and the signal line cross on a TDI indicator.

I added a loop to the code as shown to try to create vertical lines, complies but in strategy tester i get #4200 error "#define ERR_OBJECT_ALREADY_EXISTS"

Im not sure where it exists. Maybe my loop incorrect..?

Appreciate some help

Thanks Craig

 for (i=limit;i>=0;i--)  // ...mod realtime old stmt for (i=limit-1;i>=0;i--) old stmt
      {
       MaBuf[i] = (iMAOnArray(RSIBuf,0,RSI_Price_Line,0,RSI_Price_Type,i));
       MbBuf[i] = (iMAOnArray(RSIBuf,0,Trade_Signal_Line,0,Trade_Signal_Type,i));  
      }
   for (i=0;i<limit;i++)
      {    
       double currentRSI =  MaBuf[i];                                                                       >>> I included this loop to draw the lines..
       double previousRSI = (iMAOnArray(RSIBuf,0,RSI_Price_Line,0,RSI_Price_Type,i+1));
       double currentSignal = MbBuf[i];
       double previousSignal = (iMAOnArray(RSIBuf,0,Trade_Signal_Line,0,Trade_Signal_Type,i+1));
       if(previousRSI < previousSignal && currentRSI > previousSignal)
       if(!ObjectCreate(ChartID(),"VLine",OBJ_VLINE,0,0,0))
     
      Print("Error: can't create label! code #",GetLastError());
      } 
//----
   return(0);
  }
 
craigt: i get #4200 error "#define ERR_OBJECT_ALREADY_EXISTS"
if(!ObjectCreate(ChartID(),"VLine",OBJ_VLINE,0,0,0))
You can only have one object called "VLine."
 

ok, ive tried to increment the name so its not repeated, now have an error code#0 "#define ERR_NO_ERROR"...(Edited as this is incorrect, I am get getting 4200 code error)

I am trying to create a line in the indicator_separate_window.. code amendment below

any ideas..Thanks

 

  for (i=limit;i>=0;i--)  // ...mod realtime old stmt for (i=limit-1;i>=0;i--) old stmt
      {
       MaBuf[i] = (iMAOnArray(RSIBuf,0,RSI_Price_Line,0,RSI_Price_Type,i));
       MbBuf[i] = (iMAOnArray(RSIBuf,0,Trade_Signal_Line,0,Trade_Signal_Type,i));  
      }
   for (i=0;i<limit;i++)
       int lineIncrement=0;
       string lineName = StringConcatenate("VLine", lineIncrement);        // assemble the object name
      {    
       double currentRSI =  MaBuf[i];
       double previousRSI = (iMAOnArray(RSIBuf,0,RSI_Price_Line,0,RSI_Price_Type,i+1));
       double currentSignal = MbBuf[i];
       double previousSignal = (iMAOnArray(RSIBuf,0,Trade_Signal_Line,0,Trade_Signal_Type,i+1));
       if(previousRSI < previousSignal && currentRSI > previousSignal)
       if(!ObjectCreate(ChartID(),lineName,OBJ_VLINE,0,0,0))
       Print("Error: can't create label! code #",GetLastError());
       lineIncrement++;     
      } 
//----
   return(0);
  }
 

Not sure why I got Code 0, NO ERROR, I now have same error as before 4200, amended code as above...

 
craigt:

Not sure why I got Code 0, NO ERROR, I now have same error as before 4200, amended code as above...

You are still getting the same error, because you are still generating duplicates:

  1. An indicator's "OnCalculate()" can be called multiple times for the same indices due to various reasons, so don't assume that an Index will be calculated only once.
  2. Also, the number of bars (or "rates_total") can change during its lifetime so bar indexing is not guaranteed to always sync at the same bar.
  3. For these reasons, don't use the Bar Index for generating the Chart Names but use the bar's time instead.
  4. Since an indicator can be called to regenerate, you have to test for the existence of the object by using the ObjectFind() function and if found, apply a "ObjectMove()" to adjust it to its new position instead of trying to recreate it (which will give you the 4200 error).
 

ok, I have a line now, but the line moves along the chart and doesnt stay at the cross, I would like the line to stop at the cross i.e multiple lines on chart, I tried using ObjectSet but no good, what can you suggest

cheers

for (i=0;i<limit;i++)
       int lineIncrement=0;
       string lineName = StringConcatenate("VLine", lineIncrement);        // assemble the object name
      {    
       double currentRSI =  MaBuf[i];
       double previousRSI = (iMAOnArray(RSIBuf,0,RSI_Price_Line,0,RSI_Price_Type,i+1));
       double currentSignal = MbBuf[i];
       double previousSignal = (iMAOnArray(RSIBuf,0,Trade_Signal_Line,0,Trade_Signal_Type,i+1));
       if(previousRSI < previousSignal && currentRSI > previousSignal)
       if(!ObjectCreate(ChartID(),lineName,OBJ_VLINE,0,TimeCurrent(),0))
       if(!ObjectSet(lineName,OBJPROP_TIME1,TimeCurrent()))
         if(!ObjectFind(ChartID(),lineName))
            if(!ObjectMove(lineName,0,TimeCurrent(),0))
       Print("Error: can't create label! code #",GetLastError());
       lineIncrement++;     
      } 
//----
   return(0);
  }
 

Hi!

Check the parentheses of your for loop.

And you are still using bar numbers.

 
craigt:

ok, I have a line now, but the line moves along the chart and doesnt stay at the cross, I would like the line to stop at the cross i.e multiple lines on chart, I tried using ObjectSet but no good, what can you suggest

  1. Well, if you don't want it to change position, then don't use "ObjectMove()". Only use it if you want to change its position! Duh!
  2. You should use "ObjectFind()" BEFORE using "ObjectCreate()", NOT after! What is the point of using it after the error has already been generated?
  3. You have code blocks in the wrong places. Your parenthesis are misplaced, especially for the "for" loop.
  4. You are still using the Bar Index/Counter to build the name instead of using the bar time. You did not correct that.
  5. Also, you are initialising the "lineIncrement" within the for loop, instead of before/outside it.
  6. Use parenthesis on "if" statements as well to make your code more clear and prevent incorrect logic.
In coding you must learn to THINK logically in steps. Don't just throw things at it hoping it will work. Plan each step out and think of the results and consequences of EACH step and foresee what should be done for each. If need be, draw a flow chart for the sequence.
 

ok, thanks eddie, FMIC, I see I have to sort my coding out. 

thanks will let you know how i get on

cheers 

Reason: