//+------------------------------------------------------------------+
//|                                                                  |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+

#property copyright "SELL at market with TP/SL"


#property show_inputs

extern string info = "If 'lots' is set to 0, it will try to use the 'ordersize' GlobalVariable(F3)";
extern double lots = 0.01;
extern double SLpips = 20;
extern double TPpips = 50;
extern int    MagicNumber=274693;

#include <stdlib.mqh>

double pip;
int digits;

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
{
  int err;
  
  getpip();
     
  if (lots==0) lots = GlobalVariableGet("ordersize");
  if (lots==0) {
    Alert("!!! "+WindowExpertName()+" "+Symbol()+": 'ordersize' not defined !!!");
  }
  
  double SLprice=0;
  double TPprice=0;
  double Price = Bid;
  if (SLpips!=0) SLprice = Price+SLpips*pip;
  if (TPpips!=0) TPprice = Price-TPpips*pip;

  int ticket=OrderSend(Symbol(),OP_SELL,lots,Price,30,SLprice,TPprice,"",MagicNumber,0,Green);

  if (ticket < 0)
  {
     err=GetLastError();
     Alert(Symbol()+":SELL FAILED ("+err+"): "+ErrorDescription(err));
  } else {
     Alert(Symbol()+":SELL at "+DoubleToStr(Price,Digits)+" TP="+DoubleToStr(TPprice,Digits)+",SL="+DoubleToStr(SLprice,Digits));
  }

  Alert("--- END "+WindowExpertName()+" "+Symbol());

  return(0);
}
//+------------------------------------------------------------------+

//--------------------------------------------------------------------------------------
// getpip
//--------------------------------------------------------------------------------------

void getpip()
{
   if(Digits==2 || Digits==4) pip = Point;
   else if(Digits==3 || Digits==5) pip = 10*Point;
   else if(Digits==6) pip = 100*Point;
      
	if (Digits == 3 || Digits == 2) digits = 2;
	else digits = 4;
} /* getpip*/

//end

