// name of the EA : SAMER SSII ma G-string scalping EA
// DATED 5-3-2014
// BY SAMER MAHMOUD.
// rev2 / with risk control - still under testing
// rev 1 / under study


// trade on your own resposibilty


//int Magic=0;

#define Magic 0 

extern double   MovingPeriodsignal = 300  ;  

extern double   envelope_level = 300  ;  


extern double PAIR_NUMBER = 10;

extern int PROFIT_FACTOR=    50 ;// take profit in points

extern int     harvesting_corr_factor=    100   ;

extern double compounding_factor = 2 ;

extern double  Lot= 0.01 ; // for each 5000 usd 


extern double INITIAL_DEPOSIT=5000;

extern int     Slip=2;
double LotS,LotB;
double swap_long,swap_short,pair_spread_points,pair_stop_level,take_profit_points,stop_loss_points,
net_profit_points,net_stoploss_points,ACCOUNT_LOSS,ACCOUNT_PL_RATIO,AccountBalanceX,AccountEquityX;
double sleeping_time_msec,ACCOUNT_TOTAL_PROFIT_RATIO;
int ticket ;


///////////////////////////////////////////////////////////////////////////////////////////////
int start()
  {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
int h = TimeHour(TimeCurrent());
int m=TimeMinute(TimeCurrent());
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// on a chart of less than 400 bars
   if(Bars<400)
     {
      Print("bars less than 400");
      return(0);  
     }
//////////////////////////////////////////////////////////////////////////////////////////
   pair_spread_points=NormalizeDouble(MarketInfo(Symbol(),MODE_SPREAD),0); // points
   pair_stop_level=MarketInfo(Symbol(),MODE_STOPLEVEL); // points
   swap_long=NormalizeDouble(MarketInfo(Symbol(),MODE_SWAPLONG),0);
   swap_short=MarketInfo(Symbol(),MODE_SWAPSHORT);

   take_profit_points=NormalizeDouble((pair_spread_points+pair_stop_level)+PROFIT_FACTOR,0);

//net_stoploss_points = NormalizeDouble ( ( stop_loss_points -  pair_spread_points ),0);
   net_profit_points=NormalizeDouble(( take_profit_points-pair_spread_points),0);
   ACCOUNT_LOSS=NormalizeDouble(AccountEquity()-AccountBalance(),0);
   ACCOUNT_PL_RATIO=NormalizeDouble(ACCOUNT_LOSS/AccountBalance()*100,0);

   AccountBalanceX= NormalizeDouble(AccountBalance(),2);
   AccountEquityX = NormalizeDouble(AccountEquity(),2);
   ACCOUNT_TOTAL_PROFIT_RATIO=NormalizeDouble((AccountBalance()-INITIAL_DEPOSIT)/INITIAL_DEPOSIT*100,2);

   double TakeProfit=take_profit_points;
   double TPB=TakeProfit;
   double TPBx=TakeProfit*1.25;

   double TPS=TPB;
   double TPSx=TPBx;

   double net_TPBx_TPSx_points=NormalizeDouble(( TPBx-pair_spread_points),0);

   double ACCOUNT_STOPPAGE_RATIO= 30;
   double ACCOUNT_RECENT_RATIO  = NormalizeDouble((AccountEquity()/AccountBalance())*100,0);

   int total=OrdersTotal();

LotB = Lot ;
LotS = Lot ;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//



   double max1 = NormalizeDouble(iHigh(Symbol(),0, 1),Digits);
   double min1 = NormalizeDouble(iLow (Symbol(),0, 1),Digits);
   double opp1 =NormalizeDouble(iOpen(Symbol(),0,  1),Digits);
   double cl1  =NormalizeDouble(iClose(Symbol(),0, 1),Digits);

double ma1signal_1 = iMA(NULL,0,MovingPeriodsignal,0,MODE_LWMA,PRICE_CLOSE,1);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

   double spred =NormalizeDouble(MarketInfo(Symbol(),MODE_SPREAD)*Point,Digits);

   double  CORR=NormalizeDouble(harvesting_corr_factor   *Point,Digits);

////////////////////////////////////////////////////////  COMPOUNDING FOR ONE DAY IF REQUIRED ///////////////////////////////////////
   double nn=0,bb=0,counterb=0;
   for(int ui=total-1; ui>=0; ui--)
     {
      if(OrderSelect(ui,SELECT_BY_POS))
        {
         if(OrderSymbol()==Symbol())
           {
            if(OrderType()==OP_BUY && OrderMagicNumber()==Magic)
              {
               double op=OrderOpenPrice();
               double llot=OrderLots();
               double itog=op*llot;
               bb=bb+itog;
               nn=nn+llot;
               double factb=bb/nn;
               counterb=counterb+1;
              }
           }
        }
     }
   double nnn=0,bbb=0,counters=0;
   for(int usi=total-1; usi>=0; usi--)
     {
      if(OrderSelect(usi,SELECT_BY_POS))
        {
         if(OrderSymbol()==Symbol())
           {
            if(OrderType()==OP_SELL && OrderMagicNumber()==Magic)
              {
               double ops=OrderOpenPrice();
               double llots=OrderLots();
               double itogs=ops*llots;
               bbb=bbb+itogs;
               nnn=nnn+llots;
               double facts=bbb/nnn;
               counters=counters+1;
              }
           }
        }
     }
///////////////////////////////////////////////////////////////////////////

   for(int uui=total-1; uui>=0; uui--)
     {
      if(OrderSelect(uui,SELECT_BY_POS))
        {
         if(OrderSymbol()==Symbol())
           {
            if(counterb>=compounding_factor && OrderType()==OP_BUY && OrderMagicNumber()==Magic) // DAILY MAGIC NUMBER IS CHANGING FROM DAY TO DAY
              {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),factb+CORR,0,White);
              }
            if(counters>=compounding_factor && OrderType()==OP_SELL && OrderMagicNumber()==Magic)
              {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),facts-CORR,0,White);
              }
           }
        }
     }


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  CALCULATOR for SWAP GAINED PER NIGHT  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
   double SWAP_CORRL_FACTOR=1;
//////////////////////////////////////////////////////////////////////////////////////////////// 
   if(Symbol()=="EURTRY" || Symbol()=="USDTRY") {SWAP_CORRL_FACTOR=2.18;}
   double profit_gained_from_short_swap= NormalizeDouble((swap_short/(SWAP_CORRL_FACTOR*10))*nnn*10,2);
   double profit_gained_from_long_swap =  NormalizeDouble((swap_long/(SWAP_CORRL_FACTOR*10))*nn*10,2);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
double  CTLotS , CTLotB ; 
CTLotS = LotS ;
CTLotB = LotB ; 


//----
   Comment
   (
    "\n                                                              PAIR INFORMATION FOR SAMER SSII ma G-string scalping EA ", " ....PAIR_NUMBER = ",PAIR_NUMBER, " ....Time = ",h," : ",m,
    "\n                                                              --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", 
    "\n                                                              Magic = ",Magic, " .............  compounding_factor = ",compounding_factor ," trades ","......... harvesting_corr_factor(points) = ",harvesting_corr_factor,
    "\n                                                              C.T.  LotB = ",CTLotB,"  ....... C.T.  LotS = ",CTLotS,
    "\n                                                              C.T.  take_profit_points = ",take_profit_points," .......C.T. net_profit_points = ",net_profit_points,
    "\n                                                              MovingPeriodsignal = ",MovingPeriodsignal,
    "\n                                                              envelope_level in points = ",envelope_level,
    "\n                                                              pair_spread_points = ",pair_spread_points,
    "\n                                                              pair_stop_level = ",pair_stop_level,
    "\n                                                              ACCOUNT_BALANCE            = $",NormalizeDouble(AccountBalance(),0),
    "\n                                                              ACCOUNT_EQUITY             = $",NormalizeDouble(AccountEquity(),0),
    "\n                                                              ACCOUNT_TOTAL_PROFIT_RATIO             = ",ACCOUNT_TOTAL_PROFIT_RATIO," %"," ... $ ",NormalizeDouble((AccountBalance()-INITIAL_DEPOSIT),0),
    "\n                                                              ACCOUNT_FLOATING PROFIT/LOSS VALUE     =              ($ ",ACCOUNT_LOSS,")",
    "\n                                                              ACCOUNT_FLOATING PROFIT/LOSS RATIO     = ",ACCOUNT_PL_RATIO," %",
    "\n                                                              ACCOUNT_RECENT_RATIO/ACCOUNT_STOPPAGE_RATIO     = ",ACCOUNT_RECENT_RATIO," %"," / ",ACCOUNT_STOPPAGE_RATIO," %",
    "\n                                                              total number of SELL orders for this pair  = ",counters,
    "\n                                                              total number of BUY  orders for this pair  = ",counterb,
    "\n                                                              total lot sizes of SELL orders for this pair  = ",nnn,
    "\n                                                              total lot sizes of BUY  orders for this pair  = ",nn,
    "\n                                                              ----------------------------------------------------------------------------------------------------------------------------------------------------"
    );
//---- 

//////////////////////////////////////////////////////////////
   if(ACCOUNT_RECENT_RATIO<ACCOUNT_STOPPAGE_RATIO)
     {
      ObjectCreate("R",OBJ_LABEL,0,0,0);
      ObjectSet("R",OBJPROP_CORNER,2);
      ObjectSet("R",OBJPROP_XDISTANCE,130);
      ObjectSet("R",OBJPROP_YDISTANCE,100);
      ObjectSetText("R","  EA STOPPED : Equity/AccountBalance < 30% ",18,"Arial",Red);
      return(0);
     }
/*
if ( Symbol()!="")
  {
   ObjectCreate("R",OBJ_LABEL,0,0,0);
   ObjectSet("R",OBJPROP_CORNER,2);
   ObjectSet("R",OBJPROP_XDISTANCE,130);
   ObjectSet("R",OBJPROP_YDISTANCE,100);
   ObjectSetText("R","  EA STOPPED : Symbol() IS NOT  !!!!! ",26,"Arial",Red);
   return(0) ;
  }
 
 */  
   if(Period()!=PERIOD_M1)
    {
   ObjectCreate("R",OBJ_LABEL,0,0,0);
   ObjectSet("R",OBJPROP_CORNER,2);
   ObjectSet("R",OBJPROP_XDISTANCE,130);
   ObjectSet("R",OBJPROP_YDISTANCE,100);
   ObjectSetText("R","EA STOPPED : WRONG TIME FRAME : IT SHOULD BE 5 minutes",16,"Arial",Red);
   return(0) ;
  }


//---- go trading only for first ticks of new bar
if(Volume[0]>1) return(0);
{//v0
  Sleep (2000+PAIR_NUMBER*1000 ) ;  

if (  ma1signal_1 - min1 > envelope_level*Point ) // buy C.T. 
    
     { ticket = OrderSend(Symbol(),OP_BUY,CTLotB,Ask,Slip,0,Bid+TPB*Point,"BUY C.T. CASE 1",Magic,0,Lime); }
  Sleep (2000);     
      
 if ( max1 - ma1signal_1 > envelope_level*Point ) // sell C.T.
     
    { ticket = OrderSend(Symbol(),OP_SELL,CTLotS,Bid,Slip,0,Ask-TPS*Point,"SELL C.T. CASE 1",Magic,0,Red); }   
     
   Sleep (2000);     
  
if ( Ask-(facts-CORR)>600*Point && (facts-CORR)>0)//sell
    { ticket = OrderSend(Symbol(),OP_SELL,CTLotS*5,Bid,Slip,0,Ask-TPS*Point,"SELL C.T. CASE 2",Magic,0,Gold); }   

  Sleep (2000);     

if (  (factb+CORR)- Ask >600*Point &&(factb+CORR)>0 )//buy
     { ticket = OrderSend(Symbol(),OP_BUY,CTLotB*5,Ask,Slip,0,Bid+TPB*Point,"BUY C.T. CASE 2",Magic,0,Blue); }
  Sleep (2000);     

  }//v0

  
  
  }
//+------------------------------------------------------------------+
