Modify object position

 

Hi all,

So, I'm looking to modify an object position like stoploss line in a TrailingStop code.

The object is created in the right place but it do not follow the price. Also it not delete from the chart ( at this moment the Tester chart

Following is the code that I have to create the object inside the TrailingStop code and also is the code that should delete it.

 

void Trail()
  {        
      double TrailingStop = 1;      
      double PricePosition = ObjectGet("highline",OBJPROP_PRICE1);
                    
      for(int OrderCounter = 0; OrderCounter <= OrdersTotal()-1; OrderCounter++)
         {//23
        if(!OrderSelect(OrderCounter,SELECT_BY_POS))continue;
         if(OrderType()==OP_BUY && OrderSymbol()== Symbol() 
            && OrderMagicNumber()== MagicNumber)                                                                       
            {//24
               RefreshRates();
            if(Bid - OrderOpenPrice() >= (MinimumProfit*pips2dbl)&& AccountProfit() > BaseProfit) 
               {//25
               ObjectCreate("highline",OBJ_HLINE,0,0,Bid - TrailingStop * pips2dbl); 
               ObjectSet("highline",OBJPROP_COLOR,Yellow); 
               ObjectSet("highline",OBJPROP_WIDTH,1); 
               {
                if( Bid <= PricePosition)CloseAll();
               }
               //if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid - TrailingStop * pips2dbl, OrderTakeProfit(),0,Yellow))
                Print("Buy TrailingStop Failed, error # ", GetLastError());
               }//25
             }//24
           }//23          
           if(OrderType()==OP_SELL && OrderSymbol()== Symbol() 
              && OrderMagicNumber()== MagicNumber)
              {//26
                 RefreshRates();
              if(OrderOpenPrice() - Ask >= (MinimumProfit*pips2dbl) && AccountProfit() > BaseProfit)
                 {//27
                 ObjectCreate("highline",OBJ_HLINE,0,0,Ask - TrailingStop * pips2dbl); 
                 ObjectSet("highline",OBJPROP_COLOR,Yellow); 
                 ObjectSet("highline",OBJPROP_WIDTH,1); 
                 {
                  if( Ask <= PricePosition)CloseAll();
                 }
                  
                // if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask + TrailingStop * pips2dbl,OrderTakeProfit(),0,Yellow))                 
                 Print("Sell TrailingStop Failed, error # ", GetLastError());
               }//27                            
            return(0);     
          }//26   
  }

 

void CloseAll()
              {
               int OrdType;
               int GLError;
  
                for(int OrderPos = OrdersTotal()-1; OrderPos >= 0; OrderPos--)       
                if(OrderSelect(OrderPos, SELECT_BY_POS, MODE_TRADES)
                   && OrderMagicNumber()== MagicNumber 
                   && OrderSymbol()== Symbol())                                       
                   {//31
                   OrdType = OrderType();
                   if(OrdType == OP_BUY || OrdType==OP_SELL)
                      {//32
                     while(IsTradeContextBusy()) 
                        Sleep(SleepTime);  
                          RefreshRates();
                    
                     if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),Slippage*pips2points, Yellow))                
                        {
                       if(!ObjectDelete("highline"))
                         {
                         GLError = GetLastError();
                         Print("Error Closing/Deleting Order, error # ", GLError, " for ticket: ", OrderTicket());  
                         }
                        }         
                      }//32               
                    }//31      
                 }//30 

 

Any clue how to get this working ?

thank you in advance for any  help provided

Luis 

 
luisneves:

Hi all,

So, I'm looking to modify an object position like stoploss line in a TrailingStop code.

The object is created in the right place but it do not follow the price. Also it not delete from the chart ( at this moment the Tester chart) 

Following is the code that I have to create the object inside the TrailingStop code and also is the code that should delete it.

Are your braces numbered correctly ? if they are your  

if(OrderType()==OP_SELL

 is outside of your for loop . . .  sort your braces,  sort your indenting.

The line is only deleted if the OrderClose() fails,    if ( !  OrderClode() )   check your code.  Move the ObjectDelete outside the  if ( !  OrderClode() )  {  }  braces.

 
RaptorUK:

Are your braces numbered correctly ? if they are your  

 is outside of your for loop . . .  sort your braces,  sort your indenting.

The line is only deleted if the OrderClose() fails,    if ( !  OrderClode() )   check your code.  Move the ObjectDelete outside the  if ( !  OrderClode() )  {  }  braces.


Hi RaptorUK,

Thank you for your attention to my issue and sorry to get your time.

I've put out the ObjectDelete form the if(!OrderClose),but the line still there.... 

Could you tell me where I'm wrong ?

Best regards

Luis 

void CloseAll()
              {//1
               int OrdType;
               int GLError;
  
               for(int OrderPos = OrdersTotal()-1; OrderPos >= 0; OrderPos--)       
                   if(OrderSelect(OrderPos, SELECT_BY_POS, MODE_TRADES)
                      && OrderMagicNumber()== MagicNumber 
                      && OrderSymbol()== Symbol())                                       
                      {//2
                       OrdType = OrderType();
                    if(OrdType == OP_BUY || OrdType==OP_SELL)
                       {//3
                     while(IsTradeContextBusy()) 
                        Sleep(SleepTime);  
                          RefreshRates();
                                            
                    if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),Slippage*pips2points, Red))
                       GLError = GetLastError();
                       Print("Error Closing Order, error # ", GLError, " for ticket: ", OrderTicket());
                       }//3
                    if(!ObjectDelete("highline"))
                       GLError = GetLastError();
                       Print("Error Deleting Object, error # ", GLError, " for ticket: ", OrderTicket());
                      }//2
                 }//1 
 

luisneves: The object is created in the right place but it do not follow the price. Also it not delete from the chart ( at this moment the Tester chart)

               ObjectCreate("highline",OBJ_HLINE,0,0,Bid - TrailingStop * pips2dbl); 
               ObjectSet("highline",OBJPROP_COLOR,Yellow); 
               ObjectSet("highline",OBJPROP_WIDTH,1); 
  1. Once the line is created, the Create command will fail. You are not, ever, moving the line. If you want it to move, try my TLine()
  2. Check your return codes (OrderCreate) What are Function return values ? How do I use them ? - MQL4 forum
 
luisneves:


Hi RaptorUK,

Thank you for your attention to my issue and sorry to get your time.

I've put out the ObjectDelete form the if(!OrderClose),but the line still there.... 

. . .  and you have broken the error reporting for the OrderClose()

Do you understand the purpose of  { }  braces ? it seems to me that you don't,  but you really need to, if you want to make progress.

 

Hi RaptorUk,

Thank you for your alert. I'm doing my best to get information how to deal with basic issues regarding the syntax. 

Clarify me about this;

The reason that am try to get an object to do the conventional trailing stop is to avoid minimum movement of the stop loss concerning stop level

Is the above make any sense ?

Thank you in advance

Luis 

 
luisneves:

Hi RaptorUk,

Thank you for your alert. I'm doing my best to get information how to deal with basic issues regarding the syntax. 

Clarify me about this;

The reason that am try to get an object to do the conventional trailing stop is to avoid minimum movement of the stop loss concerning stop level. 

Is the above make any sense ? 

OK,  I think I understand . . .  is your Freezelevel == 0.0 ?

 

This is a problem . . .

if( Bid <= PricePosition)CloseAll();

 PricePosition is not current at this point in the code,  it was read using ObjectGet() before you attempted to move the line.  Also read what WHRoeder  wrote,  before you use ObjectCreate() you should first check if the Object already exists using ObjectFind(),  if it doesn't then create it,  if it does then just move it using ObjectSet()  fix this issue,  fix the issue with the braces and indenting,  test your code,  if it still is not doing what you think it should post the revised code.

Reason: