Trying to create an automatic stop loss.

 

hello im trying an expert advisor witch will put a stop loss at every order

we put in metatrader regardless of its type. The stop loss will be a standrad

amount of pips for every order.

1)I want to be able able to modify the stopp loss after if want to,

without it returning to the previous (standard) imput value.

2)I want it to be put in every order i put even

if i already have another open order

im not a good programmer but ive tried making an expert like this from bits and

pieces but the expert i came up with puts a stop loss on the first order but it

doesnt put it again on all other orders and in addition it does a trailing stop

instead of a standard amount of pips that doesn't change.

the Script is:

 

 

 

extern
double Stoploss = 16; //+------------------------------------------------------------------+ //| expert initialization function                                   | //+------------------------------------------------------------------+ int init()   { //----    //----    return(0);   } //+------------------------------------------------------------------+ //| expert deinitialization function                                 | //+------------------------------------------------------------------+ int deinit()   { //----    //----    return(0);   } //+------------------------------------------------------------------+ //| expert start function                                            | //+------------------------------------------------------------------+ int start()   {   if(OrderStopLoss()>0)    return;       //----      for (int i=1; i<=OrdersTotal(); i++)         if( OrderSelect(i-1,SELECT_BY_POS) && OrderCloseTime()==0 )           {            if(OrderStopLoss()>0)            return;            else if( OrderType()==OP_BUY )            {            OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*Stoploss,OrderTakeProfit(),0);            }            else if( OrderType()==OP_SELL )            {            OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*Stoploss,OrderTakeProfit(),0);           }     }    //----    return(0);   } //+------------------------------------------------------------------

Pls help

Files:
st.mq4  2 kb
 

The very first thing to do is to remove the first 2 lines in the start() function. OrderStoploss() is only relevant once you've selected an order using OrderSelect().


If you are adding the stoploss after the order is opened, then you should use, for example OrderOpenPrice()-Point*Stoploss rather than Bid-Point*Stoploss as Ask/Bid are the current prices. That's why you are getting the trailing stop.


CB

 
cloudbreaker:

The very first thing to do is to remove the first 2 lines in the start() function. OrderStoploss() is only relevant once you've selected an order using OrderSelect().


If you are adding the stoploss after the order is opened, then you should use, for example OrderOpenPrice()-Point*Stoploss rather than Bid-Point*Stoploss as Ask/Bid are the current prices. That's why you are getting the trailing stop.


CB

thanks a lot for the imput


the

if(OrderStopLoss()>0) 
   return;

part of the code was there because i want it to check if there is a stoploss at the order every pip, and if there is one do nothing, but if there isn't put one.


Ive tried the expert advisor with the changes you proposed and it doesnt even put a stopploss order on a trade :(


the changed code is this :

can you imagine what's going on? I'm really a newbie programmer and ive made this code by copying a pasting stuff from other programs and recourses of the net and

i cant really understand whats going wrong :p


If someone has a working code for an automatic stoploss at every order pls share btw..


extern double Stoploss = 16;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   
//----
     for (int i=1; i<=OrdersTotal(); i++)
        if( OrderSelect(i-1,SELECT_BY_POS) && OrderCloseTime()==0 ) 
          {
           if(OrderStopLoss()>0) 
           return;
           else if( OrderType()==OP_BUY ) 
           {
           OrderModify(OrderTicket(),OrderOpenPrice(),-Point*Stoploss,OrderTakeProfit(),0);
           }
           else if( OrderType()==OP_SELL ) 
           {
           OrderModify(OrderTicket(),OrderOpenPrice(),+Point*Stoploss,OrderTakeProfit(),0);
          }
    }

   
//----
   return(0);
  }

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

That's not how I explained to modify the code. Try this (and before all of you notice the incrementing loop, I'm letting it go in order to keep the confusion down, since there are no changes within the loop to the number of orders in the pool!).


CB


extern double Stoploss = 16;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   
//----
     for (int i=1; i<=OrdersTotal(); i++)
        if( OrderSelect(i-1,SELECT_BY_POS) && OrderCloseTime()==0 ) 
          {
           if(OrderStopLoss()>0) 
           return;
           else if( OrderType()==OP_BUY ) 
           {
           OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-Point*Stoploss,OrderTakeProfit(),0);
           }
           else if( OrderType()==OP_SELL ) 
           {
           OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+Point*Stoploss,OrderTakeProfit(),0);
          }
    }

   
//----
   return(0);
  }

//+------------------------------------------------------------------+
 
cloudbreaker:

That's not how I explained to modify the code. Try this (and before all of you notice the incrementing loop, I'm letting it go in order to keep the confusion down, since there are no changes within the loop to the number of orders in the pool!).


CB


thanks a lot for your time and help unfortunately the new expert advisor code you described just doesnt put any stop loss at all at any move. :(

 
alex_000:

thanks a lot for your time and help unfortunately the new expert advisor code you described just doesnt put any stop loss at all at any move. :(

Ok. Start littering your code with Print() statements in order to check:

- the content of variables

- the logic flow


CB

 
cloudbreaker:

Ok. Start littering your code with Print() statements in order to check:

- the content of variables

- the logic flow

- any errors (retrieve with GetLastError() function)


CB

 
alex_000 wrote >>

Pls help

Hi,

Unfortunately I can't help you fix yours but I found a great little very reasonably priced and flexible and effective commercial utility that has a 2 stage Trailing Stop Loss (TSL) that you can use either on its own or in conjunction with other AEs (it overrides the TSL in other AEs). It is from PipBoxer.Com: look for PBTS. Like all TSL, you have to have a profit that is equal to or greater than the amount of your TSL before it kicks in. Being a commercial product it is only available in compiled format of course.

Good luck with it.

…………………(8 >) Prosperous Trading (< 8)

DougRH4x

.

PS: I’m looking for someone to splice an adjustable (Trailing &) Stop Loss into the Martingale based program: PipMaker. Overcoming this one downfall of this program will make it VERY profitable!

 
DougRH4x wrote >>

Hi,

Unfortunately I can't help you fix yours but I found a great little very reasonably priced and flexible and effective commercial utility that has a 2 stage Trailing Stop Loss (TSL) that you can use either on its own or in conjunction with other AEs (it overrides the TSL in other AEs). It is from PipBoxer.Com: look for PBTS. Like all TSL, you have to have a profit that is equal to or greater than the amount of your TSL before it kicks in. Being a commercial product it is only available in compiled format of course.

Good luck with it.

…………………(8 >) Prosperous Trading (< 8)

DougRH4x

.

PS: I’m looking for someone to splice an adjustable (Trailing &) Stop Loss into the Martingale based program: PipMaker. Overcoming this one downfall of this program will make it VERY profitable!