Slingshot_2.0





//+----------------------------------------------------------------------------------------------+
//|                                                                         Slingshot_2.0 EA.mq4 |
//|                                                             based on template by Ryan Klefas |
//|                                                                    http://www.metaquotes.net |
//|                                                                 originally modified by Orest |
//|                                                                            olponom@yahoo.com |
//|               multiple trade filters and multiple trade options added Feb. 2009 by Gary Hill |
//+----------------------------------------------------------------------------------------------+

extern string    basic = "==== Basic Settings ====================";
extern double    TakeProfit=0;
extern double    Stoploss=0;
extern double    Lots=0.1;

extern string    manage = "==== Order Management ====================";
extern int       BreakEvenAtProfit=0;
extern int       BreakEvenShift=0;               // Move the breakeven point up or down around the order open price
extern int       TrailingStop=0;
extern bool      OnlyTrailAfterProfit = false;   // Trailing Stop will only trails when order is profitable
extern bool      Move_SL_to_Prev_Hi_Lo = true;   


extern string    account="==== Account Settings ====================";
extern int       MinimumEquity=0;                // New orders will be disabled if equity drops below minimum
extern int       MinimumBalance=0;               // New orders will be disabled if balance drops below minimum

extern string    activity = "==== EA Activity Settings ====================";
extern int       ConcurrentOrdersMax=1;

extern string    id = "==== Identity Settings ====================";
extern int       ExpertID=999999;               // Magic number: for identifying an EA's orders
extern bool      TimeSpecific=true;            // If true, Time frames are considered in determining
                                                // whether an order belongs to the EA or not
extern string    ExpertName="SlingShot 2.0";    // Expert name: for aesthetic purposes


extern int TimeToTrade1 = 7;    // GMT Time - adjust for your broker difference
extern int TimeToTrade2 = 20;   // GMT Time - adjust for your broker difference

extern string    maxmacd = "==== MACD Colored Parameters ====================";

extern int FastEMA       = 12;
extern int SlowEMA       = 26;
extern int SignalSMA     = 9;
extern int applied_price = 0;

extern string stoch = "==== Stochastic Parameters ================";

extern int KPeriod = 8;
extern int DPeriod = 3;
extern int Slowing = 3;

extern string zzi = "==== ZZi Parameters =======================";

extern int CCIPeriod = 14;

extern string    money = "==== Money Management ====================";
extern bool Money_Management = false;
extern double Risk = 0.01;
extern int Max_StopLoss = 100;

extern string trades = "==== Trade Management ===================";
extern string tradedefinitions = "MMF = MaxMacdFilter, SF = StochFilter, ZF = ZZiFilter";
extern string tradesremark1 = "---- One Filter Trades ----";
extern bool TradeUsingMMF = true;
extern bool TradeUsingSF  = false;
extern bool TradeUsingZF  = false;
extern string tradesremark2 = "---- Two Filter Trades ----";
extern bool TradeUsingMMFandSF = false;
extern bool TradeUsingMMFandZF = false;
extern bool TradeUsingSFandZF  = false; 
extern string tradesremark3 = "---- Three Filter Trades ----";
extern bool TradeUsingMMFandSFandZF = false;   



bool      DisableClosingOrders=false;    // If true, the order closing section of the EA will not run
bool      DisableNewOrders=false;        // If true, the order creating section of the EA will not run

bool belowLowPreviousBar    = false;
bool aboveClosePreviousBar  = false;

bool aboveHighPreviousBar   = false;
bool belowClosePreviousBar  = false;
datetime barTime=0; 
bool BUY_SIGNAL = false;
bool SELL_SIGNAL = false;
int mmacdtrend = 0, strend = 0, ztrend;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   Comment(ExpertName + " is waiting for the next tick to begin.");
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{    
      string comment = "";
      //Comment(ExpertName + " has started.");

      int buyOrders=0, sellOrders=0, buyPending=0, sellPending=0;
      checkOrderStatus(buyOrders, sellOrders, buyPending, sellPending);
      int instantOrders = buyOrders + sellOrders;
      int pendingOrders = buyPending + sellPending;
      int allOrders     = instantOrders + pendingOrders;
      advancedStopManager(instantOrders);
      int total = 0;
//+------------------------------------------------------------------+
//| variable and indicator declaration                               |
//+------------------------------------------------------------------+

      if( barTime < Time[0] ) 
      {
         // we have a new bar opened
         barTime = Time[0]; // keep the new bar open time
         belowLowPreviousBar    = false;
         aboveClosePreviousBar  = false;

         aboveHighPreviousBar   = false;
         belowClosePreviousBar  = false;
         BUY_SIGNAL = false;
         SELL_SIGNAL = false;

         total = OrdersTotal();
         if (total > 0 && Move_SL_to_Prev_Hi_Lo) moveStopLosses();   
      }

//+------------------------------------------------------------------+
//| close existing orders under apprpriate conditions                |
//+------------------------------------------------------------------+

      if (DisableClosingOrders==false && allOrders>0 && barTime == Time[0])
      {
         int cnt;
         total = OrdersTotal();

         for(cnt=0;cnt<total;cnt++)
         {
            OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

            if (orderBelongsToMe() && OrderOpenTime()< Time[0])
            {
               if (OrderType()==OP_BUY)
               {
                  if (OrderProfit() > 0)   //   Bid-OrderOpenPrice()>Point*TrailingStop)
                  {
                     closeBuyOrder();
                  }
               }
               else if (OrderType()==OP_SELL)
               {
                  if (OrderProfit() > 0) //OrderOpenPrice()-Ask>Point*TrailingStop)
                  {
                     closeSellOrder();
                  }
               }
            }  
         }
      }

 //+------------------------------------------------------------------+
 //|  Checking for Opening new trades                                 |
 //+------------------------------------------------------------------+

      if(TimeCurrent() <= Time[0]) MaxMacdTrend(mmacdtrend);
      if(TimeCurrent() <= Time[0]) StochTrend(strend);
      if(TimeCurrent() <= Time[0]) ZZiTrend(ztrend);
      
      if (! IsTimeToTrade())
      {
         mmacdtrend = 0;
         strend = 0;
         ztrend = 0;
      }
      
      if(TradeUsingMMF == true && mmacdtrend == 1) bool TradeOptionBuy1 = true;
      if(TradeUsingSF == true && strend == 2) bool TradeOptionBuy2 = true;
      if(TradeUsingZF == true && ztrend == 3) bool TradeOptionBuy3 = true;
      if(TradeUsingMMFandSF == true && mmacdtrend == 1 && strend == 2) bool TradeOptionBuy4 = true;
      if(TradeUsingMMFandZF == true && mmacdtrend == 1 && ztrend == 3) bool TradeOptionBuy5 = true;
      if(TradeUsingSFandZF == true && strend == 2 && ztrend == 3) bool TradeOptionBuy6 = true;
      if(TradeUsingMMFandSFandZF == true && mmacdtrend == 1 && strend == 2 && ztrend == 3) bool TradeOptionBuy7 = true;
        
      if(TradeUsingMMF == true && mmacdtrend == -1) bool TradeOptionSell1 = true;
      if(TradeUsingSF == true && strend == -2) bool TradeOptionSell2 = true;
      if(TradeUsingZF == true && ztrend == -3) bool TradeOptionSell3 = true;
      if(TradeUsingMMFandSF == true && mmacdtrend == -1 && strend == -2) bool TradeOptionSell4 = true;
      if(TradeUsingMMFandZF == true && mmacdtrend == -1 && ztrend == -3) bool TradeOptionSell5 = true;
      if(TradeUsingSFandZF == true && strend == -2 && ztrend == -3) bool TradeOptionSell6 = true;
      if(TradeUsingMMFandSFandZF == true && mmacdtrend == -1 && strend == -2 && ztrend == -3) bool TradeOptionSell7 = true;
      
      // if no open orders then start looking for setup
      if (instantOrders == 0)
      { 
         if (TradeOptionBuy1 == true && Low[0] < Low[1] &&  Ask >= NormalizeDouble(Close[1]+1*Point, Digits)) sendBuyOrder();     
         if (TradeOptionBuy2 == true && Low[0] < Low[1] &&  Ask >= NormalizeDouble(Close[1]+1*Point, Digits)) sendBuyOrder();     
         if (TradeOptionBuy3 == true && Low[0] < Low[1] &&  Ask >= NormalizeDouble(Close[1]+1*Point, Digits)) sendBuyOrder();                 
         if (TradeOptionBuy4 == true && Low[0] < Low[1] &&  Ask >= NormalizeDouble(Close[1]+1*Point, Digits)) sendBuyOrder();
         if (TradeOptionBuy5 == true && Low[0] < Low[1] &&  Ask >= NormalizeDouble(Close[1]+1*Point, Digits)) sendBuyOrder();
         if (TradeOptionBuy6 == true && Low[0] < Low[1] &&  Ask >= NormalizeDouble(Close[1]+1*Point, Digits)) sendBuyOrder();
         if (TradeOptionBuy7 == true && Low[0] < Low[1] &&  Ask >= NormalizeDouble(Close[1]+1*Point, Digits)) sendBuyOrder();                    
      
         if (TradeOptionSell1 == true && High[0] > High[1] && Bid <= NormalizeDouble(Close[1]-1*Point, Digits)) sendSellOrder();      
         if (TradeOptionSell2 == true && High[0] > High[1] && Bid <= NormalizeDouble(Close[1]-1*Point, Digits)) sendSellOrder();     
         if (TradeOptionSell3 == true && High[0] > High[1] && Bid <= NormalizeDouble(Close[1]-1*Point, Digits)) sendSellOrder();                 
         if (TradeOptionSell4 == true && High[0] > High[1] && Bid <= NormalizeDouble(Close[1]-1*Point, Digits)) sendSellOrder();
         if (TradeOptionSell5 == true && High[0] > High[1] && Bid <= NormalizeDouble(Close[1]-1*Point, Digits)) sendSellOrder();
         if (TradeOptionSell6 == true && High[0] > High[1] && Bid <= NormalizeDouble(Close[1]-1*Point, Digits)) sendSellOrder();
         if (TradeOptionSell7 == true && High[0] > High[1] && Bid <= NormalizeDouble(Close[1]-1*Point, Digits)) sendSellOrder();                    
      }       
  return(0);
}
  
//+------------------------------------------------------------------+
//| middle-man modules                                               |
//+------------------------------------------------------------------+

void sendBuyOrder() // Standard Version 1.71
{ versatileOrderTaker(1, Ask); }
   
//+------------------------------------------------------------------+

void sendSellOrder() // Standard Version 1.71
{ versatileOrderTaker(2, Bid); }

//+------------------------------------------------------------------+

void sendBuyPending(double entryPrice) // Standard Version 1.71
{ versatileOrderTaker(3, entryPrice); }

//+------------------------------------------------------------------+

void sendSellPending(double entryPrice) // Standard Version 1.71
{ versatileOrderTaker(4, entryPrice); }

//+------------------------------------------------------------------+

void closeSellOrder() // Standard Version 1.71
{ versatileOrderCloser(2); }

//+------------------------------------------------------------------+

void closeBuyOrder() // Standard Version 1.71
{ versatileOrderCloser(1); }

//+------------------------------------------------------------------+

void deletePending() // Standard Version 1.71
{ versatileOrderCloser(3); }

//+------------------------------------------------------------------+




//+------------------------------------------------------------------+
//| modules and functions                                            |
//+------------------------------------------------------------------+

void trailingStopManager() // Standard Version 1.71
{
   
   int cnt, total = OrdersTotal();

   for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if (TrailingStop>0 && orderBelongsToMe())
      {
         if (OrderType()==OP_BUY)
         {
            if ((Bid-OrderOpenPrice()>Point*TrailingStop) || OnlyTrailAfterProfit==false)
            {
               if (OrderStopLoss()<Bid-Point*TrailingStop)
                  OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green); 
            }
         }
         else if (OrderType()==OP_SELL)
         {
            if ((OrderOpenPrice()-Ask>Point*TrailingStop) || OnlyTrailAfterProfit==false)
            {
               if ((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                  OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); 
            }
         }
      }  
   }
}


void moveStopLosses() 
{
   int cnt, total = OrdersTotal();
    double dblStop, dblTightestStop;

   for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      int index = iBarShift(NULL, 0 , OrderOpenTime()); 
      if (index==-1) return; 
     
      if (orderBelongsToMe() && OrderProfit() < 0)
      {
         if (OrderType()==OP_BUY)
         {
          dblStop = Low[index]-(MarketInfo(Symbol(),MODE_SPREAD)*Point)-1*Point;
          dblTightestStop = Bid - MarketInfo(Symbol(),MODE_STOPLEVEL) * Point;
          if ( dblStop > dblTightestStop && Stoploss>0)
               dblStop = OrderOpenPrice()-Stoploss*Point;
          
          if (dblStop < OrderOpenPrice() && dblStop != OrderStopLoss() && dblStop < dblTightestStop)           
               OrderModify(OrderTicket(),OrderOpenPrice(),dblStop,OrderTakeProfit(),0,Green); 
         }
         else if (OrderType()==OP_SELL)
         {
         dblStop = High[index]+(MarketInfo(Symbol(),MODE_SPREAD)*Point)+1*Point;   //MarketInfo(Symbol(),MODE_SPREAD)*Point+
         dblTightestStop = Ask + MarketInfo(Symbol(),MODE_STOPLEVEL) * Point;
         if ( dblStop < dblTightestStop  && Stoploss>0)
               dblStop = OrderOpenPrice()+Stoploss*Point;
               
         if (dblStop > OrderOpenPrice() && dblStop != OrderStopLoss() && dblStop > dblTightestStop)           
             OrderModify(OrderTicket(),OrderOpenPrice(),dblStop,OrderTakeProfit(),0,Red); 
         }
      }  
   }
}

//+------------------------------------------------------------------+

void breakEvenManager() // Standard Version 1.71
{

   for(int cnt=0;cnt<OrdersTotal();cnt++)
   {
   OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if (BreakEvenAtProfit>0 && orderBelongsToMe())
      {
         if (OrderType()==OP_BUY)
         {
            if (Bid-OrderOpenPrice()>=Point*BreakEvenAtProfit)
            {
               if (OrderStopLoss()!=OrderOpenPrice() + BreakEvenShift*Point)
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+ BreakEvenShift*Point,OrderTakeProfit(),0,LimeGreen); 
            }
         }
         else if (OrderType()==OP_SELL)
         {
            if (OrderOpenPrice()-Ask>=Point*BreakEvenAtProfit)
            {
               if (OrderStopLoss()!=OrderOpenPrice() - BreakEvenShift*Point)
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()- BreakEvenShift*Point,OrderTakeProfit(),0,Tomato); 
            }
         }
      }
   }
}
   
//+------------------------------------------------------------------+

 // Standard Version 1.71
void checkOrderStatus(int& buyOrders,int& sellOrders,int& buyPending, int& sellPending)
{
   for(int cnt=OrdersTotal();cnt>=0;cnt--)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if ( orderBelongsToMe() )
      {  
         if (OrderType()==OP_BUY)
            {buyOrders++;}
         else if (OrderType()==OP_SELL)
            {sellOrders++;}
         else if (OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)
            {buyPending++;}
         else if (OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)
            {sellPending++;}
      }
   }
}

//+------------------------------------------------------------------+

void advancedStopManager(int instantOrders) // Standard Version 1.71
{
    
   if (instantOrders>=1)
   {
      if (BreakEvenAtProfit>0)
         breakEvenManager();
      if (TrailingStop>0)
         trailingStopManager();
    }
}

//+------------------------------------------------------------------+

bool orderBelongsToMe() // Standard Version 1.71
{
   if (OrderSymbol()==Symbol() && OrderMagicNumber()==simpleMagicGenerator() )
       return (true);
   else
      return (false);
}

//+------------------------------------------------------------------+

int simpleMagicGenerator() // Standard Version 1.71
{

   int truMagic;
   
   if (TimeSpecific)
      truMagic = ExpertID + (Period()*17 + 203);
   else
      truMagic = ExpertID;      

   return (truMagic);

}

//+------------------------------------------------------------------+

string commentString() // Standard Version 1.71
{
   string tfString, fullComment;
   
   switch (Period())  
   {   
      case 1:      tfString = "1 Min";    break;
      case 5:      tfString = "5 Min";    break;
      case 15:     tfString = "15 Min";   break;  
      case 30:     tfString = "30 Min";   break;  
      case 60:     tfString = "1 Hour";   break;  
      case 240:    tfString = "4 Hour";   break;  
      case 1440:   tfString = "Daily";    break;  
      case 10080:  tfString = "Weekly";   break;  
      case 40320:  tfString = "Monthly";  break;  
      default:     tfString = "Unknown";   
   }

   fullComment=StringConcatenate(ExpertName, ": ", tfString);
   return (fullComment);
}

//+------------------------------------------------------------------+

double lotMaker() // Standard Version 1.71 Simplified
{
   if (! Money_Management)
         return (Lots);        

	double pipValue = MarketInfo(Symbol(), MODE_TICKVALUE);
	double lots = AccountFreeMargin() * Risk / (Max_StopLoss * pipValue);
	
	double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
	int digits = 0;
	if(lotStep <= 0.01)
		digits = 2;
	else if(lotStep <= 0.1)
		digits = 1;
	lots = NormalizeDouble(lots, digits);
  	
  	double minLots = MarketInfo(Symbol(), MODE_MINLOT);
  	if(lots < minLots)
  		lots = minLots;
  	
  	double maxLots = MarketInfo(Symbol(), MODE_MAXLOT);
  	if(lots > maxLots)
  		lots = maxLots;
  	
  	return (lots);
}

//+------------------------------------------------------------------+

bool newOrderPrevention(int allOrders) // Standard Version 1.71
{

   if (DisableNewOrders)
      { Comment("New Orders Disabled:  User option"); return (true); }
   else if (AccountEquity()<MinimumEquity)
      { Comment("New Orders Disabled:  Equity too low"); return (true);}
   else if (AccountBalance()<MinimumBalance)
      { Comment("New Orders Disabled:  Balance too low"); return (true);}
   else if (allOrders>=ConcurrentOrdersMax)
      { Comment("New Orders Disabled:  Too many existing orders"); return (true);}
   else
      return (false);
}

//+------------------------------------------------------------------+

void versatileOrderTaker(int simpleType, double entryPrice) // Standard Version 1.71
{

   // Variable calculations

   double Stopvalue=0, TPvalue=0;
   int ticket, oType, slippage=3;
   string typeName;
   color c_color;
   
   c_color = Green;

   switch (simpleType)
   {
      case 4: 
      if (entryPrice<Bid)  { oType=OP_SELLSTOP;  typeName="SELLSTOP"; }
      if (entryPrice>Bid)  { oType=OP_SELLLIMIT; typeName="SELLLIMIT"; }
      if (Stoploss>0)      {Stopvalue = entryPrice+Stoploss*Point;}
      if (TakeProfit>0)    {TPvalue = entryPrice-TakeProfit*Point;}
      break;
         
      case 3:
      if (entryPrice>Ask)  { oType=OP_BUYSTOP;  typeName="BUYSTOP"; }
      if (entryPrice<Ask)  { oType=OP_BUYLIMIT; typeName="BUYLIMIT"; }
      if (Stoploss>0)      {Stopvalue = entryPrice-Stoploss*Point;}
      if (TakeProfit>0)    {TPvalue = entryPrice+TakeProfit*Point;}
      break;

      case 2:
      oType=OP_SELL; typeName="SELL"; 
      c_color = Red;
      if (Stoploss>0)      {Stopvalue = entryPrice+Stoploss*Point;}
      if (TakeProfit>0)    {TPvalue = entryPrice-TakeProfit*Point;}
      break;

      case 1:
      oType=OP_BUY; typeName="BUY"; 
      if (Stoploss>0)      {Stopvalue = entryPrice-Stoploss*Point;}
      if (TakeProfit>0)    {TPvalue = entryPrice+TakeProfit*Point;}
      break;
      
      default:
      Print ("versatileOrderTaker has been passed an invalid SimpleType parameter: " + simpleType);
   }

   // Send Order
   
   ticket=OrderSend(Symbol(),oType,lotMaker(),entryPrice,slippage,Stopvalue,TPvalue,commentString(),simpleMagicGenerator(),0,c_color);
   if (ticket>0)
     {if (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print(ExpertName + " " + typeName + " order at ",OrderOpenPrice()); }
   else 
      Print("Error opening " + typeName + " order: ",GetLastError());
}

//+------------------------------------------------------------------+

void versatileOrderCloser(int simpleType) // Standard Version 1.71
{
   int cnt, total = OrdersTotal();

   for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      
      if ( orderBelongsToMe() )
      {
         switch (simpleType)
         {
            case 4:
            case 3:
               if (OrderType()>OP_SELL)   OrderDelete(OrderTicket());
            break;
         
            case 2:
               if (OrderType()==OP_SELL)  OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE); 
            break;
         
            case 1:
               if (OrderType()==OP_BUY)   OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE); 
            break;
         
            default:
            Print ("versatileOrderCloser has been passed an invalid SimpleType parameter: " + simpleType);
         }
      }
   }
}

//+------------------------------------------------------------------+

bool IsTimeToTrade()
{
bool res = false;
      //if (Hour() >= TimeToTrade1 && Hour() <= TimeToTrade2 && DayOfWeek()!=0 && DayOfWeek()!=6)
      if (Hour() >= TimeToTrade1 && Hour() <= TimeToTrade2)
      {
         res = true;  
      }
return(res);
}

//+------------------------------------------------------------------+

void StochTrend(int& strend)
{
   double s1 = 0.0, s2 = 0.0 ,s3 = 0.0, s4 = 0.0;
   double High1 = 0.0, High2 = 0.0, Low1 = 0.0, Low2 = 0.0;
   
   for (int j=1; j<=500; j++)
         {        
            double value1 = iCustom(NULL, 0, "Stochastic",KPeriod,DPeriod,Slowing,0,j);
            double value2 = iCustom(NULL, 0, "Stochastic",KPeriod,DPeriod,Slowing,0,j+1);
            double value3 = iCustom(NULL, 0, "Stochastic",KPeriod,DPeriod,Slowing,0,j+2);
            
            if(s1 == 0 && s2 == 0 && s3 == 0 && s4 == 0)
            {
               if(value1 > value2 && value2 < value3) s1 = value2;
               if(value1 < value2 && value2 > value3) s1 = value2;
            }
            else if(s1 != 0 && s2 == 0 && s3 == 0 && s4 == 0)
            {
               if(value1 > value2 && value2 < value3) s2 = value2;
               if(value1 < value2 && value2 > value3) s2 = value2;
            }
            else if(s1 != 0 && s2 != 0 && s3 == 0 && s4 == 0)
            {
               if(value1 > value2 && value2 < value3) s3 = value2;
               if(value1 < value2 && value2 > value3) s3 = value2;
            }
            else if(s1 != 0 && s2 != 0 && s3 != 0 && s4 == 0)
            {
               if(value1 > value2 && value2 < value3) s4 = value2;
               if(value1 < value2 && value2 > value3) s4 = value2;
            }
            else if(s1 != 0 && s2 != 0 && s3 != 0 && s4 != 0) break;
         }
     
         if(s1 > s2)
         {
            High1 = s1;
            Low1 = s2;
            High2 = s3;
            Low2 = s4;
         }
         if(s1 < s2)
         {
            Low1 = s1;
            High1 = s2;
            Low2 = s3;
            High2 = s4;
         }
         
         double value4 = iCustom(NULL, 0, "Stochastic",KPeriod,DPeriod,Slowing,0,1);
              
         if(s1 == High1)
         {
            if(value4 < Low1) strend = -2;                    // = Breakout low = short trade     
            if(value4 < Low1 && High1 < High2) strend = -2;   // = Trade continuation = short trade       
         }
         
         if(s1 == Low1)
         { 
            if(value4 > High1) strend = 2;                    // = Breakout high = long trade   
            if(value4 > High1 && Low1 > Low2) strend = 2;     // = Trade continuation = long trade        
         } 
}
//+------------------------------------------------------------------+

void ZZiTrend(int& ztrend)
{
   double ss1 = 0.0 ,ss2 = 0.0, ss3 = 0.0, ss4 = 0.0;
   double HHigh1 = 0.0, HHigh2 = 0.0, LLow1 = 0.0, LLow2 = 0.0;
   
   for (int ii=1; ii<=500; ii++)
         {
            double vvalue1 = iCustom(NULL, 0, "Zee Zee i",CCIPeriod,0,ii);
            double vvalue2 = iCustom(NULL, 0, "Zee Zee i",CCIPeriod,0,ii+1);
            double vvalue3 = iCustom(NULL, 0, "Zee Zee i",CCIPeriod,0,ii+2);
            
            if(ss1 == 0 && ss2 == 0 && ss3 == 0 && ss4 == 0)
            {
               if(vvalue1 > vvalue2 && vvalue2 < vvalue3) ss1 = vvalue2;
               if(vvalue1 < vvalue2 && vvalue2 > vvalue3) ss1 = vvalue2;
            }
            else if(ss1 != 0 && ss2 == 0 && ss3 == 0 && ss4 == 0)
            {
               if(vvalue1 > vvalue2 && vvalue2 < vvalue3) ss2 = vvalue2;
               if(vvalue1 < vvalue2 && vvalue2 > vvalue3) ss2 = vvalue2;
            }
            else if(ss1 != 0 && ss2 != 0 && ss3 == 0 && ss4 == 0)
            {
               if(vvalue1 > vvalue2 && vvalue2 < vvalue3) ss3 = vvalue2;
               if(vvalue1 < vvalue2 && vvalue2 > vvalue3) ss3 = vvalue2;
            }
            else if(ss1 != 0 && ss2 != 0 && ss3 != 0 && ss4 == 0)
            {
               if(vvalue1 > vvalue2 && vvalue2 < vvalue3) ss4 = vvalue2;
               if(vvalue1 < vvalue2 && vvalue2 > vvalue3) ss4 = vvalue2;
            }
            else if(ss1 != 0 && ss2 != 0 && ss3 != 0 && ss4 != 0) break;
         }
         
         if(ss1 > ss2)
         {
            HHigh1 = ss1;
            LLow1 = ss2;
            HHigh2 = ss3;
            LLow2 = ss4;
         }
         if(ss1 < ss2)
         {
            LLow1 = ss1;
            HHigh1 = ss2;
            LLow2 = ss3;
            HHigh2 = ss4;
         }
         
         double vvalue4 = iCustom(NULL, 0, "Zee Zee i",CCIPeriod,0,1);
               
         if(ss1 == HHigh1)
         {
            if(vvalue4 < LLow1) ztrend = -3;                    // = Breakout low = short trade     
            if(vvalue4 < LLow1 && HHigh1 < HHigh2) ztrend = -3;   // = Trade continuation = short trade
         }
         if(ss1 == LLow1)
         { 
            if(vvalue4 > HHigh1) ztrend = 3;                    // = Breakout high = long trade   
            if(vvalue4 > HHigh1 && LLow1 > LLow2) ztrend = 3;     // = Trade continuation = long trade 
         }
}
//+------------------------------------------------------------------+    

void MaxMacdTrend(int& mmacdtrend)
{
      int TimeFrame = 0;

      int bluemarker1 = 0, bluemarker2 = 0; 
      int redmarker1 = 0, redmarker2 = 0;
      double bluenew = 0.0, blueold = 0.0;
      double rednew = 0.0, redold = 0.0;
      
      for (int i=1; i<=500; i++)
      {
         double macd3 =  iCustom(NULL, 0, "MACD_inColor",TimeFrame,FastEMA,SlowEMA,SignalSMA,applied_price,2,i); 
         double macd33 = iCustom(NULL, 0, "MACD_inColor",TimeFrame,FastEMA,SlowEMA,SignalSMA,applied_price,2,i+1);
         double macd4 =  iCustom(NULL, 0, "MACD_inColor",TimeFrame,FastEMA,SlowEMA,SignalSMA,applied_price,3,i);
         double macd44 = iCustom(NULL, 0, "MACD_inColor",TimeFrame,FastEMA,SlowEMA,SignalSMA,applied_price,3,i+1);
         
         if (bluemarker1 == 0)   // testing for first blue set
         {
            if (macd3 > 0 && macd33 == 0)  //ox    start of blueset1
            { 
               bluemarker1 = 1; 
               if (macd3 > bluenew) bluenew = macd3;
            }
            if (macd3 > 0 && macd33 > 0)    // xx  values within blueset1
            {
               if (macd3 >= macd33 && macd3 >= bluenew) bluenew = macd3;
               else if (macd33 > bluenew) bluenew = macd33;
            }    
            if (macd3 == 0 && macd33 > 0)       // xo   end of blueset1
            {
               if (macd33 > bluenew) bluenew = macd33;
            }
         }
   
         else if (bluemarker1 == 1)   // testing for second blue set
         {
            if (macd3 > 0 && macd33 == 0)  //ox    start of blueset2
            { 
               bluemarker2 = 1; 
               if (macd3 > blueold) blueold = macd3;
            }
            if (macd3 > 0 && macd33 > 0)    // xx  values within blueset2
            {
               if (macd3 >= macd33 && macd3 >= blueold) blueold = macd3;
               else if (macd33 > blueold) blueold = macd33;
            }    
            if (macd3 == 0 && macd33 > 0)       // xo   end of blueset2
            {
               if (macd33 > blueold) blueold = macd33;
            }
         }                                           
         
         if (redmarker1 == 0)   // testing for first red set
         {
            if (macd4 < 0 && macd44 == 0)  //ox    start of redset1
            { 
               redmarker1 = 1; 
               if (macd4 < rednew) rednew = macd4;
            }
            if (macd4 < 0 && macd44 < 0)    // xx  values within redset1
            {
               if (macd4 <= macd44 && macd4 <= rednew) rednew = macd4;
               else if (macd44 < rednew) rednew = macd44;           
            }    
            if (macd4 == 0 && macd44 < 0)       // xo   end of redset1
            {
               if (macd44 < rednew) rednew = macd44;
            }
         }
            
         else if (redmarker1 == 1)   // testing for second red set
         {
            if (macd4 < 0 && macd44 == 0)  //ox    start of redset2
            { 
               redmarker2 = 1; 
               if (macd4 < redold) redold = macd4;
            }
            if (macd4 < 0 && macd44 < 0)    // xx  values within redset2
            {
               if (macd4 <= macd44 && macd4 <= redold) redold = macd4;
               else if (macd44 < redold) redold = macd44;
            }    
            if (macd4 == 0 && macd44 < 0)       // xo   end of redset2
            {
               if (macd44 < redold) redold = macd44;
            }
         }
         
         if (bluemarker1 == 1 && bluemarker2 == 1 && redmarker1 == 1 && redmarker2 ==1) // = 2 sets each of the most recent blue and red bars have been analyzed
         {         
            double macdblue = iCustom(NULL, 0, "MACD_inColor",TimeFrame,FastEMA,SlowEMA,SignalSMA,applied_price,2,1); 
            double macdred = iCustom(NULL, 0, "MACD_inColor",TimeFrame,FastEMA,SlowEMA,SignalSMA,applied_price,3,1); 
     
            if (mmacdtrend == 0 || mmacdtrend == -1 && macdblue > 0 && bluenew > blueold)
            {
               mmacdtrend = 1;
            }
            else if (mmacdtrend == 0 || mmacdtrend == 1 && macdred < 0 && rednew < redold)   
            {
               mmacdtrend = -1;
            }
            break;
         }       
      }           
}  

//+------------------------------------------------------------------+
       



Sample





Analysis



Market Information Used:

Series array that contains open time of each bar
Series array that contains close prices for each bar
Series array that contains the lowest prices of each bar
Series array that contains the highest prices of each bar


Indicator Curves created:


Indicators Used:




Custom Indicators Used:
MACD_inColor
Stochastic
Zee Zee i

Order Management characteristics:
Checks for the total of open orders

It can change open orders parameters, due to possible stepping strategy
It automatically opens orders when conditions are reached
It Closes Orders by itself

Other Features: