Get Object properties when moved not while being moved

 

I think I know the answer, I though I would check with those that are more knowledgeable than me just in case someone has a good idea.

I'm coding for an Indicator, I want to do something when a Trend Line has been moved by the user, ideally I want to only do the something when the TL has been moved to it's final position not while the user is in the process of moving it.

If it had been an EA I would have simply inserted a Sleep to allow time for the user to finish moving the TL (maybe 0.5s).

Mmmmm, it just occurred to me, can I detect if the mouse button is down ?

Any other suggestions ?

 
RaptorUK:


Mmmmm, it just occurred to me, can I detect if the mouse button is down ?



Most of my code is done now, will investigate the mouse button option in the morning, unless anyone else has any other suggestions ?
 

void mouse_event(int dwFlags,int dx,int dy,int dwData,int dwExtraInfo);

in winuser32.mqh, don't know what flags to use and stuff. To me windows api is obscure. Probably needs some google. hm. Googled it for you.

This flag is if left button down :0x0002

this flag is if left button up: 0x0004

More here: link.

Hf.

 
forexCoder:

void mouse_event(int dwFlags,int dx,int dy,int dwData,int dwExtraInfo);

in winuser32.mqh, don't know what flags to use and stuff. To me windows api is obscure. Probably needs some google. hm. Googled it for you.

This flag is if left button down :0x0002

this flag is if left button up: 0x0004

More here: link.

Hf.


Hey, thanks for that :-)

Seems that "The mouse_event function synthesizes mouse motion and button clicks." rather than telling me what is what.

 

I came across this, https://www.mql5.com/en/forum/115921 but I'm a little concerned about this statement:

"Finally, and most importantly, do not use this on a live account."

my Indicator will be used on Live and Demo accounts.

My Indicator can work very well without the need to detect if a mouse button is pressed, it would have been a nice addition but is not essential.

 
RaptorUK:

I think I know the answer, I though I would check with those that are more knowledgeable than me just in case someone has a good idea.

I'm coding for an Indicator, I want to do something when a Trend Line has been moved by the user, ideally I want to only do the something when the TL has been moved to it's final position not while the user is in the process of moving it.

If it had been an EA I would have simply inserted a Sleep to allow time for the user to finish moving the TL (maybe 0.5s).

Mmmmm, it just occurred to me, can I detect if the mouse button is down ?

Any other suggestions ?

I figured out a work around for this . . . . just by accident.

To check if the user is in the process of moving the trend line and has one of the anchors click and is being dragged . . . . try setting the price of each of the anchors to the price of the anchors, if it fails then the line either doesn't exist or is being moved, I get an error 4202.

if ( ObjectSet( TL_Name, OBJPROP_PRICE1, ObjectGet(TL_Name, OBJPROP_PRICE1 ) )  )       // the TL is not being moved, also do for PRICE2

 

Or, you could do it like this ...

#property indicator_chart_window

#import "user32.dll"
   int GetKeyState(int nVirtKey);
#import

#define TRENDNAME "raptorIND"
#define TIMEOUT  5   // measured in seconds

#define VK_LBUTTON 0x01
#define VK_RBUTTON 0x02

// trendline position
datetime startT = 0;
datetime endT =   0;
double   startP=  0.0;
double   endP  =  0.0;

bool lineHasMoved= false;
datetime whenDitMove=0;
//+------------------------------------------------------------------+
int init(){
   ObjectDelete(TRENDNAME); // just in case there is one already
   
   int start = WindowFirstVisibleBar()/2;
   ObjectCreate(TRENDNAME,OBJ_TREND,0,Time[start],Close[start],Time[1],Close[1]);
   ObjectSet(TRENDNAME,OBJPROP_RAY, false);
   
   LineChanged(false);  // store position
   
   return(0);
}
//+------------------------------------------------------------------+
int deinit(){
   ObjectDelete(TRENDNAME);
   Comment("");

   return(0);
}
//+------------------------------------------------------------------+
int start(){
   if( lineHasMoved ){
      if( KeyDown(VK_LBUTTON) ){       // don't do anything if the mouse button is down
         Print("left mouse");
         whenDitMove = TimeCurrent();  // wait for a timeout from after the mouse was LAST pressed.
         return( 0 );
      }
      
      if( TimeCurrent() - whenDitMove > TIMEOUT ){
         Alert("trendline has moved");
      
         // do stuff here or set a flag to do stuff
         
         Comment("");
         lineHasMoved= false;  
         LineChanged(false);  // store new position
      }
   }
   else{
      lineHasMoved = LineChanged();
      if( lineHasMoved ){
         whenDitMove = TimeCurrent();
         Comment("line is moving");
      }
   }
   
   //Comment( "key=" + GetKeyState(VK_LBUTTON) );
   
   return(0);
}
//+------------------------------------------------------------------+
bool LineChanged(bool test=true){   // test or set the globals with the trendline co-ordinates
   if( test ){
      if( startT != ObjectGet(TRENDNAME,OBJPROP_TIME1) )
         return( true );
      if( endT   != ObjectGet(TRENDNAME,OBJPROP_TIME2) )
         return( true );
      if( startP != ObjectGet(TRENDNAME,OBJPROP_PRICE1) )
         return( true );
      if( endP   != ObjectGet(TRENDNAME,OBJPROP_PRICE2) )
         return( true );
      return( false );
    }
    
    // then we are setting instead.
    startT = ObjectGet(TRENDNAME,OBJPROP_TIME1);
    endT   = ObjectGet(TRENDNAME,OBJPROP_TIME2);
    startP = ObjectGet(TRENDNAME,OBJPROP_PRICE1);
    endP   = ObjectGet(TRENDNAME,OBJPROP_PRICE2);
    
    return( true );
}
//-----------------------------------------------------------------------------------+
bool KeyDown( int nVirtKey ){

   if( GetKeyState(nVirtKey) > 1000 )
      return( true );

   return( false );
}
 
dabbler:

Or, you could do it like this ...

Why didn't you say that back in July last year ? :-) no need for it now . . . but thanks anyway, it's useful to know about.
 

It's possible to do that by handling of chart's events.


Read this for help: https://docs.mql4.com/constants/chartconstants/enum_chartevents

Types of Chart Events - Chart Constants - Constants, Enumerations and Structures - MQL4 Reference
Types of Chart Events - Chart Constants - Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
Types of Chart Events - Chart Constants - Constants, Enumerations and Structures - MQL4 Reference
 
ALEKSANDR SHUKALOVICH:

It's possible to do that by handling of chart's events.


Read this for help: https://docs.mql4.com/constants/chartconstants/enum_chartevents

Why are you replying to a 9 year old topic??

Don't bring old topics to the top for no good reason!

When this topic was started OnChartEvent() didn't exist!