MQL5- Loop to check if position is profiting or losing?

 

MQL5- Loop to check if position is profiting or losing?

How to look for profiting and looking positions?

Returning the ticket/deal numbers

 
What have you got so far?
 
nothing at the moment
 
jon: nothing at the moment
learn to code it, or pay (Freelance) someone to code it.
We're not going to code it for you.
We are willing to help you when you post your attempt (using SRC) and the nature of your problem.
 

Forum on trading, automated trading systems and testing trading strategies


Welcome,

  • Usually people who can't code don't receive free help on this forum, though it could happen if you are lucky, be patient.
  • If you show your attempts and describe well your problem, you will most probably receive an answer from the community.
  • If you don't want to learn to code, nothing bad, you can either look at the Codebase if something free already exists, or in the Market for paid products (sometimes free also).
  • Finally, you also have the option to hire a programmer in the Freelance section.

Good luck.


 

I got "Invalid Stop" errors for the PositionModify

 

void Trail_Winning_Position()
{

int    digits1=(int)SymbolInfoInteger(Symbol_1,SYMBOL_DIGITS); // number of decimal places
double point1=SymbolInfoDouble(Symbol_1,SYMBOL_POINT);         // point
double bid1=SymbolInfoDouble(Symbol_1,SYMBOL_BID);               // current price for closing
double SL1=bid1-Trailing_Stop_Loss*point1;                            // unnormalized SL value
SL1=NormalizeDouble(SL1,digits1);                              // normalizing Stop Loss
double TP1=bid1+Take_Profit*point1;                                   // unnormalized TP value
TP1=NormalizeDouble(TP1,digits1);                              // normalizing Take Profit

int    digits2=(int)SymbolInfoInteger(Symbol_2,SYMBOL_DIGITS); // number of decimal places
double point2=SymbolInfoDouble(Symbol_2,SYMBOL_POINT);         // point
double ask2=SymbolInfoDouble(Symbol_2,SYMBOL_ASK);               // current price for closing
double SL2=ask2+Trailing_Stop_Loss*point2;                            // unnormalized SL value
SL2=NormalizeDouble(SL2,digits2);                              // normalizing Stop Loss
double TP2=ask2-Take_Profit*point2;                                   // unnormalized TP value
TP2=NormalizeDouble(TP2,digits2);                              // normalizing Take Profit

int    digits3=(int)SymbolInfoInteger(Symbol_1,SYMBOL_DIGITS); // number of decimal places
double point3=SymbolInfoDouble(Symbol_1,SYMBOL_POINT);         // point
double ask3=SymbolInfoDouble(Symbol_1,SYMBOL_ASK);               // current price for closing
double SL3=ask3+1000*point3;                            // unnormalized SL value
SL3=NormalizeDouble(SL3,digits3);                              // normalizing Stop Loss
double TP3=ask3-1000*point3;                                   // unnormalized TP value
TP3=NormalizeDouble(TP3,digits3);                              // normalizing Take Profit

int    digits4=(int)SymbolInfoInteger(Symbol_2,SYMBOL_DIGITS); // number of decimal places
double point4=SymbolInfoDouble(Symbol_2,SYMBOL_POINT);         // point
double bid4=SymbolInfoDouble(Symbol_2,SYMBOL_BID);               // current price for closing
double SL4=bid4-1000*point4;                            // unnormalized SL value
SL4=NormalizeDouble(SL4,digits4);                              // normalizing Stop Loss
double TP4=bid4+1000*point4;                                   // unnormalized TP value
TP4=NormalizeDouble(TP4,digits4);                              // normalizing Take Profit

      if ( PositionSelect(Symbol_1) )
      {
         if ( PositionGetDouble(POSITION_PROFIT)>0 && (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) )
         {
            if  ( trade.PositionModify(Symbol_1, SL1, TP1) )
            Print("Trailing Started for ", Symbol_1);
         }
      
      }
      
      if ( PositionSelect(Symbol_1) )
      {
         if ( PositionGetDouble(POSITION_PROFIT)>0 && (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) )
         {
            if  ( trade.PositionModify(Symbol_1, SL3, TP3) )
            Print("Trailing Started for ", Symbol_1);
         }
      
      }
      
      if ( PositionSelect(Symbol_2) )
      {
         if ( PositionGetDouble(POSITION_PROFIT)>0 && (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) )
         {
            if  ( trade.PositionModify(Symbol_2, SL2, TP2) )
            Print("Trailing Started for ", Symbol_2);
         }
      
      }
      
      if ( PositionSelect(Symbol_2) )
      {
         if ( PositionGetDouble(POSITION_PROFIT)>0 && (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) )
         {
            if  ( trade.PositionModify(Symbol_2, SL4, TP4) )
            Print("Trailing Started for ", Symbol_2);
         }
      
      }
}      

void Close_Losing_Position()
{
      if ( PositionSelect(Symbol_1) )
      {
         if ( PositionGetDouble(POSITION_PROFIT)<0 )
         {
               if( !trade.PositionClose(Symbol_1,9999) )
               {
            //--- failure message
            Print(Symbol_1, "PositionClose() method failed. Return code=",trade.ResultRetcode(),
                  ". Code description: ",trade.ResultRetcodeDescription());
               }
                
               else
               {
            Print(Symbol_1, "PositionClose() method executed successfully. Return code=",trade.ResultRetcode(),
                  " (",trade.ResultRetcodeDescription(),")");
               }
         }
      }
      
      if ( PositionSelect(Symbol_2) )
      {
         if ( PositionGetDouble(POSITION_PROFIT)<0 )
         {
                  if( !trade.PositionClose(Symbol_2,9999) )
                  {
               //--- failure message
               Print(Symbol_2, "PositionClose() method failed. Return code=",trade.ResultRetcode(),
                     ". Code description: ",trade.ResultRetcodeDescription());
                  }
                    
                  else
                  {
               Print(Symbol_2, "PositionClose() method executed successfully. Return code=",trade.ResultRetcode(),
                     " (",trade.ResultRetcodeDescription(),")");
                  }
         }
      }
}


 

 
You have to check your SL/TP against the StopLevels.
The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • 2016.08.01
  • MetaQuotes Software Corp.
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
Reason: