Navigation´╝ÜHome > Content >

__BUY-D.mq4

Time: 2018-07-08 | Download file:__BUY-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=142555;
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();
   OpenBuy();
   
   Print("Init Complete BUY-D");
   Comment(" ");
  }

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

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




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

int start()
  {
  } // start()






//ENTRY LONG (buy, Ask) 
void OpenBuy()
     {
      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)          
        {
         // place order - NO TP OR SL
         ticket=OrderSend(mySymbol,OP_BUY,Lots,Ask,Slippage,0,0,"BUY-D",MagicNumber,White);
         gle=GetLastError();
         if(gle==0)
           {
            Print("BUY PLACED Ticket="+ticket+" Ask="+Ask+" Lots="+Lots);
            break;
           }
          else 
           {
            Print("-----ERROR-----  Placing BUY order: Lots="+Lots+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); 
            
            RefreshRates();
            //Sleep(500);

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


      // 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=Ask-( LossLimit*myPoint );
         if(ProfitMade  >0) TP=Ask+( ProfitMade*myPoint );
         OrderModify(ticket,OrderOpenPrice(),SL,TP,0,White);
         gle=GetLastError();
         if(gle==0)
           {
            Print("BUY MODIFIED Ticket="+ticket+" Ask="+Ask+" Lots="+Lots+" SL="+SL+" TP="+TP);
            break;
           }
          else 
           {
            Print("-----ERROR-----  Modifying BUY 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 modifying BUY order"); 
               Print("-----ERROR-----  Giving up on modifying BUY order"); 
               return(gle);
              }
           }
        }//while - modify order
        
        
     }//BUYme



// 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