//+------------------------------------------------------------------+
//|                                                   JJ's Grail.mq4 |
//|                                                              Les |
//|                                           Copyright c@2013 Lesbo |
//+------------------------------------------------------------------+
#property copyright "Les"
#property link      "Copyright c@2013 Lesbo"

//--- input parameters
extern string    info_7="Set the info distance across the screen";
extern int       Xf=550;
extern string    info_8="Trade automatically ? ";
extern bool      autotrade=true;
extern string    info_9="Set stoploss and take profit in POINTS";
extern int       stoploss=200;
extern int       takeprofit=100;
extern int       microlots=1;
extern string    info_0="Stochastic settings";
extern int       stoch_k=3;
extern int       stoch_d=3;
extern int       stoch_slowing=2;
extern string    info_1="Stochastic levels";
extern double    stoch_high_level=95.0;
extern double    stoch_low_level=5.0;
extern string    info_2="RSI settings";
extern int       rsi_period=2;
extern string    info_10="RSI levels";
extern double    rsi_high_level=95.0;
extern double    rsi_low_level=5.0;
extern string    info_3="You wanna use the Bollinger bands ?";
extern bool      use_bollies = true;
extern string    info_4="Bollinger band settings";
extern int       bol_period=20;
extern int       bol_deviation=2;
extern string    info_5="The following are used by the";
extern string    info_6="program - Please do not adjust";
extern int       bars=0;
extern bool      gosell=false;
extern bool      gobuy=false;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
// -------------------- Show Spread -----------------------//
string sym = Symbol();                                     //  
double bid   =MarketInfo(sym,MODE_BID);                    //
double ask   =MarketInfo(sym,MODE_ASK);                    //
int spread   =((ask-bid)*100000);                          //
string minorjpy = StringSubstr( Symbol(), 3, 3);
if(minorjpy=="JPY")
   {
   spread=spread/100;
   }
color I28 = Red;
double hi0=High[1];
double cl0=Close[1];
double lo0=Low[1];
double op0=Open[1];                                           //               
if (spread<25){I28=Lime;}                                  //                  
if (spread<20){I28=Yellow;}                                //
if (spread<15){I28=MediumOrchid;}                          // 
string sprd = "Spread  "+DoubleToStr(spread,0);            //
ObjectDelete("Ic28");                                      //
ObjectCreate("Ic28",OBJ_LABEL,0,0,0,0,0);                  //
ObjectSet("Ic28",OBJPROP_XDISTANCE,900+Xf);                //
ObjectSet("Ic28",OBJPROP_YDISTANCE,30);                    //
ObjectSetText("Ic28",sprd,16,"Arial", I28);                //
// ---------------- End Show Spread -----------------------//   


//------- Stick all the whys and wherefores in here -------//
double rsi = iRSI(Symbol(),0,rsi_period,0,0);
double stoc1=iStochastic(Symbol(),0,stoch_k,stoch_d,stoch_slowing,1,1,1,0);
double bolhi=iBands(Symbol(),0,bol_period,bol_deviation,0,0,MODE_UPPER,0);
double bollo=iBands(Symbol(),0,bol_period,bol_deviation,0,0,MODE_LOWER,0);
// use bollinger bands for a sell //
if(use_bollies==true && bars<iBars(Symbol(),0) && stoc1>stoch_high_level
   && Ask>bolhi && rsi>rsi_high_level)         {   gosell=true;   }
// use bollinger bands for a buy //
if(use_bollies==true && bars<iBars(Symbol(),0) && stoc1<stoch_low_level
   && Bid<bollo && rsi<rsi_low_level)         {   gobuy=true;   }
// do NOT use bollinger bands for a sell //
if(use_bollies==false && bars<iBars(Symbol(),0) && stoc1>stoch_high_level
   && rsi>rsi_high_level)         {   gosell=true;   }
// do NOT use bollinger bands for a buy //
if(use_bollies==false && bars<iBars(Symbol(),0) && stoc1<stoch_low_level
   && rsi<rsi_low_level)         {   gobuy=true;   }
//----- End Stick all the whys and wherefores in here -----//

// --------------------- Buy Trade ------------------------//
if(gobuy==true && autotrade == false)
   {PlaySound("alert.wav");Alert("Trade possibility ",Symbol() ,"!!!");}
if(gobuy==true && autotrade == true)
  {
  int buySL=stoploss;
  int buyTP=takeprofit;
  string buyCmt1="JJ";
  string buysym = Symbol();
  double buybid   =MarketInfo(buysym,MODE_BID); // Request for the value of Bid
  double buyask   =MarketInfo(buysym,MODE_ASK); // Request for the value of Ask   
  int    buyspread=((buyask-buybid)*100000);
  int    buyMagic1=1949;
  double buypoint =MarketInfo(buysym,MODE_POINT);//Request for Point  
  double buysl = buybid - (buySL*Point);
  double buytp = buyask + (buyTP*Point);
  double buylots = microlots;
  buylots=buylots/100;
  int buyticket1 = OrderSend(buysym,OP_BUY,buylots,buyask,10,0,0,buyCmt1,buyMagic1,0,SkyBlue);// ticket1 = OrderSend(sym,OP_SELL,Lots_Sys1,bid,10,sl_sys1,tp_sys1,comm1,Magic1,0,Red);
    if (buyticket1 > 0)
    {
    double buyPrice1 =OrderOpenPrice();
    OrderModify(buyticket1,buyPrice1,buysl,buytp,0,Yellow);
    int buybadone = GetLastError();
    if (buybadone==0)
      {PlaySound("alert.wav");}
    if (buybadone > 0)
      {Alert(buybadone);}
    }
  }  
// ------------------- End Buy Trade ----------------------//

// --------------------- Sell Trade -----------------------//
if(gosell==true && autotrade == false )
   {PlaySound("alert.wav");Alert("Trade possibility ",Symbol() ,"!!!");}
if(gosell==true && autotrade == true)
  {
  int sellSL=stoploss;
  int sellTP=takeprofit;
  string sellCmt1="JJ";
  string sellsym = Symbol();
  double sellbid   =MarketInfo(sellsym,MODE_BID); // Request for the value of Bid
  double sellask   =MarketInfo(sellsym,MODE_ASK); // Request for the value of Ask   
  int    sellspread=((sellask-sellbid)*100000);
  int    sellMagic1=1949;
  double sellpoint =MarketInfo(sellsym,MODE_POINT);//Request for Point  
  double sellsl = sellask + (sellSL*Point);
  double selltp = sellbid - (sellTP*Point);
  double selllots = microlots;
  selllots=selllots/100;
  int sellticket1 = OrderSend(sellsym,OP_SELL,selllots,sellbid,10,0,0,sellCmt1,sellMagic1,0,Red);// ticket1 = OrderSend(sym,OP_SELL,Lots_Sys1,bid,10,sl_sys1,tp_sys1,comm1,Magic1,0,Red);
    if (sellticket1 > 0)
    {
    double sellPrice1 =OrderOpenPrice();
    OrderModify(sellticket1,sellPrice1,sellsl,selltp,0,Yellow);
    int sellbadone = GetLastError();
    if (sellbadone==0)
      {PlaySound("alert.wav");}
    if (sellbadone > 0)
      {Alert(sellbadone);}
    }
   }
// ------------------- End Sell Trade ---------------------//

bars=iBars(Symbol(),0);
gobuy=false;
gosell=false;   




//----
   return(0);
  }
//+------------------------------------------------------------------+