Download MetaTrader 5
To add comments, please log in or register
Millions of traders download codes from MQL5 library. Share your app with them!
musetti
51
musetti 2014.02.27 18:49 

hello,

I would like to create a script wich CLOSE in 3 step one manual open position. EXAMPLE : If I have open a long position of 1 lot, I would like that my script close 0.33 lot 30 pips more higher than my open price (take profit), 0.33 lot 60 pips higher than open price order and finally 0.33 lot 90 pips higher than open price order. In fact, i want to close every 30 pips 1/3 of my contrat. I have begin to learn code there is few days and could you correct my bad script of learner :) thanks

//process for a LONG manual position

extern double ticket;

int start()
{
double lot = 1;


if((OrderSelect(ticket,SELECT_BY_TICKET)==true) && (OrderType() ==OP_BUY)) {

OrderClose(ticket,lot/3,OrderOpenPrice()+30*Point,5,Red);

OrderClose(ticket,lot/3,OrderOpenPrice()+60*Point,5,Red);

OrderClose(ticket,lot/3,OrderOpenPrice()+90*Point,5,Red);
}


return(0);
}
Alain Verleyen
Moderator
28564
Alain Verleyen 2014.02.27 19:36  
abricot91:

hello,

I would like to create a script wich CLOSE in 3 step one manual open position. EXAMPLE : If I have open a long position of 1 lot, I would like that my script close 0.33 lot 30 pips more higher than my open price (take profit), 0.33 lot 60 pips higher than open price order and finally 0.33 lot 90 pips higher than open price order. In fact, i want to close every 30 pips 1/3 of my contrat. I have begin to learn code there is few days and could you correct my bad script of learner :) thanks

...



Hello,

Welcome to mql4.com forum.

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.

qjol
3243
qjol 2014.02.27 20:16  
double lot = 1.0;
lot/3.0

u also need to adjust the code according to MarketInfo() MODE_MINLOT & MODE_LOTSTEP

or 4 the new code SymbolInfoDouble() ENUM_SYMBOL_INFO_DOUBLE SYMBOL_VOLUME_MIN SYMBOL_VOLUME_STEP

B.T.W. the structure of ur code is wrong

whroeder1
13613
whroeder1 2014.02.27 22:06  
OrderClose(ticket,lot/3,OrderOpenPrice()+30*Point,5,Red);
OrderClose(ticket,lot/3,OrderOpenPrice()+60*Point,5,Red);
OrderClose(ticket,lot/3,OrderOpenPrice()+90*Point,5,Red);
That of course won't work.
  1. You have to wait for OrderClosePrice() to reach your target OrderOpenPrice()+30*Point and then call close with market price.
  2. Adjust for 4/5 brokers, adjust lot/3 to lotstep.
  3. Remember that you've closed 1/3 at the first TP, so on the next target you close 1/2 of the remaining 2/3. (Including recovery from restarts)
  4. Easier, is write a script that opens 3 positions with the take profits set.
       int nSplit = 3;
       while(lot >= market_minLot){
          double   size     = NormalizeLots(lot / nSplit);
          double   TP       = Bid + 30*pips2dbl * nSplit;
          int ticket = OrderSend( market_pair,   op_code,          size,
                                  oop,           Slippage_Pips*pips2points,
                                  SL,            TP,               orderComment,
                                  MN,            NO_EXPIRATION,    orderColor );
          if(ticket < 0){ ...
          lots -= size;
       }
    ///////////////////////////////////
    double   NormalizeLots(double lots){
       lots  = MathFloor(lots / market_lotStep + 0.001) * market_lotStep;
       if(lots < market_minLot) lots = 0;
       return(lots);
    }
    

Keith Watford
Moderator
7907
Keith Watford 2014.02.27 22:35  

First of all, you will need to check whether profit target has been met. You can use something similar to this

extern double ticket;
  if(ticket>0 && OrderSelect(ticket,SELECT_BY_TICKET) && OrderType ==OP_BUY)
     {
     order_pips_profit=(OrderClosePrice() - OrderOpenPrice() )/PipDecimal; // PipDecimal is an extern input eg.0.0001
     if(order_pips_profit>=90) 
         {
         //code to close whole position
         )
     if(order_pips_profit>=60) 
         {
         //code to close part position
         )
     if(order_pips_profit>=30) 
         {
         //code to close part position
         )

You will also need to take into account that when you close part of a position, the remaining part-position will be assigned a new ticket number.

So ticket will need to hold new ticket number

Note: I believe that

extern double ticket;

can be re-assigned with a new value, but

input double ticket;

cannot.

whroeder1
13613
whroeder1 2014.02.27 23:00  
GumRai: First of all, you will need to check whether profit target has been met. You can use something similar to this
The big item is remembering. Did you do the previous close, what is the new ticket number, what is the original lot size. If it isn't programmed to handle a restart, the code isn't useful.
Daniel Petrovai
440
Daniel Petrovai 2014.02.28 23:21  
abricot91:

hello,

I would like to create a script wich CLOSE in 3 step one manual open position. EXAMPLE : If I have open a long position of 1 lot, I would like that my script close 0.33 lot 30 pips more higher than my open price (take profit), 0.33 lot 60 pips higher than open price order and finally 0.33 lot 90 pips higher than open price order. In fact, i want to close every 30 pips 1/3 of my contrat. I have begin to learn code there is few days and could you correct my bad script of learner :) thanks



I seriously doubt the fact that you will be able to do it with a script. What you need is an EA to monitor your trade and exit in smaller lots if the target is reached.

Alternatively you can write a script to place 3 smaller orders and assign stop loss to each one of them and different profit targets.

An EA is a more elegant solution but if your pc is off or disconnected, there will be no trade management for you so you will need an emergency stop in place.

Keith Watford
Moderator
7907
Keith Watford 2014.03.01 04:33  
WHRoeder:
The big item is remembering. Did you do the previous close, what is the ticket number, what is the original lot size. If it isn't programmed to handle a restart, the code isn't useful.


My post was intended to help the OP make a start

what is the ticket number

Did you not see

extern double ticket;

and I pointed out that

"You will also need to take into account that when you close part of a position, the remaining part-position will be assigned a new ticket number."

Your code

   int nSplit = 3;
   while(lot >= market_minLot){
      double   size     = NormalizeLots(lot / nSplit);
      double   TP       = Bid + 30*pips2dbl * nSplit;
      int ticket = OrderSend( market_pair,   op_code,          size,
                              oop,           Slippage_Pips*pips2points,
                              SL,            TP,               orderComment,
                              MN,            NO_EXPIRATION,    orderColor );
      if(ticket < 0){ ...
      lots -= size;
   }
///////////////////////////////////
double   NormalizeLots(double lots){
   lots  = MathFloor(lots / market_lotStep + 0.001) * market_lotStep;
   if(lots < market_minLot) lots = 0;
   return(lots);

although the idea behind it is sound, it doesn't work.

Where do market_minlot and market_lotStep get their values from?

James Hodges
2782
James Hodges 2014.03.01 07:27  
WHRoeder:
That of course won't work.
  1. Easier, is write a script that opens 3 positions with the take profits set.

Listen to the man.
If we are really still talking about a script we are talking about a human being powered tool. Not software that is monitoring market conditions and keeping track of what the ticket number is of the open trade... yeah yeah I know ... as soon as you load the script IT can get the ticket number.. but .. it's a script. A human powered tool. If you've got to to be there to run it when it gets to one of your target prices, you may as well just reach up there and close 1/3 of your trade. It's not something that sits and waits on the market to move. That would be an expert advisor.

Even if you decide to take Mr. Roeder's advice and make a triple entry script like he describes.. It will not be invoked until the human pulls the trigger on it... all it does is save some mouse clicking. Which is a valuable thing when you need to close 14 orders in rapid succession or something like that.....

Now if you are talking about an Expert Advisor that waits for a trigger and then places a trade that it slowly closes one third at a time that's a different story.. but even then it's much simpler to do like he says and just have it throw 3 trades at 0.33, set a stoploss, and maybe employ a trail and space out the takeprofits. on each.. I have had brokerages that would not let me do a partial close.

Sometimes we know so many ways to do the same thing we over-complicate things and get all upset when someone wants to do it another way. It's good to just sit back and watch the idea's bounce around. I know if I'm not careful I may learn something..... Everybody enjoy the weekend! PipPip.....Jimdandy

/
To add comments, please log in or register