//+------------------------------------------------------------------+
//|                                         swb stealth_v1.01_R2.mq4 |
//|                                                  Dennis Hamilton |
//|                                              ramble_32@yahoo.com |
//+------------------------------------------------------------------+

#property copyright "Dennis Hamilton"
#property link      "ramble_32@yahoo.com"

extern double    lot_1=0.05;          // **
extern double    tp_1=05;             // **
extern double    sl_1=500;             // **
extern double    offset=40;           // **
extern double    reset=100;            // **
extern int       magic=47;            // **
extern bool      power_lots=true;     // **
extern int       open_charts=1;       // **
extern bool      oc_reset=false;      // **
extern bool      daily_rsi=true;      // **
extern int       rsi_hi=55;           // **
extern int       rsi_lo=45;           // **

double pt;
double range;     // **
double std=0.1;   // **
double AE1;       // **
double p_lot;     // **
double MTF_RSI;   // **

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   pt=Point;                              // **
   if(Digits==3 || Digits==5) pt*=10;     // **
//----
   tp_1*=pt;                              // **
   sl_1*=pt;                              // **
   offset*=pt;                            // **
   reset*=pt;                             // **
//----
   if(StringLen(Symbol())>6) std=1.0;     // **
   AE1=AccountEquity(); p_lot=lot_1/AE1;  // **
//----
   if(open_charts>1) GlobalVariableSet("open_charts",open_charts); // **
   if(!GlobalVariableCheck("open_charts") || oc_reset) GlobalVariableSet("open_charts",1); // **
   oc_reset=false; // **
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   int cmd, t_cnt=0, p_cnt=0; // **
   for(int i=0; i<OrdersTotal(); i++) // **
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES); // **
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue; // **
      cmd=OrderType();   // **
      if(cmd<2) t_cnt++; // **
      if(cmd>3) p_cnt++; // **
   }
//+------------------------------------------------------------------+
   double AB=AccountBalance(), st_lot=lot_1, pl_factor=1;  // **
   double o_charts=GlobalVariableGet("open_charts"); // **
//+------------------------------------------------------------------+
   if(power_lots)  // **
   {
      if(AB>AE1) st_lot=NormalizeDouble((p_lot*AB)/o_charts,Digits); // **
      pl_factor=st_lot/lot_1; // **
      double st_lot2=st_lot;  // **
   }
//----------------------------+
   if(OrdersTotal()==0) // **
   {
      if(signal()==0) OrderSend(Symbol(),4,st_lot,Ask+offset,3,0,0,"",magic,0,Blue); // **
      if(signal()==1) OrderSend(Symbol(),5,st_lot,Bid-offset,3,0,0,"",magic,0, Red); // **
   }
//+------------------------------------------------------------------+
   double OOP, OTP; int delete_cnt=0;
   for(i=0; i<OrdersTotal(); i++) // **
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES); // **
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue; // **
      cmd=OrderType();        // **
      OOP=OrderOpenPrice();   // **
      OTP=OrderTakeProfit();  // **
      if(cmd==1 || cmd==5){ tp_1*=-1; sl_1*=-1; }  // **
      if(OTP==0) OrderModify(OrderTicket(),0,OOP-sl_1,OOP+tp_1,0,Magenta); // **
   }
//+------------------------------------------------------------------+
   double profit=0, t_lot=0; string OP="NULL"; // **
   for(i=0; i<OrdersTotal(); i++)
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
      t_lot+=OrderLots(); //  **
      profit+=OrderProfit();
      OP="BUY"; if(cmd==1) OP="SELL"; // **
   }
//+------------------------------------------------------------------+
   double balance=0; // **
   for(i=0; i<OrdersHistoryTotal(); i++) // **
   {
      OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); // **
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue; // **
      balance+=OrderProfit(); // **
   }
//+------------------------------------------------------------------+
   for(i=OrdersTotal()-1; i>=0; i--) // **
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES); // **
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;  // **
      cmd=OrderType();  // **
      OOP=OrderOpenPrice();  // **
      if(cmd==4 && Ask<=OOP-reset) delete_cnt+=1;  // **
      if(cmd==5 && Bid>=OOP+reset) delete_cnt+=1;  // **
      if(delete_cnt>0) OrderDelete(OrderTicket()); // **
   }
//+------------------------------------------------------------------+
   MTF_RSI=iCustom(NULL,0,"#MTF_RSI",1440,20,0,0,0);     // **
//----
   string multi="Multiplier = 0.0", T1="", RSI="";       // **
   string st_lot3=DoubleToStr(lot_1,2), max_level="10";  // **
//----
   string line_1="OP = "+OP+"  |  Open = "+t_cnt +"  |  Pending = "+p_cnt+"  |  Lots = "+DoubleToStr(st_lot,2)+"  |  Total Lots = "+DoubleToStr(t_lot,2)+"  |  "+multi+"\n";     // **
   string line_2="Balance = "+DoubleToStr(balance,2)+"  |  Profit = " +DoubleToStr(balance+profit,2) +"  |  Account Equity = "+DoubleToStr(AccountEquity(),2) +"  |  Range = "+DoubleToStr(range/pt,0)+"  |  Spread = "+DoubleToStr((Ask-Bid)/pt,1)+"\n"; // **
   string line_3="Start Lots = "+st_lot3+"  |  Power Lots = "+DoubleToStr(st_lot2,2)+"  |  Charts = "+open_charts+"  |  T1 SL = 0.0  |  Target = 0.0  |  RS120 = "+DoubleToStr(MTF_RSI,0)+" / "+signal(); // **
//----
   Comment(line_1, line_2, line_3); // **
//----
   return(0);
  }
//+------------------------------------------------------------------+
int signal()
{
   int signal=7;
   MTF_RSI=iCustom(NULL,0,"#MTF_RSI",1440,20,0,0,0); // **
   if(daily_rsi && MTF_RSI>0) // **
   {
      if(MTF_RSI>rsi_hi) signal=0; // **
      if(MTF_RSI<rsi_lo) signal=1; // **
   }
  return(signal);
}

