Navigation´╝ÜHome > Content >

__SELL-D.mq4

Time: 2016-07-22 | Download file:__SELL-D.mq4

//+--------------+
//|One(Buy/Sell) |
//+--------------+
#property copyright "Ron Thompson"
#property link      "http://www.ForexMT4.com/"

// This code is NEVER to be SOLD individually 
// This code is NEVER to be INCLUDED as part of a collection that is SOLD


// This is really a script, so...
#property show_inputs


// EA SPECIFIC
extern double Lots                =       0.01 ;
extern double ProfitMade          =       0   ; 
extern double LossLimit           =       0   ;
extern int    Slippage            =       0   ;

// Trade control
int           MagicNumber=142222;
double        myPoint;                              // support for 3/5 decimal places
int           loopcount;                            // count of order attempts
int           maxloop=25;                           // maximum number of attempts to handle errors

// used for verbose error logging
#include 


//+-------------+
//| Custom init |
//|-------------+
// Called ONCE when EA is added to chart or recompiled

int init()
  {
   // get normalized Point based on Broker decimal places
   myPoint = SetPoint();
   OpenSell();

   Print("Init Complete SELL-D");
   Comment(" ");
  }

//+----------------+
//| Custom DE-init |
//+----------------+
// Called ONCE when EA is removed from chart

int deinit()
  {
   Print("DE-Init Complete SELL-D");
   //Comment(" ");
  }




//+-----------+
//| Main      |
//+-----------+
// Called EACH TICK and each Bar[]

int start()
  {
  } // start()



   //ENTRY SHORT (sell, Bid)
void OpenSell()
     {
      int      gle=0;
      int      ticket=0;
      
      double SL=0;
      double TP=0;
      int loopcount;

      string mySymbol=Symbol();
      if(Symbol()=="!EqvEURUSDm") mySymbol="EURUSDm";

      // PLACE order is independent of MODIFY order. 
      // This is mandatory for ECNs and acceptable for retail brokers

      loopcount=0;
      while(true)
        {
         ticket=OrderSend(mySymbol,OP_SELL,Lots,Bid,Slippage,0,0,"SELL-D",MagicNumber,Red);
         gle=GetLastError();
         if(gle==0)
           {
            Print("SELL PLACED Ticket="+ticket+" Bid="+Bid+" Lots="+Lots);
            break;
           }
            else 
           {
            Print("-----ERROR-----  placing SELL order: Lots="+Lots+" SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); 
                            
            RefreshRates();
            //Sleep(500);

            loopcount++;
            if(loopcount>maxloop)
              {
               Alert("-----ERROR-----  Giving up on placing SELL order"); 
               Print("-----ERROR-----  Giving up on placing SELL order"); 
               return(gle);
              }
           }
        }//while

      
      // modify the order for users TP & SL
      loopcount=0;
      while(true)
        {
         // don't set TP and SL both to zero, they're already there
         if(LossLimit==0 && ProfitMade==0) break;
         
         if(LossLimit  ==0) SL=0;
         if(ProfitMade ==0) TP=0;
         if(LossLimit   >0) SL=Bid+( LossLimit*myPoint );
         if(ProfitMade  >0) TP=Bid-( ProfitMade*myPoint );
         OrderModify(ticket,OrderOpenPrice(),SL,TP,0,Red);
         gle=GetLastError();
         if(gle==0)
           {
            Print("SELL MODIFIED Ticket="+ticket+" Bid="+Bid+" Lots="+Lots+" SL="+SL+" TP="+TP);
            break;
           }
            else 
           {
            Print("-----ERROR-----  modifying SELL order: Lots="+Lots+" SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); 
                            
            RefreshRates();
            //Sleep(500);

            loopcount++;
            if(loopcount>maxloop)
              {
               Alert("-----ERROR-----  Giving up on placing SELL order"); 
               Print("-----ERROR-----  Giving up on placing SELL order"); 
               return(gle);
              }
           }

        }//while

     }//SELLme





// Function to correct the value of Point
// for brokers that add an extra digit to price
// Courtesy of Robert Hill

double SetPoint()
{
   double mPoint;
  
   if (Digits < 4)
      mPoint = 0.01;
   else
      mPoint = 0.0001;
  
   return(mPoint);
}

Recommend