//+------------------------------------------------------------------+
//|                                              Sell with SL and TP |
//|                               Copyright © 2008, smjones          |
//|                                               sjcoinc            |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, smjones"
#property link      "sjcoinc2000@yahoo.com"


#property show_inputs

string Input = " Sell Price ";
double Lots = .02;
bool   UseMoneyMgmt = true;
double RiskPercent = .5;
bool   UseStop = true;
bool   UseTakeProfit = true;
double Sentiment=0;

extern int NumDays = 5;
extern int pipMult=2;


string Note="0 in Entry field means Market Order Sell";
double Entry = 0.0000;
double StopLoss;
double TakeProfit;
  //global
double Poin;


//+------------------------------------------------------------------+
//                                                                   +
//+------------------------------------------------------------------+
int init(){
  


if (Point == 0.00001) Poin = 0.0001; 
else if (Point == 0.001) Poin = 0.01; 
else Poin = Point; 
  
 int DayRng = 0;  
  for (int i = 1; i <= NumDays; i++) DayRng = DayRng + (iHigh(NULL, PERIOD_D1, i) - iLow(NULL, PERIOD_D1, i))/Poin;
  DayRng /= NumDays;
    
  
 StopLoss=(DayRng/pipMult);
 TakeProfit=(StopLoss*2);
 
 return(0);
}
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start(){
 
   double Risk = RiskPercent / 100;
  if (UseMoneyMgmt) Lots = NormalizeDouble( AccountBalance()*Risk/StopLoss/(MarketInfo(Symbol(), MODE_TICKVALUE)),2); 
   
  int Mode = OP_SELLSTOP;
  if (Bid < Entry && Entry > 0) Mode = OP_SELLLIMIT; 
  if (Entry == 0) {Entry = Bid; Mode = OP_SELL;}
  double SLS=Entry+StopLoss*Poin, TPS=Entry-TakeProfit*Poin;
  if (UseStop == false) SLS = 0;
  if (UseTakeProfit == false) TPS = 0;
  if(Lots > 0) OrderSend(Symbol(),Mode, Lots, Entry, 2, SLS, TPS, Sentiment,0, NULL, Red);
 
            
   return(0);
  }
//+------------------------------------------------------------------+
