Saving TP and SL from an order, to use in other order.

 

Hello! 

Thanks for taking the time to look at my topic. First of all I want to know how I could save the the SL and TP values of a recently opened order so I can use these values for opening a new trade. 

For example: 

I open a long(1) trade and the TP= 10 and SL=5,

After this I want to open a new trade long(2) but use the TP and SL of long(1). 

Between opening long (1) and (2) there could also be situations where the EA opens other trades long and short but these don't need to copy the values of (1). So it is not like I can use the values of the previous trade. 


I will explain my idea a bit: 

So if the EA opens a trade short at €10 and the stop loss is €11, there is a range of lets say €10.50-€11 where if the close is within this area it opens another trade short but it takes the TP and SL of the original trade. 

But as you probably understand, in the time where it could maybe reach this area more trades are placed etc. That's why it isn't possible to use the values of the previous trade.

I hope this explains what I would like to achieve. 

   //---    StopLoss defining
   double slL                  =     (slLong || slLong1);          // Stoploss long
  
   //---    Opening position long 
   if(BreakoutL == true)
     {
         while(OpenOrders<6)
           {
               //---    Buying at the specified symbol with specified SL and TP
               double volume= 0.25;                                       // specify a trade operation volume
               string symbol= _Symbol;                                       //specify the symbol, for which the operation is performed
               int    digits= (int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);   // number of decimal places
               double point=  SymbolInfoDouble(symbol,SYMBOL_POINT);          // point
               double bid=    SymbolInfoDouble(symbol,SYMBOL_BID);                                            // current price for closing LONG
               double SL=     slL;                                            // unnormalized SL value
               SL=NormalizeDouble(SL,digits);                                 // normalizing Stop Loss
               double TP=     tpLong;                                         // unnormalized TP value
               TP=NormalizeDouble(TP,digits);                                 // normalizing Take Profit
            //--- receive the current open price for LONG positions
               double open_price=SymbolInfoDouble(symbol,SYMBOL_ASK);
               string comment=StringFormat("Buy %s %G lots at %s, SL=%s TP=%s",
                                           symbol,volume,
                                           DoubleToString(open_price,digits),
                                           DoubleToString(SL,digits),
                                           DoubleToString(TP,digits));
               if(!trade.Buy(volume,symbol,open_price,SL,TP,comment))
                 {
                  //--- failure message
                  Print("Buy() method failed. Return code=",trade.ResultRetcode(),
                        ". Code description: ",trade.ResultRetcodeDescription());
                 }
               else
                 {
                  Print("Buy() method executed successfully. Return code=",trade.ResultRetcode(),
                        " (",trade.ResultRetcodeDescription(),")");
                   OpenOrders++;
                 }          
           }
     }
 
   double slL                  =     (slLong || slLong1);          // Stoploss long

A or B is either zero or one. That makes no sense.

 
William Roeder #:

A or B is either zero or one. That makes no sense.


Hey, I indeed forgot to change this to make it so both of them get recognised as stop losses. But both of them have been defined earlier in the script, so they do represent a value. 
 

You could save the SL and TP of the long trade_1 and if the conditions you want to see are met you can submit an order and set SL and TP.

You could write your own class which you store in a list and which contains the conditions, the SL and TP. When you want to submit a new order you can loop through the list and check if the conditions for copying SL and TP are met and then copy them or not. Of course you would need to manage the list (deleting elements that are not needed anymore e.g. because the corresponding position is closed) - so the class would need an additional variable with which you can identify its position.

 
Benjamin Fotteler #:

You could save the SL and TP of the long trade_1 and if the conditions you want to see are met you can submit an order and set SL and TP.

You could write your own class which you store in a list and which contains the conditions, the SL and TP. When you want to submit a new order you can loop through the list and check if the conditions for copying SL and TP are met and then copy them or not. Of course you would need to manage the list (deleting elements that are not needed anymore e.g. because the corresponding position is closed) - so the class would need an additional variable with which you can identify its position.

Hey thanks allot for your response! I have no idea how to do this, but it’s nice to know what I have to start figuring out! Again your answer is much appreciated!