Download MetaTrader 5

Wich kind of Type for this function ?

To add comments, please log in or register
Reza nasimi
192
Reza nasimi  

Hi 

Some body help me please  to understand if I have a function to directly pass a double parameter to another void function to change StopLoss , which kind of type my first function should have ?

Should it be a double function with a return ? -which in this case I don't now what I'm going to do with this value -

Or void? - which seems to be OK   

 

I'm talking about  SL_Send() 

 

void SL_Send ()

{

      if(OrdersTotal()!= 0)

    {

      for(int i=0;i<OrdersTotal();i++)

        {

        if(! OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

            {

                if(OrderType()==OP_BUY)

                    {

                         if(OrderStopLoss()< Bid - TrailingStop*Point || OrderStopLoss()==0  )

                           {          

                            ModifyStopLoss(Bid - TrailingStop*Point);  

                           } 

                    }

                 if(OrderType()==OP_SELL)

                    {

                         if(OrderStopLoss()> Ask + TrailingStop*Point || OrderStopLoss()==0  )

                           {

                            ModifyStopLoss(Ask + TrailingStop*Point); 

                           }

                    } 

            } 

       }

    }

}

  

//+------------------------------------------------------------------+

  void ModifyStopLoss(double ldStopLoss) 

  {

   bool fm;

   fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);

   

  }

Marco vd Heijden
Moderator
5550
Marco vd Heijden  
Depending on what it returns you can check the return value of OrderModify inside the function then it can be the void type like you have now but if you want to return the outcome of the OrderModify it will also have to be the boolean type, since OrderModify returns either true or false.
Reza nasimi
192
Reza nasimi  

sorry my English is not good enough,

Do you mean my functionModifyStopLoss() "  must be boolean ?

would you explain more please ?

Marco vd Heijden
Moderator
5550
Marco vd Heijden  
dr.Rez:

sorry my English is not good enough,

Do you mean my function " ModifyStopLoss() "  must be boolean ?

would you explain more please ?


bool  OrderModify( 
   int        ticket,      // ticket 
   double     price,       // price 
   double     stoploss,    // stop loss 
   double     takeprofit,  // take profit 
   datetime   expiration,  // expiration 
   color      arrow_color  // color 

 

Returned value:

If the function succeeds, it returns true, otherwise false.

So, it depends on what you want.

So in your case the return value is stored in

bool fm

In the ModifyStopLoss()

if you want this value returned then your function must be bool, otherwise void.

So, it depends on what you want.

if you want to return the value.

  bool ModifyStopLoss(double ldStopLoss) 

  {
   bool fm;
   fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
   
   return(fm);
  }
Reza nasimi
192
Reza nasimi  

Thank you Mrco

now I changed  ModifyStopLoss() to bool and write return (fm); 

and in OnTicket () I add  this: if(OrdersTotal()>0){SL_Send();} 

I test it on a chart  but it's not working.

I have Sell positions open.

 

input double TrailingStop = 50 ;


int OnInit() {return(INIT_SUCCEEDED);}

void OnDeinit(const int reason){}

 

 

void OnTick()

  {

  if(OrdersTotal()>0){SL_Send();}

  }

 

 

 

  void SL_Send ()

{

      if(OrdersTotal()!= 0)

    {

      for(int i=0;i<OrdersTotal();i++)

        {

        if(! OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

            {

                if(OrderType()==OP_BUY)

                    {

                         if(OrderStopLoss()< Bid - TrailingStop*Point || OrderStopLoss()==0  )

                           {          

                            ModifyStopLoss(Bid - TrailingStop*Point);  

                           } 

                       

                    }

              

               

                if(OrderType()==OP_SELL)

                    { 

                   if(OrderStopLoss()> Ask + TrailingStop*Point || OrderStopLoss()==0  )

                           {

                            ModifyStopLoss(Ask + TrailingStop*Point); 

                           }

                    } 

            } 

        }

    } 


}

  

//+------------------------------------------------------------------+

  bool ModifyStopLoss(double ldStopLoss) 

  {

   bool fm;

   fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);

   return (fm);

  }


Marco vd Heijden
Moderator
5550
Marco vd Heijden  

What it returns has nothing to do with the other operation.

What value did you give to ldStopLoss?

Is there any errors in the log ?

Reza nasimi
192
Reza nasimi  

there is not any error

i send this value 

ModifyStopLoss(Ask + TrailingStop*Point);

am I wrong ? 

Ernst Van Der Merwe
5080
Ernst Van Der Merwe  
 void SL_Send ()

{

      for(int i=0;i<OrdersTotal();i++)

        {

        if( OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

            {

                if(OrderType()==OP_BUY)

                    {

                         if(OrderStopLoss()< Bid - TrailingStop*Point || OrderStopLoss()==0  )

                           {          

                            ModifyStopLoss(Bid - TrailingStop*Point);  

                           } 

                       

                    }

              

               

                if(OrderType()==OP_SELL)

                    { 

                   if(OrderStopLoss()> Ask + TrailingStop*Point || OrderStopLoss()==0  )

                           {

                            ModifyStopLoss(Ask + TrailingStop*Point); 

                           }

                    } 

            } 

        }


}
Reza nasimi
192
Reza nasimi  

Thank you pipPod 

To add comments, please log in or register