Need help understand stop loss and take profit - page 2

 
Ralph Freshour:

Regarding the Buy Stop and Stop Limit:

In the struct MqlTradeRequest I don't see a .property for Buy Stop?

Also, if I am buying a stock using the Buy Stop and Stop Limit, then (I'm assuming) I don't need to fill in the Stop Loss and Take Profit properties? I can set them to 0.00?

Thank you...

You can leave them at zero , but you can predefine them if you will and set them but remember they will be for the price the order will be opened at.

Try this code example - place in experts .-you can also add the magic number which i forgot .

There is also the stoplimit order which may interest you 

enum pending_direction
{
pd_none=0,//none
pd_buy=1,//buy
pd_sell=2//sell
};
enum pending_type
{
pt_none=0,//none
pt_stop=1,//stop
pt_limit=2,//limit
pt_stop_limit=3//stop-limit
};
input pending_direction test_direction=pd_buy;//direction : 
input pending_type test_type=pt_limit;//type : 
input double test_volume=2;//volume :
input double test_price=0;//price : 
input double test_stoplimitprice=0;//stoplimitprice (for stoplimit orders): 
input double test_sl=0;//stop loss :
input double test_tp=0;//take profit : 
input bool test_expires=false;//does it have expiration time ? 
input datetime test_expiration=0;//expiration :
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  
  MqlTradeResult send=SendPendingOrder(_Symbol,test_direction,test_type,test_price,test_volume,test_sl,test_tp,test_expires,test_expiration,test_stoplimitprice);
  Print("::> "+send.comment);
  ExpertRemove();
//---
   return(INIT_SUCCEEDED);
  }
  

MqlTradeResult SendPendingOrder(string symbol,
                      pending_direction direction,
                      pending_type type,
                      double price,
                      double volume,
                      double sl,
                      double tp,
                      bool expires,
                      datetime expiration,
                      double stoplimitprice)
                      {
                      MqlTradeResult result={0};
                      MqlTradeRequest req={0};
                      req.symbol=symbol;
                      req.action=TRADE_ACTION_PENDING;
                      //buy
                      if(direction==pd_buy)
                        {
                        if(type==pt_stop){req.type=ORDER_TYPE_BUY_STOP;}
                        if(type==pt_limit){req.type=ORDER_TYPE_BUY_LIMIT;}
                        if(type==pt_stop_limit){req.type=ORDER_TYPE_BUY_STOP_LIMIT;req.stoplimit=stoplimitprice;}
                        }
                      //sell
                      if(direction==pd_sell)
                        {
                        if(type==pt_stop){req.type=ORDER_TYPE_SELL_STOP;}
                        if(type==pt_limit){req.type=ORDER_TYPE_SELL_LIMIT;}
                        if(type==pt_stop_limit){req.type=ORDER_TYPE_SELL_STOP_LIMIT;req.stoplimit=stoplimitprice;}
                        }              
                      req.price=price;
                      req.volume=volume;
                      if(sl!=0) req.sl=sl;
                      if(tp!=0) req.tp=tp;
                      req.type_filling=ORDER_FILLING_RETURN;
                      req.type_time=ORDER_TIME_GTC;
                      if(expires){req.type_time=ORDER_TIME_SPECIFIED;req.expiration=expiration;}
                      bool send=OrderSend(req,result);
                      return(result);
                      }