Download MetaTrader 5
To add comments, please log in or register
Download MetaTrader 5 and start trading today!
Tim
45
Tim 2014.03.01 19:49 

Hi Everyone,

I've got a problem that I've been struggling with for two days without success so I hope someone can tell me where I'm going wrong. The below code is part of an EA that places three MAs on the chart. The code below looks at each MA and places an up arrow if the MA is going up and a down arrow if the MA is going down. It works absolutely perfectly, so what is the problem?

It works perfectly if I stay on one time frame only but I want it to do the same as I change time frames. As I change time frames it puts in the new arrows but does not delete the arrows from the previous time frame. In the end it ends up with 6 arrows, three up and three down and nothing I've tried to do will delete them. You'll see from the code that ObjectsDeleteAll(0,OBJ_ARROW_DOWN); ObjectsDeleteAll(0,OBJ_ARROW_UP); are in there and they must be deleting the arrows when I'm on PERIOD_D1 because only the correct arrows appear but when I swap time frames the command stops working. Anyway any advice would be greatly appreciated. Here is the code:

//Places Moving Average Arrows - 1=Red/Down and 2=Blue/Up
if(i==1)
{
if(Buf_0[5]<Buf_0[1])
{
PlaceArrow(2,Symbol()+"arMA13UP",Buf_0[1]);
}
if(Buf_0[1]<Buf_0[5])
{
PlaceArrow(1,Symbol()+"arMA13DOWN",Buf_0[1]);
}
if(Buf_1[5]<Buf_1[1])
{
PlaceArrow(2,Symbol()+"arMA23UP",Buf_1[1]);
}
if(Buf_1[1]<Buf_1[5])
{
PlaceArrow(1,Symbol()+"arMA23DOWN",Buf_1[1]);
}
if(Buf_2[5]<Buf_2[1])
{
PlaceArrow(2,Symbol()+"arMA20UP",Buf_2[1]);
}
if(Buf_2[1]<Buf_2[5])
{
PlaceArrow(1,Symbol()+"arMA20DOWN",Buf_2[1]);
}
}

//+---------------------------------------------------------------------------------+
//| The function that places an arrow on screen | |
//+---------------------------------------------------------------------------------+
string PlaceArrow(int intArrowType,string strArrowName,double dblArrowPoint)
{
int intArrowCode=0;
color colArrowColour="";
ObjectsDeleteAll(0,OBJ_ARROW_DOWN);
ObjectsDeleteAll(0,OBJ_ARROW_UP);

switch(intArrowType)
{
case 1:
ObjectCreate(0,strArrowName,OBJ_ARROW_DOWN,0,Time[0],dblArrowPoint); // Create the Doji Type object
colArrowColour="clrRed";
intArrowCode=234;
break;
case 2:
ObjectCreate(0,strArrowName,OBJ_ARROW_UP,0,Time[0],dblArrowPoint); // Create the Doji Type object
colArrowColour="clrBlue";
intArrowCode=233;
break;
}
ObjectSetInteger(0,strArrowName,OBJPROP_COLOR,colArrowColour);
ObjectSetInteger(0,strArrowName,OBJPROP_ANCHOR,ANCHOR_TOP);
ObjectSetInteger(0,strArrowName,OBJPROP_ARROWCODE,intArrowCode);
ObjectSetInteger(0,strArrowName,OBJPROP_STYLE,STYLE_SOLID);
ObjectSetInteger(0,strArrowName,OBJPROP_WIDTH,1);
ObjectSetInteger(0,strArrowName,OBJPROP_BACK,True);

//--- return the value
return(0);
Alain Verleyen
Moderator
28537
Alain Verleyen 2014.03.01 20:05  

Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.

Keith Watford
Moderator
7897
Keith Watford 2014.03.01 22:44  
string PlaceArrow(int intArrowType,string strArrowName,double dblArrowPoint)
{
int intArrowCode=0;
color colArrowColour="";
ObjectsDeleteAll(0,OBJ_ARROW_DOWN);
ObjectsDeleteAll(0,OBJ_ARROW_UP);

switch(intArrowType)
{
case 1:
ObjectCreate(0,strArrowName,OBJ_ARROW_DOWN,0,Time[0],dblArrowPoint); // Create the Doji Type object
colArrowColour="clrRed";
intArrowCode=234;
break;
case 2:
ObjectCreate(0,strArrowName,OBJ_ARROW_UP,0,Time[0],dblArrowPoint); // Create the Doji Type object
colArrowColour="clrBlue";
intArrowCode=233;
break;
}
ObjectSetInteger(0,strArrowName,OBJPROP_COLOR,colArrowColour);
ObjectSetInteger(0,strArrowName,OBJPROP_ANCHOR,ANCHOR_TOP);
ObjectSetInteger(0,strArrowName,OBJPROP_ARROWCODE,intArrowCode);
ObjectSetInteger(0,strArrowName,OBJPROP_STYLE,STYLE_SOLID);
ObjectSetInteger(0,strArrowName,OBJPROP_WIDTH,1);
ObjectSetInteger(0,strArrowName,OBJPROP_BACK,True);

//--- return the value
return(0);

You are creating your arrows, then changing the object arrowcode

You can make a quick fix by making this change

ObjectsDeleteAll(0,OBJ_ARROW_DOWN);
ObjectsDeleteAll(0,OBJ_ARROW_UP);

//Change to

ObjectsDeleteAll(0,OBJ_ARROW);

But you should really tidy up your code so that it is consistent.

If I may make a suggestion

Create your objects in init

Move your objects in the main program

Delete objects in deinit, preferably without using ObjectsDeleteAll

I also note that your function is declared as a string, yet returns(0)



JPS1
239
JPS1 2014.03.02 07:25  

Please do not delete all arrows of any type. Use code that selects only the arrows your indi draws. This avoids confusion and blame when another person's indi deletes items it did not create and user thinks it's your indi that is the cause. Thank you ! :)


 for(int i=ObjectsTotal()-1;i>=0;i--)
   if(StringFind(ObjectName(i),strArrowName,0)>=0)
     ObjectDelete(ObjectName(i));
Tim
45
Tim 2014.03.02 20:36  

Thanks for all your suggestions - it really is appreciated. I did not post the whole code because it runs to over 500 lines. Its only my first full length EA so I've still got a lot to learn and your suggestions will be a big help.

Thanks again

/
To add comments, please log in or register