Download MetaTrader 5

Wich kind of Type for this function ?

To add comments, please log in or register
Have you developed a trading robot? Publish it in the Market and make profit!
dr.Rez
43
dr.Rez 2016.07.07 10:26 

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
4685
Marco vd Heijden 2016.07.07 10:40  
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.
dr.Rez
43
dr.Rez 2016.07.07 10:54  

sorry my English is not good enough,

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

would you explain more please ?

Marco vd Heijden
Moderator
4685
Marco vd Heijden 2016.07.07 10:57  
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);
  }
dr.Rez
43
dr.Rez 2016.07.07 11:17  

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
4685
Marco vd Heijden 2016.07.07 11:44  

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 ?

dr.Rez
43
dr.Rez 2016.07.07 12:05  

there is not any error

i send this value 

ModifyStopLoss(Ask + TrailingStop*Point);

am I wrong ? 

Ernst Van Der Merwe
4727
Ernst Van Der Merwe 2016.07.07 15:45  
 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); 

                           }

                    } 

            } 

        }


}
dr.Rez
43
dr.Rez 2016.07.07 23:29  

Thank you pipPod 

To add comments, please log in or register