expert advisor - miscellaneous questions - page 21

 
Marco vd Heijden:

Or you could start a counter once dragging has been detected there are many ways to do it.,

( I just " have " one way for now to do it for me - that way in your latest comment )
Mr. Marco you saved my day, thanks a lot man.

Not for now but I will try later which one I want to add additionally few functions to this ' SL and TP ' functions. I just really really need to research a lot of things about that before I start to write script for that.

All the best to you man! 

 

If I remember right - I seen a long time ago one " Order Modifier " EA's work like this: which one it was not updates while dragging, when I " Drag OFF " drag line, after dragging and then " Stop Loss and Take Profit " values could change just once.
So I three and more times read your latest comment and also tried change a little a bit more that I could stop updates while I dragging.

Q:     So is it not possible, please?

Thanks in advance. 

 

Well for as long as the boolean flag drag == 1 you could use this same flag for disabling updates.

Like i said you can also start a counter here's an example of that:

//+------------------------------------------------------------------+
//|                                                  Drag Hline2.mq4 |
//|      Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

double price;
bool drag;
int cnt=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(1);
   ObjectCreate(0,"line",OBJ_HLINE,0,0,Ask);
   price=ObjectGetDouble(0,"line",OBJPROP_PRICE,0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   if(price!=ObjectGetDouble(0,"line",OBJPROP_PRICE,0))
     {
      drag=1;
     }

   if(drag==1)
     {
      cnt++;       // increase counter
        {
         if(cnt>=2)// if counter == 2 seconds
           {
            price=ObjectGetDouble(0,"line",OBJPROP_PRICE,0); // store new value
            Print(" New price: ",DoubleToString(price));
            PlaySound("ok.wav");
            cnt=0;  // reset counter
            drag=0; // reset drag
           }
        }
     }
  }
//+------------------------------------------------------------------+

Or simply use a while loop this one works very well:

//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   while(price!=ObjectGetDouble(0,"line",OBJPROP_PRICE,0))
    {
     PlaySound("ok.wav");
     price=ObjectGetDouble(0,"line",OBJPROP_PRICE,0);
     Comment(price);
    }
  }
//+------------------------------------------------------------------+
It's just saying while (i'm dragging) update the price until they become both equal (when i stop dragging)
 
Marco vd Heijden:

Well for as long as the boolean flag drag == 1 you could use this same flag for disabling updates.
Like i said you can also start a counter here's an example of that:
Or simply use a while loop this one works very well:
It's just saying while (i'm dragging) update the price until they become both equal (when i stop dragging)

( and your codes )

What a brilliant examples man?! That is really very very useful comment, thanks a lot. And now I am trying to add more things for that lines ( designs and functions ).
( Please, do not blame me for my this question: I have " graphics() " which is I am using it for graphical objects, and I am calling it through Init() and I need to call it through OnTimer(), so my question is: Am I doing wrong, please? )

All the best to you! 

 

You can run into error 4200 which is failed to create object because it already exists.

So you have to see if the object exists before you try to (re)-create or modify it.

You can use:

   if(ObjectFind(0,"Long")<0)
     {
      //Object does not exist

     }
  
and/or

   if(ObjectFind(0,"Long")>=0)
     {
      // Object already exists
    
     }
 
Marco vd Heijden:

You can use:

Hmm, this is really new Function for me.
I could try after I research about that. ( I saw it a lot of times but I never use it for me. )

Thanks a lot.

 

#Convert Price to Pixels - Open 

I use " HLine " for Take Profit function.
I have few objects ( Label, RecLabel... and so on ) and I would like to that objects could moves with " HLine " object. So I has been found below function. But I would not like to convert " X ". I want to convert only Price to Y.
         I never tried anything with below code.
Q:    
Cause I want to be sure could it be help me for my this concern?
         ( if I know it could help me then I will start to try it for my concern ).
Q:     And is there any methods for some objects could moves with " HLine "?

// this is just example for that what I am thinking
datetime time = 0; // I just want to ignore this because I just want to give to X = 20; fixed mode ( should not change ever )
double price = ObjectGetDouble( 0, "line", OBJPROP_PRICE, 0 );

int x,y;

ChartTimePriceToXY( long chart_id, int sub_window, datetime time, double price, int& x, int& y );

Thanks in advance.

 

You will have to be a bit more specific you can do many things in many ways.

These functions are used in the OnChartEvent() function.

ChartPriceToXY - will give you the price time conversion to coordinates , is this really what you need ?

Maybe you mean ChartXYToTimePrice() ?

https://www.mql5.com/en/docs/chart_operations/chartxytotimeprice

If you don't need the value, you can just pass it as a zero see example below.

//+------------------------------------------------------------------+
//|                                                    CrossHair.mq4 |
//|      Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create some lines
   ObjectCreate(0,"X",OBJ_HLINE,0,0,Ask);          // X = Horizontal Axis (time==0)
   ObjectCreate(0,"Y",OBJ_VLINE,0,TimeCurrent(),0);// Y = Vertical Axis (price==0)      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- clean up  
   ObjectsDeleteAll(0,0,EMPTY);  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//--- If this is an event of a mouse click on the chart
   if(id==CHARTEVENT_CLICK)
     {
      //--- Prepare variables
      int      x     =(int)lparam;
      int      y     =(int)dparam;
      datetime time  =0;
      double   price =0;
      int      window=0;
      //--- Convert the X and Y coordinates in terms of date/time
      if(ChartXYToTimePrice(0,x,y,window,time,price))
        {
         ObjectMove(0,"X",0,0,price); // notice time==0 because horizontal axis
         ObjectMove(0,"Y",0,time,0);  // notice price==0 because vertical axis
        }
      else
         Print("ChartXYToTimePrice return error code: ",GetLastError());
      Print("+--------------------------------------------------------------+");
     }
  }  
//+------------------------------------------------------------------+



Documentation on MQL5: Chart Operations / ChartXYToTimePrice
Documentation on MQL5: Chart Operations / ChartXYToTimePrice
  • www.mql5.com
Chart Operations / ChartXYToTimePrice - Reference on algorithmic/automated trading language for MetaTrader 5
 

I already read that documentation but I was not clearly figured out it until your latest comment. Actually but I do not tried yet, because I struggle below code, I already tried few ways for good results, but I can't.
Mr. Marco
thanks for your latest great comment which is I feel I can use it to my objects moves issue.

I really spent a lot time to find solution to my concern, I can't get good results.
But anyway I can get ( currently opening positions ) Take Profit Prices which is I want to " hline " objects could call Take Profit prices to itself, and it's work. And it is just works initial times. Yes I know because it is in " Init() ". But I tried put it in " OnTimer() " but id does not work correctly.

Please let me know something that help me to figure out what I could learn, and do for my this issue.
I will start to research again about this issue after 7 hours later.
If I get good comment that will be better for me.

// init()------------------------------------------------------------

for ( i = OrdersTotal() - 1; i >= 0; i-- )
{
    if  ( ! OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) ) continue;
    if  ( OrderSymbol() == Symbol() ) tpprice = OrderTakeProfit();

    ObjectCreate    (
                        "#" + IntegerToString( OrderTicket() ) + " -" + "tphline", // name
                        OBJ_HLINE,
                        0,      // subwindow
                        0,      // time
                        tpprice // price1
                    );
    tpprice =
    ObjectGetDouble (
                        0,
                        "#" + IntegerToString( OrderTicket() ) + " -" + "tphline", // name
                        OBJPROP_PRICE, 0
                    );

}
// ontimer() --------------------------------------------------------

if  ( tpprice != ObjectGetDouble( 0, "#" + IntegerToString( OrderTicket() ) + " -" + "tphline", OBJPROP_PRICE, 0 ) )
{
    tpdrag = 1;
}

if  ( tpdrag == 1 )
{
    tpprice = ObjectGetDouble( 0, "#" + IntegerToString( OrderTicket() ) + " -" + "tphline", OBJPROP_PRICE, 0 );
    Print( "tpdrag: ", tpdrag, " - Price: ", DoubleToString( tpprice, Digits ) );

    // actually here is a graphical objects functin()
    // here one of them
    // also which one soon I will try to  below objects could moves together " tphline " - but I can't take a time to research about your latest comment and so...
    ObjectCreate( "recl object", OBJ_RECTANGLE, 0, 0, tpprice ); // I feel I could add something to " string name " - I already tried few things not good results

    tpdrag = 0;
}

//---
return;

Thanks in advance.
All the best to you!

 

Please use the styler it's under the Tools tab.

I have no idea what you are trying to accomplish so i have to guess what you want to do this is never good.

But you can look at the example here:

//+------------------------------------------------------------------+
//|                                                    Stealth 4.mq4 |
//|      Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Marco vd Heijden, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

static input int takeprofit=500;// Take Profit
static input int stoploss=500;  // Stop Loss

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(1);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   for(int order=OrdersTotal(); order>=0; order--)
     {
      bool selected=OrderSelect(order,SELECT_BY_POS);
        {
         if(selected==1)
           {
            if(Symbol()==OrderSymbol()) // only for current chart symbol
              {
               switch(OrderType())
                 {
                  case OP_BUY: // for buy order
                    {
                     // if objects not found - create them
                     if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-TP")<0)
                       {
                        ObjectCreate(0,"#"+IntegerToString(OrderTicket())+"-TP",OBJ_HLINE,0,0,OrderOpenPrice()+takeprofit*Point());
                        ObjectSet("#"+IntegerToString(OrderTicket())+"-TP",7,3);
                       }
                     if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-SL")<0)
                       {
                        ObjectCreate(0,"#"+IntegerToString(OrderTicket())+"-SL",OBJ_HLINE,0,0,OrderOpenPrice()-stoploss*Point());
                        ObjectSet("#"+IntegerToString(OrderTicket())+"-SL",7,3);
                       }
                     // if objects exist
                     if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-TP")>=0)
                       {
                        if(Bid>ObjectGetDouble(0,"#"+IntegerToString(OrderTicket())+"-TP",OBJPROP_PRICE,0))
                          {
                           bool close=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrBlue);
                             {
                              if(close==0)
                                {
                                 Alert(" Order Close Error! # "+IntegerToString(OrderTicket()));
                                }
                              if(close==1)
                                {
                                 Alert(" Order: "+IntegerToString(OrderTicket())+" Closed due to TP Profit = "+DoubleToString(OrderProfit(),2));
                                 ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-TP");
                                 ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-SL");
                                }
                             }
                          }
                       }
                     if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-SL")>=0)
                       {
                        if(Ask<ObjectGetDouble(0,"#"+IntegerToString(OrderTicket())+"-SL",OBJPROP_PRICE,0))
                          {
                           bool close=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrBlue);
                             {
                              if(close==0)
                                {
                                 Alert(" Order Close Error! # "+IntegerToString(OrderTicket()));
                                }
                              if(close==1)
                                {
                                 Alert(" Order: "+IntegerToString(OrderTicket())+" Closed due to SL Profit = "+DoubleToString(OrderProfit(),2));
                                 ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-TP");
                                 ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-SL");
                                }
                             }
                          }
                       }
                    }
                  break;

                  case OP_SELL: // for sell order
                    {
                     // if objects not found - create them
                     if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-TP")<0)
                       {
                        ObjectCreate(0,"#"+IntegerToString(OrderTicket())+"-TP",OBJ_HLINE,0,0,OrderOpenPrice()-takeprofit*Point());
                        ObjectSet("#"+IntegerToString(OrderTicket())+"-TP",7,3);
                       }
                     if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-SL")<0)
                       {
                        ObjectCreate(0,"#"+IntegerToString(OrderTicket())+"-SL",OBJ_HLINE,0,0,OrderOpenPrice()+stoploss*Point());
                        ObjectSet("#"+IntegerToString(OrderTicket())+"-SL",7,3);
                       }
                     // if objects exist
                     if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-TP")>=0)
                       {
                        if(Ask<ObjectGetDouble(0,"#"+IntegerToString(OrderTicket())+"-TP",OBJPROP_PRICE,0))
                          {
                           bool close=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrBlue);
                             {
                              if(close==0)
                                {
                                 Alert(" Order Close Error! # "+IntegerToString(OrderTicket()));
                                }
                              if(close==1)
                                {
                                 Alert(" Order: "+IntegerToString(OrderTicket())+" Closed due to TP Profit = "+DoubleToString(OrderProfit(),2));
                                 ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-TP");
                                 ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-SL");
                                }
                             }
                          }
                       }
                     if(ObjectFind(0,"#"+IntegerToString(OrderTicket())+"-SL")>=0)
                       {
                        if(Bid>ObjectGetDouble(0,"#"+IntegerToString(OrderTicket())+"-SL",OBJPROP_PRICE,0))
                          {
                           bool close=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrBlue);
                             {
                              if(close==0)
                                {
                                 Alert(" Order Close Error! # "+IntegerToString(OrderTicket()));
                                }
                              if(close==1)
                                {
                                 Alert(" Order: "+IntegerToString(OrderTicket())+" Closed due to SL Profit = "+DoubleToString(OrderProfit(),2));
                                 ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-TP");
                                 ObjectDelete(0,"#"+IntegerToString(OrderTicket())+"-SL");
                                }
                             }
                          }
                       }
                    }
                  break;
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+


So you can see that you can use ObjectGetDouble directly, there is no need to copy the value to another double because the object itself holds the value, and when you drag the line that value changes automatically, and will be seen the next time you read it.

Reason: