Get a line object to substitute the trailing stop - page 3

 
int start()
  {//0
//---- Number ONE to DO  ==>  CHECK OPEN TRADES
   if(TotalOpenEA > 0)
     {//1
      TotalOpenEA = 0;
      TotalBuy = 0;
      TotalSell =0;
      TotalBuyStop =0;
      TotalSellStop=0;
      TotalBuyLimit =0;
      TotalSellLimit =0;
      double BuyLots,SellLots,HighestLot;
      for(int i = OrdersTotal()-1; i >= 0 ; i--)
        {//2
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
         if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue;
         TotalOpenEA++;
         if(HighestLot < OrderLots())HighestLot=OrderLots();    <== This is enough to gets highest lot
         //---- check order type
         if(OrderType()==OP_BUY)
           {//3
            TotalBuy++;
            if(OrderLots()> BuyLots)BuyLots=OrderLots();
           }//3 
         if(OrderType()==OP_SELL)         
           {//4
            TotalSell++;
            if(OrderLots()> SellLots)SellLots=OrderLots();      <== OK
           }//4
         if(OrderType()==OP_BUYSTOP)
           {//5
            TotalBuyStop++;
            //if(OrderLots()> BuyLots)BuyLots=OrderLots();      <== WRONG
            }//5
          if(OrderType()==OP_SELLSTOP)
            {//6
             TotalSellStop++;
             //if(OrderLots()> SellLots)BuyLots=OrderLots();    <== WRONG        
            }//6
          if(OrderType()==OP_BUYLIMIT)
            {//7
             TotalBuyLimit++;
             //if(OrderLots()> BuyLots)BuyLots=OrderLots();     <== WRONG         
            }//7
          if(OrderType()==OP_SELLLIMIT)
            {//8
             TotalSellLimit++;
             //if(OrderLots()> SellLots)BuyLots=OrderLots();    <== WRONG           
            }//8
        }//2
     }// 1                      
//finish this loop for all OrderTypes      
          
        //end loop

Don't paste your garbage to the code we have to do it step by step

We have done a loop in the open trades and there are several cases possible

we start this ea for the first time what will we see and what has to happen in that case ??

 
deVries:

Don't paste your garbage to the code we have to do it step by step

We have done a loop in the open trades and there are several cases possible

we start this ea for the first time what will we see and what has to happen in that case ??






Hi deVries;

Sorry if I have done something wrong and that was not my intention.  

When I ask what code you required I was in doubt if was your code updated per your request or my existing code where your code should be inside.

I know that I need to learn a lot of mql and without the help of people like you (RaptorUk, WHRoeder, Kronin,just to mention those that have shown  the up most patience and interest to support people like me) for sure that  I could become a copy guy, just taking others code.

So, moving on and regarding your third line of text ;

As far I understood we need to check if are any orders open and if yes which one are from our ea. First we need to set to zero the variables just to avoid the start from last values. Every time the loop executes we store the sum of  in TotalOpenEA variable.From there we going forward identifying what type of orders are open (if any). Every time the loop executes we store each order sorted by order type in the proper variables (TotalBuy, TotalSell and so on).

Then we need to confirm what the value of the last lot just to limit the lot size to the proper lot size for the first order.

Luis 

 

Assume  "TotalOpenEA"  is after the loop finished  zero  ( == 0 )

Then your intention is to open a trade some distance from price that moment

price can go up xx  you buy

or price goes down xx you sell

I like to see your attempt to make that code it opens one buystop one sellstop 

 
deVries:

Assume  "TotalOpenEA"  is after the loop finished  zero  ( == 0 )

Then your intention is to open a trade some distance from price that moment

price can go up xx  you buy

or price goes down xx you sell

I like to see your attempt to make that code it opens one buystop one sellstop 


Hi deVries;

Back to "my lesson" here is what I've done:

extern string UserSettings        = "=== User Settings ===";
extern double FixedLotSize        = 0.01;
extern double OpenDistance        =   2.0;
extern int Slippage               =   0;
extern double StopLoss            =  50.0;
extern double TakeProfit          =  50.0;
extern int    MagicNumber         =  101010;



int    TotalOpenEA,TotalBuy,TotalSell,TotalBuyStop,Ticket,
       TotalSellStop,TotalBuyLimit,TotalSellLimit;

//++++ These are adjusted for 5 digit brokers.
int     pips2points;      // slippage  3 pips    3=points    30=points
double  pips2dbl;         // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;      // DoubleToStr(dbl/pips2dbl, Digits.pips)
//---

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if(Digits % 2 == 1)  // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
     {pips2dbl = Point*10; pips2points = 10;   Digits.pips = 1;}
     else {pips2dbl = Point;    pips2points =  1;   Digits.pips = 0;}
     // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl       
      
    if(OrdersTotal() >0 )TotalOpenEA=OrdersTotal(); 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {//0
//---- Number ONE to DO  ==>  CHECK OPEN TRADES
   if(TotalOpenEA > 0)
     {//1
      TotalOpenEA = 0;
      TotalBuy = 0;
      TotalSell =0;
      TotalBuyStop =0;
      TotalSellStop=0;
      TotalBuyLimit =0;
      TotalSellLimit =0;
      double BuyLots,SellLots,HighestLot;
      for(int i = OrdersTotal()-1; i >= 0 ; i--)
        {//2
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
         if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue;
         TotalOpenEA++;
         if(HighestLot < OrderLots())HighestLot=OrderLots();
         //---- check order type
         if(OrderType()==OP_BUY)
           {//3
            TotalBuy++;
            if(OrderLots()> BuyLots)BuyLots=OrderLots();
           }//3 
         if(OrderType()==OP_SELL)         
           {//4
            TotalSell++;
            if(OrderLots()> SellLots)SellLots=OrderLots();
           }//4
         if(OrderType()==OP_BUYSTOP)
           {//5
            TotalBuyStop++;
           }//5
          if(OrderType()==OP_SELLSTOP)
            {//6
             TotalSellStop++;            
            }//6
          if(OrderType()==OP_BUYLIMIT)
            {//7
             TotalBuyLimit++;             
            }//7
          if(OrderType()==OP_SELLLIMIT)
            {//8
             TotalSellLimit++;            
            }//8
        }//2
     }// 1                      
//finish this loop for all OrderTypes      
          
        //end loop
        
                
//+----------------------------------------------------------------------------+                

   double BuyPrice = Ask + OpenDistance * pips2dbl;
                
                       
         Ticket = OrderSend(Symbol(),OP_BUYSTOP,FixedLotSize,BuyPrice,Slippage * pips2points,BuyPrice - StopLoss * pips2dbl,BuyPrice + TakeProfit * pips2dbl,"Buy Stop Order",MagicNumber,0,Green);
         if(Ticket > 0)
           {
          if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY Stop Order Opened : ",OrderOpenPrice());
           }
        else Print("Error opening BUY Stop Order : ",GetLastError());
           
//-----------------------------------------------------------------------------+
         
   double SellPrice = Bid - OpenDistance * pips2dbl;
   
                              
         Ticket = OrderSend(Symbol(),OP_SELLSTOP,FixedLotSize,SellPrice,Slippage * pips2points,SellPrice + StopLoss * pips2dbl,SellPrice - TakeProfit * pips2dbl,"Sell Stop Order",MagicNumber,0,Red);
         if(Ticket > 0)
           {
          if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL Stop Order Opened : ",OrderOpenPrice());
           }
        else Print("Error opening SELL Stop Order : ",GetLastError());
           }//0
           
           
//+---------------------------------------------------------------------------+
 

A few things to work on...

  • FixedLotSize        = 0.01;    what if minlotsize you have to use for opening a trade is bigger ?
  • OpenDistance        =   2.0;   what if the stoplevel to open a new trade is bigger then the size you have chosen ?
  • Slippage            =   0;         for what reason is this value 0 , do you know why slippage is used opening and closing trades ??  requote ???
  • Did you do a strategytest with this ???  if not then do it now....
  • how many trades did it make in the first minute ??  fix it that way it can't open more then needed just one of both
Show me the next improvements on this.......

 
deVries:

A few things to work on...

  • FixedLotSize        = 0.01;    what if minlotsize you have to use for opening a trade is bigger ?
  • OpenDistance        =   2.0;   what if the stoplevel to open a new trade is bigger then the size you have chosen ?
  • Slippage            =   0;         for what reason is this value 0 , do you know why slippage is used opening and closing trades ??  requote ???
  • Did you do a strategytest with this ???  if not then do it now....
  • how many trades did it make in the first minute ??  fix it that way it can't open more then needed just one of both
Show me the next improvements on this.......


 Hi deVries,

So, what I've done was to open an order and try to close the opposite pending order once an op_buy or op_sell is open, but seems that's work....

Here is the code. Can't get why...

Please could you clarify me where I'm failing ? 

 

//+------------------------------------------------------------------+
//|                                                         Luis.mq4 |
//|                               Copyright © 2013,  Tjipke de Vries |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013,  Tjipke de Vries"
#property link      "https://forum.mql4.com/54215/"


extern string UserSettings        = "=== User Settings ===";
extern bool   DynamicLotSize      = false;
extern double PercentageToTrade   =   1;
extern double FixedLotSize        = 0.01;
extern double OpenDistance        =   2.0;
extern int    Slippage            =   3;
extern int    StopDistance        =   1;
extern double StopLoss            =  50.0;
extern double TakeProfit          =  50.0;
extern int    MagicNumber         =  101010;



int    TotalOpenEA,TotalBuy,TotalSell,TotalBuyStop,Ticket,
       TotalSellStop,TotalBuyLimit,TotalSellLimit;
double lotmin,lotmax,lotstep,LotSize,RiskAmount;

//++++ These are adjusted for 5 digit brokers.
int     pips2points;      // slippage  3 pips    3=points    30=points
double  pips2dbl;         // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;      // DoubleToStr(dbl/pips2dbl, Digits.pips)
//---

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if(Digits % 2 == 1)  // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
     {pips2dbl = Point*10; pips2points = 10;   Digits.pips = 1;}
     else {pips2dbl = Point;    pips2points =  1;   Digits.pips = 0;}
     // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl       
      
    if(OrdersTotal() >0 )TotalOpenEA=OrdersTotal(); 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {//0
//---- Number ONE to DO  ==>  CHECK OPEN TRADES
   if(TotalOpenEA > 0)
     {//1
      TotalOpenEA = 0;
      TotalBuy = 0;
      TotalSell =0;
      TotalBuyStop =0;
      TotalSellStop=0;
      TotalBuyLimit =0;
      TotalSellLimit =0;
      double BuyLots,SellLots,HighestLot;
      for(int i = OrdersTotal()-1; i >= 0 ; i--)
        {//2
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
         if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue;
         TotalOpenEA++;
         if(HighestLot < OrderLots())HighestLot=OrderLots();
         //---- check order type
         if(OrderType()==OP_BUY)
           {//3
            TotalBuy++;
            if(OrderLots()> BuyLots)BuyLots=OrderLots();
           }//3 
         if(OrderType()==OP_SELL)         
           {//4
            TotalSell++;
            if(OrderLots()> SellLots)SellLots=OrderLots();
           }//4
         if(OrderType()==OP_BUYSTOP)
           {//5
            TotalBuyStop++;
           }//5
          if(OrderType()==OP_SELLSTOP)
            {//6
             TotalSellStop++;            
            }//6
          if(OrderType()==OP_BUYLIMIT)
            {//7
             TotalBuyLimit++;             
            }//7
          if(OrderType()==OP_SELLLIMIT)
            {//8
             TotalSellLimit++;            
            }//8
        }//2
     }// 1                      
//finish this loop for all OrderTypes      
          
        //end loop
                
   //+----------------------------------------------------------------------------+     
     // Money Management 
  
      if(DynamicLotSize == true)
        {//1
       RiskAmount = AccountBalance() * (PercentageToTrade / 100000);
       LotSize = RiskAmount;
      }//1
    else LotSize = FixedLotSize;   
          
 //+----------------------------------------------------------------------------+ 
  // Lot Size verification 
     {//2
       lotmin = MarketInfo(Symbol(),MODE_MINLOT);
       lotmax = MarketInfo(Symbol(),MODE_MAXLOT);
       lotstep = MarketInfo(Symbol(),MODE_LOTSTEP);
                    
       LotSize = MathFloor(LotSize/lotstep) * lotstep;
     
       if(LotSize < lotmin)
        LotSize = lotmin;
       if(LotSize > lotmax)
        LotSize = lotmax;             
      } //2                     
//+----------------------------------------------------------------------------+        
         double StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) * Point;
    double UpperStopLevel=Ask+StopLevel; 
    double LowerStopLevel=Bid-StopLevel;
    RefreshRates();
    double BuyPrice = Ask + OpenDistance * pips2dbl;
          if(BuyPrice < UpperStopLevel)BuyPrice = UpperStopLevel + StopDistance * pips2dbl;           
                    while(IsTradeContextBusy()) Sleep(10);
                       RefreshRates();   
         Ticket = OrderSend(Symbol(),OP_BUYSTOP,FixedLotSize,BuyPrice,Slippage * pips2points,BuyPrice - StopLoss * pips2dbl,BuyPrice + TakeProfit * pips2dbl,"Buy Stop Order",MagicNumber,0,Green);
         if(Ticket > 0)
           {//3
          if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY Stop Order Opened : ",OrderOpenPrice());
            
           }//3
        else Print("Error opening BUY Stop Order : ",GetLastError());
           
//-----------------------------------------------------------------------------+
     UpperStopLevel=Ask+StopLevel; 
    LowerStopLevel=Bid-StopLevel;
    RefreshRates();   
    double SellPrice = Bid - OpenDistance * pips2dbl;
         if(SellPrice > LowerStopLevel)SellPrice = LowerStopLevel - StopDistance * pips2dbl ;
                   while(IsTradeContextBusy()) Sleep(10);
                       RefreshRates();          
    Ticket = OrderSend(Symbol(),OP_SELLSTOP,FixedLotSize,SellPrice,Slippage * pips2points,SellPrice + StopLoss * pips2dbl,SellPrice - TakeProfit * pips2dbl,"Sell Stop Order",MagicNumber,0,Red);
    if(Ticket > 0)
      {//4
    if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL Stop Order Opened : ",OrderOpenPrice());
       
      }//4
    else Print("Error opening SELL Stop Order : ",GetLastError());
                        
//+---------------------------------------------------------------------------+
                                  
      int total = OrdersTotal();
    
        for(int OpenType = total -1; OpenType >=0; OpenType--)
           {//5
            OrderSelect(OpenType,SELECT_BY_POS,MODE_TRADES);
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
              if(OrderType()== OP_BUY || OrderType()== OP_SELL)
                {//6
         for(int OrderPending = total-1; OrderPending >=0; OrderPending--)
             {//7
              OrderSelect(OrderPending,SELECT_BY_POS,MODE_TRADES);
             if(OrderSymbol()==Symbol()&& OrderMagicNumber()== MagicNumber)
             if(OrderType()== OP_SELLSTOP || OrderType() == OP_BUYSTOP)
                {//8
                  OrderDelete(OrderTicket());
                }//8
             }//7
          }//6
       }//5
    }//0
      
 //+---------------------------------------------------------------------------+   
 

How do you wanna make your program ????

If you don't understand what your coding is doing

Don't you have any idea what

information this piece of  coding

   if(TotalOpenEA > 0)
     {//1
      TotalOpenEA = 0;
      TotalBuy = 0;
      TotalSell =0;
      TotalBuyStop =0;
      TotalSellStop=0;
      TotalBuyLimit =0;
      TotalSellLimit =0;
      double BuyLots,SellLots,HighestLot;
      for(int i = OrdersTotal()-1; i >= 0 ; i--)
        {//2
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
         if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue;
         TotalOpenEA++;
         if(HighestLot < OrderLots())HighestLot=OrderLots();
         //---- check order type
         if(OrderType()==OP_BUY)
           {//3
            TotalBuy++;
            if(OrderLots()> BuyLots)BuyLots=OrderLots();
           }//3 
         if(OrderType()==OP_SELL)         
           {//4
            TotalSell++;
            if(OrderLots()> SellLots)SellLots=OrderLots();
           }//4
         if(OrderType()==OP_BUYSTOP)
           {//5
            TotalBuyStop++;
           }//5
          if(OrderType()==OP_SELLSTOP)
            {//6
             TotalSellStop++;            
            }//6
          if(OrderType()==OP_BUYLIMIT)
            {//7
             TotalBuyLimit++;             
            }//7
          if(OrderType()==OP_SELLLIMIT)
            {//8
             TotalSellLimit++;            
            }//8
        }//2
     }// 1                      

 is giving you,.....

you have to use it to open one new buystop and one new sellstop

you are only allowed to place one new buystop if total buytrades is 0 and total buystoptrades is also 0

and for sellstop you only allowed to place a new sellstop if   total selltrades is 0 and total sellstoptrades is also 0

the code you see here can give you that information so use that information  

till now you don't have to close opposite pending order  

Reason: