Can someone tell me where am I wrong?

 
bool LongPositionOpen()
{
   MqlTradeRequest mrequest;                             // Will be used for trade requests
   MqlTradeResult mresult;                               // Will be used for results of trade requests
   
   ZeroMemory(mrequest);
   ZeroMemory(mresult);
   
   double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);    // Ask price
   
  
   if(!PositionSelect(_Symbol))
     {
      
      mrequest.action = TRADE_ACTION_DEAL;               // Immediate order execution
      mrequest.price = NormalizeDouble(Ask,_Digits);     // Lastest Ask price
      mrequest.sl = 0;                            // Stop Loss
      mrequest.tp = 0;                         // Take Profit
      mrequest.symbol = _Symbol;                         // Symbol
      mrequest.volume = Lot;                             // Number of lots to trade
      mrequest.magic = 0;                                // Magic Number
      mrequest.type = ORDER_TYPE_BUY;                    // Buy Order
      mrequest.type_filling = ORDER_FILLING_FOK;         // Order execution type
      mrequest.deviation=5;                              // Deviation from current price
      return(OrderSend(mrequest,mresult));               // Send order
     
     if(Buy_Condition == true)
      {
         mrequest.action = TRADE_ACTION_SLTP;
         
         do Sleep(100); while(PositionSelect(_Symbol) == false);
         double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         
         if(StopLoss > 0) mrequest.sl = positionOpenPrice - (StopLoss *_Point);
         if(TakeProfit > 0) mrequest.tp = positionOpenPrice + (TakeProfit *_Point);      
      
         if(mrequest.sl > 0 && mrequest.tp > 0) return(OrderSend(mrequest,mresult));
         
      
         Buy_Condition = false;
         Sell_Condition = true;
      } 
      
    }

      return(false);
}       
//Open a Short Position
bool ShortPositionOpen()
  {
   
   MqlTradeRequest mrequest;                             // Will be used for trade requests
   MqlTradeResult mresult;                               // Will be used for results of trade requests
   
   ZeroMemory(mrequest);
   ZeroMemory(mresult);
   
   double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);    // Bid price
    Trade.Buy(_Symbol, Lot);
   if(!PositionSelect(_Symbol))
     {
      
      mrequest.action = TRADE_ACTION_DEAL;               // Immediate order execution
      mrequest.price = NormalizeDouble(Bid,_Digits);     // Lastest Bid price
      mrequest.sl = 0;                                   // Stop Loss
      mrequest.tp = 0;                                   // Take Profit
      mrequest.symbol = _Symbol;                         // Symbol
      mrequest.volume = Lot;                             // Number of lots to trade
      mrequest.magic = 0;                                // Magic Number
      mrequest.type= ORDER_TYPE_SELL;                    // Sell order
      mrequest.type_filling = ORDER_FILLING_FOK;         // Order execution type
      mrequest.deviation=5;                              // Deviation from current price
      return(OrderSend(mrequest,mresult));                       // Send order
      
      if(Sell_Condition == true)
      {
         mrequest.action = TRADE_ACTION_SLTP;
         
         do Sleep(100); while(PositionSelect(_Symbol) == false);
         double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         
         if(StopLoss > 0) mrequest.sl = positionOpenPrice + (StopLoss *_Point);
         if(TakeProfit > 0) mrequest.tp = positionOpenPrice - (TakeProfit *_Point);      
      
         if(mrequest.sl > 0 && mrequest.tp > 0)  return(OrderSend(mrequest,mresult));
         
      
         Buy_Condition = false;
         
      } 
     
 
 }
  
     return (false);
  
  
  }
input int StopLoss= 50 ;     // Stop Loss level (in points)
input int TakeProfit= 51 ;  

Cans someone tell me why I my StopLoss and TakeProfit doesn't trigger when opening a position?

When I put mrequest.tp = TakeProfit ; 

                 mrequest.sl= StopLoss ; 

I get Invalid Stops, I even tried putting the numbers like i was declaring, but it didn't work.

Hope you can help me.

Regards 

 
Gouveiaa:

Cans someone tell me why I my StopLoss and TakeProfit doesn't trigger when opening a position?

When I put mrequest.tp = TakeProfit ; 

                 mrequest.sl= StopLoss ; 

I get Invalid Stops, I even tried putting the numbers like i was declaring, but it didn't work.

Hope you can help me.

Regards 

   request.sl=0;                                // Stop Loss is not specified
   request.tp=0;                                // Take Profit is not specified   


Please visit codebase to study examples. https://www.mql5.com/en/code

                  SL=XXXXX;
                  if(SL!=-1 && sl+StepTrall*_Point<SL && SL>=OOP+StartTrall*_Point)
                    {
                     request.action    = TRADE_ACTION_SLTP;
                     request.position  = PositionGetInteger(POSITION_TICKET);
                     request.sl        = SL;
                     request.tp        = PositionGetDouble(POSITION_TP);
                     if(!OrderSend(request,result)) Print("error ",GetLastError());
MQL5 Code Base
MQL5 Code Base
  • www.mql5.com
MQL5 Source Code Library for MetaTrader 5
 
Marco vd Heijden:


Please visit codebase to study examples. https://www.mql5.com/en/code

I have that request.sl and request.tp but it doesn' work.

 

SL=XXXXX;
                  if(SL!=-1 && sl+StepTrall*_Point<SL && SL>=OOP+StartTrall*_Point)
                    {
                     request.action    = TRADE_ACTION_SLTP;
                     request.position  = PositionGetInteger(POSITION_TICKET);
                     request.sl        = SL;
                     request.tp        = PositionGetDouble(POSITION_TP);
                     if(!OrderSend(request,result)) Print("error ",GetLastError());


I didn't understood this

 I didn't understood this

 
Gouveiaa:

I have that request.sl and request.tp but it doesn' work.

 

 I didn't understood this

bool LongPositionOpen()
{
   MqlTradeRequest mrequest;                             // Will be used for trade requests
   MqlTradeResult mresult;                               // Will be used for results of trade requests
   
   ZeroMemory(mrequest);
   ZeroMemory(mresult);
   
   double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);    // Ask price
   
  
   if(!PositionSelect(_Symbol))
     {
      
      mrequest.action = TRADE_ACTION_DEAL;               // Immediate order execution
      mrequest.price = NormalizeDouble(Ask,_Digits);     // Lastest Ask price
      mrequest.sl = 0;                            // Stop Loss
      mrequest.tp = 0;                         // Take Profit
      mrequest.symbol = _Symbol;                         // Symbol
      mrequest.volume = Lot;                             // Number of lots to trade
      mrequest.magic = 0;                                // Magic Number
      mrequest.type = ORDER_TYPE_BUY;                    // Buy Order
      mrequest.type_filling = ORDER_FILLING_FOK;         // Order execution type
      mrequest.deviation=5;                              // Deviation from current price
      return(OrderSend(mrequest,mresult));               // Send order
     
     if(Buy_Condition == true)
      {
         mrequest.action = TRADE_ACTION_SLTP;
         
         do Sleep(100); while(PositionSelect(_Symbol) == false);
         double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         
         if(StopLoss > 0) mrequest.sl = positionOpenPrice - (StopLoss *_Point);
         if(TakeProfit > 0) mrequest.tp = positionOpenPrice + (TakeProfit *_Point);      
      
         if(mrequest.sl > 0 && mrequest.tp > 0) return(OrderSend(mrequest,mresult));
         
      
         Buy_Condition = false;
         Sell_Condition = true;
      } 
      
    }

      return(false);
}       
//Open a Short Position
bool ShortPositionOpen()
  {
   
   MqlTradeRequest mrequest;                             // Will be used for trade requests
   MqlTradeResult mresult;                               // Will be used for results of trade requests
   
   ZeroMemory(mrequest);
   ZeroMemory(mresult);
   
   double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);    // Bid price
    Trade.Buy(_Symbol, Lot);
   if(!PositionSelect(_Symbol))
     {
      
      mrequest.action = TRADE_ACTION_DEAL;               // Immediate order execution
      mrequest.price = NormalizeDouble(Bid,_Digits);     // Lastest Bid price
      mrequest.sl = 0;                                   // Stop Loss
      mrequest.tp = 0;                                   // Take Profit
      mrequest.symbol = _Symbol;                         // Symbol
      mrequest.volume = Lot;                             // Number of lots to trade
      mrequest.magic = 0;                                // Magic Number
      mrequest.type= ORDER_TYPE_SELL;                    // Sell order
      mrequest.type_filling = ORDER_FILLING_FOK;         // Order execution type
      mrequest.deviation=5;                              // Deviation from current price
      return(OrderSend(mrequest,mresult));                       // Send order
      
      if(Sell_Condition == true)
      {
         mrequest.action = TRADE_ACTION_SLTP;
         
         do Sleep(100); while(PositionSelect(_Symbol) == false);
         double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         
         if(StopLoss > 0) mrequest.sl = positionOpenPrice + (StopLoss *_Point);
         if(TakeProfit > 0) mrequest.tp = positionOpenPrice - (TakeProfit *_Point);      
      
         if(mrequest.sl > 0 && mrequest.tp > 0)  return(OrderSend(mrequest,mresult));
         
      
         Buy_Condition = false;
         
      } 
     
 
 }
  
     return (false);
  
  
  }
 
#define EXPERT_MAGIC 123456  // MagicNumber of the expert
//+------------------------------------------------------------------+
//| Modification of Stop Loss and Take Profit of position            |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request;
   MqlTradeResult  result;
   int total=PositionsTotal(); // number of open positions   
//--- iterate over all open positions
   for(int i=0; i<total; i++)
     {
      //--- parameters of the order
      ulong  position_ticket=PositionGetTicket(i);// ticket of the position
      string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol 
      int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // number of decimal places
      ulong  magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber of the position
      double volume=PositionGetDouble(POSITION_VOLUME);    // volume of the position
      double sl=PositionGetDouble(POSITION_SL);  // Stop Loss of the position
      double tp=PositionGetDouble(POSITION_TP);  // Take Profit of the position
      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);  // type of the position
      //--- output information about the position
      PrintFormat("#%I64u %s  %s  %.2f  %s  sl: %s  tp: %s  [%I64d]",
                  position_ticket,
                  position_symbol,
                  EnumToString(type),
                  volume,
                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),
                  DoubleToString(sl,digits),
                  DoubleToString(tp,digits),
                  magic);
      //--- if the MagicNumber matches, Stop Loss and Take Profit are not defined
      if(magic==EXPERT_MAGIC && sl==0 && tp==0)
        {
         //--- calculate the current price levels
         double price=PositionGetDouble(POSITION_PRICE_OPEN);
         double bid=SymbolInfoDouble(position_symbol,SYMBOL_BID);
         double ask=SymbolInfoDouble(position_symbol,SYMBOL_ASK);
         int    stop_level=(int)SymbolInfoInteger(position_symbol,SYMBOL_TRADE_STOPS_LEVEL);
         double price_level;
         //--- if the minimum allowed offset distance in points from the current close price is not set
         if(stop_level<=0)
            stop_level=150; // set the offset distance of 150 points from the current close price
         else
            stop_level+=50; // set the offset distance to (SYMBOL_TRADE_STOPS_LEVEL + 50) points for reliability
 
         //--- calculation and rounding of the Stop Loss and Take Profit values
         price_level=stop_level*SymbolInfoDouble(position_symbol,SYMBOL_POINT);
         if(type==POSITION_TYPE_BUY)
           {
            sl=NormalizeDouble(bid-price_level,digits);
            tp=NormalizeDouble(ask+price_level,digits);
           }
         else
           {
            sl=NormalizeDouble(ask+price_level,digits);
            tp=NormalizeDouble(bid-price_level,digits);
           }
         //--- zeroing the request and result values
         ZeroMemory(request);
         ZeroMemory(result);
         //--- setting the operation parameters
         request.action  =TRADE_ACTION_SLTP; // type of trade operation
         request.position=position_ticket;   // ticket of the position
         request.symbol=position_symbol;     // symbol 
         request.sl      =sl;                // Stop Loss of the position
         request.tp      =tp;                // Take Profit of the position
         request.magic=EXPERT_MAGIC;         // MagicNumber of the position
         //--- output information about the modification
         PrintFormat("Modify #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
         //--- send the request
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
         //--- information about the operation   
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
        }
     }
  }
//+------------------------------------------------------------------+