Download MetaTrader 5

[Help] OBJ_TEXT on chart.

To add comments, please log in or register
Mohamad Zulhairi Baba
6126
Mohamad Zulhairi Baba  

Hello friends,

‌I develop and EA to open order base on multiple strategy. 

  1. C‌ode should draw a OBJ_TEXT on each new OP (Chart & snippet code as below)
  2. On the next Sell Order(same strategy), it should also draw OBJ_TEXT again (Sell Order), as marked in blue circle.
  3. so i figure out what's wrong, the previous (ObjectFind(0,name) already exist.
  4. I change to code as per 2nd snippet, but still no luck. Any idea how to do that?

sell order

string CommentSell = "Sell Order",
       CommentBuy  = "Buy Order";

void OnTick()
{
   // Trade Logic
   if(Startegy1()=="Sell")
   {
      Sell_Ticket = OrderSend(Symbol(),Type,LotSize,pPrice,Slippage,SL_Level,TP_Level,CommentSell,Magic,0,Arrow);
      if(Sell_Ticket)
      {
         for(int order = 0; order <= OrdersTotal() - 1; order++)
         {
            bool select = OrderSelect(order,SELECT_BY_POS);
            RefreshRates();
            if(select && OrderType()==OP_SELL && OrderMagicNumber() == Magic) OpenOrderTag(CommentSell,OrderOpenPrice()+50*Point,clrRed);
         }
      }
   }
  
   if(Startegy1()=="Buy")
   {
    // code goes here
   }
}


void OpenOrderTag(string name, double price, color col)
{
   ObjectCreate(name,OBJ_TEXT,0,Time[0],price);
   ObjectSetText(name,name,7,"Arial",EMPTY);
   ObjectSet(name, OBJPROP_COLOR,col);
}‌

Revised code.

OpenOrderTagTicket(Sell_Ticket,CommentSell,OrderOpenPrice()+50*Point,clrRed);
void OpenOrderTagTicket(int Ticket, string name, double price, color col)
{
   string x = IntegerToString(Ticket);
   ObjectCreate(name+x,OBJ_TEXT,0,Time[0],price);
   ObjectSetText(name+x,name,7,"Arial",EMPTY);
   ObjectSet(name+x,OBJPROP_COLOR,col);
}
whroeder1
15917
whroeder1  
Mohamad Zulhairi Baba:
  1. I change to code as per 2nd snippet, but still no luck. Any idea how to do that?
  1. Luck is irrelevant. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. Check your return codes (ObjectCreate) and find out why. 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
  3.       Sell_Ticket = OrderSend(Symbol(),Type,LotSize,pPrice,Slippage,SL_Level,TP_Level,CommentSell,Magic,0,Arrow);
          if(Sell_Ticket)
          {
             for(int order = 0; order <= OrdersTotal() - 1; order++)
             {
                bool select = OrderSelect(order,SELECT_BY_POS);
                RefreshRates();
                if(select && OrderType()==OP_SELL && OrderMagicNumber() == Magic)
    OrderSend returns a ticket number or -1. Since non-zero is true, your if statement is always true.
  4. Why are you doing all that? If you have a valid Sell_Ticket, just select by ticket.
Mohamad Zulhairi Baba
6126
Mohamad Zulhairi Baba  
whroeder1:
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. Check your return codes and find out why. 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


hi whroeder1, thanks for replying.

  1. The code work, but it only work once... as per my above explanation.. But anyway, found the solution already, like 1 hour ago.. (my mistake, i miss que the trade logic)

S‌OLVE

Mohamad Zulhairi Baba
6126
Mohamad Zulhairi Baba  
whroeder1:
  1. Luck is irrelevant. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. Check your return codes (ObjectCreate) and find out why. 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
  3.       Sell_Ticket = OrderSend(Symbol(),Type,LotSize,pPrice,Slippage,SL_Level,TP_Level,CommentSell,Magic,0,Arrow);
          if(Sell_Ticket)
          {
             for(int order = 0; order <= OrdersTotal() - 1; order++)
             {
                bool select = OrderSelect(order,SELECT_BY_POS);
                RefreshRates();
                if(select && OrderType()==OP_SELL && OrderMagicNumber() == Magic)
    OrderSend returns a ticket number or -1. Since non-zero is true, your if statement is always true.
  4. Why are you doing all that? If you have a valid Sell_Ticket, just select by ticket.

I have another logic for if(!Sell_Ticket), to check for error.. Order Failed & no OBJ_TEXT will be drawn.

if(!Sell_Ticket) Print("Order ",Ticket," failed. Error: ",ErrorDescription(GetLastError()));

anyway Thanks Whoeder1 for your reply.

honest_knave
Moderator
2326
honest_knave  
Mohamad Zulhairi Baba:

I have another logic for if(!Sell_Ticket), to check for error.. Order Failed & no OBJ_TEXT will be drawn.

if(!Sell_Ticket) Print("Order ",Ticket," failed. Error: ",ErrorDescription(GetLastError()));

anyway Thanks Whoeder1 for your reply.


OrderSend does NOT return true/false.

OrderSend returns the ticket number, or -1 if it failed.

T‌his:‌

if(!Sell_Ticket)

M‌eans the same as this:

if(Sell_Ticket == false)

M‌eans the same as this:

if(Sell_Ticket == 0)

As @whroeder1 has pointed out, this will probably never happen. The only time it can happen is if OrderSend succeeds, and the ticket number is 0.‌ I have never seen this.

Remember: if OrderSend fails, it returns -1

‌You should be writing:‌

if(Sell_Ticket < 0)
Mohamad Zulhairi Baba
6126
Mohamad Zulhairi Baba  
honest_knave:


OrderSend does NOT return true/false.

OrderSend returns the ticket number, or -1 if it failed.

T‌his:‌

if(!Sell_Ticket)

M‌eans the same as this:

if(Sell_Ticket == false)

M‌eans the same as this:

if(Sell_Ticket == 0)

The only time your logic works is if OrderSend succeeds, and the ticket number is 0.‌

Hi honest_Knave,

‌‌Thanks for pointing out. should've change to

if(Sell_Ticket == -1) Print("Order ",Ticket," failed. Error: ",ErrorDescription(GetLastError()));

‌and select by ticket as Whoeder1 advise.

OrderSelect - Trade Functions - MQL4 Reference
OrderSelect - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderSelect - Trade Functions - MQL4 Reference
To add comments, please log in or register