Help with Shift on Moving average EA

 
Hello

I'm trying to add the Shift or MA_Shift to my code e.g Shift= -8

extern double Lots = 1.0;
//---- Set stoploss to 0.0 if you don't want to use it
extern double StopLoss= 30.0;
//---- Set ProfitTarget to 0.0 if you don't want to use it
extern double ProfitTarget = 50.0;
//---- Set TrailingStop to 0.0 if you don't want to use it
extern double TrailingStop = 20.0;
//---- Slippage / Max deviation from quoted price
extern int    Slippage= 2;

//---- Do we want the expert to work accross all time frame 
//---- or just the currently selected time frame
extern bool   TimeSpecific = true;
extern bool   UseCompletedBars = true;

// Create any user input for the indicators you will use here
// For example:
extern string MA1Array   = "Close";
extern string MA2Array   = "Close";
extern int    MA1Periods = 12;
extern int    MA2Periods = 26;
extern string MA1Method  = "Simple";
extern string MA2Method  = "Simple";
extern int Deviation=2;

//+------------------------------------------------------------------+
//|                    EXPERT BASIC INITIALISATION                   |
//|     YOU SHOULD NOT HAVE TO MODIFY ANYTHING BELOW THIS BOX        |
//+------------------------------------------------------------------+
int start()
  {
     int RealTime = 0;
     if( UseCompletedBars )
     {
      if(timeprev==Time[0]){return(0);} timeprev = Time[0];
      RealTime = 1;
     }
     
     //---- Let's get our Array
     int MA1ArraySelected = 0;
     //------------------ CLOSE -------------------------
       if(MA1Array == "C")      {MA1ArraySelected = PRICE_CLOSE;}
  else if(MA1Array == "CLOSE")  {MA1ArraySelected = PRICE_CLOSE;}
  else if(MA1Array == "Close")  {MA1ArraySelected = PRICE_CLOSE;}
  else if(MA1Array == "c")      {MA1ArraySelected = PRICE_CLOSE;}
  else if(MA1Array == "close")  {MA1ArraySelected = PRICE_CLOSE;}
  //------------------ LOW -------------------------
  else if(MA1Array == "L")    {MA1ArraySelected = PRICE_LOW;}
  else if(MA1Array == "LOW")  {MA1ArraySelected = PRICE_LOW;}
  else if(MA1Array == "Low")  {MA1ArraySelected = PRICE_LOW;}
  else if(MA1Array == "l")    {MA1ArraySelected = PRICE_LOW;}
  else if(MA1Array == "low")  {MA1ArraySelected = PRICE_LOW;}
  //------------------ HIGH -------------------------
  else if(MA1Array == "H")     {MA1ArraySelected = PRICE_HIGH;}
  else if(MA1Array == "HIGH")  {MA1ArraySelected = PRICE_HIGH;}
  else if(MA1Array == "High")  {MA1ArraySelected = PRICE_HIGH;}
  else if(MA1Array == "h")     {MA1ArraySelected = PRICE_HIGH;}
  else if(MA1Array == "high")  {MA1ArraySelected = PRICE_HIGH;}
  //------------------ OPEN-------------------------
  else if(MA1Array == "O")     {MA1ArraySelected = PRICE_OPEN;}
  else if(MA1Array == "OPEN")  {MA1ArraySelected = PRICE_OPEN;}
  else if(MA1Array == "Open")  {MA1ArraySelected = PRICE_OPEN;}
  else if(MA1Array == "o")     {MA1ArraySelected = PRICE_OPEN;}
  else if(MA1Array == "open")  {MA1ArraySelected = PRICE_OPEN;}
  //------------------ Typical -------------------------
  else if(MA1Array == "TYPICAL") {MA1ArraySelected = PRICE_TYPICAL;}
  else if(MA1Array == "Typical") {MA1ArraySelected = PRICE_TYPICAL;}
  else if(MA1Array == "typical") {MA1ArraySelected = PRICE_TYPICAL;}
  else if(MA1Array == "T")       {MA1ArraySelected = PRICE_TYPICAL;}
  else if(MA1Array == "t")       {MA1ArraySelected = PRICE_TYPICAL;}
  //------------------ MEDIAN -------------------------------
  else if(MA1Array == "MEDIAN") {MA1ArraySelected = PRICE_MEDIAN;}
  else if(MA1Array == "Median") {MA1ArraySelected = PRICE_MEDIAN;}
  else if(MA1Array == "median") {MA1ArraySelected = PRICE_MEDIAN;}
  else if(MA1Array == "M")      {MA1ArraySelected = PRICE_MEDIAN;}
  else if(MA1Array == "m")      {MA1ArraySelected = PRICE_MEDIAN;}
  //------------------ DEFAULT -------------------------------
  else                       
  {
   Alert("Please select a valid array in open, high, Low, Close, Typical, Median" );
   return(-1);
  }
  
       //---- Let's get our Array
     int MA2ArraySelected = 0;
     //------------------ CLOSE -------------------------
       if(MA2Array == "C")      {MA2ArraySelected = PRICE_CLOSE;}
  else if(MA2Array == "CLOSE")  {MA2ArraySelected = PRICE_CLOSE;}
  else if(MA2Array == "Close")  {MA2ArraySelected = PRICE_CLOSE;}
  else if(MA2Array == "c")      {MA2ArraySelected = PRICE_CLOSE;}
  else if(MA2Array == "close")  {MA2ArraySelected = PRICE_CLOSE;}
  //------------------ LOW -------------------------
  else if(MA2Array == "L")    {MA2ArraySelected = PRICE_LOW;}
  else if(MA2Array == "LOW")  {MA2ArraySelected = PRICE_LOW;}
  else if(MA2Array == "Low")  {MA2ArraySelected = PRICE_LOW;}
  else if(MA2Array == "l")    {MA2ArraySelected = PRICE_LOW;}
  else if(MA2Array == "low")  {MA2ArraySelected = PRICE_LOW;}
  //------------------ HIGH -------------------------
  else if(MA2Array == "H")     {MA2ArraySelected = PRICE_HIGH;}
  else if(MA2Array == "HIGH")  {MA2ArraySelected = PRICE_HIGH;}
  else if(MA2Array == "High")  {MA2ArraySelected = PRICE_HIGH;}
  else if(MA2Array == "h")     {MA2ArraySelected = PRICE_HIGH;}
  else if(MA2Array == "high")  {MA2ArraySelected = PRICE_HIGH;}
  //------------------ OPEN-------------------------
  else if(MA2Array == "O")     {MA2ArraySelected = PRICE_OPEN;}
  else if(MA2Array == "OPEN")  {MA2ArraySelected = PRICE_OPEN;}
  else if(MA2Array == "Open")  {MA2ArraySelected = PRICE_OPEN;}
  else if(MA2Array == "o")     {MA2ArraySelected = PRICE_OPEN;}
  else if(MA2Array == "open")  {MA2ArraySelected = PRICE_OPEN;}
  //------------------ Typical -------------------------
  else if(MA2Array == "TYPICAL") {MA2ArraySelected = PRICE_TYPICAL;}
  else if(MA2Array == "Typical") {MA2ArraySelected = PRICE_TYPICAL;}
  else if(MA2Array == "typical") {MA2ArraySelected = PRICE_TYPICAL;}
  else if(MA2Array == "T")       {MA2ArraySelected = PRICE_TYPICAL;}
  else if(MA2Array == "t")       {MA2ArraySelected = PRICE_TYPICAL;}
  //------------------ MEDIAN -------------------------------
  else if(MA2Array == "MEDIAN") {MA2ArraySelected = PRICE_MEDIAN;}
  else if(MA2Array == "Median") {MA2ArraySelected = PRICE_MEDIAN;}
  else if(MA2Array == "median") {MA2ArraySelected = PRICE_MEDIAN;}
  else if(MA2Array == "M")      {MA2ArraySelected = PRICE_MEDIAN;}
  else if(MA2Array == "m")      {MA2ArraySelected = PRICE_MEDIAN;}
  //------------------ DEFAULT -------------------------------
  else                       
  {
   Alert("Please select a valid array in open, high, Low, Close, Typical, Median" );
   return(-1);
  }
  
  //---- Let's get our method
  int MA1MethodSelected = 0;
  //------------------ CLOSE -------------------------
       if(MA1Method == "Simple")  {MA1MethodSelected = MODE_SMA; }
  else if(MA1Method == "SIMPLE")  {MA1MethodSelected = MODE_SMA; }
  else if(MA1Method == "S")       {MA1MethodSelected = MODE_SMA; }
  else if(MA1Method == "s")       {MA1MethodSelected = MODE_SMA; }
  else if(MA1Method == "simple")  {MA1MethodSelected = MODE_SMA; }
  //------------------ LOW -------------------------
  else if(MA1Method == "Exponential") { MA1MethodSelected = MODE_EMA;}
  else if(MA1Method == "EXPONENTIAL") { MA1MethodSelected = MODE_EMA;}
  else if(MA1Method == "E")           { MA1MethodSelected = MODE_EMA;}
  else if(MA1Method == "e")           { MA1MethodSelected = MODE_EMA;}
  else if(MA1Method == "exponential") { MA1MethodSelected = MODE_EMA;}
  //------------------ DEFAULT -------------------------------
  else                       
  {
   Alert("Please select a valid Method: Simple or Exponential" );
   return(-1);
  }
  
  //---- Let's get our method
  int MA2MethodSelected = 0;
  //------------------ CLOSE -------------------------
       if(MA2Method == "Simple")  {MA2MethodSelected = MODE_SMA; }
  else if(MA2Method == "SIMPLE")  {MA2MethodSelected = MODE_SMA; }
  else if(MA2Method == "S")       {MA2MethodSelected = MODE_SMA; }
  else if(MA2Method == "s")       {MA2MethodSelected = MODE_SMA; }
  else if(MA2Method == "simple")  {MA2MethodSelected = MODE_SMA; }
  //------------------ LOW -------------------------
  else if(MA2Method == "Exponential") { MA2MethodSelected = MODE_EMA;}
  else if(MA2Method == "EXPONENTIAL") { MA2MethodSelected = MODE_EMA;}
  else if(MA2Method == "E")           { MA2MethodSelected = MODE_EMA;}
  else if(MA2Method == "e")           { MA2MethodSelected = MODE_EMA;}
  else if(MA2Method == "exponential") { MA2MethodSelected = MODE_EMA;}
  //------------------ DEFAULT -------------------------------
  else                       
  {
   Alert("Please select a valid Method: Simple or Exponential" );
   return(-1);
  }
     int MagicNumber = 0;
     MagicNumber = MakeMagicNumber( ExpertID, TimeSpecific );
     
     EnterLong = false;
     ExitLong  = false;
     EnterShort = false;
     ExitShort = false;
     
     TrailingAlls(0,TrailingStop);
//+------------------------------------------------------------------+
//|                    EXPERT BASIC CALCULATION                      |
//|                          START HERE                              |
//+------------------------------------------------------------------+
double MA11 = iMA(NULL,0,MA1Periods,0,MA1MethodSelected, MA1ArraySelected,0+RealTime);
double MA12 = iMA(NULL,0,MA1Periods,0,MA1MethodSelected, MA1ArraySelected,1+RealTime);
double MA21 = iMA(NULL,0,MA2Periods,0,MA2MethodSelected, MA2ArraySelected,0+RealTime);
double MA22 = iMA(NULL,0,MA2Periods,0,MA2MethodSelected, MA2ArraySelected,1+RealTime);

if( MA11 > MA21 && MA12 < MA22 ) { EnterLong = True; }
if( MA11 < MA21 && MA12 > MA22 ) { EnterShort = True; }

//+------------------------------------------------------------------+
//|                    EXPERT BASIC CALCULATION                      |
//|                          END HERE                                |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|     YOU SHOULD NOT HAVE TO MODIFY ANYTHING BELOW THIS BOX        |
//+------------------------------------------------------------------+

// ENTER LONG CONDITION
if(EnterLong == true && CountLongs(MagicNumber)== 0)
{
   //CLOSE OPEN ORDER
   CloseShorts(MagicNumber);
   // PLACE THE ORDER
   OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,StopLong(Bid,StopLoss),TakeLong(Ask,ProfitTarget),ExpertName,MagicNumber,EXPIRATION,EnterLongColor);
} 
// ENTER SHORT CONDITION
if(EnterShort == true && CountShorts(MagicNumber)== 0)
{
   //CLOSE OPEN ORDER
   CloseLongs(MagicNumber);
   // PLACE THE ORDER
   OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,StopShort(Ask,StopLoss),TakeShort(Bid,ProfitTarget),ExpertName,MagicNumber,EXPIRATION,EnterShortColor);
} 
if(ExitLong == true && CountLongs(MagicNumber)== 1)
{
   //CLOSE OPEN ORDER
   CloseLongs(MagicNumber);
}
if(ExitShort == true && CountShorts(MagicNumber)== 1)
{
   //CLOSE OPEN ORDER
   CloseShorts(MagicNumber);
}
   return(0);
 }
//+------------------------------------------------------------------+
//|                     EXTERNAL FUNCTIONS                           |
//|     YOU SHOULD NOT HAVE TO MODIFY ANYTHING BELOW THIS BOX        |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Make Magic Number                                                |
//+------------------------------------------------------------------+
int MakeMagicNumber( int ExpertID, bool TimeSpecific )
{
   int SymbolCode  = 0;
   int PeriodCode  = 0;
   int MagicNumber = 0; 
   
   //---- Symbol Code
        if( Symbol() == "AUDCAD" || Symbol() == "AUDCADm" ) { SymbolCode = 1000; }
   else if( Symbol() == "AUDJPY" || Symbol() == "AUDJPYm" ) { SymbolCode = 2000; }
   else if( Symbol() == "AUDNZD" || Symbol() == "AUDNZDm" ) { SymbolCode = 3000; }
   else if( Symbol() == "AUDUSD" || Symbol() == "AUDUSDm" ) { SymbolCode = 4000; }
   else if( Symbol() == "CHFJPY" || Symbol() == "CHFJPYm" ) { SymbolCode = 5000; }
   else if( Symbol() == "EURAUD" || Symbol() == "EURAUDm" ) { SymbolCode = 6000; }
   else if( Symbol() == "EURCAD" || Symbol() == "EURCADm" ) { SymbolCode = 7000; }
   else if( Symbol() == "EURCHF" || Symbol() == "EURCHFm" ) { SymbolCode = 8000; }
   else if( Symbol() == "EURGBP" || Symbol() == "EURGBPm" ) { SymbolCode = 9000; }
   else if( Symbol() == "EURJPY" || Symbol() == "EURJPYm" ) { SymbolCode = 1000; }
   else if( Symbol() == "EURUSD" || Symbol() == "EURUSDm" ) { SymbolCode = 1100; }
   else if( Symbol() == "GBPCHF" || Symbol() == "GBPCHFm" ) { SymbolCode = 1200; }
   else if( Symbol() == "GBPJPY" || Symbol() == "GBPJPYm" ) { SymbolCode = 1300; }
   else if( Symbol() == "GBPUSD" || Symbol() == "GBPUSDm" ) { SymbolCode = 1400; }
   else if( Symbol() == "NZDJPY" || Symbol() == "NZDJPYm" ) { SymbolCode = 1500; }
   else if( Symbol() == "NZDUSD" || Symbol() == "NZDUSDm" ) { SymbolCode = 1600; }
   else if( Symbol() == "USDCAD" || Symbol() == "USDCADm" ) { SymbolCode = 1700; }
   else if( Symbol() == "USDCHF" || Symbol() == "USDCHFm" ) { SymbolCode = 1800; }
   else if( Symbol() == "USDJPY" || Symbol() == "USDJPYm" ) { SymbolCode = 1900; }
                     
   
   //---- Period Code
   if( TimeSpecific )
   {
              if( Period() == 1 )    { PeriodCode = 10; }
         else if( Period() == 5 )    { PeriodCode = 20; }
         else if( Period() == 15 )   { PeriodCode = 30; }
         else if( Period() == 30 )   { PeriodCode = 40; }
         else if( Period() == 60 )   { PeriodCode = 50; }
         else if( Period() == 240 )  { PeriodCode = 60; }
         else if( Period() == 1440 ) { PeriodCode = 70; }
         else if( Period() == 10080 ){ PeriodCode = 80; }
   }
   else
   {
      PeriodCode = 0;
   }
   //---- Calculate MagicNumber
   MagicNumber = ExpertID+SymbolCode+PeriodCode;
   return(MagicNumber);
}

//+------------------------------------------------------------------+
//| Calculate Stop Long                                              |
//+------------------------------------------------------------------+
double StopLong(double price,int stop)
{
 if(stop==0)
 {
  return(0);
 }
 else
 {
  return(price-(stop*Point));
 }
}
//+------------------------------------------------------------------+
//| Calculate Stop Short                                             |
//+------------------------------------------------------------------+
double StopShort(double price,int stop)
{
 if(stop==0)
 {
  return(0);
 }
 else
 {
  return(price+(stop*Point));
 }
}
//+------------------------------------------------------------------+
//| Calculate Profit Target Long                                     |
//+------------------------------------------------------------------+
double TakeLong(double price,int take)
{
 if(take==0) {  return(0);}
 else        {  return(price+(take*Point));}
}
//+------------------------------------------------------------------+
//| Calculate Profit Target Short                                     |
//+------------------------------------------------------------------+
double TakeShort(double price,int take)
{
 if(take==0) {  return(0);}
 else        {  return(price-(take*Point));}
}
//+------------------------------------------------------------------+
//| Calculate concurrent Long position                               |
//+------------------------------------------------------------------+
int CountLongs(int MagicNumber)
{
 int count=0;
 int trade;
 int trades=OrdersTotal();
 for(trade=0;trade<trades;trade++)
 {
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
  
  if( OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber )
   continue;
   
  if(OrderType()==OP_BUY)
   count++;
 }//for
 return(count);
}
//+------------------------------------------------------------------+
//| Calculate concurrent short position                              |
//+------------------------------------------------------------------+
int CountShorts(int MagicNumber)
{
 int count=0;
 int trade;
 int trades=OrdersTotal();
 for(trade=0;trade<trades;trade++)
 {
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
  
  if(OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber )
   continue;
   
  if(OrderType()==OP_SELL)
  count++;
 }//for
 return(count);
}
//+------------------------------------------------------------------+
//| Close Long Position                                              |
//+------------------------------------------------------------------+
void CloseLongs(int MagicNumber)
{
 int i = OrdersTotal();
 
 while( CountLongs(MagicNumber) != 0 && i >= 0 )
 {  
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   
   if( OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber )
   {
     i--;
     continue;
   }
   else if(OrderType()==OP_BUY || OrderType()== OP_BUYLIMIT)
   {
     OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,ExitLongColor);
     i--;
   }
 }
}
//+------------------------------------------------------------------+
//| Close Short Position                                             |
//+------------------------------------------------------------------+
void CloseShorts(int MagicNumber)
{
 int i = OrdersTotal();
 
 while( CountShorts(MagicNumber) != 0 && i >= 0 )
 {  
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   
   if( OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber)
   {
     i--;
     continue;
   }
   else if(OrderType()== OP_SELL || OrderType()==OP_SELLLIMIT )
  {
   OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,ExitShortColor);
  }
 }
}
//+------------------------------------------------------------------+
//| Calculates Trailing Stops                                        |
//+------------------------------------------------------------------+
void TrailingAlls(int start,int stop)
{
 int profit;
 double stoptrade;
 double stopcal;
 
 if(stop==0) return;
 
 int trade;
 for(trade=OrdersTotal()-1;trade>=0;trade--)
 {
  if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
   continue;

  if(OrderSymbol()!=Symbol())
   continue;

  if(OrderType()==OP_BUY)
  {
   profit=NormalizeDouble((Bid-OrderOpenPrice())/Point,0);
   if(profit<start)
    continue;
   stoptrade=OrderStopLoss();
   stopcal=Bid-(stop*Point);
   if(stoptrade==0||(stoptrade!=0&&stopcal>stoptrade))
    OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,EnterLongColor);
  }//Long
  
  if(OrderType()==OP_SELL)
  {
   profit=NormalizeDouble((OrderOpenPrice()-Ask)/Point,0);
   if(profit<start)
    continue;
   stoptrade=OrderStopLoss();
   stopcal=Ask+(stop*Point);
   if(stoptrade==0||(stoptrade!=0&&stopcal<stoptrade))
    OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,EnterShortColor);
  }//Shrt
  
 }//for