How to add stealth orders -hide my stop loss/ take profit as market orders

 

I'm having trouble placing my stop loss and take profit orders as stealth orders - becoming market orders when hit. Where the broker cannot see them. As here it goes straight to zero. As most often when I tried.( In one it did alot better for a while.) How can I get the similar? The file ending in 51 is stealth. You'll find this idea where I close the trade at the bottom section. There I comment out what works -as I'd like -as in file 41. Also I commented out the order modify where I open the trades. Thank-You for any help. Deetrader999

//+------------------------------------------------------------------+
//|                                                     15ea4151.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Deetrader999"
#property link      "http://catget@comcast.net"
//---- input parameters
extern int MAGICMA= 1000;
extern double    TakeProfit=10000.0;
extern double    Lots=0.01;
extern double    TrailingStop=0.0;
extern double  slip=3.0;
static datetime lastbar;
bool bopin=True, sopin=False;
extern int stoploss = 35;
bool curnum=True, endtr=True;
double    atrp, bslss, sslss;
int  Current=0 ;
extern int maperiods=2;
int stp,tkp,slp,pos,mult;
extern double posprof=0,negprof=0,prof=0;
double adjma1b,adjma1s,adjhigh1,adjlow1;
//account balance begin
extern double ablots=0.1;
double ablots1,ablots2;
extern double reserve1=0,abperlot1=120,reserve2=0,abperlot2=120;
extern bool useab1=false,useab2=false;
extern double userab2=0,drawd=90;
double c1,abor,newab2,maxab2,maxab21,maxqty2,maxqty22;
double bablots, sablots;

//acct balance end

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
lastbar=Time[1];
   stp=stoploss;
   tkp=TakeProfit;
   slp=slip;
   if(Digits==5||Digits==3)
  {
  mult=10;
  }
  else mult=1;
  stp=stp*mult;
  tkp=tkp*mult;
  slp=slp*mult;
  prof=prof*mult;
  posprof=posprof*mult;negprof=negprof*mult;
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- 
//---Do every tick stuff here
   
   int cnt, ticket, total;
   double shortEma, longEma, stopllevel;
   
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
//Do once per bar stuff here     
     

//if( Hour()<=0 && Hour()<4|| Hour()>=4 && Hour()<8||
//Hour()>=8 && Hour()<12|| Hour()>=12 && Hour()<16||
//Hour()>=16 && Hour()<20|| Hour()>=20 && Hour()<0)
//if(Hour()==0 || Hour()==4 || Hour()==8 || Hour()==12 || Hour()==16 || Hour()==20)
//  {
   if (IsNewBar()) //IsNewBar checks for a new bar & does once per bar stuff
//curnum does one trade, order per bar and ok to do if true
// bopin sets to 0 each bar and 1 if a buy opinion given, sopin  fora sell opinion
   {
   
   //Do new bar stuff 
 
   endtr=True;
   curnum=True;
//   Print("time",Time[0]);
  }  //isnewbar 
    atrp=iATR(NULL, 0, 14, Current + 1);
 //   double sar1 = iSAR(NULL, 0, 0.02, 0.2, Current + 0);
 //   double sar2 = iSAR(NULL, 0, 0.02, 0.2, Current + 1);
    double ma1 = iMA(NULL, 0, maperiods, 0, MODE_SMA, PRICE_CLOSE, Current + 0);
    double ma2 = iMA(NULL, 0, maperiods, 0, MODE_SMA, PRICE_CLOSE, Current + 1);
    double Buy2 = MathMax(High[1],High[2]);
    double Sell2=MathMin(Low[1],Low[2]);
//    adjbcl=(Close[1]+atrp)+prof*Point;  //for atr%
//    adjscl=(Close[1]-atrp)-prof*Point;   //for atr%
    adjma1b=ma1+(posprof*Point)-(negprof*Point);  //for ma 
    adjma1s=ma1-(posprof*Point)+(negprof*Point);  //for ma
    adjhigh1=High[1]+(posprof*Point)-(negprof*Point);//for channel
    adjlow1=Low[1]-(posprof*Point)+(negprof*Point);  //for channel
//    bool buycl=( High[0]>ma1 && High[0]>High[1]);
//    bool sellcl=(Low[0]<ma1 && Low[0]<Low[1]);
     bool buycl=( High[0]>adjma1b && High[0]>adjhigh1);
     bool sellcl=(Low[0]<adjma1s && Low[0]<adjlow1);

//2nd str( max acct bal -reserve )/amt per micro lot. gives # of lots. if loses money lots stay the same
//if make new $.lots may change. if loss in acct bal is greater than chosen drawd, then simple current acct bal
// /divided by 1st method amt per lot. ist method is current acct bal/amt per lot.
if(useab1==true){
     ablots1= ((AccountBalance()-reserve1)/abperlot1);
     ablots2=(MathFloor(ablots1));
     ablots=ablots2*Lots;}
     Print("useab1 ",useab1,"ablots1 ",ablots1,"ablots2 ",
     ablots2,"ablots ",ablots);
     
    total  = OrdersTotal(); //This is regular program buy or sell signal
   if(total < 1) 
     {
//     if( High[0]> Close[1]+Close[1]/10000)
       if (buycl && curnum==True )   
        {
        
        curnum=False;
//        Print("Hello b");
         bopin=True;
         sopin=False;
 //           bslss=Bid-stp*Point;
            bslss=OrderOpenPrice()-stp*Point;
            ticket=OrderSend(Symbol(),OP_BUY,ablots,Ask,slp,0,0,"My EA",MAGICMA,0,Green);
            if(ticket>0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) {Print("BUY order opened : ",OrderOpenPrice());
   //            OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-stp*Point,OrderOpenPrice()+tkp*Point,0,Green);
//              Print("New buy stop",bslss,Point,bmo);
              }
              }
            else Print("Error opening BUY order : ",GetLastError());
            return(0);
           }          
//            if (Low[0]<Close[1]-Close[1]/10000) Print("hello s");
   
     if( sellcl && curnum==True )   
        
         {
   
         curnum=False;
         bopin=False;
         sopin=True;
 //           sslss=Ask+stoploss*Point;
            
            ticket=OrderSend(Symbol(),OP_SELL,ablots,Bid,slp,0,0,"My EA",MAGICMA,0,Red);
            if(ticket>0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) {Print("SELL order opened : ",OrderOpenPrice());
   
   //           OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+stp*Point,OrderOpenPrice()-tkp*Point,0,Green);           
              sslss=OrderOpenPrice()+stp*Point;
              }
              }
            else Print("Error opening SELL order : ",GetLastError()); 
            return(0);
         }
         return(0);
         }
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        {
         
         int count=0;
         for (pos=OrdersTotal()-1; pos>=0; pos--)
         if (OrderSelect(pos,SELECT_BY_POS)//Only my orders
         && OrderMagicNumber()==MAGICMA  //with my magic number
         && OrderSymbol()==Symbol() ){   //and my pair
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
          //      if (sellcl && curnum==True && endtr==True)
               bool buystp=(Low[0]<=(OrderOpenPrice()-stp*Point));
               bool buytkp=( High[0]>=( OrderOpenPrice()+tkp*Point));
   
                if  (( curnum==True && endtr==True) && ( buystp || buytkp || sellcl))         
            //  if one trade per bar and no trades closed this bar and either a buy stop,
            // buy take profit or basic sell signal then close trade
                {
                 endtr=False;
                 if(OrderClose(OrderTicket(),OrderLots(),Bid,slp,Violet)) // close position
                 continue;
                 Alert("OrderCloseFailed:",GetLastError());
//                 return(0); // exit
                }
              
 
//            check for trailing stop
//            Print(" before trailing stop",TrailingStop);
           //ts begins
               if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }// if buy
         else // go to short position
           {
            // should it be closed?
         //   if(buycl && curnum==True && endtr==True)
             bool sellstp=(High[0]>=(OrderOpenPrice()+stp*Point));
             bool selltkp=(Low[0]<=(OrderOpenPrice()-tkp*Point));
   
                  if (( curnum==True && endtr==True)&& ( sellstp || selltkp || buycl  ))      
 
              {
               endtr=False;
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
              }
            // check for trailing stop
           //ts begins
           if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           count++;
           }//count down
        
        }//ordertype
        
        }// for    
          
//    }//isnewbar
    }//orders total
 //}// time equal
   return(0);
  }//start
    bool IsNewBar()
  {
  datetime curbar =Time[0];//Open time of current Bar
  if(lastbar!=curbar)
  {
    lastbar=curbar;
    return(true);
    }
    return(false);
  }    

  
  //---------------------------------
Files:
15ea41.mq4  10 kb
 
Check if OrderOpenPrice() vs OrderClosePrice() meet your Distance. Then OrderClose(). Example Here. The example does-not consider FreezeLevel. Also there's no-error-processing if closing Fails.