How to make a line that mirrors price and redraws as price moves(looking for hints and advice vs finished code)

 

so obviously drawing lines is easy and setting new values to them is easy.. What im trying to do is make a line on the chart that mirrors the bid line and moves with it... As prices moves the line moves with it... But if i manually move the line to a certain level-- it will then stay there.. 


So its dynamic with the bid line till i manually move it then its static.... I feel like the move and stay part should be easy.. But the main problem is having the line move with bid and redraw... 


Im not looking to have this done for me---  but rather just some advice in the right direction to look... ( I like trying to figure these things out-- but sometimes just not sure the right direction to go or look).. 


Thanks.. 

 

Create your line. Update it on each tick.

Use OnChartevent() to catch manual move, use a boolean to say "stop updating on each tick".

Show your attempt if you want coding help.

 

Example:

//+------------------------------------------------------------------+
//|                                                  BidLineLock.mq4 |
//|                                      Copyright 2017, nicholishen |
//|                         https://www.forexfactory.com/nicholishen |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, nicholishen"
#property link      "https://www.forexfactory.com/nicholishen"
#property version   "1.00"
#property strict

#include <ChartObjects\ChartObjectsLines.mqh>

class BidLineLock : public CChartObjectHLine
{
   bool m_locked;
public:
   BidLineLock():m_locked(false){}
   void OnTick()
   {
      if(!m_locked)
         Price(0,SymbolInfoDouble(_Symbol, SYMBOL_BID));
   }
   void OnChartEvent(const int &id,const string &name)
   {
      if(id == CHARTEVENT_OBJECT_CLICK && name == Name())
         Selected(true);  
      if(id == CHARTEVENT_OBJECT_DRAG && name == Name())
         m_locked = true;
   }
   bool Create()
   {
      if(!Create(0,"__bid_line_lock__",0,SymbolInfoDouble(_Symbol, SYMBOL_BID)))
         return false;
      Selectable(true);
      return true;
   }
};
//+------------------------------------------------------------------+
BidLineLock line;
//+------------------------------------------------------------------+
int OnInit()
{
   if(!line.Create())
      return INIT_FAILED;
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void OnTick()
{
   line.OnTick();
}
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   line.OnChartEvent(id,sparam);
}
//+------------------------------------------------------------------+
 

Hi Alain and nicholshen... I just noticed your messages.. thanks :)


Im working on the code right now... Trying hard to come up with myself... When i get it ill post it here.. (i want to make sure it works-- kinda embarrassing showing newb code with you good coders here ... 

 

Hi Alain and nicholshen.


So here is what i came up with...  It finds the different between bid and the buylineprice... if the differene of the two is less then 2 pips-- then the line is moved to bid.. if its more then 2 pips(in the case that i moved the line myself)-- then the line just stays where it is.. The full code made it so once the line is moved over the threshold (in this case 2 pips) then even if price comes back to within 2 pips-- the line doesnt move.. 

                          BuyLineBidDifference=MathAbs(NormalizeDouble((BuyLinePrice-Bid)/pips,1));
                                       if(BuyLineBidDifference<2){
                                        ObjectMove(0,"BuyLine",0,0,Bid);
                                        BuyLinePrice=Bid;}


I didnt even know about SymbolInfoDouble.... Thanks for posting your code nicholshen... Nice to learn something new... The funny thing about learning to code is -- you just dont know what you dont know.. 


best to you guys.. Bruce