Download MetaTrader 5
To add comments, please log in or register
Register as a seller and start making profit!
Stephen Reynolds
484
Stephen Reynolds 2015.06.01 14:07 

Hi

What im trying to do is get the ObjectCreate () function to draw a trendline between 2 highestHighs on the price chart and stochastic on the stochastic chart. Ive managed to code this so far but don't know why ObjectCreate() doesn't draw any object on the sub window specified?

<CODE REMOVED>

                                                                            

Problem is here     line2 = ObjectCreate(_Symbol,"Trendline",OBJ_TREND,1,time1,stochastic1,time2,stochastic2);     where I use the sub_window parameter I put in an index 1 thinking that its going to draw the object on that sub window (stochastic) but it never does.


If anyone can show me what i'm doing wrong I would be great full.


Thanks Stephen

whroeder1
13610
whroeder1 2015.06.01 14:37  
renzbub: show me what i'm doing wrong I would be great full.
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.
    Also remove all that extra white space.
  2.    for(int i=1; i<=3; i++)
       {
          highest1 = iHighest(NULL,0,MODE_HIGH,i); 
          highestHigh1 = High[highest1];  
          time1 = iTime(NULL,0,highest1);   // 3rd parameter uses index from iHighest for time anchor
         
          stochastic1 = iStochastic(_Symbol,_Period,KPeriod,DPeriod,Slowing,StochMethod,StochPrice,MODE_MAIN,highest1);    // Calculates main line
          signal1 = iStochastic(_Symbol,_Period,KPeriod,DPeriod,Slowing,StochMethod,StochPrice,MODE_SIGNAL,highest1);    // Calculates signal line  
       }
    Why are you setting these five variables three times but do nothing with the first two values? Don't you want to find the first high among the first n bars, not the high of the third bar?

  3. iHighest - MQL4 Documentation requires five arguments, you pass four.

  4.    for(i=4; i<=10; i++)
       {
          highest2 = iHighest(NULL,0,MODE_HIGH,i);
          highestHigh2 = High[highest2];                                        
          time2 = iTime(NULL,0,highest2);
         
          stochastic2 = iStochastic(_Symbol,_Period,KPeriod,DPeriod,Slowing,StochMethod,StochPrice,MODE_MAIN,highest2);    // Calculates main line
          signal2 = iStochastic(_Symbol,_Period,KPeriod,DPeriod,Slowing,StochMethod,StochPrice,MODE_SIGNAL,highest2);    // Calculates signal line
       }
    
    Why are you setting these variable six times but do nothing with the first five values?
  5.    line1 = ObjectCreate(_Symbol,"Trendline",OBJ_TREND,0,time1,highestHigh1,time2,highestHigh2);
       line2 = ObjectCreate(_Symbol,"Trendline",OBJ_TREND,1,time1,stochastic1,time2,stochastic2);         // <<<  PROBLEM!         
    First line create object "Trendline". Second fails, because object already exists. Check your return codes (OrderModify and OrderSelect) Check your return codes (OrderCreate) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  6. You would be greatly full of what?
Stephen Reynolds
484
Stephen Reynolds 2015.06.01 16:00  
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.
    Also remove all that extra white space.
  2. Why are you setting these five variables three times but do nothing with the first two values? Don't you want to find the first high among the first n bars, not the high of the third bar?

  3. iHighest - MQL4 Documentation requires five arguments, you pass four.

  4. Why are you setting these variable six times but do nothing with the first five values?
  5. First line create object "Trendline". Second fails, because object already exists. Check your return codes (OrderModify and OrderSelect) Check your return codes (OrderCreate) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  6. You would be greatly full of what?

Of course, I want to find the highest highs of the first 3 bars but I see my mistake is that I'm not using the for loop correctly along with the functions.

As far as I understood the for loop would set i as 1 for the curent bar, and set the 4th parameter in iHighest so it could use the i to count through 1 to 3 bars and therefore set the 5 variables as it iterates through. And when it has found the highest value it would stop all 5 variables
at the highest high where I could then call them up using the ObjectCreate () function to draw objects using time1 and highestHigh1 as anchor points.

I was trying to do exactly the same with the following for loop only counting through bars 4 - 10 and setting time2 and highestHigh2 as anchor points.

I then wanted to use stochastic1, stochastic2 as anchor points also for the second ObjectCreate() function.

whroeder1
13610
whroeder1 2015.06.01 17:15  
renzbub: I'm not using the for loop correctly along with the functions.
No loops are required. iHighest - MQL4 Documentation
Stephen Reynolds
484
Stephen Reynolds 2015.06.03 19:07  
WHRoeder:
renzbub: I'm not using the for loop correctly along with the functions.
No loops are required. iHighest - MQL4 Documentation

Thanks for the help with my previous problem your advise works wonders. However I'm still concerned about the ObjectCreate(0 function?

In the code below I'm trying to draw a simple object on a sub window of iStochastic :

// Input variables 
input int KPeriod = 14;
input int DPeriod = 3;
input int Slowing = 3;
input ENUM_MA_METHOD StochMethod = MODE_SMA;
input ENUM_STO_PRICE StochPrice = STO_LOWHIGH;

// OnTick() event handler                
void OnInit() 
{  
   double stoch = iStochastic(_Symbol,_Period,KPeriod,DPeriod,Slowing,StochMethod,StochPrice,MODE_MAIN,0);
   datetime time=iTime(NULL,0,0);

   bool line1=ObjectCreate("Thumb",OBJ_ARROW_THUMB_UP,1,time,stoch);
}

Why cant I get the ObjectCreate() function to put an object on sub window? Ive put 1 on 3rd parameter as required but it still doesnt draw any object?
When I place 0 it does draw the object okay on the main chart but why not stochastic?

Here I'm confused? Im beginning to think ObjectCreate () can only draw objects on main window?

Keith Watford
Moderator
7904
Keith Watford 2015.06.03 22:19  

ObjectCreate() returns a bool

If you checked GetLastError() if it returns false you will probably see why.

I am almost 100% certain that you will find that the object is not being created in the sub-window because an object of the same name already exists in the main window. Left over from your previous code.

It is usually good practice to delete objects in deinit.

I bet that if you add

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  
  ObjectDelete(0,"Thumb");
//---
   
  }

 your object will be drawn

Stephen Reynolds
484
Stephen Reynolds 2015.06.04 11:33  
GumRai:

ObjectCreate() returns a bool

If you checked GetLastError() if it returns false you will probably see why.

I am almost 100% certain that you will find that the object is not being created in the sub-window because an object of the same name already exists in the main window. Left over from your previous code.

It is usually good practice to delete objects in deinit.

I bet that if you add

 your object will be drawn

 

Ive tried the code :

// OnTick() event handler                
void OnInit() 
{  
   double stoch = iStochastic(_Symbol,_Period,KPeriod,DPeriod,Slowing,StochMethod,StochPrice,MODE_MAIN,0);
   double price = iHigh(NULL,0,0);
   datetime time=iTime(NULL,0,0);

   bool line1=ObjectCreate("Thumb",OBJ_TEXT,1,time,price);
}

// OnDeinit() event handler     
void OnDeinit(const int reason)
{  
  ObjectDelete(0,"Thumb");
}

Although I see the code works the object still doesn't appear on sub window.

Though I cant see why this code is needed as far as I understand there isn't an object being created on the main window as long as I input 1 on 3rd parameter of ObjectCreate(). 

Keith Watford
Moderator
7904
Keith Watford 2015.06.04 12:01  
   bool line1=ObjectCreate("Thumb",OBJ_TEXT,1,time,price);

As I said in my earlier post, check for errors

It's all very well giving a value to bool  line1, but you do nothing to check if it is false. 

What is the chart symbol, if it is something like Gold, price would place the object outside of the extremes of window 1

Press Ctrl+B and see if the object has been created and where it is placed 

Stephen Reynolds
484
Stephen Reynolds 2015.06.04 16:15  
GumRai:

As I said in my earlier post, check for errors

It's all very well giving a value to bool  line1, but you do nothing to check if it is false. 

What is the chart symbol, if it is something like Gold, price would place the object outside of the extremes of window 1

Press Ctrl+B and see if the object has been created and where it is placed 

I see that the value to bool line 1 is false when I use 1 as sub window and true when I use 0 as sub window.

// Input variables 
input int KPeriod = 5;
input int DPeriod = 3;
input int Slowing = 3;
input ENUM_MA_METHOD StochMethod = MODE_SMA;
input ENUM_STO_PRICE StochPrice = STO_LOWHIGH;



// OnTick() event handler                
void OnInit() 
{  
   double stoch = iStochastic(_Symbol,_Period,KPeriod,DPeriod,Slowing,StochMethod,StochPrice,MODE_MAIN,0);
   double price = iHigh(NULL,0,0); 
   datetime time=iTime(NULL,0,0);  

   bool line1=ObjectCreate("Thumb",OBJ_TEXT,1,time,price);   // false on 1 and stoch  and 1 and price     
                                                               // true on 0 and price
   if(line1=ObjectCreate("Thumb",OBJ_TEXT,1,time,price) == false)
   {
      bool obj1 = ObjectCreate("Thumbdown",OBJ_ARROW_THUMB_DOWN,0,time,price);
   }
   else
   {
      bool obj2 = ObjectCreate("Thumbup",OBJ_ARROW_THUMB_UP,0,time,price);
   }
}


Ive checked to see if its out of visible range using Ctrl+B but it never appears.


I still cant figure out why its false?

Alain Verleyen
Moderator
28551
Alain Verleyen 2015.06.04 16:50  
renzbub:

I see that the value to bool line 1 is false when I use 1 as sub window and true when I use 0 as sub window.


Ive checked to see if its out of visible range using Ctrl+B but it never appears.


I still cant figure out why its false?

As I said you in your other topic, don't run such code in OnInit(). Your object is probably created with a time or price of 0, check your values.

Read the suggestion from Gumrai and WHRoeder again, you don't check the return value of ObjectCreate properly.

/
To add comments, please log in or register