Horizontal lines dont move

 

I am trying to draw 4 horizontal line calculated using a MA but when i run this code, it draw the lines but dosnt update them 

What should i do?

int start()

    {
//---- BURADA EMA'YA GORE +-30 ve +-300 RANGE'leri çiziyor.
    double x;
    x =iMA(NULL,0,EMA,0,MODE_EMA,PRICE_CLOSE,0);
    ObjectCreate("-30",OBJ_HLINE,0,TimeCurrent(),x-30,0,0,0,0);
    ObjectCreate("+30",OBJ_HLINE,0,TimeCurrent(),x+30,0,0,0,0);
    ObjectCreate("-300",OBJ_HLINE,0,TimeCurrent(),x-300,0,0,0,0);
    ObjectCreate("+300",OBJ_HLINE,0,TimeCurrent(),x+300,0,0,0,0);
    ObjectMove("-30",1,TimeCurrent(),x-30);
    ObjectMove("+30",1,TimeCurrent(),x+30);
    ObjectMove("-300",1,TimeCurrent(),x-300);
    ObjectMove("+300",1,TimeCurrent(),x+300);

    
 
  1. Why are you trying to create them each tick? Once in init; the price doesn't matter. Modify them in start.

  2. If you run on the EURUSD the prices are like 1.23456. That ±30 will not be on the chart.

  3. Horizontal lines only have one price/time coordinate. You are trying to modify the second.

  4. Your code
     Documentation
    double x =iMA(
    NULL,
    0,           
    EMA,
    0,
    MODE_EMA,
    PRICE_CLOSE,
    0
    );
    double  iMA(
     string             symbol,        // symbol
     ENUM_TIMEFRAMES    timeframe,     // timeframe
     int                ma_period,     // MA averaging period
     int                ma_shift,      // MA shift
     ENUM_MA_METHOD     ma_method,     // averaging method
     ENUM_APPLIED_PRICE applied_price, // applied price
     int                shift          // shift
    );
    Is EMA an int/length? Write self-documenting code. Zero is not a proper enumeration.

  5. Start using the new Event Handling Functions.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference
 

Thanks for the reply William , you are the best.

I found an easier way, added Envelope Indicator to chart, it solved my problem :)

Now i am trying to put a delay after STOPLOSS hits but when i try to compile it gives the errors below. (btw that code is yours, i found on some other thread :))



int start()

    {

    
//----   
   double MaCurrent;
   int ticket, total;

   if(Bars<20)
     {
      Print("bars less than 20");
      return(0);  
     }
   if(TakeProfit<8)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
   MaCurrent=iMA(NULL,0,EMA,0,MODE_EMA,PRICE_CLOSE,0);
   

  //...
  //---- Find my open order,
  for(int index = OrdersTotal() - 1; index >= 0; index--) if (
     OrderSelect(index, SELECT_BY_POS)
  && OrderMagicNumber() == 34567 || 56789       // with my magic number,
  && OrderSymbol()      == Symbol() )   // in my chart.



    static datetime newAllowed;
    newAllowed = TimeCurrent() + 20 * 1; // No new trade until this one closes 20 sec * min


if (TimeCurrent() < newAllowed) return(0);  // No new trades yet.

// Now I can open a new one.
   
   total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(3*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
     
      
     if(OrdersTotal()>0){
         for(i=1; i<=OrdersTotal(); i++)          // Checking to make sure there are no open orders. Keep this set to zero unless you want the advisor to open more than one buy at a time or more than one sell at a time.
         {
            if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
               {
                   if(OrderMagicNumber()==34567) {int halt1=1;}///if this magicnumber has an order open... halt!
                   if(OrderMagicNumber()==56789) {int halt2=1;}///if this magicnumber has an order open... halt!
                   }
                  }
                  }
       // check for long position (BUY) possibility
      if(
         
          (Close[0]-MaCurrent)<=buyfark )
         
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-Stoploss*Point,Bid+TakeProfit*Point,"Scalp",34567,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      // check for short position (SELL) possibility
      if ((Close[0]-MaCurrent)>=sellfark)
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+Stoploss*Point,Ask-TakeProfit*Point,"Scalp",56789,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     }
     return(0);
   }
// the end.
Reason: